How can I determine b and a values for a data signal when using filter() function
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Hello,
How do I determine the suitable b and a values when using filter function to filter a data signal?
Accepted Answer
Star Strider
on 10 May 2020
Use any of the filter design functions that produce transfer function output.
However the transfer function coefficients can create unstable filters. It is always best to have the filter functions create zero-pole-gain (z,p,k) representations rather than transfer function representations, then use the zp2sos function to convert them to second-order-section representation to produce robust, stable filters. The filtfilt function can use them, as well as digital filter objects (such as thouse produced by lowpass, bandpass, and similar functions) to filter signals.
14 Comments
Dhanushka Palipana
on 10 May 2020
for filtfilt function (also for filter funtion), the definition needs transfer functions, right?
For a given data signal(I only have is the data), can you please tell what steps I should follow to get these coefficients?
For filtfilt, not necessarily. The filtfilt function can use transfer function vectors, second-order-section representations, or digital filter objects. It is extremely versatile.
Here is an example of code to design an elliptic bandpass filter that I did recentlly to filter an electrocardigram signal to illustrate the procedure:
Fs = 1000; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
Wp = [ 5 40]/Fn; % Normalised Passband (Passband = 5 Hz To 40 Hz)
Ws = [ 2 45]/Fn; % Normalised Stopband (Passband = 2 Hz To 45 Hz)
Rp = 1; % Passband Ripple/Attenuation
Rs = 50; % Stopband Ripple/Attenuation
[n,Wp] = ellipord(Wp, Ws, Rp, Rs); % Calculate Elliptic Filter Optimum Order
[z,p,k] = ellip(n, Rp, Rs, Wp,'bandpass'); % Elliptic Filter
[sos,g] = zp2sos(z,p,k); % Second-Order-Section For Stability
EKG = filtfilt(sos, g, EKG); % Filtered EKG
The filter passband and stopband frequencies must be between 0 Hz and the Nyquist frequency (although not equal to either one). All filter design code (regardless of the design you choose) follows these same essential steps. Only the functions change, and those depend on the filter you want to design (elliptic, Butterworth, Chebyshev, and other IIR filters). This creates a stable, robust filter. (The only filter this does not apply to is the Bessel filter, since it does not have a digital filter rperesentation.)
I can help you with FIR filters as well if you want to use them, however that is a different (and more complicated) discussion.
There are also other ways to design filters, such as designfilt (introduced in R2014a). The documentation for digitalFilter explores options for designfilt. I encourage you to explore them as well.
Dhanushka Palipana
on 10 May 2020
Another question, is it ok to decide these b and a coefficients by trial and error to get a better fit for the input signal?
Dhanushka Palipana
on 10 May 2020
Thank you very much. I'll go through those 2 links you mentioned. As a person with a very primary knowledge about these things, I was thinking if trial and error to get such b and a coefficients is ok too.
Star Strider
on 10 May 2020
Yes!
Trial and error is an important part of signal processing, since even the best initial design does not always produce the desired result (at least in the filters I design). Even though my filter code usually runs correctly when I first try it (using the design steps I described in my previous Comment), the filter does not always do what I want it to, and needs to be changed slightly.
Dhanushka Palipana
on 10 May 2020
Thank you very much. You were very helpful!
I'll try everything you mentioned. :D
Star Strider
on 10 May 2020
As always, my pleasure!
What I wrote is a guide, not a definitive solution for all problems, although it should get you most of the way to the filter design and implementation that give you the result you want. (The lowpass function and its friends are also helpful, although I did not mention them because you have already discovered them.)
Dhanushka Palipana
on 10 May 2020
But designing a lowpass filter like that using filtfilt() or filter() is the same as using lowpass() in Matlab. Am I right?
Star Strider
on 10 May 2020
Essentially, yes. The lowpass, bandpass, and related functions design different types of filters, depending on the function and requirements. They design both FIR and IIR filters. You can examine the filters the function creates by requesting the second (digital filter) output, and then right-clicking on it (or displaying it separately, as you would other variables). Everything you need to know about it is there. You can also use the digital filter object to filter other signals (using the same filter specifications) with filtfilt.
Dhanushka Palipana
on 10 May 2020
Thanks again!!! :D
Star Strider
on 10 May 2020
As always, my pleasure!
Star Strider
on 10 May 2020
One thing I forgot to mention is that when you are designing your own filters, it is best to always use the freqz function to be certain that the filter is doing what you want it to.
Dhanushka Palipana
on 10 May 2020
I will do so. I'll read about it a bit more and try designing. I really appreciate your help.
Star Strider
on 10 May 2020
As always, my pleasure!
More Answers (0)
Categories
Find more on Digital Filter Design in Help Center and File Exchange
See Also
on 10 May 2020
on 10 May 2020
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)