2016-12-11 189 views
1

我一直在寫一個非常簡單的代碼來去除信號中的噪聲。信號只是一個正弦波,噪聲是一個隨機矩陣,噪聲信號是兩者的加法。MATLAB上的低通濾波器Butterworth濾波器

的代碼是:

close all;clear;clc; 

%% Declarations 

ts = 0.001; 
fs = 1/ts; 
fc = 5; 
t = 0:ts:2; 
Wn = pi*fc/(2*fs); 
n = 3; 

%% Preparation 

signal = cos(2*pi*fc*t); 
noise = rand(1, length(signal)); % Generating Random Noise 
noisySignal = signal + noise; 

%% Filtering Stage 

[b,a] = butter(n, Wn, 'low'); 
filteredSignal = filter(b, a, noisySignal); 
filteredSignal = filteredSignal - mean(filteredSignal); % Subtracting the mean to block DC Component 

%% Plotting 

figure(1) 
subplot(3,1,1) 
plot(t, signal, 'linewidth', 1.5) 
title('Signal') 
ylim([-1.5 1.5]) 
grid minor 

subplot(3,1,2) 
plot(t, noise) 
title('Noise') 
ylim([-1.5 2]) 
grid minor 

subplot(3,1,3) 
plot(t, noisySignal) 
title('Noisy Signal') 
ylim([-1.5 1.5]) 
grid minor 

figure(2) 
plot(t, filteredSignal, 'r', 'linewidth', 1.5) 
hold on 
plot(t, signal, 'linewidth', 1.5) 
hold off 
legend('Filtered Signal', 'Original Signal') 
grid minor 
ylim([-1.5 1.5]) 

圖2;這是比較濾波信號和原始信號的圖形;總是顯示如下圖所示。

comparison

我相信Wn變量是不正確的,但我不知道如何計算正確的歸一化頻率。

回答

2

this example form Matlab's documentation,如果你想在截止頻率是在在fs Hz的採樣頻率fc赫茲,你應該使用:

Wn = fc/(fs/2); 
[b,a] = butter(n, Wn, 'low'); 

但是你要注意,這將產生一個巴特沃思的過濾器在截止頻率下衰減3dB。由於您的正弦信號的頻率fc產生,過濾後的正弦將有大約70%的原始信號的振幅:

Filtered signal at cutoff frequency

如果你想要更少的信號衰減,則應該增加濾波器的截止頻率。當然,這樣做也會讓更多的噪聲通過,所以確切的數量是在應用程序可以容忍多少信號衰減以及需要消除多少噪聲之間進行權衡。例如,添加的1Hz的保證金,並與

Wn = (fc+1)/(fs/2); 
n = 7; 
[b,a] = butter(n, Wn, 'low'); 

增加濾波器的階(這使你少衰減相同幅度)會給你:

Filtered signal with some margin

+0

是不是該過濾信號有偏移嗎? – Tes3awy

+0

['rand()'](https://www.mathworks.com/help/matlab/ref/rand.html)添加的噪音在'(0,1)'範圍內具有均勻分佈,這平均輸入偏移0.5。對於無偏噪聲,您可能要使用'(2 * rand(...) - 1)'(範圍爲'(-1,1)')或'randn(...)'(高斯分配)。 – SleuthEye

+0

非常感謝您的詳細解釋 – Tes3awy