savitzky golay derivation graph plot

10 views (last 30 days)
Alexai
Alexai on 15 Dec 2022
Commented: Alexai on 29 Dec 2022
I want to draw a first differential graph and a second differential graph using savitzky golay in the Matlab plot, which code should I use?
Here's an example of a code. x1=[1 2 3 4 5]; x2=[1 5 2 9 4]; x3=[3 2 1 6 4]; y=[500 505 510 515 520]; %this is wavelength plot (y, x1) hold on plot (y, x2) hold on plot (y, x3) I'm going to plot three graphs(x1,x2,x3)

Accepted Answer

Sai Kiran
Sai Kiran on 23 Dec 2022
Hi,
I understand that you want to calculate the first & second differential using the Savitzky-Golay filter. This can be calculated by the 'sgolayfilt' function.
sgolayfilt(x,order,framelen) where x is an array on which we perform the differential and framelen is the size of the x.
Please refer to the below documentation for more information on sgolayfilt function.
% Load the signal to be filtered
x1 = [1 2 3 4 5];
x2 = [1 5 2 9 4];
x3 = [3 2 1 6 4];
y = [500 505 510 515 520]; % this is the wavelength
% Compute the first derivative of each signal using the Savitzky-Golay filter
dx1 = sgolayfilt(x1, 1, 5);
dx2 = sgolayfilt(x2, 1, 5);
dx3 = sgolayfilt(x3, 1, 5);
% Plot the first derivative of each signal
figure;
plot(y, dx1);
hold on;
plot(y, dx2);
plot(y, dx3);
xlabel('Wavelength');
ylabel('First derivative');
legend('x1', 'x2', 'x3');
% Compute the second derivative of each signal using the Savitzky-Golay filter
ddx1 = sgolayfilt(x1, 2, 5);
ddx2 = sgolayfilt(x2, 2, 5 );
ddx3 = sgolayfilt(x3, 2, 5);
% Plot the second derivative of each signal
figure;
plot(y, ddx1);
hold on;
plot(y, ddx2);
plot(y, ddx3);
xlabel('Wavelength');
ylabel('Second derivative');
legend('x1', 'x2', 'x3');
I hope it resolves your query!! Let me know if you have any questions.
Regards,
Sai Kiran Ratna

More Answers (0)

Community Treasure Hunt

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

Start Hunting!