MATLAB digital filter design IIR (impulse response invariant method, bilinear transformation method), FIR

 

Table of contents

1. Butterworth low-pass filter is designed by impulse response invariant method and bilinear transformation method respectively

2. Butterworth High Pass Filter Design

 3. FIR filter design


1. Butterworth low-pass filter is designed by impulse response invariant method and bilinear transformation method respectively

  Requirements: passband cutoff frequency f p=200Hz, stopband cutoff frequency fs =400Hz, δp=1dB, δs=30dB, sampling interval T=1ms.

clc;clear;close;

% 数字滤波器的技术指标要求

ap=1;as=30;fp=200;fs=400;

Fs=1000;

% 采用冲激响应不变法

% 给定的模拟频率转模拟角频率

wap=2*pi*fp;

was=2*pi*fs;

% 设计模拟滤波器

[N,wac]=buttord(wap,was,ap,as,'s');  % N为阶数,wac为3dB截止频率

[z,p,k]=buttap(N);     % 创建巴特沃斯低通滤波器 z零点p极点k增益

[Bap,Aap]=zp2tf(z,p,k);% 由零极点和增益确定归一化Han(s)系数

[Bbs,Abs]=lp2lp(Bap,Aap,wac);     % 低通到低通 计算去归一化Ha(s)

[B,A]=impinvar(Bbs,Abs,Fs);       % 模拟域到数字域

[H1,w]=freqz(B,A);               % 根据H(z)求频率响应特性

% 绘制数字滤波器频响幅度谱

figure(1);

f=w*Fs/(2*pi);

subplot(211);

plot(f,20*log10(abs(H1)));          % 绘制幅度响应

xlabel('频率/Hz');

ylabel('H1幅值/dB');

subplot(212);

plot(f,unwrap(angle(H1)));          % 绘制相位响应

xlabel('频率/Hz');

ylabel('角度/Rad');



% 采用双线性变换法

% 给定的模拟频率转数字频率

wp=(2*pi*fp)/Fs

ws=(2*pi*fs)/Fs

% 数字指标转模拟指标 预畸变

wap=2*tan(wp/2)*Fs;

was=2*tan(ws/2)*Fs;

% 设计模拟滤波器

[N,wac]=buttord(wap,was,ap,as,'s');  % N为阶数,wac为3dB截止频率

[z,p,k]=buttap(N);     % 创建巴特沃斯低通滤波器 z零点p极点k增益

[Bap,Aap]=zp2tf(z,p,k);% 由零极点和增益确定归一化Han(s)系数

[Bbs,Abs]=lp2lp(Bap,Aap,wac);    % 低通到低通 计算去归一化Ha(s)

[B,A] = bilinear(Bbs,Abs,Fs);      % 模拟域到数字域

[H1,w] = freqz(B,A);             % 根据H(z)求频率响应特性

% 绘制数字滤波器频响幅度谱

figure(2);

f=w*Fs/(2*pi);

subplot(211);

plot(f,20*log10(abs(H1)));          % 绘制幅度响应

xlabel('频率/Hz');

ylabel('H1幅值/dB');

subplot(212);

plot(f,unwrap(angle(H1)));          % 绘制相位响应

xlabel('频率/Hz');

ylabel('角度/Rad');

  The amplitude response and phase response of the Butterworth low-pass filter designed by the impulse response invariant method are shown in Figure 1.1, and the amplitude response and phase response of the Butterworth low-pass filter designed by the bilinear transformation method are shown in Figure 1.2 ).

  The impulse response invariance method approximates the unit impulse response of the original analog filter through the unit impulse response, and the time domain characteristics are approximated very well, and the conversion between the analog domain and the digital domain is linear. The conversion between analog domain and digital domain by the bilinear transformation method is nonlinear, and there is distortion, but it overcomes the multi-value mapping and can eliminate spectral aliasing.

Figure 1.1 Magnitude response and phase response diagram of Butterworth low-pass filter designed by impulse response invariant method
Figure 1.2 Magnitude response and phase response diagram of Butterworth low-pass filter designed by bilinear transformation method

