MATLAB——Design of IIR digital filter

1. Basic knowledge
1.1. The basic steps of digital filter design
We know that the design of analog filter is the basis of digital filter design. In the process of learning digital signal processing, the design steps of IIR digital filter are
(1) determine the sampling interval Ts or sampling frequency fs.
(2) According to the relationship between the analog frequency and the digital frequency, convert the index of the given digital filter into the index of the analog filter. (Ω=ω/Ts)
(3) Design the analog filter according to the index of the analog filter.
(4) Convert H(s) into H(z) according to the impulse response invariant method and the bilinear transformation method.

1.2. Impulse Response Invariant Method
According to z = es T z=e^{sT}z=es T , maps the S plane to the Z plane, but not a one-to-one mapping. The impulse response invariance method is only suitable for filter design with limited bandwidth.

1.3. Bilinear transformation method
By compressing the S plane, the phenomenon of spectrum aliasing is eliminated. However, nonlinear distortion is generated in this process, so it is necessary to correct it. The mapping relationship between the S plane and the Z plane of this method is
s = 2 T ∗ 1 − z − 1 1 + z − 1 s=\frac{2}{T}*\frac{1-z^{-1}} {1+z^{-1}}s=T21+z11z1
The pre-distortion correction formula is
Ω = 2 T ∗ tan ( ω 2 ) Ω=\frac{2}{T}*tan(\frac{ω}{2})Oh=T2tan(2oh)
2. Specific topic
2.1, topic 1 (impulse response invariant method)
use the impulse response invariant method to design a Butterworth low-pass digital filter, the passband frequency is required, the passband ripple is less than 1dB, the stopband frequency is , and the amplitude attenuation is greater than 15dB. The sampling frequency is Fs=2000Hz.
There are several functions that come with MATLAB, which need to be explained.[n,wn]=buttord(fp,fs,rp,as,'s')
in the analog filter designthis function is used to calculate Order and cutoff frequency of the Butterworth filter. Its input parameters include passband cutoff frequency (fp), stopband cutoff frequency (fs), passband maximum attenuation (rp) and stopband minimum attenuation (as), and the type of filter ('s' for analog filter , 'z' indicates a digital filter). The output of this function includes the order (n) and the cutoff frequency (wn). [z,p,k]=buttap(n);This function is used to generate the poles and zeros of a Butterworth filter. Its input parameter is the order (n), its output parameters are the poles (p) and zeros (z), and a coefficient (k), which is used to normalize the gain of the filter. [b,a]=zp2tf(z,p,k);zp2tf: This function is used to convert the poles and zeros into the numerator and denominator form of the transfer function. Its input parameters are poles (p), zeros (z) and coefficients (k), and its outputs are numerator coefficients (b) and denominator coefficients (a). Transformation to digital filter function[b,a]=lp2lp(B,A,Wn)A low-pass prototype is transformed into a low-pass filter with different cut-off frequencies, where the cut-off frequency Wn is specified, and the inputs are analog filters The coefficients of the numerator and denominator, and the cut-off frequency obtained earlier. [bz,az]=impinvar(b,a,fs);










The impinvar function transforms an analog filter into a digital filter using impulse response invariance. Specifically, the impinvar function first discretizes the analog filter and then uses the discretized impulse response as the impulse response of the digital filter.
Note
freqz and freqs
freqz and freqs are functions used in MATLAB to plot the frequency response of a filter. (j for short, one discrete, one continuous)
The freqz function plots the discrete-time frequency response of a digital filter with the syntax:
freqz(b,a,n,fs)
where b and a are digital filters respectively The numerator and denominator coefficients of , n is the number of points for computing the discrete-time frequency response, and fs is the sampling frequency.

The freqs function can draw the frequency response of the analog filter, and its syntax is:
freqs(b,a,w)
where b and a are the numerator coefficient and denominator coefficient of the analog filter respectively, and w is the frequency point for calculating the frequency response. It should be noted that the freqs function calculates the frequency response of the analog filter, so the input frequency parameter w is in radians.
the code

clc;
%写出已知条件,设置采样点数
wp=0.25*pi;ws=0.4*pi;
rp=1;as=15;
fs=2000;Nn=128;
%由数字指标映射到模拟滤波器指标
WP=wp*fs;WS=ws*fs;
%利用巴特沃斯滤波器的设计方法,设计一个模拟滤波器
[N,Wn]=buttord(WP,WS,rp,as,'s');
[z,p,k]=buttap(N);
[B,A]=zp2tf(z,p,k);
%转换到数字滤波器
[b,a]=lp2lp(B,A,Wn);%从一个低通原型变换为具有不同截止频率的低通滤波器
[bz,az]=impinvar(b,a,fs);%根据冲激响应不变法得到数字滤波器
freqz(bz,az,Nn,fs);

Running Results
insert image description here
2.2 Topic 2 (Bilinear Transformation Method)
Design Butterworth low-pass digital filter with bilinear transformation method, the passband frequency is required, the passband ripple is less than 1dB, the stopband frequency is , the amplitude attenuation is greater than 15dB, and the sampling frequency is It is Fs=100Hz.
With the above foundation, let's learn by analogy.

WS=2*fs*tan(ws/2);
WP=2*fs*tan(wp/2);

Added process for correcting distortion.

[bz,az]=bilinear(b,a,fs);

Change to bilinear invariant method

freqz(bz,az,Nn,fs);

[H,w]=freqz(bz,az);
figure;
subplot(211);
plot(w/pi,abs(H));
subplot(212);
plot(w/pi,angle(H));

In the last drawing part, I made a comparison.
The first is the magnitude-frequency and phase-frequency images plotted directly by freqz.
The freqz function calculates the frequency response of a digital filter and returns the magnitude and phase information of the frequency response. where w is the frequency and H is the complex value of the frequency response. Because usually we are more concerned about the frequency at 0 00 top pThe variation between π, so use plot(w/pi,abs(H)) and plot(w/pi,angle(H)) to change the frequency from radians to ratios on the unit circle . It is the file of my second drawing window.

full code

wp=0.25*pi;ws=0.4*pi;
rp=1;as=15;
fs=100;
Ts=1/fs;
Nn=128;

WS=2*fs*tan(ws/2);
WP=2*fs*tan(wp/2);

[N,Wn]=buttord(WP,WS,rp,as,'s');
[z,p,k]  =buttap(N);
[B,A]=zp2tf(z,p,k);
[b,a]=lp2lp(B,A,Wn);
[bz,az]=bilinear(b,a,fs);

freqz(bz,az,Nn,fs);

[H,w]=freqz(bz,az);
figure;
subplot(211);
plot(w/pi,abs(H));
subplot(212);
plot(w/pi,angle(H));

operation result
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/m0_46155417/article/details/129496442