TheWhiteFang

DSP1 Q1 FIR FILTER

Jan 11th, 2016
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.96 KB | None | 0 0
  1. % from demo program filtdem in lab1
  2. %% create the test signal
  3. clear all;
  4. Fs = 200; % sampling frequency
  5. t=(1:100)/Fs ; % obtain sampling time vector [ 1*1/Ts  2*1/Ts ...  100/Ts]
  6. s1= sin(2*pi*10*t);   % create 5 hz test signal , 100 samples
  7. s2= sin(2*pi*30*t);%s2= sin(2*pi*15*t);   % create 15 hz test signal , 100 samples
  8. s3= sin(2*pi*50*t);%s3= sin(2*pi*30*t);   % create 30 hz test signal , 100 samples
  9.  
  10. s= s1+s2+s3 ; % Create a test signal that contain 3 different frequecy 5,15 and 30Hz
  11. plot(t,s)  % plot the signal vs time(seconds)
  12.  
  13. %% Design the filter
  14.  
  15. %b = fir1( n , wn );
  16. a=-1;
  17. b=fir1(33,0.2);
  18. %figure()
  19. %freqz(b,a,512,Fs);
  20. % implement an IIR bandpass filter using the elliptic method
  21. %[b,a] = ellip(4,0.1,40,[10 20]*2/Fs);
  22. %From ellip help instruction, cut off frequency Wp must be 0.0 < Wp < 1.0,
  23. % with 1.0 corresponding to half the sample rate, thus frequency used in this function is divided by Fs/2
  24.  
  25. % check the frequency response of the filter that has been designed earlier
  26. % to see if it meet the desired specification
  27. figure()
  28. freqz(b,a,512,Fs); % short cut way to get frequency response plot (Fs = sampling frequency in Hz)
  29. figure();
  30. [H,w] = freqz(b,a,512);
  31. %  Obtain the frequency response H, evaluated at N=512 points equally spaced around the
  32. %  upper half of the unit circle, frequency ranging from 0 to pi
  33. % w in radians/sample (discrete time frequency)
  34. f = w*Fs/(2*pi);  %conversion to frequency in Hz based on formula w = 2*pi*f/Fs
  35.  
  36. % Plot frequency response in Hz
  37. figure,plot(f,abs(H)), title('frequency response of the elliptic IIR bandpass filter');
  38. ylabel('magnitude of H(w)'); xlabel('Frequency in Hz');
  39.  
  40. % apply the filter to filter the input signal s and get output signal sf
  41. sf = filter(b,a,s);
  42. figure, plot(t,sf);
  43. xlabel('Time in seconds');
  44. ylabel('waveform magnitude');
  45. axis([0 1 -1 1]);
  46.  
  47.  
  48. %% Find the frequency content (freq. spectrum) of the signal before filtering
  49.  
  50. % Obtain freq spectrum by performing 512 point DFT using fft algorithm, a higher number gives spectrum
  51. % with better resolution,
  52. % Take note that the DFT sample below are for frequency ranging from 0->fsampling/2
  53. S = fft(s,512) ;   % DFT samples vector [S(1) S(2) .....S(512) ]
  54.  
  55. % Find freq spectrum for signal after filtering
  56. SF = fft(sf,512) ; % DFT samples vector [SF(1) SF(2) .....SF(512) ]
  57.  
  58.  
  59. % Plot the frequency spectrum S for the signal before filtering  
  60. % frequency in hertz
  61. % Due to the periodic nature of the frequency spectrum of discrete time signal
  62. % we only take half of the 512 samples
  63. wk = (0:511) * (2*pi/512)  ;  % frequency vector in omega 0-2pi
  64. fk = wk * Fs/(2*pi);
  65. figure, plot(fk(1:256), abs(S(1:256)), 'b-' ); grid on  % black line
  66. xlabel(' Frequency (Hz)  ');
  67. ylabel('Magnitude of fourier transform samples');
  68.  
  69. hold on
  70. % Plot the frequency spectrum SF for the filtered signal  
  71. plot(fk(1:256), abs(SF(1:256)), 'r--'); % red dotted line
  72. xlabel(' Frequency (Hz)  ');
  73. ylabel('Magnitude of fourier transform samples');
Add Comment
Please, Sign In to add comment