Different frequency responses using [z,p,k] method and [b,a] for 2nd order elliptical filter

Hi, I am designing an elliptical filter and using 2nd order. My code is below, however, both of them display different frequency responses. Why is that so? Am I doing something wrong here? However, when I am plotting for 4th order, both responses are same.
%Filter design
[b,a]=ellip(2,20,25,200/210,'high');
% [b,a]=ellip(2,20,25,[2000 9000]/(fs/2),'bandpass');
fvtool(b,a)
[z,p,k] = ellip(2,20,25,200/210,'high');
sos = zp2sos(z,p,k);
fvtool(sos)

 채택된 답변

You are doing everything correctly, however you only need to use the second-order-section (‘sos’) representation, since it essentially guarantees an efficient, stable filter. Transfer-function implementations can produce unstable or unreliable results.
What may be confusing the issue however are the arguments to the ellip function. This designs a second-order filter with a passband attenuation of 20 dB and a stopband attenuation of 25 dB. It may be worth reconsidering those values in order to get a usable filter. I suggest that you start with the ellipord function, and go from there.
Fs = 420;
[z,p,k] = ellip(2,20,25,200/210,'high');
[sos,g] = zp2sos(z,p,k);
figure
freqz(sos, 2^16, Fs)
.

댓글 수: 10

Thanks @Star Strider, I have 2 follow up questions:
  1. I couldn't actually find how to filter a signal using the [z,p,k] representation (the examples given for filters just depict how to get the response plots and not how to filter a signal using [z,p,k]). That is why I was using [b,a] method. Could you please guide me on how to do that? Using the [b,a] I can simply do:
y_filtered=filter(b,a,y_original);
2. I checked the ellipord function but since I want to control all the factors of an elliptical filter, I don't want to use ellipord function. Since, I am not much aware what would be the ideal and/or suitable passband, ripple dB values with the order that I am using (to provide me a stable filter), I was just giving it a try with random values (since this orientation of filter gives me a good result when applied in my algorithm). Could you explain or provide a link for that? I need to implement it in an MCU and don't want to go wrong before I finalize all aspects of this filter.
O.K.
I was suggesting ellipord because wt will calculate the optimal order given the passband and stopband frequencies and attenuations. As a general ruls, the passband ripple (or attenuation) is about 1 dB, and the stopband ripple (or attenuation) is about 50 dB (usually 50 to 75 dB, depending on the signal and the requirements).
To actually use the results from zp2sos to filter a signal, this approach works:
y_filtered = filtfilt(sos, g, original);
Use filtfilt with all filters (FIR and IIR) to do the actual filtering. It produces minimal phase delay and distortion.
I am not familiar with whatever ‘MCU’ is. If you want to implement any filter in hardware, that is a completely different problem. The DSP System Toolbox is will help you to design, test and implement digital filters in hardware, so I refer you to it.
.
Thank you and yes I meant hardware.
@Star Strider, you mentioned in above comment "Use filtfilt with all filters (FIR and IIR) to do the actual filtering. It produces minimal phase delay and distortion" So when I implement my filter in hardware, does the actual filtering happens in the same way as filtfilt does? Since you said "actual filtering", I got confused. For now I am not using DSP system toolbox.
I am not certain that it is possible to use filtfilt with discrete filtering hardware, since I have never tried it. The filtfilt documentation only mentions that ‘digitalFilter objects are not suported for code generation’, however that does not imply anything about filtfilt. (I do not have the MATLAB Coder or the DSP Toolbox, so I cannot test any of that.)
I define ‘actual filtering’ as applying a filter to a signal, and draw a distinction between a digitalFilter object (called a filter), the filter function (that I discourage using for actual signal filtering), and filtfilt. The terms can be confusing, and the distinction is not always obvious from the context.
.
Hi @Star Strider, how do I use..filter, instead of filtfilt as you suggested:
y_filtered=filter(b,a,y_original);
I am using sos representation, but I don't want to use filter instead of filtfilt. When I use filter as below:
y_filtered=filter(sos,g,y_original);
I get error as:
"First two arguments must be vectors." 'g' is not a vector. I tried finding how to use, but I couldn't, could you help? Thanks!
I advise against using filter, since it introduces phase lag and phase distortion while filtfilt does not.
Also, filter can only use transfer function representation for filters, not second-order-section representation, although it can also take a digitalFilter object as the first argument:
  • Use filter in the form dataOut = filter(d,dataIn) to filter a signal with a digitalFilter d. The input can be a double- or single-precision vector. It can also be a matrix with as many columns as there are input channels.
See Input Arguments for a full discussion.
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Paul
Paul 2021년 7월 19일
편집: Paul 2021년 7월 19일
If fvtool is like freqz, you need to make sure that the sos input has more than one row. Otherwise, the input might not be interpreted as an sos input. Here's an example with freqz.
[b,a]=ellip(2,20,25,200/210,'high');
[z,p,k] = ellip(2,20,25,200/210,'high');
[sos,g] = zp2sos(z,p,k);
freqz(b,a)
freqz(sos)
freqz([sos;[g 0 0 1 0 0]]) % add another section that is gain g for the expected response

댓글 수: 3

Thank you @Paul, this makes sense....however, why did you add 1 and 0s in:
freqz([sos;[g 0 0 1 0 0]])
? What do they mean?
Each second order section is of the form [b a], so an sos array for n sections is n x 6 is of the form:
[b1 a1;
[b2 a2;
.
.
.
bn an]
So that second section represents
b2 = [g 0 0]
a2 = [1 0 0]
which is just a gain of g. As I showed (and stated in doc freqz) if you want to use sos as input to freqz, it has to have at least two sections. So I added a second section that is effectively a unity gain of g to a) make freqz realize the input is sos, and b) to make the gain of the product of those sos's match the gain of the filter.

댓글을 달려면 로그인하십시오.

카테고리

태그

질문:

2021년 7월 19일

댓글:

2021년 8월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by