Borrar filtros
Borrar filtros

How to filter noisy signal by using IIR filter

35 visualizaciones (últimos 30 días)
Nur Fauzira Saidin
Nur Fauzira Saidin el 1 de Mayo de 2015
Respondida: Star Strider el 2 de Mayo de 2015
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? Thank you in advanced!
% 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 order n= ');n
%Calculate the filter coefficient
[b,a]=butter(n,wn,'high');
% Filtering
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');
%Plot the filter response
figure, freqz(b,a,500,f);
title ('Magnitude and phase response of the IIR butterworth filter');
As an example;
Enter the sampling frequency of the sine signal (Hz): 100
Enter the amplitude of the sine signal: 2
Enter the input frequency of the sine signal (Hz): 1
Enter the phase of the sine signal (rad): 0
Enter the pass band frequency fp = 2000
Enter the stop band frequency fs = 4000
Enter the pass band attenuation rp = 0.8
Enter the stop band attenuation rs = 45
Enter the sampling frequency f = 10000
Filter order n=
n =
5
  2 comentarios
Star Strider
Star Strider el 1 de Mayo de 2015
I did not run your code, but I see some problems that you need to resolve:
In this line:
f = input('Enter the input frequency of the sine signal (Hz): ');
you then overwrite ‘f’ with this line:
f=input ('Enter the sampling frequency f = ');
so you need to name them differently and then use them appropriately in your code.
Also, you have to normalise by the Nyquist frequency (sampling frequency/2):
wp=2*fp/f;
ws=2*fs/f;
not the sampling frequency, and check to be sure that ‘fp’ and ‘fs’ are less than f/2.
Here, you probably want a lowpass filter instead:
[b,a]=butter(n,wn,'high');
Experiment with these and see if these changes improve your code.
(I’m not listing this as an Answer because it isn’t one.)
Nur Fauzira Saidin
Nur Fauzira Saidin el 2 de Mayo de 2015
Editada: Nur Fauzira Saidin el 2 de Mayo de 2015
Okay, I've made some changes in my coding.
% Sine signal with noise
Fs = input ('Enter the sampling frequency of the sine signal (Hz): ');
amp = input ('Enter the amplitude of the sine signal: ');
f1 = 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*f1*t) + phase) + 0.5*randn(size(t));
%Program to design a Butterworth Lowpass IIR 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 = ');
f2=input ('Enter the sampling frequency f = ');
%Normalized the frequencies
wp=fp/(f2/2);
ws=fs/(f2/2);
%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);
%Filtering
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 Lowpass IIR Filter Coefficient');
%Plotting the filter response
figure, freqz(b,a,500,f2);
title ('Magnitude and phase response of the IIR butterworth filter');
As an example;
Enter the sampling frequency of the sine signal (Hz): 100
Enter the amplitude of the sine signal: 1
Enter the input frequency of the sine signal (Hz): 1
Enter the phase of the sine signal (rad): 0
Enter the pass band frequency fp = 2000
Enter the stop band frequency fs = 3000
Enter the pass band attenuation rp = 0.9
Enter the stop band attenuation rs = 40
Enter the sampling frequency f = 10000
Filter ordern n=
n =
9
Is this correct? The filtered signal looks better than the previous one, since I used lowpass filter. But actually I want the signal to experience all kinds of filters; lowpass, highpass, bandpass and bandstop. Is that possible to do so?

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 2 de Mayo de 2015
‘But actually I want the signal to experience all kinds of filters; lowpass, highpass, bandpass and bandstop. Is that possible to do so?’
Quite definitely! You simply have to design each filter, use trapz to be certain its stable, and implement it appropriately. Digital (and all) filter design is somewhat heuristic, so you have to experiment with them to get the result you want. (This generally involves getting the correct passband and stopband.)
I (with absolutely no humility) refer you to my filter design outline http://www.mathworks.com/matlabcentral/answers/184520-how-to-design-a-lowpass-filter-for-ocean-wave-data-in-matlab in which I went into some detail as to filter design workflow. Alter the ‘lowpass’ instructions for bandpass, bandstop, high-pass or whatever filter you want to design. The essential steps don’t change.

Más respuestas (0)

Categorías

Más información sobre Digital and Analog Filters en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by