January 4, 2013

differentiation of noisy data with octave / matlab



I once had the problem that i had to differentiate noisy data - which is a problem. The differentiation process basically behaves like a high-pass filter so that the high frequency noise becomes dominant. The problem gets worse when you not only have to make the first but also higher derivatives.

Lets look at an example: we take a simple sin function and add a small amount of noise. Figure 1 shows the resulting curve. The noise is hardly visible, so Fig.2 shows a magnified version.

Fig.1 - a sine curve with some noise
 Fig.2. Here the noise curve (circles) is compared with the analytical sine curve.
Now we are interested in the second derivative of our curve. Fig.3 shows the result when octaves gradient function is used:
Fig.3 second derivative when the gradient function is used.
Still, the expected curve (which is -sin(x)) is visible, but we have a considerable amount of noise. This is due to the nature of the gradient function, which only considers the difference of two subsequent x values. This is ok for many applications but it has its limitations for noisy data.

How can one improve the derivative? One way is to make a fourier filtering afterwards. Another way is to apply a "smoothed" or "filtered" gradient. The basic idea here is to include more than 2 points in the differentiation process.

I'm not very good a theory. There is a very sophisticated explanation on the page of

Pavel Holoborodko
("Numerical Methods / Numerical Differentiation"). Here i found two Algorithms I implemented for octave.

The first one is the "low noise Lanczos Gradient":

function [x2,y2] = lanczos_gradient(x1,y1,DOrder);
%
%  -----------------------------------------------
%  usage :
%            [x2,y2] = lanczos_gradient(x1,y1,o);
%
%  parameters :
%
%   x1 : x values (x2 will be cropped by 2 times o)
%   y1 : function values to be list to differenciated
%   o  : order of Lanczos-Differentiation (odd number)

%  IMPORTANT : x1-values have to be aequidistant !
%
%  -----------------------------------------------
%
  NumPoints = length(y1);
  h = x1(2)-x1(1);
  y2=[];
  m = (DOrder-1)/2;
  for i=m+1:NumPoints-m   
    NewValue = 0;
    for k = 1:m
      %printf("\n NumPoints %d und %d ",i+k,i-k)
      NewValue = NewValue + 3/h * k * (y1(i+k) - y1(i-k)) / (m * (m+1) * (2*m+1));
    endfor
    y2 = [y2,NewValue];
  endfor
  x2=x1(m+1:NumPoints-m); 
endfunction
 

 The second one is the "Smooth noise-robust differentiator":

function [x2,y2] = filtered_gradient(x1,y1,DOrder);
%
%  -----------------------------------------------
%  usage :
%            [x2,y2] = filtered_gradient(x1,y1,O);
%
%  parameters :
%
%   x1 : x values (x2 will be cropped by 2 times O)
%   y1 : function values to be list to differenciated
%   O  : order of the Filter (an odd integer number)
%
%  IMPORTANT : x1-values have to be aequidistant !
%
%  -----------------------------------------------
%
  NumPoints = length(y1);
  h = x1(2)-x1(1);
  y2=[];
  m = (DOrder-1)/2;
  for i=m+1:NumPoints-m  
    NewValue = 0;
    for k = 1:m
      %printf("\n NumPoints %d und %d ",i+k,i-k)
      NewValue = NewValue + 3/h * k * (y1(i+k) - y1(i-k)) / (m * (m+1) * (2*m+1));
    endfor
    y2 = [y2,NewValue];
  endfor
  x2=x1(m+1:NumPoints-m);
endfunction


For details how these two work, please have a look on Pavels page. If you're not sooo much into theory (as i do) let's just apply these two on data. For the example above, the result is shown in Fig. 4

Fig.4 the two Algorithms give derivatives very close the the noise-free case


As can be seen, the curves look much better.

Please keep in mind: we can apply these algorithms to our data. And yes, the results look very good (especially when we know the analytical result). But be carefull: by "smoothing" noise out, we alway reduce the amount of information in our data. So it's a tradeoff and one has to be very careful.

Have fun  playing around with this.




Here's the code to produce the pictures:

clear all;
more off;

LO = 11; % the 'order' of smoothing



x = linspace(0,8,1000);
noise = 0.0001*(rand(1,1000)-.5);
y = sin(x)+noise;


% create the filtered gradients
[xll,yll] = filtered_gradient(x,y,LO);
[xl,yl]=lanczos_gradient(x,y,LO);
[xll2,yll2] = filtered_gradient(xll,yll,LO);
[xl2,yl2] = lanczos_gradient(xl,yl,LO);



%create the simple gradient

ysimp = gradient(y)/(x(2)-x(1));
ysimp2 = gradient(ysimp)/(x(2)-x(1));



  

