Advertisement
Guest User

Code

a guest
Dec 30th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.72 KB | None | 0 0
  1. close all;
  2. clear all;
  3. clc;
  4.  
  5. %Filter specifications
  6. Ap = 0.05;
  7. Aa = 53;
  8. OmegaP1 = 1000;
  9. OmegaP2 = 1550;             % Index : 170286U
  10. OmegaA1 = 1100;
  11. OmegaA2 = 1400;
  12. OmegaS = 3800;
  13.  
  14. TS = 2*pi / OmegaS;     % sampling period
  15. Bt = min((OmegaA1 - OmegaP1),(OmegaP2 - OmegaA2));  %transiton bandwidth
  16. OmegaC1 = OmegaP1 + Bt/2;   % cutoff 1
  17. OmegaC2 = OmegaP2 - Bt/2;   % cutoff 2
  18.  
  19. deltaP = (10^(0.05*Ap) - 1)/(10^(0.05*Ap) + 1);
  20. deltaA = 10^(-0.05*Aa);
  21. delta = min(deltaP , deltaA);
  22.  
  23. Aa1 = -20*log10(delta);     %stop band attenuation
  24.  
  25. %calculating alpha
  26. if (Aa1<=21)
  27.     alpha = 0;
  28. elseif (21<Aa1 && Aa1<=50)
  29.     alpha = 0.5842*(Aa1 - 21)^0.4 + 0.07886*(Aa1 - 21);
  30. else
  31.     alpha = 0.1102*(Aa1 - 8.7);
  32. end
  33.  
  34. %calculating D
  35. if (Aa1<=21)
  36.     D = 0.9222;
  37. else
  38.     D = (Aa1 - 7.95)/14.36;
  39. end
  40.  
  41. %calculating N
  42. N = ceil(OmegaS*D/Bt + 1);
  43. if(mod(N,2)==0)
  44.     N = N+1;
  45. end
  46.  
  47.  
  48. %calculations of window function
  49. halfRange = (N-1)/2;
  50. n = -halfRange : 1 : halfRange;
  51. beta = alpha*(1 - (2*n/(N-1)).^2).^0.5;
  52.  
  53. %Generating Io_alpha
  54. bessellimit = 125;
  55. Io_alpha = 1;
  56. for k = 1:bessellimit
  57.     val_k = ((1/factorial(k))*(alpha/2).^k).^2;
  58.     Io_alpha = Io_alpha + val_k;
  59. end
  60.  
  61. %Generating Io_beta
  62. Io_beta = 1;
  63. for m = 1:bessellimit
  64.     val_m = ((1/factorial(m))*(beta/2).^m).^2;
  65.     Io_beta = Io_beta +val_m;
  66. end
  67.  
  68. wk_nT = Io_beta/Io_alpha;
  69. %Printing the results
  70. % fprintf('Filter order = %d',N);
  71.  
  72. %Plotting the kaiser function
  73. figure;
  74. stem(n,wk_nT);
  75. xlabel('n');
  76. ylabel('Amplitude');
  77. title('Kaiser window(Time domain)');
  78.  
  79. %h[n]
  80. neg = -halfRange : 1 : -1;
  81. hneg = ((1/pi)./neg).*(sin(OmegaC1*TS.*neg) - sin(OmegaC2*TS.*neg));
  82. hzero = 1 + 2*(OmegaC1 - OmegaC2)/OmegaS;
  83. nposi = 1 : 1 : halfRange;
  84. hposi = ((1/pi)./nposi).*(sin(OmegaC1*TS.*nposi) - sin(OmegaC2*TS.*nposi));
  85. h = [hneg,hzero,hposi];
  86. n = [neg,0,nposi];
  87.  
  88. figure;
  89. stem(n,h);
  90. grid on;
  91. xlabel('n');
  92. ylabel('h[n]');
  93. title('Impulse Response');
  94.  
  95. %Filter response
  96. hw_nT = h.*wk_nT;
  97.  
  98. %Question 2
  99. %Plotting the causal stopband filter
  100. n_shifted = [0:1:N-1];
  101. figure;
  102. stem(n_shifted,hw_nT);
  103. xlabel('n');
  104. ylabel('Amplitude');
  105. title('Causal Impulse Response filter(Time Domain)');
  106.  
  107. %obtaining the frequency domain impulse response response
  108. fvtool(hw_nT);
  109.  
  110. %Question 3
  111. [Hw,f] = freqz(hw_nT);      %obtaining the frequency response and corresponding frequencies
  112. w = f*OmegaS/(2*pi);        %Angular frequency
  113. log_Hw = 20.*log10(abs(Hw));
  114. figure;
  115. plot(w,log_Hw);
  116. xlabel('Angular frequency(rad/s)');
  117. ylabel('Magnitude(dB)');
  118. title('Magnitude response of the filter(Frequency domain)');
  119.  
  120. %Question 4
  121. %Plotting the magnitude response of the lower passband
  122. figure;
  123. finish = round((length(w)/(OmegaS/2)*OmegaC1));
  124. wpass_l = w(1:finish);
  125. hpass_l = log_Hw(1:finish);
  126. plot(wpass_l,hpass_l);
  127. axis([-inf, inf, -0.1, 0.1]);
  128. xlabel('Frequency (rad/s)');
  129. ylabel('Magnitude (dB)');
  130. title('Magnitude response of Lower Passband - Frequency Domain');
  131.  
  132. %Plotting the magnitude response of the lower passband
  133. figure;
  134. start = round(length(w)/(OmegaS/2)*OmegaC2);
  135. wpass_h = w(start:length(w));
  136. hpass_h = log_Hw(start:length(w));
  137. plot(wpass_h,hpass_h);
  138. axis([-inf, inf, -0.1, 0.1]);
  139. xlabel('Frequency (rad/s)');
  140. ylabel('Magnitude (dB)');
  141. title('Magnitude response of the Upper Passband - Frequency Domain');
  142.  
  143.  
  144. %Question 5
  145. %Generating the input of desired number samples
  146. %Generating the discrete signal
  147. %Component frequencies of the input
  148. O_1 = OmegaC1/2;
  149. O_2 = OmegaC1 + (OmegaC2-OmegaC1)/2;
  150. O_3 = OmegaC2 + (OmegaS/2-OmegaC2)/2;
  151.  
  152. n1 = 0:1:600;   % samples
  153. X = cos(O_1.*n1.*TS)+cos(O_2.*n1.*TS)+cos(O_3.*n1.*TS);
  154.  
  155. figure;
  156. subplot(2,1,1);
  157. stem(n1,X);
  158. xlabel('n');
  159. ylabel('Amplitude');
  160. title('Input signal(Time domain)')
  161. subplot(2,1,2);
  162. len_fft = 2^nextpow2(numel(n1))-1;
  163. x_fft = fft(X,len_fft);
  164. x_fft_plot = [abs([x_fft(len_fft/2+1:len_fft)]),abs(x_fft(1)),abs(x_fft(2:len_fft/2+1))];
  165. f = OmegaS*linspace(0,1,len_fft)-OmegaS/2;
  166. plot(f,x_fft_plot);
  167. xlabel('Frequency rad/s');
  168. ylabel('Magnitude');
  169. title('Input signal in the frequency domain');
  170. axis tight;
  171.  
  172. %Question 6
  173. % Filtering using frequency domain multiplication
  174. len_fft = length(X)+length(hw_nT)-1; % length for fft in x dimension
  175. x_fft = fft(X,len_fft);
  176. hw_nT_fft = fft(hw_nT,len_fft);
  177. out_fft = hw_nT_fft.*x_fft;
  178. out = ifft(out_fft,len_fft);
  179. rec_out = out(floor(N/2)+1:length(out)-floor(N/2));
  180.  
  181. % Ideal Output Signal
  182. ideal_out = cos(O_1.*n1.*TS)+cos(O_3.*n1.*TS);
  183.  
  184. %Obtaining the output waveforms
  185. % Frequency domain representation of output signal after filtering using the designed filter
  186. figure;
  187. subplot(2,1,1);
  188. len_fft = 2^nextpow2(numel(n1))-1;
  189. xfft_out = fft(rec_out,len_fft);
  190. x_fft_out_plot = [abs([xfft_out(len_fft/2+1:len_fft)]),abs(xfft_out(1)),abs(xfft_out(2:len_fft/2+1))];
  191. f = OmegaS*linspace(0,1,len_fft)-OmegaS/2;
  192. plot(f,x_fft_out_plot);
  193. xlabel('Frequency rad/s');
  194. ylabel('Magnitude');
  195. title('Output signal of the designed filter in the frequency domain');
  196.  
  197. % Time domain representation of output signal after filtering using the designed filter
  198. subplot(2,1,2);
  199. stem(n1,rec_out);
  200. xlabel('n');
  201. ylabel('Amplitude');
  202. title('Output signal of the designed filter in the time domain');
  203.  
  204. %Obtaining the outputs of the ideal filter
  205. figure;
  206. subplot(2,1,1);
  207. xfft_outideal = fft(ideal_out,len_fft);
  208. x_fft_outideal_plot = [abs([xfft_outideal(len_fft/2+1:len_fft)]),abs(xfft_outideal(1)),abs(xfft_outideal(2:len_fft/2+1))];
  209. plot(f,x_fft_outideal_plot);
  210. xlabel('Frequency rad/s');
  211. ylabel('Magnitude');
  212. title('Output signal of the ideal filter in the frequency domain');
  213.  
  214. % Time domain representation of output signal after filtering using ideal filter
  215. subplot(2,1,2);
  216. stem(n1,ideal_out);
  217. xlabel('n');
  218. ylabel('Amplitude');
  219. title('Output signal of the ideal filter in the time domain');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement