lowpass filter design in matlab -凯发k8网页登录
this example shows how to design lowpass filters. the example highlights some of the most commonly used command-line tools in the dsp system toolbox™. alternatively, you can use the filter builder app to implement all the designs presented here. for more design options, see .
introduction
when designing a lowpass filter, the first choice you make is whether to design an fir or iir filter. you generally choose fir filters when a linear phase response is important. fir filters also tend to be preferred for fixed-point implementations because they are typically more robust to quantization effects. fir filters are also used in many high-speed implementations such as fpgas or asics because they are suitable for pipelining. iir filters (in particular biquad filters) are used in applications (such as audio signal processing) where phase linearity is not a concern. iir filters are generally computationally more efficient in the sense that they can meet the design specifications with fewer coefficients than fir filters. iir filters also tend to have a shorter transient response and a smaller group delay. however, the use of minimum-phase and multirate designs can result in fir filters comparable to iir filters in terms of group delay and computational efficiency.
fir lowpass designs - specifying the filter order
there are many practical situations in which you must specify the filter order. one such case is if you are targeting hardware which has constrained the filter order to a specific number. another common scenario is when you have computed the available computational budget (mips) for your implementation and this affords you a limited filter order. fir design functions in the signal processing toolbox (including fir1
, firpm
, and firls
) are all capable of designing lowpass filters with a specified order. in the dsp system toolbox, the preferred function for lowpass fir filter design with a specified order is . this function designs optimal equiripple lowpass/highpass fir filters with specified passband/stopband ripple values and with a specified passband-edge frequency. the stopband-edge frequency is determined as a result of the design.
design a lowpass fir filter for data sampled at 48 khz. the passband-edge frequency is 8 khz. the passband ripple is 0.01 db and the stopband attenuation is 80 db. constrain the filter order to 120.
n = 120; fs = 48e3; fp = 8e3; ap = 0.01; ast = 80;
obtain the maximum deviation for the passband and stopband ripples in linear units.
rp = (10^(ap/20) - 1)/(10^(ap/20) 1); rst = 10^(-ast/20);
design the filter using firceqrip
and view the magnitude frequency response.
num = firceqrip(n,fp/(fs/2),[rp rst],'passedge'); fvtool(num,'fs',fs)
the resulting stopband-edge frequency is about 9.64 khz.
minimum-order designs
another design function for optimal equiripple filters is firgr
. firgr
can design a filter that meets passband/stopband ripple constraints as well as a specified transition width with the smallest possible filter order. for example, if the stopband-edge frequency is specified as 10 khz, the resulting filter has an order of 100 rather than the 120th-order filter designed with firceqrip
. the smaller filter order results from the larger transition band.
specify the stopband-edge frequency of 10 khz. obtain a minimum-order fir filter with a passband ripple of 0.01 db and 80 db of stopband attenuation.
fst = 10e3; nummin = firgr('minorder',[0 fp/(fs/2) fst/(fs/2) 1],... [1 1 0 0],[rp,rst]);
plot the magnitude frequency responses for the minimum-order fir filter obtained with firgr
and the 120th-order filter designed with firceqrip
. the minimum-order design results in a filter with order 100. the transition region of the 120th-order filter is, as expected, narrower than that of the filter with order 100.
hvft = fvtool(num,1,nummin,1,'fs',fs); legend(hvft,'n = 120','n = 100')
filtering data
to apply the filter to data, you can use the filter
command or you can use dsp.firfilter
. dsp.firfilter
has the advantage of managing state when executed in a loop. dsp.firfilter
also has fixed-point capabilities and supports c code generation, hdl code generation, and optimized code generation for arm® cortex® m and arm cortex a.
filter 10 seconds of white noise with zero mean and unit standard deviation in frames of 256 samples with the 120th-order fir lowpass filter. view the result on a spectrum analyzer.
lp_fir = dsp.firfilter('numerator',num); sa_fir = spectrumanalyzer('samplerate',fs); tic while toc < 10 x = randn(256,1); y = lp_fir(x); step(sa_fir,y); end release(sa_fir)
using dsp.lowpassfilter
dsp.lowpassfilter
is an alternative to using firceqrip
and firgr
in conjunction with dsp.firfilter
. basically, dsp.lowpassfilter
condenses the two step process into one. dsp.lowpassfilter
provides the same advantages that dsp.firfilter
provides in terms of fixed-point support, c code generation support, hdl code generation support, and arm cortex code generation support.
design a lowpass fir filter for data sampled at 48 khz. the passband-edge frequency is 8 khz. the passband ripple is 0.01 db and the stopband attenuation is 80 db. constrain the filter order to 120. create a dsp.firfilter
based on your specifications.
lp_fir = dsp.lowpassfilter('samplerate',fs,... 'designforminimumorder',false,'filterorder',n,... 'passbandfrequency',fp,... 'passbandripple',ap,'stopbandattenuation',ast);
the coefficients in lp_fir
are identical to the coefficients in num
.
num_lp = tf(lp_fir);
you can use lp_fir
to filter data directly, as shown in the preceding example. you can also analyze the filter using fvtool or measure the response using measure
.
fvtool(lp_fir,'fs',fs);
measure(lp_fir)
ans = sample rate : 48 khz passband edge : 8 khz 3-db point : 8.5843 khz 6-db point : 8.7553 khz stopband edge : 9.64 khz passband ripple : 0.01 db stopband atten. : 79.9981 db transition width : 1.64 khz
minimum-order designs with dsp.lowpassfilter
you can use dsp.lowpassfilter
to design minimum-order filters and use measure
to verify that the design meets the prescribed specifications. the order of the filter is again 100.
lp_fir_minord = dsp.lowpassfilter('samplerate',fs,... 'designforminimumorder',true,... 'passbandfrequency',fp,... 'stopbandfrequency',fst,... 'passbandripple',ap,... 'stopbandattenuation',ast); measure(lp_fir_minord)
ans = sample rate : 48 khz passband edge : 8 khz 3-db point : 8.7136 khz 6-db point : 8.922 khz stopband edge : 10 khz passband ripple : 0.0098641 db stopband atten. : 80.122 db transition width : 2 khz
nlp = order(lp_fir_minord)
nlp = 100
designing iir filters
elliptic filters are the iir counterpart to optimal equiripple fir filters. accordingly, you can use the same specifications to design elliptic filters. the filter order you obtain for an iir filter is much smaller than the order of the corresponding fir filter.
design an elliptic filter with the same sampling frequency, cutoff frequency, passband-ripple constraint, and stopband attenuation as the 120th-order fir filter. reduce the filter order for the elliptic filter to 10.
n = 10; lp_iir = dsp.lowpassfilter('samplerate',fs,... 'filtertype','iir',... 'designforminimumorder',false,... 'filterorder',n,... 'passbandfrequency',fp,... 'passbandripple',ap,... 'stopbandattenuation',ast);
compare the fir and iir designs. compute the cost of the two implementations.
hfvt = fvtool(lp_fir,lp_iir,'fs',fs); legend(hfvt,'fir equiripple, n = 120',... 'iir elliptic, n = 10');
cost_fir = cost(lp_fir)
cost_fir = struct with fields:
numcoefficients: 121
numstates: 120
multiplicationsperinputsample: 121
additionsperinputsample: 120
cost_iir = cost(lp_iir)
cost_iir = struct with fields:
numcoefficients: 25
numstates: 20
multiplicationsperinputsample: 25
additionsperinputsample: 20
the fir and iir filters have similar magnitude responses. the cost of the iir filter is about 1/6 the cost of the fir filter.
running the iir filters
the iir filter is designed as a biquad filter. to apply the filter to data, use the same commands as in the fir case.
filter 10 seconds of white gaussian noise with zero mean and unit standard deviation in frames of 256 samples with the 10th-order iir lowpass filter. view the result on a spectrum analyzer.
sa_iir = spectrumanalyzer('samplerate',fs); tic while toc < 10 x = randn(256,1); y = lp_iir(x); sa_iir(y); end release(sa_iir)
variable bandwidth fir and iir filters
you can also design filters that allow you to change the cutoff frequency at run-time. dsp.variablebandwidthfirfilter
and dsp.variablebandwidthiirfilter
can be used for such cases.