# How can I find the filter bandwidth of Savitzky-Golay filtering?

4 views (last 30 days)
Kalasagarreddi Kottakota on 13 Jan 2023
Commented: Paul on 14 Jan 2023
The matlab function sgolayfilt(x,order,framelength) takes polynomial order and framelength as input arguments. As savitzky acts as a lowpass filter. Is there a possibility to find the filter bandwidth based on the input arguments (order and framelength)?

Bruno Luong on 13 Jan 2023
Edited: Bruno Luong on 13 Jan 2023
sa-golay filter is just non-causal FIR filter, where the coefficients are computed as following
order=3;framelength=11;
B = sgolay(order,framelength,[]);
b = B((framelength-1)./2+1,:);
a = 1;
Here is the frequency response using freqz function
[h,w] = freqz(b,a,2001);
plot(w/pi,20*log10(abs(h)))
ax = gca;
ax.YLim = [-100 20];
ax.XTick = 0:.5:2;
ylabel('Magnitude (dB)')
Paul on 14 Jan 2023
I don't understand why you're asking about a causal filter. Did anything in my comment imply that the filter with coefficients b is causal?
To the contrary, the reason for the additional phase shift is that freqz assumes the inputs b,a describe a causal filter, but in this case it isn't. Hence the phase correction is needed.
Assume a simple case with a frame length of 5. The values in b are the coeffiecients of a noncausal filter of the form:
H(z) = b(1)*z^2 + b(2)*z + b(3) + b(4)*z^-1 + b(5)*z^-2
which can be rewritten as
H(z) = z^2 * (b(1) + b(2)*z^-1 + b(3)*z^-2 + b(4)*z^-3 + b(5)*z^-4)
With inputs b and a=1, freqz computes the response of the quantity inside the parentheses. The additional phase shift accounts for the z^2 outside the parentheses.
For completeness, the tf command, w/o additional arguments to specify otherwise, assumes that b, with a=1, describes a filter of the form
H(z) = b(1)*z^4 + b(2)*z^3 + b(3)*z^2 + b(4)*z + b(5)
which is why in that construction we'd divide by z^2.

Bjorn Gustavsson on 13 Jan 2023
Edited: Bjorn Gustavsson on 13 Jan 2023
QD suggestion:
x = randn(4096,1);
x_sgf = sgolayfilt(x,order,framelength);
fx = fftshift(fft(x));
fxsgf = fftshift(fft(x_sgf));
plot(medtilt1(abs(fxsgf)./abs(fx),3))
That should give you some sense of the filter characteristics. It is a rather brute-force, neither clever nor elegant way of calculating the frequency-response of the filter, but should give you a hint. The medfilt1 step I put in because the ratio became a bit spikey most likely due to numerical precision issues.
HTH

Image Analyst on 13 Jan 2023
I'm pretty sure the SG filter is a non-linear, non-causal filter like @Bruno Luong says. (The median filter is another such non-linear filter.) Thus there is no "frequency response" per se for the SG filter with certain parameters. You can only get the empirical frequency response if you know the actual input signal and compare actual, original signal to the filtered version of it. This is what @Bjorn Gustavsson showed. And of course it will vary depending on what your original input signal is.
##### 2 CommentsShowHide 1 older comment
Bjorn Gustavsson on 13 Jan 2023
@Image Analyst, I'm of the understanding that the SG-filters are linear filters (at least I recall reading it that way and getting a proper "aha-moment" when I read that the polynomial fitting actually corresponded to a normal convolution with clever filtering kernels), are you sure that you're right about the non-linear part?