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.
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]
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]
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]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With