如何在matlab中使用IIR滤波器来滤除噪声信号

时间:2015-05-01 19:48:57

标签: matlab

我想将IIR滤波器应用于嘈杂的正弦信号,但我不确定我的编程是否正确,因为我得到的滤波信号不是那么平滑。有人可以帮我吗?

% Sine signal with noise
Fs = input ('Enter the sampling frequency of the sine signal (Hz): '); 
amp = input ('Enter the amplitude of the sine signal: ');
f = input('Enter the input frequency of the sine signal (Hz): ');
phase = input('Enter the phase of the sine signal (rad): ');
Ts = 1/Fs;
t = 0:Ts:10;
randn('state',0);
y = amp*sin((2*3.14*f*t) + phase) + 0.5*randn(size(t));

%Program to design a Butterworth Highpass filter
fp=input('Enter the pass band frequency fp   = ');
fs=input('Enter the stop band frequency fs   = ');
rp=input('Enter the pass band attenuation rp = ');
rs=input('Enter the stop band attenuation rs = ');
f=input ('Enter the sampling frequency f     = ');

%Normalized the frequencies
wp=2*fp/f;
ws=2*fs/f;

%Calculate the filter order
[n,wn]=buttord(wp,ws,rp,rs);
disp('Filter ordern n= ');n

%Calculate the filter coefficient
[b,a]=butter(n,wn,'high');

% Convolution
z=filtfilt(b,a,y);

%Plot the signal
subplot(2,1,1), plot(y), title('Sine signal with noise');
subplot(2,1,2), plot(z), title('Filtered sine signal');
figure, plot([b,a]),title('Butterworth Highpass IIR Filter Coefficient');

%Plotting the filter response
figure, freqz(b,a,500,f);
title ('Magnitude and phase response of the IIR butterworth filter');

1 个答案:

答案 0 :(得分:0)

你所做的基本上是正确的,但是使用带通滤波器而不是高通滤波器可以做得更好。高通滤波器不会消除高频噪声:

Fs = 32;
amp = 1;
phase = 0;
f0 = 3.123;

Ts = 1/Fs;
t = 0:Ts:10;
randn('state',0);
y = amp*sin((2*pi*f0*t) + phase) + 0.5*randn(size(t));

% Program to design a Butterworth bandpass filter
fp1 = f0 - f0*0.2;
fp2 = f0 + f0*0.2;
n = 4;

% Normalize the frequencies
wp1 = fp1/(Fs/2);
wp2 = fp2/(Fs/2);

% Calculate the filter coefficient
[b,a] = butter(n, [wp1 wp2]);

% Convolution
z = filtfilt(b, a, y);

% Plot the signal
plot(t, y, t, z)
xlabel('Time [s]')
legend('Signal + Noise','Filtered Signal')
grid

在滤波后的信号中可以看到一些残余的相位和幅度噪声,但这只是因为滤波器具有有限的宽度,并且不能排除非常接近正弦波频率的噪声。