2. Butterworth High Pass Filter Design

  Requirements: passband cut-off frequency f p = 200Hz, stop band cut-off frequency f s = 100Hz, the amplitude characteristic decreases monotonously, the maximum attenuation at f p is 3dB, and the stopband minimum attenuation is 15dB.

   The high-pass filter can only be designed using the bilinear transformation method, and the code is as follows.

clc;clear;close;

% 采用双线性变换法

% 数字滤波器的技术指标要求

ap=3;as=15;fp=200;fs=100;

Fs=1000;

% 给定的模拟频率转数字频率

wp=(2*pi*fp)/Fs

ws=(2*pi*fs)/Fs

% 数字指标转模拟指标 预畸变

wap=2*tan(wp/2)*Fs;

was=2*tan(ws/2)*Fs;

% 设计模拟滤波器

[N,wac]=buttord(wap,was,ap,as,'s');     % N为阶数,wac为3dB截止频率

[z,p,k]=buttap(N);     % 创建巴特沃斯低通滤波器 z零点p极点k增益

[Bap,Aap]=zp2tf(z,p,k);% 由零极点和增益确定归一化Han(s)系数

[Bbs,Abs]=lp2hp(Bap,Aap,wac);     % 低通到高通 计算去归一化Ha(s)

[B,A]=bilinear(Bbs,Abs,Fs);        % 模拟域到数字域

[H1,w]=freqz(B,A);               % 根据H(z)求频率响应特性

% 绘制数字滤波器频响幅度谱

f=w*Fs/(2*pi);

subplot(211);

plot(f,20*log10(abs(H1)));          % 绘制幅度响应

xlabel('频率/Hz');

ylabel('H1幅值/dB');

subplot(212);

plot(f,unwrap(angle(H1)));          % 绘制相位响应

xlabel('频率/Hz');

ylabel('角度/Rad');

  Run the above code to get the magnitude response and phase response graph of the Butterworth high-pass filter designed using the bilinear transformation method, such as (Figure 2.1).

Figure 2.1 Magnitude response and phase response diagram of Butterworth high-pass filter designed by bilinear transformation method

 3. FIR filter design

        Requirements: The cut-off frequency of the passband is 350Hz, the cutoff frequency of the stopband is 500Hz, and the minimum attenuation of the stopband is 40dB.

  Using the Hanning window, the MATLAB code is as follows.

% 数字滤波器的技术指标要求
fp=350;fs=500;
Fs=6000;
% 模拟频率转数字频率
wp=2*pi*fp/Fs;
ws=2*pi*fs/Fs;
% 计算截止频率wc和群时延alfa
wc=(wp+ws)/2;                     % 计算截止频率
deltaw=ws-wp;                     % 过渡带宽度
N0=ceil(3.1*2*pi/deltaw);            % 查表计算阶数 采用汉宁窗
N=N0+mod(N0+1, 2);               % N为奇数
alfa=(N-1)/2;                      % 群时延
% 计算理想低通滤波器的单位冲激响应hd(n)
n=0:N-1;
hd=sin(wc*(n-alfa+eps))./(pi*(n-alfa+eps));
windows=(hanning(N))';             % 生成汉宁窗 窗函数
B=hd.*windows;                   % 获得FIR滤波器单位冲激响应
[H1,w]=freqz(B,1);
% 保存滤波器系数为MAT文件
A=1;
save('FIRFilter.mat','B','A');
% 绘制数字滤波器频响幅度谱
figure(1);
f=w*Fs/(2*pi);
subplot(211);
plot(f,20*log10(abs(H1)));          % 绘制幅度响应
xlabel('频率/Hz');
ylabel('H1幅值/dB');
subplot(212);
plot(f,unwrap(angle(H1)));          % 绘制相位响应
xlabel('频率/Hz');
ylabel('角度/Rad');

  The amplitude-frequency characteristics and phase-frequency characteristics of the designed filter are shown in (Figure 3.1). The designed filter meets the requirements of the index and has a good linear phase, and the signal will not be distorted when passing through the filter.

Figure 3.1 FIR filter characteristics

Guess you like

Origin blog.csdn.net/Cx2008Lxl/article/details/122803694