How to filter noisy signal by using IIR filter
32 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/149695/image.png)
2 个评论
Star Strider
2015-5-1
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.)
采纳的回答
Star Strider
2015-5-2
‘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.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Digital and Analog Filters 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!