figure(1)
plot(x,y, "linewidth",2)
%title "sin(x) + small amount of noise"
xlabel "x"
ylabel "y"
ax(1) = gca();


figure(2)
plot(x,sin(x),"linewidth",2,
    x,y,"o","markersize",6,"linewidth",2)
legend("sin(x)","noisy sin(x)")
xlim([1.5,1.65])
ylim([0.996,1.001])
xlabel "x"
ylabel "y"
ax(2) = gca();

figure(3)
plot(x,ysimp2)
xlabel "x"
ylabel "y''"
ax(3) = gca();

figure(4)
plot( x, -y, "linewidth",2,
      xl2, yl2,"linewidth",2,
      xll2, yll2,"linewidth",2)
xlabel "x"
ylabel "y''"
legend("-sin(x)","lanczos gradient","filtered gradient")
ax(4)=gca();

set(ax,"linewidth",2,"tickdir","out","ticklength",[0.002,0.002])
set(ax([1,3,4]),"ylim",[-1.2,1.2]) 
 


%create the plots

figure(1)
print("sinnoise.png","-dpng","-F:Helvetica:11","-S900,550")

figure(2)
print("sinnoisedetail.png","-dpng","-F:Helvetica:11","-S900,550")

  figure(3)
  print("sinnoisesimple_gradient.png","-dpng","-F:Helvetica:11","-S900,550")

  figure(4)
  print("sinnoisegradients.png","-dpng","-F:Helvetica:14","-S900,550")



August 27, 2012

How to get rid of old google chrome versions on your Mac

Google chrome is a great browser, which automatically keeps itself up to date. For some reasons, it keeps all older versions which eat up disk space. This becomes obvious when your hard drive storage is not *that* big (as on my Macbook Air with a 64 GB SSD).

I wondered why my free disk space constantly shrinks. On linux, baobab is a great tool to visualize the file sizes on you system. I googled for alternatives on mac and found a java program called jdiskreport, which does the job.

In my case, the google chrome app folder was about 2 gigs big, growing since about 1.5 years of usage. It turned out that over 90 percent of that usage was taken by old versions of chrome which aren't useful anymore.

Where are the versions located?

the standard path is
/Applications/Google Chrome.app/Contents/Versions

How to get rid of the older versions? 

You can simply delete them.
Just type

cd /Applications/Google Chrome.app/Contents/Versions

into a terminal and use ls to see which versions are installed.

now you should  delete all the versions but the actual version (with the highest number), e.g.

sudo rm -fr  21.0.1180.79

to delete the version 21.0.1180.79.
The command remove (rm) has to be used with sudo to gain administrative rights.

Another, even simpler possibility ist to just delete chrome from the /Application folder and download and reinstall chrome.

July 23, 2012

octave quickies: change the position of the y-axis from left to right

Sometimes (e.g. when you have multible plots on one page) you want the y-ticks  and y-labeling on the right side of the plot (default is left).

This can be done by changing the current axis properties:

set ( gca(), "yaxislocation","right")



May 20, 2012

high quality plots in octave

When you're about to publish in a scientific journal, one problem appears: how do I create high quality plots for my paper?


You can use all kinds of expensive programs (Origin, for example), but free software is as well capable of doing this. For print, you definitely want to use some vector based image formats, of which EPS (encapsulated postscript) is the most common.

Well let's look at a simple eps, created with octave:

generic EPS plot. we have to work on this.
 code for this example is below
This looks quite poor:

  • the proportions of data points, axis, ticks are not good
  • the fonts look messy
  • there is no color
The good news is: we definitely can improve this. The bad news: i tried the example on different machines and depending on the octave version and the installed postscript drivers / conversion tools, the results differ. So, as always the parola is: play with the given values :)

A good idea is to look whether the journal you're intending to publish in has any style guidelines. Often, detailed information about the size, font, and axis labels can be found there.

A list of task that can be done to improve the above plot:

  • we'll use colors
  • we'll use another font: a sans-serif is standard in scientific plots (e.g. Helvetica or Arial). The font size will be changed.
  • we'll change the proportions of the image. The data points and lines are the most important issue on our plot, so we'll make them quite big and fat
  • the outer proportions of the image are changed. In a two-column layout which is common in many publications, often a width-to-height ratio of 2:1 is demanded
  • we'll  make the axis a little thicker. In some cases, it is desirable to have the ticks outside
  • Often, the axis are heavily overloaded, there is too much information distracting from the plot content. This can be reduced by leaving some ticklabels out.
The result looks more nicely:

an improved version.


I hope this is a good starting point. The above pictures where created on an Ubuntu 10.04 machine with octave 3.6.x. Now the code:


