Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.22 KB | None | 0 0
  1. %%Problem A.1
  2. u=@(t) 1.0*(t>=0);
  3. x=@(t) u(t)-u(t-10);
  4. h=@(t) u(t)-u(t-10);
  5. dtau = 0.005; tau = 0:dtau:20;
  6. ti = 0; tvec = 0:0.01:20;
  7. z = NaN*zeros(1,length(tvec)); % Pre-allocate memory
  8.  
  9. for t = tvec
  10. ti = ti+1; % Time index
  11.     xh = x(t-tau).*h(tau); lxh = length(xh);
  12.     z(ti) = sum(xh.*dtau); % Trapezoidal approximation of convolution integral
  13. end
  14.     plot(tvec, z)
  15.     title('z(t)=x(t)*x(t)');
  16.     xlabel('t');
  17.     ylabel('z(t)');
  18.     figure;
  19.    
  20. %%Problem A.2
  21. N = 100; PulseWidth = 10;
  22. t = [0:1:(N-1)];
  23. X = [ones(1,PulseWidth), zeros(1,N-PulseWidth)];
  24. stairs(t,X); grid on; axis([-10,110,-0.1,1.1])
  25. title('x(t)=pulse with width of 10');
  26. xlabel('t');
  27. ylabel('x(t)');
  28. Xf=fft(X);
  29. Zf=Xf.*Xf;
  30. %%Problem A.3
  31. figure;
  32. f=[-(N/2):1:(N/2)-1]*(1/N);
  33. w=2*pi*f;
  34. subplot(211); plot(f,fftshift( abs(Zf))); grid on;
  35. title('|Z(f)|');
  36. xlabel('f');
  37. ylabel('|Z(f)|');
  38. subplot(212); plot(f,fftshift(angle(Zf))); grid on;
  39. title('phase of Z(f)');
  40. xlabel('f');
  41. ylabel('phase of Z(f)');
  42.  
  43. %%problem A.4
  44. figure;
  45. N = 100; PulseWidth = 10;
  46. t = [0:1:(N-1)];
  47. x = [ones(1,PulseWidth), zeros(1,N-PulseWidth)];
  48. f = [-(N/2):1:(N/2)-1]*(1/N);
  49. z=conv(x,x);
  50. T=[0:1:(2*N)-2];
  51. plot(T,z)
  52. axis([0 N -2 10])
  53. title('time domain operation');
  54. xlabel('f');
  55. ylabel('z(t)');
  56.  
  57. figure;
  58. xF=fft(x);
  59. zF=xF.*xF;
  60. Z=ifft(zF);
  61. plot(t,Z)
  62. title('frequency domain operation');
  63. xlabel('f');
  64. ylabel('z(t)');
  65.  
  66. %the Plots shown using the time-domain and frequency are the
  67. %exact same, as was predicted by the property of the fourier transform
  68. %which states the multiplication in the frequency domain is the same as
  69. %convolution in the time domain
  70.  
  71. %% problem A.5
  72. N = 100; PulseWidth = 5;
  73. t = [0:1:(N-1)];
  74. X = [ones(1,PulseWidth), zeros(1,N-PulseWidth)];
  75. Zf=fft(X);
  76. f=[-(N/2):1:(N/2)-1]*(1/N);
  77. w=2*pi*f;
  78. figure;
  79. subplot(211); plot(f,fftshift( abs(Zf))); grid on;
  80. title('magnitude of frequencies of x(t) with a width of 5');
  81. xlabel('f');
  82. ylabel('x(f)');
  83. subplot(212); plot(f,fftshift(angle(Zf))); grid on;
  84. title('phase plot of x(t) with a width of 5');
  85. xlabel('f');
  86. ylabel('x(f)');
  87. figure;
  88.  
  89. N = 100; PulseWidth = 25;
  90. t = [0:1:(N-1)];
  91. X = [ones(1,PulseWidth), zeros(1,N-PulseWidth)];
  92. Zf=fft(X);
  93. f=[-(N/2):1:(N/2)-1]*(1/N);
  94. w=2*pi*f;
  95. subplot(211); plot(f,fftshift( abs(Zf))); grid on;
  96. title('magnitude of frequencies of x(t) with a width of 25');
  97. xlabel('f');
  98. ylabel('x(f)');
  99. subplot(212); plot(f,fftshift(angle(Zf))); grid on;
  100. title('phase plot of x(t) with a width of 25');
  101. xlabel('f');
  102. ylabel('x(f)');
  103. figure;
  104.  
  105. %the magnitude of frequency plot it scaled according to the
  106. %time scale. in addition, the frequency plot of phase is also
  107. %scaled proportional to the time scale.
  108. %this is the same as the property which states: a time scale of a
  109. %will be :(1/|a|)X(w/a)
  110.  
  111. %%problem A.6
  112. N = 100; PulseWidth = 5;
  113. t = [0:1:(N-1)];
  114. X = [ones(1,PulseWidth), zeros(1,N-PulseWidth)];
  115. f = [-(N/2):1:(N/2)-1]*(1/N);
  116. ex1=exp(i*(pi/3).*t);
  117. wplus=fft(ex1.*X);
  118. subplot(211); plot(f,fftshift( abs(wplus))); grid on;
  119. title('magnitude plot of x(t)*e^{j*(pi/3)*t}');
  120. xlabel('f');
  121. ylabel('|z(f)| with shift');
  122. subplot(212); plot(f,fftshift(angle(wplus))); grid on;
  123. title('phase plot of x(t)*e^{j*(pi/3)*t}');
  124. xlabel('f');
  125. ylabel('angle of z(f) with shift');
  126.  
  127. figure;
  128. ex2=exp(-i*(pi/3).*t);
  129. wminus=fft(ex2.*X);
  130. subplot(211); plot(f,fftshift( abs(wminus))); grid on;
  131. title('magnitude plot of x(t)*e^{-j*(pi/3)*t}');
  132. xlabel('f');
  133. ylabel('|z(f)| with shift');
  134. subplot(212); plot(f,fftshift(angle(wminus))); grid on;
  135. title('phase plot of x(t)*e^{-j*(pi/3)*t}');
  136. xlabel('f');
  137. ylabel('angle of z(f) with shift');
  138.  
  139. figure;
  140. thing=cos(t*pi/3);
  141. wc=fft(thing.*X);
  142. subplot(211); plot(f,fftshift( abs(wc))); grid on;
  143. title('magnitude plot of x(t)*cos((pi/3)*t)');
  144. xlabel('f');
  145. ylabel('');
  146. subplot(212); plot(f,fftshift(angle(wc))); grid on;
  147. title('phase plot of x(t)*cos((pi/3)*t)');
  148. xlabel('f');
  149. ylabel('');
  150.  
  151. %this final property is an example of a shift in the frequency domain
  152. %if you multiply a function with a complex exponential, that corresponds to
  153. %a horizontal shift in the frequency domain. this is clearly seen in the
  154. %first 2 graphs for A.6. the last graph is just the real component of a
  155. %complex exponential, which is why it is not a perfect shift.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement