MATLAB: How to filter noisy signal by using IIR filter

iirfilter

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

Best Answer

‘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.