clear all;
more off;
%
% create some data: polynoms with random noise
%
points = 15
x = linspace(-5,5,points);
y1 = 0.3*x.^2 + 5 *(rand(1,points)-.5)+15;
y2 = 0.1*x.^3 + 5 *(rand(1,points)-.5);
%
% fit some curves to the noisy data
%
[p1,s1] = polyfit(x,y1,3);
[p2,s2] = polyfit(x,y2,3);
tx = linspace(min(x),max(x),500)
%
% create a generic plot
%
figure(1)
plot(  x,y1,'x',
  x,y2,'o',
  tx,polyval(p1,tx),
  tx,polyval(p2,tx))
xlabel 'X'
ylabel 'Y'
legend('y1',
'y2',
'fit1',
'fit2')
print('generic.eps')


%
% create a more advanced version of the plot
figure(2)
%
% in the plot command
% - define markersize
% - define linewidth
% - assign colors
%
plot(  x,y1,'x'
,'linewidth',6
,'color',[1,0,0]
,'markersize',6
  ,x,y2,'o'
  ,'color',[0,0,1]
  ,'markerfacecolor',[0,0,1]
  ,'markersize',6,
  tx,polyval(p1,tx),
  'linewidth',3,
  'color',[1,0,0],
  tx,polyval(p2,tx),
  'linewidth',3,
  'color',[0,0,1])
%
% now let's adjust the axis:
% - make the line thicker
% - make the ticks outside
% - adjust ticklength
% - use custom ticks
% - apply custom ticklabels
%
set(gca(),
'linewidth',2,
'tickdir','out',
'ticklength',[0.005,0.005],
'xtick',[-6:6],
'xticklabel',{'','-5','','','','','0','','','','','5',''},
'ytick',[-20:10:30],
'ylim',[-20,30],
'yticklabel',{'','-20','','0','','20',''})   
xlabel 'X'
ylabel 'Y'
%
% the legend shall appera on the bottom right of the plot
%
legend('y1','y2','fit1','fit2','location','southeast')

% let's print this thing to a eps.
% we use a custom size of 900x450,
% a defined Font and Size
% and the depsc2 driver (EPS 2 color) driver
print('test2.eps','-S900,450',
'-depsc2',
'-F:Helvetica:12',
'-tight'
)


I guess this is a good starting point for creating printable plots that look good. When you're not sure what can be done: Have a look at the attributes of your graphic and axis objects:

get( gcf())   %get current figure
get( gca())  %get current axis

you can set it with the set command

set( gca(), 'property','value')

Have a look at the octave manual. The mathworks website can also be useful, sometimes ;-)

May 17, 2012

gimp 2.8 released - also available for mac now

