2016-04-17 81 views
3

過濾不需要的頻率我正在做一個帶通濾波器,我已經創建了基於正弦波一些不需要的頻率的信號:帶通濾波器未能在MATLAB

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; 

然後創建一個巴特沃思帶通濾波器:

[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

你的時間變量t是錯誤的,e.g 1/(t(2)-t(1))應該給Fs但事實並非如此。

嘗試,而不是:

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

現在只是傳遞的整個信號[原創與「過濾」() –

+0

那不是我所看到的。看看頻率域(你自己的情節) –

+0

[this](http://i.imgur.com/Fo21pxS.png)是我得到 –