MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Alex
on 14 Nov 2011

Hey everyone, I'm trying to do the following:

Make an IIR and FIR filter that passes between 40-50 KHz, and blocks out noise that is occurring at 75Khz. There is 25 dB of stopband attenuation, and the passband ripple is set at 3.

With that noted, here is the code I have so far:

f_p=40000; %Pass Frequency in Hz

f_s=50000; %Stop Frequency in Hz

r_s=25; %Stopband Attenuation in dB

r_p=3; %Passband Ripple

d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',38e3,f_p,f_s,53e3,r_s,r_p,140e3);

%Fst1 — frequency at the edge of the end of the first stop band. Specified in normalized frequency units by default.

%Fp1 — frequency at the edge of the start of the pass band. Specified in normalized frequency units by default.

%Fp2 — frequency at the edge of the end of the pass band. Specified in normalized frequency units by default.

%Fst2 — frequency at the edge of the start of the second stop band. Specified in normalized frequency units by default.

%Ap — passband ripple in dB (the default units).

%Ast1 — attenuation in the first stopband in dB (the default units).

%Ast2 — attenuation in the second stopband in dB (the default units).

% Create the FIR filter

Hd2 = design(d,'equiripple');

fvtool(Hd2)

%Create the IIR filter

Hd1 = design(d,'butter');

fvtool(Hd1);

------------------------------------------------------------------

First off, does my methodology seem correct for creating an FIR and IIR filter that will accomplish this goal? Secondly, when I run the code, this is the error message I get:

??? Error using ==> fdesign.abstracttype.equiripple at 13 Frequency specifications must be between 0 and 1.

Error in ==> fdesign.abstracttype.superdesign at 106 Hd = feval(method, this, varargin{:});

Error in ==> fdesign.abstracttype.design at 11 varargout{1} = superdesign(this, varargin{:});

Error in ==> PEPractice at 13 Hd2 = design(d,'equiripple');

-----------------------------------------------------------------

Any insight as to what is wrong? Please help if you can!

*No products are associated with this question.*

Answer by Wayne King
on 16 Nov 2011

Accepted answer

You have to use a supported specificaton string. If you want to limit the order to a fixed value, then you have to realize, you may not be able to specify other things. In a minimum-order design, you are allowing the filter design method to find the order, if you specify it, then you have to give up certain things.

d = fdesign.lowpass('N,Fp,Ap,Ast',4,50e3,3,25,200e3); Hde = design(d,'ellip');

The above gets you more than 25 dB of stopband attenuation by 75 kHz.

But you may like:

Hd = design(d,'equiripple');

better.

Answer by Walter Roberson
on 14 Nov 2011

Do not use frequency in hertz: use proportionate frequency in terms of the fraction of the frequency compared to the nyquest frequency.

Answer by Honglei Chen
on 14 Nov 2011

The approach looks fine. I assume your sampling frequency is 140 kHz? If that's the case, I think you are just missing an `r_s` in front of 140e3 because you need to specify stop band attenuation for both sides, as indicated in the spec string.

HTH

Answer by Wayne King
on 14 Nov 2011

Hi, if you specify your filter design in Hz, then the sampling frequency has to be a trailing scalar. In the following:

d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',38e3,f_p,f_s,53e3,r_s,r_p,140e3);

You forgot the specification for your 2nd stopband. In a bandpass design there are two stopbands.

Try:

d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',38e3,f_p,f_s,53e3,r_s,r_p,r_s,140e3);

Alex
on 14 Nov 2011

Hello Wayne, I'm confused at what you're saying about the sampling frequency being a trailing scalar. What does that mean? It looks as if you used 140kHz as the sampling frequency which is fine, however I'm not sure if what you're saying gels with what Walter is saying above. Any clarification?

So I entered in the code you provided and the rest worked, however there are a few questions I have. Here is the code

d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',38e3,f_p,f_s,53e3,r_s,r_p,r_s,140e3);

Examining the arguments closely, we have these variables and the corresponding values:

'Fst1,Fp1,Fp2,Fst2,Ast1,Ap ,Ast2'

%38e3,f_p,f_s,53e3,r_s ,r_p,r_s,140e3

Clearly I have one more variable than I do values ... Why does it work? Shouldn't I have a variable that says f_s for sampling frequency? How does the function just accept this additional value?

Also, Here is another concern on mine. When the code outputted, it outputted 1 plot with 2 tabs. I assume the first tab is the FIR filter and the second is the IIR filter ? Is there any way I could label these in the code for better clarification (so that each plot is appropriately labeled) ?