gimp 2.8 startup screen
Well, this definitely could be a leap forward. Gimp 2.8 was released (see http://www.gimp.org/). Even before, gimp was my pix editor of choice on my linux as well on my mac machines. One big drawback in my eyes was the splitting into multible windows, which is especially a problem on OSX.

Well, among other features, there is a possibility to put all dialogs and windows into one single window in the new version (see red circle in the window below).

new gimp features that leap out: you can select that all dialogs are shown
in one single window (red). There are sliders to adjust e.g. the brush size (blue).
There are many other improvements, which for example are a better usability for the adjustment of parameters. Now you can directly slide the parameters as well as type in the value (see blue rectangle above).

I also see some steps forward in some tools, e.g. the free hand selection tool which can be used more easily now.

All in all i'm quite happy with this release. I've never felt the demand to use photoshop or similar. Gimp let's me do anything, even complex tasks like 'shopping' (or 'gimping'?) different photos into each other. And now, the usability is strongly increased as well as the fun using it.

See:


UPDATE:
just for the record: there's another group porting Gimp to Mac, have a look at http://www.partha.com/

April 24, 2012

Some statistics on three-letter username twitterers

While examining how many three-letter twitter names still are available, i wondered if some more general information about the behavior of the twitter users can be gained. So I crawled all the three letter names using the twitter API, which took about two weeks to run (as the API calls are limited). As a control group, i also crawled about the same number (40 k) 'average' users, that have some twitter id between zero and the actual number of accounts (roughly 540 millions as on march / april 2012). I got about 48000 and 36000 valid user profiles in the time span between march and april 2012.


1. the bare numbers

3-letter-users all users
signup date (mean) Nov 2008 Feb 2011
last activity (mean) Mar 2010 May 2011
users active in 2012 (percent) 26.2 36.2
tweets (mean) 1208.6 277.8
tweets (max) 105080 309239
following (mean) 148.0 45.2
following (max) 51183 34463
follower (mean) 4521 42.0
follower (max) 4231853 55151


what can be seen from that?
  • basically the three letter names users are "oldies" on twitter, which is no surprise (the short names are taken first)
  • most users are inactive
  • 3 letter users have more tweets, more followers and are following more people (which maybe also can be addressed to their longer membership)


2. most twitter users are lazy bums!

figure 1: histogram of activity

Figure 1 shows the a histogram of the users, plotted over the difference between their first and last activity. It is clearly visible: most twitterers immediately stop doing anything after their signup. But some percent of the users still are active after almost six years. Note there's this oscillatory behavior for the blue curve, can anyone explain this?

figure 2: last vs first activity

Another way to look at the activity is shown in figure 2. Here, we basically see a 2D-histogram of the last activity. The x-coordinate is the date of signup, the y-coordinate denotes the last activity.
The brighter the dots are, the more users fall into a combination of signup/last activity.

For both cases considered, we basically see the same behavior. The diagonal line shows us, that many users immediately stop twittering after their signup. The horizontal line at (y is about 2012.2) represents the users still active.
The difference between 3-letter-users and the average users is visible considering their signup date. The 3-letter users clearly started earlier, many of them still are active today. On the right picture (all users), it is visible when the 'hype' about twitter begain, about early/mid 2009.
What can be learned from this:


Most of the people stop using twitter (actively) after their signup. But if you once started using twitter, you somehow stay sticked to it :D





3.  Follower vs following


figure 3: follower vs following
Let's see how it is about followers versus following (note that we have logarithmic scales here). Here we generally see similar behavior, when the groups are compared. In both cases, there is a big amount of users who don't follow anyone and also have no followers (bright square in the lower left corner).
There seems to be some natural limit of the number of users you follow, about 1000 to 10000. I guess who is following more people is probably a bot. It is interesting that the overall trend is that you have about the same number of followers as people you follow. But there are also some users that seem to interest an over-proportional number of people.
Here, the 3-letter users form a more nonuniform group with some extreme outliners.


4. Follower vs Tweets


figure 4: follower vs tweets

To gain followers, it is (at least when you aren't some celebrity) necessary to create tweets. For both groups, the overall trend is that you gain about one follower every 10-50 tweets. Again, the three-letter user group is more nonuniform than the average users. The natural limit seems to be about 20000 tweets.

April 21, 2012

octave quickies: customizing the colorbar

When you create colormap or 3D-plots, you may wish to customize the axis of your plot (e.g. change the linewidth, etc). One more difficult task  is to change the colorbar of the plot.

you should customize the colorbar to your needs!


As in matlab, in octave things like lines, plot points, axis and so on are basically graphical objects (see the octave manual). To change the properties of some object, you have to know its handle (although for some, e.g. xlim, there are special function to do so).

One thing I stumble across regularly is the colorbar, where i want to change the linewidth and the ticks. The octave manual is not very verbose at this point, but you basically can use the matlab doc.

Example: we create a blank plot with a colorbar. Then we aquire the colorbar handle and play around with the colorbar properties:


figure(1)
  caxis([0,50])            %change the range
  colorbar()
  colormap( summer(250))   %use some different map than jet


                           %get the  colorbar handle
  cbh = findobj( gcf(), 'tag', 'colorbar')
                          
  set( cbh,                %now change some colorbar properties
    'linewidth', 2,
    'tickdir', 'out',
    'ticklength',[0.005,0.005],
    'ytick', [0, 10, 23, 42, 50],
    'yticklabel',{'zero','ten','23','42','fifty'})


As for all objects, you get get a complete list of the actual colorbars properties with the get function:
get( cbh) 

Example : you can change the dimensions (position, height, width) of the colorbar using the position property:

%get the standard value
octave-3.4.2:8> get(cbh,'position')
ans = 0.827500   0.110000   0.046500   0.815000
%set new value
set(cbh,'position',[0.8275 0.11, 0.02325 0.81500]) 
%(makes a slicker colorbar)









April 9, 2012

"Roadside Picnic" from Arkady and Boris Strugatsky.



cover of one edition of the book from wikipedia.

Maybe the derivatives of this novel are more famous than the book itself: there's a soviet movie of the name "Stalker" from Andrej Tarkovsky, which has its own magic. There are also two or more games called S.T.A.L.K.E.R. (or similar, i'm not a gamer type). Anyways, it's really worth reading the book!
The plot can be sketched the following: someone (or something) hits the earth and, as a result, the so-called "zones" are created. In this zones (which have the size of a small city), strange things happen and some laws of nature seem to be completely different.
Most of the time, the reader sees the events through the eyes of a guy named  Redrick "Red" Schuhart, which is a "stalker". These guys illegally enter the zones and collect items of alien technology which they sell to anybody who pays them a good price. Around the zones, an community of scientists, military forces and mostly dubious folks accumulates.

One strength of the story is the way the Strugatsky brothers describe this strange world in and around the zones, whose mysteries remain unveiled, though. More important: the description of the people living their lifes there is really great. This becomes more obvious in the second part of the book: despite of all modern sciences, e.g. the great advancements in physics and chemistry, the "event" and its implications leave the people perplexed. They strongly are altered not only in a physiological but also in a psychological way.
All attempts to understand the zones and the technology found there seem helpless. The people somehow see the hazard originating from the zones but at the same moment they seem to be addicted to its mysteries.

Well, this book really got me into thinking. Despite its excellent style of writing, it offers what every good SciFi should do: a small journey to regions or situations somehow extraordinary.



Read more in the wikipedia:
http://en.wikipedia.org/wiki/Roadside_Picnic

March 29, 2012

simple algorithm for 2D-Peakfinding

When it comes to analyzing data, a common task is to identify peaks in an x-y dataset. There are several ways to do that. Maybe the first idea that would come into one's mind is to look for zeros in the derivatives. This works fine for smooth theoretical curves but fails when noise is present (as it is certainly all kinds of experimental data).
To get rid of noise you can apply a low-pass filter on your data, but in some cases this is not desired. A simple approach is to use an algorithm that can does

  1. find peak candidates that are above some user-defined threshold level
  2. find out for each candidate whether it is the local maximum of a user defined environment
  3. return the valid peaks
the algorithm is able to find peaks even in noisy data


I've implemented such piece of code for octave that does the job. The code below created the picture shown. The function expects three input parameters: the environment for maxima, the data list and a threshold value.



function peaklist = SimplePeakFind ( environment, data,  thresh)
%
% ----------------------------------------------------------
%
% function peaklist = SimplePeakFind (  environment, data, thresh)
%
% finds the positions of peaks in a
% given data list. valid peaks
% are searched to be greater than 
% the threshold value.
% peaks are searched to be the maximum in a certain environment
% of values in the list.
%
%  ----------------------------------------------------------

  listlength = length( data );  
  peaklist = zeros(listlength,1); % create blank output
  SearchEnvHalf = max(1,floor( environment/2));
  %
  % we only have to consider date above the threshold 
  %
  dataAboveThreshIndx = find (data >= thresh);
  for CandidateIndx = 1 : length(dataAboveThreshIndx)
    Indx = dataAboveThreshIndx(CandidateIndx);  
    %
    % consider list boundaries
    %
    minindx = Indx - SearchEnvHalf;
    maxindx = Indx + SearchEnvHalf;
    if (minindx < 1)
      minindx = 1;
    end
    if ( maxindx>listlength)
      maxindx = listlength;
    end
    %
    % data( CandidateIndx ) == maximum in environment?
    %
    if (data(Indx) == max( data( minindx : maxindx)))
      peaklist(Indx) = Indx;      
    end
  end 
  % finally shrink list to non-zero-values
  peaklist = peaklist( find( peaklist));
end


In many cases, this works fine for me. Let's look at a simple test case: we numerically create a number of gaussian pulses with random widths, positions and heights. Then we add noise. 


clear all;
more off;
N = 1500;
T = linspace(-20,20,N);  %x-vector
% create some random gaussian peaks
Npeaks = 9
ppos = T(round(rand(1,Npeaks)*N))
pheight = rand(1,15)*1;
pdur    = rand(1,15)*1;
data = zeros(1,N);
for i = 1:Npeaks
  data = data + pheight(i) * exp(- ((T-ppos(i))/pdur(i)).^2);  
endfor
% apply noisenoise = rand(1,N)*0.1;
datanoise = data+noise;
% Find peaks that are maxima in an
% environment of 25 data points and that
% bigger than 0.15 (noise level)
peaklist = SimplePeakFind(25, datanoise, 0.2);
%Plot result
figure(1)
plot(T,datanoise,"linewidth",2, 
     T(peaklist), datanoise(peaklist),
     "o","markersize",5,"markerfacecolor",[1,0,0])
legend("noisy data","found peaks")
xlabel "x"
ylabel "y"
set(gca(),"tickdir","out","linewidth",2,"ticklength",[0.005,0.005])

The result is shown in the picture above: all 9 peaks are identified correctly. Depending on your data, you'll have to play around with the parameters environment and thresh. Having the the peak positions, further data analysis (like curve fitting etc) is much easier.


Links: of course people already thought of this problem:



March 26, 2012

simulating nonlinear oscillators using the julia language

In my post 'simulating nonlinear oscillators using octave' i showed you how to ... -- i guess you can read. Today i discovered, that a package similar to the odepkg also is included in julia. You just have to find it.


Basically it works quite similar, with some little tweaks.
There is a file called 'ode.jl' in the 'extras' folder located in the julia source folder. We just have to load this thing and have some ode solvers. The drawback is that up to now there  the only way (i found) to adapt the accuracy is to change the lines 


    rtol = 1.e-5
    atol = 1.e-8

in the function ode23
and
    tol = 1.0e-7
in the definition of oderkf{T}, which is a quite dirty method.

Another issue is that the only arguments of the ode solvers are up to now

(F::Function, tspan::AbstractVector, y_0::AbstractVector)


which means we have to hide additional parameters in our solution vector (and make its derivatives zero).

A code example, that works for me (asymmetric nonlinear potential)



# add the path to your juliasource/extras folder
load("/Users/user/juliadir/extras/ode.jl")


function  rhs_asym(t, x)
  #
  # position and velocity are stored in x[1] and x[2]
  #
  xp = x[1]
  vx = x[2]  
  #
  # the parameters are stored in x[3] and x[4]
  #
  k=x[3]
  alpha =x[4]
  dxp = vx
  dv = - (k * xp - alpha * xp^2)
  dx = [dxp; dv;0;0]   # the two zeros ensure that k and alpha  
                       # will not change
end




# problems can occur when you forget to give these values 
# explicitly as Floats ...
k=1.
alpha =0.2
tend=42.


initialDisplacement = 0.0
initialVelocity     = 1.
xini = [initialDisplacement; initialVelocity; k; alpha]


#
# to get higher accuracy, change the values of rtol atol or tol 
# in the ode.jl-file (or better in a copy of it).
#
# now let's integrate
(T, X) = ode45( rhs_asym, [0; tend], xini)



# we store the whole data in one matrix and save it, 
# so we can load and plot with e.g. octave
#
M=[T1 Xlin]
csvwrite("oscil.csv",M)

March 25, 2012

poor man's astronomy

moon, venus and jupiter are close together these nights
Light pollution is an issue these days. Due to the street lights and neon signs there's a glow over the night sky (at least in cities). Therefor it is quite difficult to see any stars. This may be the reason why the night sky is almost unknown to most people (including me), as there is almost no possibility to see it.
One way to overcome this problem is to surf the web or use programs. You don't have to spend a bunch of money into telescopes or such things. A great software tool is stellarium ( http://www.stellarium.org/ ). It allows you to look at the sky for any location on earth and any time. It also shows all kinds of different additional information, like star names etc. The best: it is free software and available for all platforms.

Anyway it is more fun to go outside and look by yourself: often it is sufficient to just get 1 km out of town and look up. A great help in identifying sky objects are smartphones. I just discovered that stellarium is also available for iOS, which is really nice. Up to now, i used a free version of star3map ( http://www.xyzw.us/star3map/ ) which also does the job. It shows stars, planets and also satellites.
Even without a telescope, i was able to identify the brightest stars and planets like saturn, mars, jupiter and venus.  The latter two are besides the moon the brightest objects right now, they are even visible within the city (see photo i took using my ipod). With tough luck i also saw the ISS, which a little more complicated as it moves very fast and its visibility depends strongly on the position of the sun.

March 24, 2012

the julia language

There's yet another language for numerical computing: julia (http://julialang.org). I stumbled on this reading the octave mailing list. From the description on the website this sounds quite promising to me.
Their approach is basically to use some dialect of matlab (the language), but with emphasis on computational speed. The use of a LLVM just-in-time compiler shall produce a boost in speed comparable to native C++.

On their site they also show some benchmark results, which are quite impressive. One nice thing is that julia (from git) compiles without any problems using Mac OSX Lion and the raw developer tools (without Xcode).  As far as I can see, almost all of the basic math functions in octave are already implemented.
A big drawback still is the missing of real plotting interface (it is basic 2D up to now).

One sad thing is that octave in almost all aspect is far behind the other products shown (julia, python, matlab, R,  javascript). Anyways, I think I'll stuck to octave at least for a while from now, due to the following reasons:
  • matlab is no alternative to me (you know, 'free' as in freedom)
  • octave and especially octave-forge provide a plethora of additional functions that make the life quite easy (e.g. the odepkg)   (see update 2)
  • the programming experience in octave is easy and  comfortable to me. Not comparable to the pains of python, javascript or C 
  • considering computation speed, up to now i didn't find things like arrayfun and the whole bunch of struct functions. I doubt that any loop-based calculations can win against these ...   (see edit below ...)
  • despite the whole fuckup of getting octave and gnuplot talk to each other on Macs the plotting in octave is quite awesome and powerful. And a comprehensive plotting interface is a MUST in my opinion. (see update 3)
Anyways, let's not forget that julia still is in an early stage of development. I'm curious whether the guys can push this further. Certainly it is good to have a fresh wind in the free software computing scene. Let's have an eye on this.

[update Mar 25]
some blogposts elsewhere concerning julia:

[update 2, Mar 26]

concerning computational speed:

i've tested julia versus octave. My test case was to calculate some transform function for a 2d matrix of initial values. for each value, about 8000 auxiliary matrices have to be calculated.
My octave code for this problem is highly optimized, using pararrayfun and some other tricks. For julia, i only coded some stupid nested for-loops.
Well, what can i say? At least for this problem, julia is about 10 times faster than octave, even without further optimization. That's really impressive ...

From the manual:
"In general, unlike many other technical computing languages, Julia does not expect programs to be written in a vectorized style for performance. Julia’s compiler uses type inference and generates optimized code for scalar array indexing, allowing programs to be written in a style that is convenient and readable, without sacrificing performance, and using less memory at times."
concerning completeness
i found out julia already offers ode23, ode45 etc. you only have to load the file ode.jl from the 'extras' folder. but up to now, i did not find the time to play around with this ...

[update 3, Apr 03]

from the mailing list it is visible that there are strong efforts to create an plotting interface. See e.g. the wiki on the git repository https://github.com/JuliaLang/julia/wiki/Graphics


[update 4, May 31]
Unfortunately I've not so much time to have a closer look at Julia right now. But there seem to be strong improvement considering plots right now, according to the dev list. Besides 2d, 3d and imagesc seem to be implemented in a addon called gaston.

March 22, 2012

Simulating nonlinear oscillators using octave

Hi there,

numerics are usefull especially when nonlinear problems are considered. Here, I want to show how to calculate the trajectory for some simple one-dimensional oscillators.
You can imagine such an oscillator as a little mass attached to a spring. There is a point of rest, where all forces vanish. When the oscillator is displaced, a force appears originating from the spring.

When energy is put in the system, the mass oscillates. Depending on the form of the potential (which basically is the integral of the force with respect to the displacement)  we can distinguish harmonic and anharmonic oscillators.

From textbooks (or wikipedia) we know the following items, which we want to check using numerics:
  • for the harmonic (linear) oscillator, the trajectory is a pure sine wave of some frequency
  • for the anharmonic (nonlinear) oscillator, more frequency components appear:
    • there are only odd components for the symmetric nonlinear potential
    • there are odd and even components for the asymmetric nonlinear potential


1. The linear (harmonic) oscillator
This is a well known textbook example. The potential energy of the harmonic oscillator is simply a parabula:

Epot = 1/2 k x^2
harmonic potential. The potential energy grows
quadratically with the displacement.

The resulting force is the negative derivative of the potential:


F = - dE/ds = - k x

This is Hooke's law which is a simple model for springs.

Force acting in the harmonic potential
It is also the reason why the harmonic oscillator is called the linear oscillator: the force is directly proportional to the displacement (linear dependency).
The equation of motion is can be derived from the force

d^2 x /dt^2 = F/m = -k/m x

where x is the displacement and k the spring constant.  We can easily give the solution for the equation:

x(t) = A1 cos( k^2/m^2   t   + A2) 

where A1 and A2 are two constants that have to be adapted to the initial values of the problem. The numerical solution using ODEs can be done following the steps described in one earlier post. As the linear oscillator is a special case of the more general problem i'll give the code later in this text.

After integration, we get a trajectory looking as the following  (k=1, initial position =0, initalspeed = 2)
trajectory of the harmonic oscillator
This looks like a nice, pure sine wave (as expected). To get more insight, we use the fourier fransform
of the trajectory x(t).  You have to be careful using results from octaves ode solvers, because in most cases the output points are not equally spaced in time. Using the interpolation function interp1 can help to linearize the data.
The fourier spectrum looks like the following:


fourier components for the harmonic oscillator:
 there is only one frequency present
As expected, we only see one single frequency component, which means we indeed have a harmonic oscillation here.

2. The anharmonic / nonlinear oscillator
Considering the example with the spring, Hooke's law is valid for small displacements. When the displacement gets bigger, additional terms can appear. We want to consider two cases: a symmetric and an antisymmetric nonlinear potential. We give the potential energy:


Epot,sym = 1/2 k x^2 + 1/3 alpha |x|^3

Epot,asym = 1/2 k x^2 + 1/3 alpha x^3


here alpha is a nonlinearity constant. The curves of the potential are shown below:

Two aharmonic potentials, compared with the
harmonic one. They differ in their symmetry
Again, the forces occuring are the derivatives of the potential with respect to the displacement:


Fsym = - dEpot,sym/ds = - (k x + alpha |x| x)
Fasym= - dEpot,asym/ds = - (k x + alpha x^2)


We see additional dependencies between the force and the displacement  which are of the order of x^2. This is why these oscillators are called nonlinear oscillators. The difference to the linear case becomes clear when we look at the force plot:

Forces derived from the potentials above. Note that the
asymmetric force is equal to the symmetric force
for positive displacements, but differs for negative
displacements.


For small displacements, the forces are almost identical. They begin to differ  considerably, when the displacement grows. For the symmetric potential, the absolute value of the force is equal for -x and x. For the asymmetric potential, the force for negative displacements is smaller than for positive values.

The equations of movements can be written as

(symmetric)
d^2 x /dt^2 = F/m = - (k/m x + alpha/m |x| x)

(asymmetric)
d^2 x /dt^2 = F/m = - (k/m x + alpha/m x^2)

Again, we can easily solve this with the odepkg (see code sample below)I used the same input energy as for the harmonic example resulting in  the following trajectories:

Trajectories for the nonlinear oscilators (two kinds), compared with the one
 of the harmonic oscillator


The two new trajectories basically also show  sine osciallations. As the force for the symmetric potential is stronger for big displacements, the maximum amplitude is smaller. This also results in a higher frequency for the oscillation. For the asymmetric potential, the oscillation is shifted  slightly towards negative values in the mean.
We can get the fourier components of this oscillations when analyzing the time traces x(t) of the signals.
We have a closer  look on the symmetric harmonic potential:
spectrum for the symmetric nonlinear oscillator.
Only odd multibles of the base frequency are generated
Besides the fact, that the base frequency is higher than for the harmonic oscillator, we see an additional frequency component! Its value is three times the one of the base frequency (third harmonic). Also, energy is converted into the fifth harmonic.

A similar behavior can be seen for the asymmetric potential:




spectrum for the asymmetric nonlinear oscillator.
Here, even and odd multibles of the base frequency are generated
Here we have generated additional frequencies at two, three and four times the base frequency. The conversion efficiency is higher than for the symmetric potential. 

As a result, the simulations show the expected behavior. We can see that nonlinear oscillators are capable of generating multiples of the base frequency.  This is extensively used in nonlinear optics, where light is sent through nonlinear crystals. The crystal structure is responsible for the potential, in most cases they have asymmetric potentials.
When e.g. light from a laser (at a wavelength of 1064 nm) of high intensity passes such a crystal, green light (532 nm) can be generated.

see 


  • https://en.wikipedia.org/wiki/Second-harmonic_generation



  • https://de.wikipedia.org/wiki/Frequenzverdopplung (german)


  • for further details.


    3. Code to solve the linear and nonlinear oscillators using octave
    clear all;
    more off;


    %
    % right hand side of the ODE for the symmetric harmonic potential
    %
    function dx = rhs_sym(t, x, k, alpha,tend)
      t/tend     %just prints the progress 
      xp = x(1);  % position
      vx = x(2); % velocity
      dxp = vx;   % position change
      dv = -(k * xp + alpha * xp * abs(xp) );  %velocity change
      dx = [dxp, dv]; %give the derivative
    endfunction




    %
    % right hand side of the ODE for the asymmetric harmonic potential
    %
    function dx = rhs_asym(t, x, k, alpha,tend)
      t/tend
      xp = x(1);
      vx = x(2);  
      dxp = vx;
      dv = -(k * xp + alpha * xp^2);
      dx = [dxp, dv];
    endfunction




    % program parameters
    k =1;
    alpha =0.2;
    initialDisplacement = 0;
    initialVelocity     = 2;
    xini = [initialDisplacement, initialVelocity];




    tend = 15;  %integrate up to that time
    % set integration options
    options = odeset( 'InitialStep',tend/1e3,
    'MaxStep',tend/1e3,
    'RelTol',1e-6,
    'AbsTol',1e-6,
    'NormControl','on')




    % linear case, using alpha = 0
    [T1, Xlin] = ode45( @rhs_sym, [0, tend], xini, options, k, 0, tend);


    %NL case, symetric potential:
    [T2, XnlS] = ode45( @rhs_sym, [0, tend], xini, options, k, alpha,\
    tend);
         
    %NL case, symetric potential:  
    [T3, XnlA] = ode45( @rhs_asym, [0, tend], xini, options, k, alpha,\    tend);


    % plot the result
    plot (T1, Xlin(:,1),'color',[0.5,0.5,0.5],
          T2, XnlS(:,1),'.','color',[0,0,1],
          T3, XnlA(:,1),'.','color',[1,0,0]);
    xlabel 'time'
    ylabel 'displacement'




    Hint: for the analysis of the frequency components shown above, the integration time has to be set to a higher value and the odeoptions have to be changed to higher accuracy...