Matlab - 在系统上使用低通滤波器

时间:2014-12-16 16:27:24

标签: matlab signals system lowpass-filter

我必须遵循以下代码:

clc
clear all
close all

Fs = 6000;                    % Sampling frequency
T = 1/Fs;                     % Sample time
N=512;                        % Length of signal
t = (0:N-1)*T;                % Time vector


% Sum of a 1kHz sinusoid and a 2.5kHz sinusoid
x1=4*sin(2*pi*1000*t);
x2=sin(2*pi*2500*t); 

figure   
subplot(3,1,3)
stem(t,x1);

figure
subplot(2,1,1)
stem(t,x2);

x=x1+x2;
y=fft(x);
fx=(-N/2:(N/2-1))*(Fs/N);
figure, plot(fx,abs(fftshift(y))); 

我想看到信号的低部分,这意味着频域中有一个脉冲,我正在使用这个滤波器:

fc = 1500;
Wn = (2/Fs)*fc;
b = fir1(20,Wn,'low',kaiser(21,3));

fvtool(b,1,'Fs',Fs)


z = filter(b,1,y);


plot(t(1:100),abs(z(1:100)))

xlabel('Time (s)')
ylabel('Amplitude')

但是我看到一个周期性的信号,我怎么能看到低频中的一个脉冲?

1 个答案:

答案 0 :(得分:2)

您应该使用z = filter(b,1,x);

Fs = 6000;                   
T = 1/Fs;              
N=512;                       
t = (0:N-1)*T;               
x1 = 4*sin(2*pi*1000*t);
x2 = sin(2*pi*2500*t); 
x = x1 + x2;
NFFT = 2^nextpow2(N);
y = fft(x,NFFT)/N;
fx = Fs/2*linspace(0,1,NFFT/2+1);
subplot(211)
plot(fx,2*abs(y(1:NFFT/2+1))) 

fc = 1500; 
Wn = (2/Fs)*fc;
b = fir1(20,Wn,'low',kaiser(21,3));
z = filter(b,1,x);
Z = fft(z,NFFT)/N;
subplot(212)
plot(fx,2*abs(Z(1:NFFT/2+1))) 

enter image description here

仅绘制正频率更为常见。

您的N已经是2的幂,但我在NFFT更改的情况下定义了N

顺便说一下,您不需要在每个figure之前使用plot