关于matlab frezq fir1函数

输入信号x为频率0.1 Hz和0.3 Hz的等幅正弦波之和,利用fir1函数设计滤波器h,去除0.3 Hz的正弦信号,得到输出信号y。用freqz函数观察滤波器h的频率响应。显示输入信号x和输出信号y的波形。用fft函数对信号做快速傅里叶变换,并显示输入输出信号的幅频曲线。

具体步骤如下,0.1Hz与0.3Hz频率差距太小,精度自行调整,容易频谱泄漏:

clc,clear,close all;
t = -100:1:100;
L = length(t);
fs = 2;
x1 = sin(2*pi*0.1.*t/fs);
x2 = sin(2*pi*0.3.*t/fs);
x3 = x1 + x2;
%时域信号
figure(1)
subplot(311)
plot(t,x1,"LineWidth",1.5)
grid on 
subplot(312)
plot(t,x2,"LineWidth",1.5)
grid on
subplot(313)
plot(t,x3,"LineWidth",1.5)
grid on
%滤波器设计
wn = 0.2;  %截止频率wn为0.2pi(0.2Hz),wn = 2pi*f/fs
N = 60;  %阶数选择
hn = fir1(N-1,wn,boxcar(N));  %10阶FIR低通滤波器
figure(2)
freqz(hn,1);
figure(3)
y = fftfilt(hn,x3);  %经过FIR滤波器后得到的信号
plot(t,y,"LineWidth",1.5)
grid on
%频谱分析
Y = fft(y);  %输出信号的fft
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = fs*(0:(L/2))/L;
plot(f,P1,"LineWidth",1.5) 
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')

img


img


img


img