Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numerical Differentiation of List in Mathematica

How do I numerically differentiate a list in Mathematica without first fitting it to a mathematical expression (ie. using FindFit)?

Specifically, I would like to find the point of maximum slope in the list.

I have considered using Differences and finding the maximum difference, but noise in the data renders that inaccurate. Smoothing the data with MovingAverage does not help either.

Thanks in advance.

like image 975
flamearchon Avatar asked Jun 28 '11 06:06

flamearchon


1 Answers

You could try a ListConvolve with a Gaussian kernel to smooth your data. One of the nice features of this is that the derivative of the convolution with a Gaussian kernel is equivalent to convolving with a derivative of the Gaussian kernel.

Here is some sample data:

data = Table[Sin[x] + .5 RandomReal[{-1, 1}], {x, 0, 6 \[Pi], .05}];
ListLinePlot[data]

enter image description here

This is a simple convolution with a Gaussian kernel:

data2 = 
  Block[{\[Sigma] = 2}, 
   ListConvolve[
    Table[1/(Sqrt[2 \[Pi]] \[Sigma]) E^(-x^2/(2 \[Sigma])), 
         {x, -2 , 2, 1/10}
    ], data, {11, 11}
   ]
  ];  
ListLinePlot[data2]

enter image description here

Convolution with the first derivative of a Gaussian:

data3 = 
  Block[{\[Sigma] = 1}, 
   ListConvolve[
    Table[-((E^(-(x^2/(2 \[Sigma]))) x)/(Sqrt[2 \[Pi]] \[Sigma]^2)), 
      {x, -2 \[Sigma],2 \[Sigma], \[Sigma]/10}
    ], data, {11, 11}
   ]
  ];
ListLinePlot[data3]

enter image description here

You may want to play with the sigma parameter to see what gets optimal results in your case.

The whole theory behind this is called Scale Space. Note that the above statement about convolution holds for continuous space. For a discrete implementation, the kernel could be chosen somewhat better.

Note further that, just as MovingAverage, a convolution can move the features of your data.

like image 65
Sjoerd C. de Vries Avatar answered Nov 24 '22 00:11

Sjoerd C. de Vries