Advertisement
Guest User

632 lab 4 AB- Final

a guest
Mar 24th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.75 KB | None | 0 0
  1. %% Part A
  2. %problem 1
  3. xn=[1 6/7 5/7 4/7 3/7 2/7 1/7 zeros(1,121)];                               %declaring xn as an array
  4. n=0:127;                                                                   %delclaring n
  5. xf=fft(xn);                                                                %finding DTFT of xn
  6. xf=fftshift(xf);                                                           %shifting xf to be centered at 0
  7. plot(n.*2*pi/128,abs(xf));                                                 %plotting from -pi to pi
  8. title("fft calculated magnitude plot");                                    %setting a title
  9.  
  10.  
  11. %problem 2
  12. disp('For A2: The DTFT was calculated using the following equation: Sum (from n=-infinity to infinity) of x[n]*e^(-j*omega*n). The result for our given x[n] is: exp(-0i.*o) + (6/7)*exp(-i.*o) + (5/7)*exp(-2i.*o) + (4/7)*exp(-3i.*o) + (3/7)*exp(-4i.*o) + (2/7)*exp(-5i.*o) + (1/7)*exp(-6i.*o)')
  13. xomega=@(o) exp(-0i.*o) + (6/7)*exp(-i.*o) + (5/7)*exp(-2i.*o) + (4/7)*exp(-3i.*o) + (3/7)*exp(-4i.*o) + (2/7)*exp(-5i.*o) + (1/7)*exp(-6i.*o); %creating the function for the hand calculated DTFT
  14. figure;
  15. omega=n.*2*pi/128;                                                         %setting an index for plotting
  16. xf2=xomega(omega);                                                         %instantiating the function
  17. xf2=fftshift(xf2);                                                         %shifting xf2 to be centered at 0
  18. plot(omega,abs(xf2));                                                      %plotting
  19. title("Manually calculated magnitude plot");                               %setting a title
  20.                                                                            
  21. %this part of the code is creating a graph comparing parts 1 and 2
  22. figure;
  23. plot(omega,abs(xf2));
  24. hold on;
  25. plot(omega,abs(xf),'-.g');
  26. hold off;
  27. title("comparison of manually calculated spectrum and fft spectrum");
  28.  
  29.  
  30. %Problem 3
  31. Xn=ifft(fftshift(xf));                                                     %preforming the Inverse DTFT
  32. figure;    
  33. stem(n,Xn);                                                                %plotting the result
  34. title("original signal for part 3 ");                                      %giving it a title
  35. disp("Part A3: yes, the original signal is the exact same, due to the fact that we are finding the spectrum of the signal, then without any changes, recovereing the signal using the inverse DTFT");
  36.  
  37.  
  38. %% Part B
  39. un= @(n) 1.0.*(n>=0);                                                      %declaring a general step function
  40. und= @(n) un(n).*(mod(n,1)==0);                                            %declaring a discrete step function
  41. n=[0:15];                                                                  %declaring n for making the function
  42. xn=sin(2*pi.*n/10).*(und(n)-und(n-10));                                    %creating the function xn
  43. omega=linspace(-pi,pi,1001);                                               %doing the DTFT
  44. W_omega1=exp(-j).^((0:length(xn)-1)'*omega);
  45. X1=(xn*W_omega1);
  46. plot(omega, abs(X1));                                                      %plotting the DTFT
  47. title("magnitude of x[n] FT");                                             %adding a title
  48. figure;    
  49.  
  50. hn=und(n)-und(n-10);                                                       %creating the function h[n]
  51. W_omega2=exp(-j).^((0:length(hn)-1)'*omega);                               %preforming the DTFT
  52. X2=(hn*W_omega2);
  53. plot(omega, abs(X2));                                                      %plotting the DTFT
  54. title("magnitude of h[n] FT");                                             %adding a title
  55. figure;
  56.  
  57. yf=X1.*X2;                                                                 %multiplying the spectrum of xn and hn
  58. plot(omega,abs(yf));                                                       %plotting the result
  59. title("magnitude of X[\Omega]H[\Omega]");                                  %adding a title
  60. figure;
  61.  
  62. yn=conv(xn,hn);                                                            %preforming a convolution on the time domain functions of xn and hn
  63. W_omega3=exp(-j).^((0:length(yn)-1)'*omega);                               %preforming the DTFT
  64. X3=(yn*W_omega3);
  65. plot(omega,abs(X3));                                                       %plotting the result
  66. title("magnitude of y[n]");                                                %adding a title
  67.  
  68. %creating a graph to compart parts 3 and 5
  69. figure;
  70. plot(omega,abs(yf));                                                      
  71. hold on;
  72. plot(omega, abs(X3), '-.g');
  73. hold off;
  74. title("comparison of part 3 and part 5");
  75. disp("Part B6 answer: convolution in the time domain is the same thing as multiplication in the frequency domain, so the results of the 2 operations should be the same");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement