Digital Signal Processing (2)-Digital Filter

Introduction

Digital filter is an algorithm or device composed of digital multiplier, adder and delay unit. The function of the digital filter is to perform arithmetic processing on the digital code of the input discrete signal to achieve the purpose of changing the signal spectrum .
Insert picture description here

The system function of the digital filter is:
Insert picture description here

The corresponding difference equation is: (where x(n) is the input of the system, and y(n) is the output of the system)
Insert picture description here

By replacing z in the H(z) formula with e jw , the frequency response of w in the interval (0~π) can be obtained .
Use the freqz function in MATLAB to directly plot the frequency domain response curve of the digital filter.
Insert picture description here

Digital filters include finite impulse response (FIR) filters and infinite impulse response (IIR) filters. FIR filter a(1)=1, a(2)...a(N+1) are all 0, FIR filter only processes the input signal , so it has stability; IIR filter introduces the output signal Feedback , so there is instability.

MATLAB program

FIR filters are more commonly used in engineering, and the MATLAB program demonstrates the design of FIR filters.

clc
clear
close all
 
N=512;          % 信号长度
Fs=1e6;         % 采样频率
dt=1/Fs;        % 采样间隔
t=(0:N-1)*dt;   % 时间序列
F1=15e3;        % 信号频率1 /Hz
F2=50e3;        % 信号频率2 /Hz
xn=2*cos(2*pi*F1*t)+cos(2*pi*F2*t+0.5*pi+1);% 15kHz+50kHz
 
 
%% FIR滤波器设计
FilterLen = 128;          % 滤波器长度
FilterStep = FilterLen-1; % 滤波器阶数
 
FreqStart=10e3;% 低截止频率
FreqEnd  =20e3;% 高截止频率
w1 = FreqStart/Fs*2*pi;% 归一化
w2 = FreqEnd  /Fs*2*pi;% 归一化
 
window = ones(FilterLen,1);% 矩形窗
% window = hanning(FilterLen);% 汉宁窗
% window = blackman(FilterLen);% 布莱克曼窗
 
% hn = fir1(FilterStep,w2/pi,'low',window);  % 生成低通滤波器
% hn = fir1(FilterStep,w2/pi,'high',window); % 生成高通滤波器
hn = fir1(FilterStep,[w1/pi,w2/pi],'bandpass',window);% 生成带通滤波器
% hn = fir1(FilterStep,[w1/pi,w2/pi],'stop',window);    % 生成带阻滤波器
 
figure;
freqz(hn,1);% 绘制归一化的滤波器的特性曲线
 
figure;
subplot(3,2,3);
plot(hn,'o-');
xlabel('点数/n');
title('滤波器时域图');
 
subplot(3,2,1);
plot(t,xn);
xlabel('时间/s');
title('原始信号');
 
 
%% FFT分析原始信号
NN=512;% NN点DFT
XN=fft(xn,NN)/NN;% 计算signal的NN点快速傅里叶变换
f0=Fs/NN;       % 频率分辨率
f=(0:NN-1)*f0;  % 频率序列
fk=(0:NN-1);    % 谱线序列
A=abs(XN);      % 幅值序列
Phase=atan(-real(XN)./imag(XN))/pi*180;  % 相位序列
 
subplot(3,2,2);
plot(f(1:NN/2),A(1:NN/2));
xlabel('频率/Hz');ylabel('幅度');
title('原始信号频谱');
 
 
%% Filtering
% 方法1:
% yn = conv(xn,hn);% 对原始序列和滤波器序列求卷积得到滤波后的结果
% yn = yn(FilterLen/2:FilterLen/2+N-1);% 滤波结果是居中的数据
 
% 方法2:
% 矩阵法计算卷积
a = N;
b = FilterLen+N-1;
Hn = zeros(a,b);% 初始化一个a行b列的矩阵
for i=1:a
    for j=1:FilterLen
        Hn(i,j+i-1) = hn(j);
    end
end
yn=xn*Hn;
yn = yn(FilterLen/2:FilterLen/2+N-1);
 
subplot(3,2,5);
plot(t,yn);
xlabel('时间/s');
title('滤波器输出信号');
 
 
%% FFT分析输出信号
NN=512;% NN点DFT
XN=fft(yn,NN)/NN;% 计算signal的NN点快速傅里叶变换
f0=Fs/NN;       % 频率分辨率
f=(0:NN-1)*f0;  % 频率序列
fk=(0:NN-1);    % 谱线序列
A=abs(XN);      % 幅值序列
Phase=atan(-real(XN)./imag(XN))/pi*180;  % 相位序列
 
subplot(3,2,6);
plot(f(1:NN/2),A(1:NN/2));
xlabel('频率/Hz');ylabel('幅度');
title('输出信号频谱');
 
 
%% 滤波器参数存为txt文件
fid=fopen('hn.txt','w'); %需要改文件名称的地方
count=fprintf(fid,'%d\n',round(hn*65536)); %需要导出的数据名称
 
 


operation result

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/meng1506789/article/details/112946208