Butterworth filter: Number of coefficients?

Hello
I am trying to create a low pass filter. My device has a sample rate of 100 and the cutoff value should be 10 Hz.
But according to the manual the filter should also have a setting "number of coefficients". And this should be 40 in my case...
How can I implement the number of coefficients into the filter?
Thanks.
My Code:
Fs = 100;
Fco = 10;
Fn = Fs/2;
Wn = Fco/Fn;
[b,a]=butter(1,Wn,'low')
m_filtered=filter(b,a,m)

 Accepted Answer

The first argument to butter is the filter order, and the number of coefficients in the transfer function vectors will be 1 less than that, so:
Fs = 100;
Fco = 10;
Fn = Fs/2;
Wn = Fco/Fn;
n = 40;
[b,a]=butter(n-1,Wn,'low');
figure
freqz(b,a,2^14,Fs)
should do what you want.
Note that this creates an unstable filter.
Are you absolutely certain that you want to use a IIR filter here? A filter order of 40 is more typical of a FIR filter, especially for a lowpass application such as this. (If you want a FIR filter instead, use the kaiserord and fir1 functions to create it.)

8 Comments

The manual is talking about a "FIR ==> Low Pass". So you are right, thanks!
I think that I should use fir1 instead. But I never worked with fir1...
Does this code work?
And if the number of coefficients is 10 instead of 40, the -1 rule still applies?
***(m= my RAW DATA)***
Fs = 100;
Fco = 10;
Fn = Fs/2;
Wn = Fco/Fn;
n = 20;
b=fir1(n-1,Wn,'low')
m_filtered=filter(b,m)
The easiest way to check filter performance is with the freqz function:
figure
freqz(b,1,2^16,Fs)
The filter appears to do what you want, and is stable.
I would use filtfilt rather than filter, because it will create a phase-neutral output for all filters:
m_filtered = filtfilt(b,1,m);
Also, it is necessary to specify both ‘b’ and ‘a’, since filtfilt (and I suspect filter as well) wants both of them.
The fir1 function likes odd filter orders, so specifying them as such will produce an appropriate filter. Here, ‘n-1’ creates an odd-order filter. Higher orders are slower, however the rolloff to the stopband is steeper.
I checked the link of you and there was a picture of an ECG wave. So, basically a zero phase filter would keep the time as it is, right?
Example:
So, if I would like to design a filter for a device with a sampling rate of 100 and a cut off value of 1 Hz
I should set FS= 100 ; and Fco= 1
Then the numer of coefficients can be kept as n-1 => In this case for 400. Are high values ok? Or does it make the software too slow?
Fs = 100;
Fco = 1;
Fn = Fs/2;
Wn = Fco/Fn;
n = 400;
b=fir1(n-1,Wn,'low')
m_filtered=filtfilt(b,1,m)
Sorry, I didn't get the 'a' is a necessarity topic. Will a filter still work with a defined as 1
Thank you so much for your help!!!
My pleasure!
So, basically a zero phase filter would keep the time as it is, right?
Yes. However the important thing is that there is no phase lag and no phase distortion.
Are high values ok? Or does it make the software too slow?
It is always best to use the shortest filter that produces the desired results. A filter length of 400 seems excessive for a simple lowpass filter, however it might be appropriate for a much more complicated filter, such as a filter with a number of passbands and stopbands.
Sorry, I didn't get the 'a' is a necessarity topic. Will a filter still work with a defined as 1
Yes. For FIR filters, ‘a’ is usually 1.
.
Just can repeat myself: Thanks a lot!
The filter is used for medical data. And the recommendation is: Take the sample size by 4 and divide it by the cut off value.
So according to the example it would be: 100*4/1=> 400
I am not sure if it's too much. But this is what the manual says...
My pleasure!
The filter order can be anything you want that meets your minimum requirements. My observation was simply a suggestion. (My background is also in biomedical signal processing, among other related topics.)
If my Answer helped you solve your problem, please Accept it!
.
Thanks again :)
I did it!
As always, my pleasure!
Thank you!

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!