带通滤波器未能在matlab中过滤不需要的频率

时间:2016-04-17 04:58:25

标签: matlab filter

我正在制作一个带通滤波器,我根据正弦曲线创建了一些不需要的频率信号:

Fs = 8e3; % Sampling Rate
fn = Fs/2; % Nyquist frequency
L = 1e3;  % Length of signal 
T = 1/Fs; % Sampling period
t = T*linspace(0,L,Fs); % Time domain

% Frequencies in Hz
f1 = 1500;
f2 = 700;
f3 = 2500;
f4 = 3500;

% Signal
x = 6*sin(2*pi*f1*t);

% Noise
noise = 3*sin(2*pi*f2*t)...
      + 2*sin(2*pi*f3*t)...
      + 1*sin(2*pi*f4*t);

x_noise = x + noise;
然后我创建了一个Butterworth带通滤波器:

[b,a] = butter(10,[1000 2000]/fn,'bandpass');

带时响应(带freqz)的时间和频率空间信号如下所示:

Fig. 1 Signal with corruption | Fig. 2 Frequency with bandpass response

我会从这里想出来,只是做

xf = filter(b,a,x_noise);

会产生与原始信号非常相似的东西,但唉,我得到的是really far from the filtered signal with a high response far from the bandpass

我在这里做错了什么?

这是完整的代码:

clear all
Fs = 8e3; % Sampling Rate
fn = Fs/2; % Nyquist frequency
L = 1e3;  % Length of signal 
T = 1/Fs; % Sampling period
t = T*linspace(0,L,Fs); % Time domain

% Frequencies in Hz
f1 = 1500;
f2 = 700;
f3 = 2500;
f4 = 3500;

% Signal
x = 6*sin(2*pi*f1*t);

% Noise
noise = 3*sin(2*pi*f2*t)...
      + 2*sin(2*pi*f3*t)...
      + 1*sin(2*pi*f4*t);

x_noise = x + noise;

subplot(221); 
idx = 1:round(length(t)/30);
plot(t(idx),x(idx),t(idx),x_noise(idx));
xlabel('Time (s)'); ylabel('Signal Amplitudes');
legend('Original signal','Noisy signal');

% Frequency space
f = fn*linspace(0,1,L/2+1);
X = fft(x_noise)/L;

[b,a] = butter(10,[1000 2000]/fn,'bandpass');
h = abs(freqz(b,a,floor(L/2+1)));

subplot(222);
plot(f,abs(X(1:L/2+1)),f,h*max(abs(X)));
xlabel('Freq (Hz)'); ylabel('Frequency amplitudes');
legend('Fourier Transform of signal','Filter amplitude response');

% Filtered signal
xf = filter(b,a,x_noise);
subplot(223)
plot(t(idx),xf(idx));
xlabel('Time (s)'); ylabel('Signal Amplitudes');
legend('Filtered signal');

% Filtered in frequency space
Xf = abs(fft(xf)/L);
subplot(224);
plot(f,Xf(1:L/2+1),f,h*5e-6);
xlabel('Freq (Hz)'); ylabel('Frequency amplitudes');
legend('Fourier Transform of filtered signal','Bandpass');

1 个答案:

答案 0 :(得分:1)

您的时间变量t有误,例如1/(t(2)-t(1))应该Fs,但它没有。

尝试改为:

t = T*(0:L-1);