Thank you!

Answer by Wayne King
on 14 Nov 2011

fdesign.bandpass() works with normalized frequencies unless you input the sampling frequency as a scalar trailing all other specifications. Your filter specification:

'Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2'

expects 7 input argument values in the correct order. If you want to specify those frequencies, Fst1, Fp1, etc. in Hz (not normalized frequency), then you must add an 8th input argument at the end of the others. You do NOT change the specification string, just add a comma and then your sampling frequency. You should NOT use a specification for sampling frequency.

That is how the fdesign interface is designed. All of them work that way.

You have created two designs Hd1 and Hd2. You could have designated them as

HdFIR = design(d,'equiripple'); % and HdIIR = design(d,'butter');

if that makes easier to remember. To label fvtool() output, you can use title()

fvtool(HdFIR); title('FIR Filter');

Or you can use a handle and then set a legend.

hfvt = fvtool([HdFIR HdIIR]); legend(hfvt,'FIR Filter','IIR Filter');

Answer by Alex
on 16 Nov 2011

Does anyone know how I can make a Lowpass filter in MatLab that will utilize a similar method? Essentially have one that has a passband ripple of 3 db, frequency cutoff of 50KHz, and 25 dB of stopband attenuation at 75KHz ?

Answer by Wayne King
on 16 Nov 2011

You did not tell us your sampling frequency. I'm assuming that your sampling frequency is not 140 kHz in this instance because your proposed stopband frequency is higher than the Nyquist. So I'll assume 200 kHz for this example.

d = fdesign.lowpass('Fp,Fst,Ap,Ast',50e3,75e3,3,25,200e3);

By the way, are you reading the MATLAB documentation? This is all explained with examples.

Alex
on 16 Nov 2011

Hey Wayne,

thank you for the post. I have been reading the documentation but one of the things that I'd like to do to the lowpass and the bandpass versions is specify the order of the filter and see what it looks like. I noticed it isn't one of the arguments, so I'm hoping you might be able to guide me into how I can specify the order.

Any insight would be great!

Answer by Wayne King
on 16 Nov 2011

Hi Alex, if you read the documentation, order specification is possible in both fdesign.bandpass and fdesign.lowpass. Please look in the documentation for specification strings with N, Na, or Nb.

Some of these order specifications require the DSP System Toolbox, but you can certainly specify an order with just Signal. For example, just for fdesign.lowpass

'N,F3dB' 'Nb,Na,F3dB' 'N,F3dB,Fst' (*) 'N,F3dB,Ap' (*) 'N,F3dB,Ast' (*) 'N,F3dB,Ap,Ast' (*) 'N,Fc' 'N,Fc,Ap,Ast' 'N,Fp,Ap' 'N,Fp,Ap,Ast' 'N,Fp,F3dB' (*) 'N,Fp,Fst' 'N,Fp,Fst,Ap' (*) 'N,Fp,Fst,Ast' (*) 'N,Fst,Ast' 'N,Fst,Ap,Ast' (*) 'Nb,Na,Fp,Fst' (*)

The above all allow you to specify the order.

Answer by Alex
on 16 Nov 2011

Hey Wayne,

I reviewed your notes and the documentation, but the problem I run into is that there appears to be no specification that allows me to use all of my information. Please see the line of code I used below and the ensuing error:

d = fdesign.lowpass('N,Fp,Fst,Ap,Ast',4,50e3,75e3,3,25,200e3);

Error in ==> PEPractice at 13 d = fdesign.lowpass('N,Fp,Fst,Ap,Ast',4,50e3,75e3,3,25,200e3);

As i noted, there is no method that allows the usage of all : N,Fp,Fst,Ap,Ast (and that can be seen from your list above). The closest ones are 'N,Fp,Fst,Ast' and 'N,Fst,Ap,Ast' unless I'm missing something.

Am I missing something here?

-Alex

Answer by Alex
on 16 Nov 2011

Hey Wayne,

I will try that, but I have another question.

We know the cutoff frequency is 50 KHz but the load and internal impedance on the filter is 50 Ohms. How can I get both the BandPass and the LowPass codings to account for this 50 Ohms?

Thank you!

Show 1 older comment

Alex
on 17 Nov 2011

I'm not sure, but I'm hoping that Wayne will chime in to give indication if that 50 Ohms can be modeled in or not.

Honglei Chen
on 17 Nov 2011

What effect of this 50 Ohm do you want to take into consideration? If these are pure resistance then they are frequency independent and I don't see why it will affect the frequency response except a constant scale.

## 0 Comments