How can I determine b and a values for a data signal when using filter() function

Hello,
How do I determine the suitable b and a values when using filter function to filter a data signal?

 Accepted Answer

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

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.
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?
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.
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.
Thank you very much. You were very helpful!
I'll try everything you mentioned. :D
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.)
But designing a lowpass filter like that using filtfilt() or filter() is the same as using lowpass() in Matlab. Am I right?
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.
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.
I will do so. I'll read about it a bit more and try designing. I really appreciate your help.

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!