MATLAB Answers

How to search for the optimal parameters of a filter

24 views (last 30 days)
Z Liang
Z Liang on 26 Jul 2019
Commented: Star Strider on 2 Aug 2019
ep_mean is a 26xN matrix of a single trial, recorded through 26 electrodes, over N time points (roughly 220 points sampled)
I applied the sgolayfilt function to try to smooth the signal. I run into the problem that I don't know what values of the two parameters for sgolayfilt, the order and the frame length, are optimal for the filtering process. I also don't know if this is a technical or a philosophical question.
So I tried a grid sweep of parameter combinations using the following code:
snr_mat = [];
for i1 = 1:99
if mod(i1,2) == 0
c = i1+1;
c = i1+2;
for i2 = c:2:101
rd = i1;
fl = i2;
filt = sgolayfilt(ep_mean,rd,fl,[],2);
snr_mat(i1,i2) = snr(ep_mean,ep_mean-filt);
snr_mat(snr_mat>300) = 0; % to remove what I thought were artifacts, but now I think it's my setup/interpretation of the function that's flawed
y = sgolayfilt(ep_mean,3,21,[],2);
ylim([-10 10])
title('rd = 3; fl = 21','fontsize',16)
x = sgolayfilt(ep_mean,11,41,[],2);
ylim([-10 10])
title('rd = 11; fl = 41','fontsize',16)
[M1,I1] = max(snr_mat);
[M2,I2] = max(M1);
rd = I1(I2);
fl = I2;
z = sgolayfilt(ep_mean,rd,fl,[],2);
title(['rd = ' num2str(rd) '; fl = ' num2str(fl)],'fontsize',16)
To produce these figures:
The first figure plots the magnitude of the SNR after using each pair of order (rd) and framelength (fl) parameters. It seems the higher both are, and the closer they are to each other, the better then SNR. But the second figure clarifies what I think to be happening, which is that higher values for rd and fl do not necessarily filter better, they just more closely match the unfiltered signal so the snr function calculates a lower noise and higher ratio. This feels quite stupid to me, what I did I mean.
So my question becomes how to identify the optimal parameters (rd, fl) in the sgolayfilt function to filter a given signal. What metric can I employ to assess the performance of the filtering process? And is a grid sweep the best way to test different parameter combinations?
Thanks for your help!


Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 26 Jul 2019
The Savitzky-Golay filter is very good for some applications and not appropriate for others. I use it as a relatively straightforward way to reduce broad-band noise, since frequency-selective filters are completely ineffective in that situation. (The best way to see if broad-band noise is a problem is to do a Fourier transform of your signal.)
If you simply want to reduce high-frequency noise, then a frequency-selective filter is optimal, and there are several ways to design and implement them in MATLAB.
With respect to sgolayfilt, and the problem you pose, I always use a low-order filter (usually 3) and adjust the frame length until I am happy with the result. I only use it where a frequency-selective filter would not provide the result I want.


Z Liang
Z Liang on 2 Aug 2019
Thank you, I found a paper here that tries to answer half the question. But for now I'm content with whatever my supervisor is happy with!
Star Strider
Star Strider on 2 Aug 2019
As always, my pleasure!
Thank you for the paper: ‘Optimum window length of Savitzky-Golay filters with arbitrary order’. I saved it to my library directory.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!