Problems with calculating Second derivative and SG smoothing

3 views (last 30 days)
Hi,
I am relatively new to Matlab and have encountered some problems with generating the same results as I got using another programme (Origin 8.6). The problem lies between calculating the second derivative and the S-Golay smoothing.
Currently I use the following code for the differentiation and smoothing:
TwoDerv = diff(Data(:,2),2);
TwoDervSG = sgolayfilt(TwoDerv, 2, 17)
This code generates data that looks like the following:
The previous programme I used created data that looked like this (when usng the same parameters):
I have tried varying the smoothing window, and still do not get the correct results. it appears to be smoothing correctly in the x axis, but incorrectly in the y axis.
Any help would be appreciated. Thanks,
Liberty
  1 Comment
Azzi Abdelmalek
Azzi Abdelmalek on 21 Jan 2013
Edited: Azzi Abdelmalek on 21 Jan 2013
Liberty, the png format is not allowed, use instead, jpg format for your images.

Sign in to comment.

Answers (2)

Thorsten
Thorsten on 21 Jan 2013
Edited: Thorsten on 21 Jan 2013
diff(x, 2) is just one way to numerically approximate the derivative. Origin may use a different method.
The result by Origin is obviously more smooth, so may be Origin has combined taking the derivative with some smoothing operation. What is striking are the y values that can only be smaller for a smoother signal, but are actually larger in Origin figure (or is there a 10-<number> scaling factor of the y-axis missing in the Origin figure)?
  1 Comment
Liberty Foreman
Liberty Foreman on 21 Jan 2013
yes I forgot to mention that the scaling on the origin picture is not accurate. Should be x10^-3. Smoothing is being used in both cases, S-golay smoothing polynomial 2 with window size 17 in both cases, which is why I am confused as to why the results differ so much. Yes I too believe it is the differentiation method. Do you know of any alternatives?
Thank you for your help so far.

Sign in to comment.


Shashank Prasanna
Shashank Prasanna on 21 Jan 2013
Liberty, to answer the more general question of approximating derivatives, there are numbers of ways and the simplest as Thorsten mentioned as well would be to take successive difference as you have done above. A more popular method used extensively is called Automatic Differentiation. Although I haven't used this particular implementation, you can try it out:
That said there is probably no way to compare results definitively unless we can find out what Origin is doing and repeat the same in MATLAB.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!