简体   繁体   中英

How to filter noisy signal by using IIR filter in matlab

I want to apply IIR filter to noisy sine signal but I am not sure if my programming is correct because the filtered signal that I got is not that smooth. Can somebody help me on this?

% 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');

What you are doing is basically right, but it would be done better by using a bandpass filter instead of a highpass filter. The highpass filter will not get rid of the high frequency noise:

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

There is some residual phase and amplitude noise visible in the filtered signal, but thats just because the filter has a finite width and cannot exclude the noise which is very near the sine wave frequency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM