原理参考相关数字信号处理书籍。
clc
close all
clear
%%
fs = 10e6; %% 采样率10Mhz
f1 = 200e3; %% 低频200khz
f2 = 1.5e6; %% 高频 1.5Mhz
len = 1e4; %%仿真数据长度
n = 0:len-1;
din1 = sin(2*pi*f1/fs*n) ; %%产生低频正弦信号
din2 = sin(2*pi*f2/fs*n) ; %%产生高频正弦信号
din = din1 + din2;
figure(1)
plot(din)
title('滤波前时域图')
%% 低通滤波器设计
Fs = fs; % 采样率
N = 16; % 滤波器阶数
Fpass = 200e3; % 通带截止频率
Fstop = 1e6; % 阻带起始频率
[low_hn] = lowpass_fir(Fs,N,Fpass,Fstop); %% 产生滤波系数
lowpass_out = filter(low_hn,1,din);
figure(2)
plot(lowpass_out);
title('低通滤波后时域图')
% figure(3)
fvtool(low_hn);
title('低通滤波器幅频响应曲线')
low_hn_dec = fix(2^10*low_hn);
%% 高通滤波器设计
Fs = fs; % 采样率
N = 16; % 滤波器阶数
Fstop = 300e3; % 阻带截止频率
Fpass = 1e6; % 通带起始频率
[high_hn] = highpass_fir(Fs,N,Fpass,Fstop);
highpass_out = filter(high_hn,1,din);
figure(4)
plot(highpass_out);
title('高通滤波后时域图')
high_hn_dec = fix(2^10*high_hn);
% figure(5)
fvtool(high_hn);
title('高通滤波器幅频响应曲线')
%% 带通滤波器设计
Fs = fs; % 采样率
N = 16; % 滤波器阶数
Fstop1 = 200e3; % First Stopband Frequency
Fpass1 = 1.2e6; % First Passband Frequency
Fpass2 = 1.6e6; % Second Passband Frequency
Fstop2 = 2.5e6; % Second Stopband Frequency
band_hb = bandpass_fir(Fs,N,Fstop1,Fpass1,Fpass2,Fstop2);
bandpass_out = filter(band_hb,1,din);
figure(5)
plot(highpass_out);
title('带通滤波后时域图')
band_hb_dec = fix(2^10*band_hb);
fvtool(band_hb);
title('带通滤波器幅频响应曲线')
%% 带阻滤波器
Fs = fs; % Sampling Frequency
N = 16; % 滤波器阶数
Fpass1 = 200e3; % 通带截止频率
Fstop1 = 1.2e6; % 阻带起始频率
Fstop2 = 1.6e6; % 阻带截止频率
Fpass2 = 3.0e6; % 通带起始频率
bandstop_hb = bandstop_fir(Fs,N,Fstop1,Fpass1,Fpass2,Fstop2);
bandstop_out = filter(bandstop_hb,1,din);
bandstop_hb_dec = fix(2^10*bandstop_hb);
figure(7)
plot(bandstop_out);
title('带阻滤波后时域图')
fvtool(bandstop_hb);
title('带阻滤波器幅频响应曲线')
%% 产生FPGA仿真数据
input = fix((2^6-1).*din);
fid=fopen('din.txt','w');
for k=1:length(input)
fprintf(fid,'%d \n',real(input(k)));
end
fclose(fid)
function [b] = lowpass_fir(Fs,N,Fpass,Fstop)
%LOWPASS_FIR Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.5 and Signal Processing Toolbox 8.1.
% Generated on: 16-Oct-2023 10:21:09
% Equiripple Lowpass filter designed using the FIRPM function.
% All frequency values are in Hz.
% Fs = 100000; % Sampling Frequency
% N = 15; % Order
% Fpass = 1000; % Passband Frequency
% Fstop = 10000; % Stopband Frequency
Wpass = 1; % Passband Weight
Wstop = 1; % Stopband Weight
dens = 20; % Density Factor
% Calculate the coefficients using the FIRPM function.
b = firpm(N, [0 Fpass Fstop Fs/2]/(Fs/2), [1 1 0 0], [Wpass Wstop], ...
{dens});
% Hd = dfilt.dffir(b);
% [EOF]
```matlab
function [b] = highpass_fir(Fs,N,Fpass,Fstop)
%HIGHPASS_FIR Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.5 and Signal Processing Toolbox 8.1.
% Generated on: 16-Oct-2023 10:27:58
% Equiripple Highpass filter designed using the FIRPM function.
% All frequency values are in Hz.
% Fs = 100000; % Sampling Frequency
% N = 15; % Order
% Fstop = 5000; % Stopband Frequency
% Fpass = 20000; % Passband Frequency
Wstop = 1; % Stopband Weight
Wpass = 1; % Passband Weight
dens = 20; % Density Factor
% Calculate the coefficients using the FIRPM function.
b = firpm(N, [0 Fstop Fpass Fs/2]/(Fs/2), [0 0 1 1], [Wstop Wpass], ...
{dens});
% Hd = dfilt.dffir(b);
% [EOF]
function b = bandpass_fir(Fs,N,Fstop1,Fpass1,Fpass2,Fstop2)
%BANDPASS_FIR Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.5 and Signal Processing Toolbox 8.1.
% Generated on: 16-Oct-2023 10:39:50
% Equiripple Bandpass filter designed using the FIRPM function.
% All frequency values are in Hz.
% Fs = 100000; % Sampling Frequency
% N = 15; % Order
% Fstop1 = 2000; % First Stopband Frequency
% Fpass1 = 6000; % First Passband Frequency
% Fpass2 = 8000; % Second Passband Frequency
% Fstop2 = 15000; % Second Stopband Frequency
Wstop1 = 1; % First Stopband Weight
Wpass = 1; % Passband Weight
Wstop2 = 1; % Second Stopband Weight
dens = 20; % Density Factor
% Calculate the coefficients using the FIRPM function.
b = firpm(N, [0 Fstop1 Fpass1 Fpass2 Fstop2 Fs/2]/(Fs/2), [0 0 1 1 0 ...
0], [Wstop1 Wpass Wstop2], {dens});
Hd = dfilt.dffir(b);
% [EOF]