Dear all, how to select the values for k and f, of savitzky-golay filter?

 Accepted Answer

You pick them to give the signal you want. There is no one right value for all situations. It's a judgment call. A longer frame width will take more points into consideration when fitting the polynomial and give a smoother curve. A higher polynomial will more accurately "hug" the actual/noisy training data, while a lower polynomial order will give a smoother, less "huggy" fit.

5 Comments

Thanks a lot, but still is there any range specified for window size and polynomial...I guess I can go till 10th degree polynomial?how much window size?
You're not going to get much smoothing if you do beyond about 3 or 4 in polynomial order. Indeed, you might actually get overfitting and false, spurious values.
The frame length must be at least 1 plus the polynomial order, and no more than the number of data points in your image. Why don't you just do a double for loop and try all combinations and see which looks best?
Thanks again, i am going to try. Actually, i was thinking that there must be a table representing some range values of appropriate window size and corresponding polynomial.
Try this:
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Make a noisy sine wave signal
x = 1 : 3000;
period = 500
y = sin(2*pi*x/period);
noiseAmplitude = 0.8;
y = y + noiseAmplitude * rand(size(y));
subplot(2,1,1);
plot(x, y, 'b-', 'LineWidth', 2);
grid on;
title('Noisy Signal', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Try orders of 1-4, and frame lengths of 3 to about a quarter of your signal length.
frameLengths = 3 : 8 : 201
for polynomialOrder = 2 : 4
for k = 1 : length(frameLengths)
% Make sure windowWidth is long enough given the polynomial order we are using.
windowWidth = max([polynomialOrder + 1, frameLengths(k)]);
% Make sure windowWidth is odd
if rem(windowWidth, 2) == 0
windowWidth = windowWidth + 1;
end
caption = sprintf('Smoothed Signal with Polynomial Order = %d, windowWidth = %d', polynomialOrder, windowWidth);
fprintf('%s\n', caption);
% Now smooth with a Savitzky-Golay sliding polynomial filter
smoothY = sgolayfilt(y, polynomialOrder, windowWidth);
subplot(2,1,2);
plot(x, smoothY, 'b-', 'LineWidth', 2);
grid on;
title(caption, 'FontSize', fontSize);
promptMessage = sprintf('Do you want to Continue processing,\nor Quit processing?');
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Continue', 'Quit', 'Continue');
if contains(buttonText, 'Quit')
break;
end
end
end
Adapt as needed for your signal.
really thankful....I am trying to implement this discussion on my problem.

Sign in to comment.

More Answers (0)

Products

Release

R2015a

Tags

Community Treasure Hunt

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

Start Hunting!