Advertisement
Quiv

ur filter

May 26th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.44 KB | None | 0 0
  1.  
  2. clear all
  3.  
  4. NN = 200;
  5. dt = 0.0005;
  6. TT = (0:dt:dt*(NN-1));
  7.  
  8. f1 = 50;
  9. f2 = 100;
  10. f3 = 150;
  11. f4 = 200;
  12. x = zeros(1,NN);
  13. for n=1:NN
  14. x(n) = 0.5*sin(2*pi*f1*dt*n) ...
  15.      + 0.5*sin(2*pi*f2*dt*n) ...
  16.      + 0.5*sin(2*pi*f3*dt*n) ...
  17.      + 0.5*sin(2*pi*f4*dt*n);
  18. end
  19. figure(1);
  20. subplot(3,2,1)
  21. plot(TT,x,'k')
  22. xlabel('T (sec)')
  23. ylabel('x[k]')
  24. axis( [ 0 dt*(NN-1)  -1.5 1.5 ])
  25. set(gca,'fontsize',12)
  26. grid on
  27. title('FIR Filter')
  28.  
  29. % ------ Take the FFT
  30.  
  31. del_f = 1/(dt*NN);
  32. FF = (0:del_f:del_f*(NN-1));
  33.  
  34. XX = (1/NN)*fft(x);
  35. subplot(3,2,2)
  36. plot(FF,abs(XX),'k')
  37. xlabel('Freq (Hz)')
  38. ylabel('X(z)')
  39. axis( [ 0 250  0 .5 ])
  40. set(gca,'XTick',[ 0 f1 f2 f3 f4 ])
  41. set(gca,'fontsize',12)
  42.  
  43. % Frequency domain filter
  44.  
  45. %  Hanning
  46. HH = zeros(1,NN);
  47.  
  48. HH = zeros(1,NN);
  49. for n=1:6
  50. HH(n) = 0.;
  51. HH(NN-n+1) = 0.;
  52. end
  53.  
  54. MM = 30;
  55. for n=3:34
  56. HH(n) = 0.5*(1 - cos(2*pi*(n-5)/MM));
  57. HH(NN-n+1) = HH(n);
  58. end
  59.  
  60. for n=49:NN/2
  61. HH(n) = 0.;
  62. HH(NN-n+1) = HH(n); % Notice that this supplies the negative frequencies.
  63. end
  64.  
  65. % --- Mutliply XX by HH
  66. YY = zeros(1,NN);
  67. for n=1:NN
  68. YY(n) = HH(n)*XX(n);
  69. end
  70.  
  71. subplot(3,2,3)
  72. plot(FF,HH,'k--')
  73. hold on
  74. plot(FF,abs(YY),'k');
  75. axis( [ 00 250  0 1.1 ])
  76. hold off
  77. xlabel('Freq (Hz)')
  78. ylabel('H(z),X(z)')
  79. set(gca,'fontsize',12)
  80. %legend('H','Y')
  81.  
  82. % --- Inv FFT and plot
  83.  
  84. y = NN*real(ifft(YY));
  85. subplot(3,2,4)
  86. plot(TT,y,'k')
  87. ylabel('y[k]')
  88. xlabel('T (sec)')
  89. set(gca,'fontsize',12)
  90.  
  91. % Get the FIR filter from inverse HH
  92. h = ifft(HH);
  93.  
  94. subplot(3,2,5)
  95. plot(real(h),'ok');
  96. axis( [ 1 8 -.2 .2  ])
  97. grid on
  98. ylabel('h[k]')
  99. xlabel('k')
  100. set(gca,'fontsize',12)
  101.  
  102. subplot(3,2,6)
  103. plot(real(h),'ok');
  104. axis( [  193 200 -.2 .2 ])
  105. grid on
  106. ylabel('h[k]')
  107. xlabel('k')
  108. set(gca,'fontsize',12)
  109.  
  110. saveas(gca,'fft.png')
  111.  
  112. figure(2);
  113.  
  114. % ---------------------------------------
  115.  
  116. % This part tests the FIR filter
  117.  
  118. % First, use h to develop a small FIR filter
  119. hh = zeros(1,17);
  120. hh(8) = real(h(1));
  121. for n=2:7
  122. hh(7+n) = real(h(n));
  123. end
  124.  
  125. for n=1:8
  126. hh(n) = hh(16-n);
  127. end
  128.  
  129. subplot(3,2,1)
  130. plot(hh,'ok')
  131. axis( [ 1 15 -.3 .3 ])
  132. grid on
  133. xlabel('k')
  134. ylabel('h[k]')
  135.  
  136. zz = conv(x,hh);       % Convolve the input with hh
  137.  
  138. for n=1:NN
  139. z(n) = zz(n);
  140. end
  141.  
  142. subplot(3,2,3)
  143. plot(TT,z,'k')
  144. axis( [ 0 .2 -1 1 ])
  145. grid on
  146. xlabel('sec')
  147. ylabel('y[k]')
  148.  
  149. % Plot the FFT of the output
  150. subplot(3,2,5)
  151. plot(FF,(1/NN)*abs(fft(z)),'k');
  152. axis( [ 0 200 0 .3 ])
  153. set(gca,'XTick',[ 0 f1 f2 f3 ])
  154. xlabel('Freq (Hz)')
  155. ylabel('Y(z)')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement