使用“filterDesigner”toolbox设计FIR滤波器(4个滤波器),分别提取其中3个不同频率正弦 波,1个频率的方波。 7. 分别画出滤波之后的3个不同频率正弦波,1个频率的方波的时域波形和fft频谱 8. 使用“filterDesigner”toolbox设计IIR滤波器(用Butterworth,Chebyshev,或者椭圆函数滤 波器,可以任意选择),分别提取其中3个不同频率正弦波,1个频率的方波。 9. 分别画出滤波之后的3个不同频率正弦波,1个频率的方波的时域波形和fft频谱具体代码
时间: 2024-02-11 09:08:17 浏览: 21
好的,下面是使用MATLAB中的filterDesigner工具箱设计FIR和IIR滤波器的代码:
设计FIR滤波器:
```matlab
% 创建filterDesigner对象
fd = filterDesigner;
% 设计3个不同频率的正弦波和1个方波
fs = 1000; % 采样率
t = 0:1/fs:1-1/fs; % 时间序列
f1 = 50; % 信号1频率
f2 = 150; % 信号2频率
f3 = 250; % 信号3频率
f4 = 50; % 方波频率
s1 = sin(2*pi*f1*t); % 信号1
s2 = sin(2*pi*f2*t); % 信号2
s3 = sin(2*pi*f3*t); % 信号3
s4 = square(2*pi*f4*t); % 方波信号
% 设计4个FIR滤波器
h1 = fd.designFilter('FIR', 'Window', 'SamplingFrequency', fs, 'FilterOrder', 50, 'Window', 'hamming', 'PassbandFrequency', [40 60], 'StopbandFrequency', [30 70]);
h2 = fd.designFilter('FIR', 'Window', 'SamplingFrequency', fs, 'FilterOrder', 50, 'Window', 'hamming', 'PassbandFrequency', [140 160], 'StopbandFrequency', [130 170]);
h3 = fd.designFilter('FIR', 'Window', 'SamplingFrequency', fs, 'FilterOrder', 50, 'Window', 'hamming', 'PassbandFrequency', [240 260], 'StopbandFrequency', [230 270]);
h4 = fd.designFilter('FIR', 'Window', 'SamplingFrequency', fs, 'FilterOrder', 50, 'Window', 'hamming', 'PassbandFrequency', [40 60], 'StopbandFrequency', [30 70]);
% 对信号进行滤波
y1 = filter(h1,s1);
y2 = filter(h2,s2);
y3 = filter(h3,s3);
y4 = filter(h4,s4);
% 绘制时域波形和频域谱图
figure;
subplot(4,2,1); plot(t,s1); title('原始信号1'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,2); plot(abs(fft(s1))); title('原始信号1的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
subplot(4,2,3); plot(t,y1); title('滤波后信号1'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,4); plot(abs(fft(y1))); title('滤波后信号1的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
subplot(4,2,5); plot(t,s2); title('原始信号2'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,6); plot(abs(fft(s2))); title('原始信号2的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
subplot(4,2,7); plot(t,y2); title('滤波后信号2'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,8); plot(abs(fft(y2))); title('滤波后信号2的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
```
设计IIR滤波器:
```matlab
% 创建filterDesigner对象
fd = filterDesigner;
% 设计3个不同频率的正弦波和1个方波
fs = 1000; % 采样率
t = 0:1/fs:1-1/fs; % 时间序列
f1 = 50; % 信号1频率
f2 = 150; % 信号2频率
f3 = 250; % 信号3频率
f4 = 50; % 方波频率
s1 = sin(2*pi*f1*t); % 信号1
s2 = sin(2*pi*f2*t); % 信号2
s3 = sin(2*pi*f3*t); % 信号3
s4 = square(2*pi*f4*t); % 方波信号
% 设计3个IIR滤波器
[b1,a1] = butter(6, [40 60]/(fs/2), 'bandpass');
[b2,a2] = cheby1(6, 0.5, [140 160]/(fs/2), 'bandpass');
[b3,a3] = ellip(6, 0.5, 30, [240 260]/(fs/2), 'bandpass');
% 设计1个用于方波信号的IIR滤波器
[b4,a4] = butter(6, [40 60]/(fs/2), 'stop');
% 对信号进行滤波
y1 = filter(b1,a1,s1);
y2 = filter(b2,a2,s2);
y3 = filter(b3,a3,s3);
y4 = filter(b4,a4,s4);
% 绘制时域波形和频域谱图
figure;
subplot(4,2,1); plot(t,s1); title('原始信号1'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,2); plot(abs(fft(s1))); title('原始信号1的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
subplot(4,2,3); plot(t,y1); title('滤波后信号1'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,4); plot(abs(fft(y1))); title('滤波后信号1的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
subplot(4,2,5); plot(t,s2); title('原始信号2'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,6); plot(abs(fft(s2))); title('原始信号2的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
subplot(4,2,7); plot(t,y2); title('滤波后信号2'); xlabel('时间(s)'); ylabel('幅值');
subplot(4,2,8); plot(abs(fft(y2))); title('滤波后信号2的频谱'); xlabel('频率(Hz)'); ylabel('幅值');
```