Advertisement
Guest User

Untitled

a guest
May 24th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. delta = @(n) (mod(n,1) == 0).*1.0.*(n == 0);
  2. n = (-10:10);
  3. figure;
  4. stem(n,delta(n-3));
  5. title('Figure A.1 I. delta[n-3]');xlabel('n');ylabel('delta[n-3]');
  6. % II. u[n+1]
  7. u = @(n) (mod(n,1) == 0).*1.0.*(n>=0);
  8. n = (-10:10);
  9. figure;
  10. stem(n,u(n+1));
  11. title('Figure A.1 II. u[n+1]');xlabel('n');ylabel('u[n+1]');
  12. % III.
  13. x = @(n) cos(n*pi/5).*u(n);
  14. figure;
  15. stem(n,x(n));
  16. title('Figure A.1 III. cos(n*pi/5).*u(n)');xlabel('n');ylabel('cos(n*pi/5).*u(n)');
  17. % IV.
  18. figure;
  19. stem(n,x(n-3));
  20. title('Figure A.1 IV. x1(n)');xlabel('n');ylabel('x(n-3)');
  21. % V.
  22. figure;
  23. stem(n,x(-1.0.*n));
  24. title('Figure A.1 V. x2(n)');xlabel('n');ylabel('x(-n)');
  25. % In x1(n), a shift to the right by 3 is performed and in x2(n), a
  26. % reflection in the y-axis is performed.
  27.  
  28.  
  29. % I.
  30. u = @(n) (mod(n,1) == 0).*1.0.*(n>=0);
  31. y = @(n) 5.*exp(-1.0.*n/8).*(u(n)-u(n-10));
  32. n = (-10:70);
  33. figure;
  34. stem(n,y(n));
  35. title('Figure A.2 I. y(n)');xlabel('n');ylabel('y(n)');
  36. % II.
  37. figure;
  38. stem(n,y(3.0.*n));
  39. title('Figure A.2 II. y1(n)');xlabel('n');ylabel('y(3n)');
  40. % III.
  41. figure;
  42. stem(n,y((1/3).*n));
  43. title('Figure A.2 III. y1(n)');xlabel('n');ylabel('y((1/3)n)');
  44. % In y1[n], y[n] is scaled down and compressed by a factor of 3. This is an
  45. % example of downsampling because sample points are lost. In y2[n], y[n] is
  46. % time scaled and stretched by a factor of 3, no sample points are lost.
  47.  
  48.  
  49. % I.
  50. t = (-10:0.1:70);
  51. uc = @(t) 1.0.*(t>=0);
  52. z = @(t) 5.*exp(-1.0.*t/8).*(uc(t)-uc(t-10));
  53. figure;
  54. plot(t,z(t));
  55. title('Figure A.3 Ia. z(t)');xlabel('t');ylabel('z(t)');
  56. y3 = @(t) z(t/3);
  57. figure;
  58. plot(t,y3(t));
  59. title('Figure A.3 Ib. y3(t)');xlabel('t');ylabel('y3(t)');
  60. figure;
  61. stem(n,y3(n));
  62. title('Figure A.3 Ic. y3(n)');xlabel('n');ylabel('y3(n)');
  63. % II.
  64. % Y2[n] obtained in part 2 III is not the same as as Y3[n] obtained here
  65. % because in part 2 III, the scaling is performed on a discrete function
  66. % and so sample points are lost due to down sampling. Y3[n] is a discrete
  67. % graph of a Y3(t), where the transformation is performed on the continuous
  68. % function. Here, no sample points are lost to down sampling.
  69.  
  70.  
  71. n = (0:13);
  72. y = [2000; zeros(length(n)-1,1)];
  73. for s = 2: length(n)-1,1;
  74. y(s) = 1.02.*y(s-1);
  75. end
  76. figure
  77. stem(n,y);
  78. title('Figure B.1 y(n)');xlabel('n');ylabel('y(n)');
  79.  
  80.  
  81. n = (0:13);
  82. y = [2000; zeros(length(n)-1,1)];
  83.  
  84. for s = 2: length(n)-1,1;
  85. y(s) = 1.02.*y(s-1) + 100.*s;
  86. end
  87. figure
  88. stem(n,y);
  89. title('Figure B.3 y(n)');xlabel('n');ylabel('y(n)');
  90.  
  91. delta=@(n)1.0*(n==0);
  92. x = @(n)cos((pi/5).*n) + delta(n-20) - delta(n-35);
  93. n =[0:1:44];
  94. figure
  95. stem (n, x(n));
  96. xlabel('n'); ylabel('x[n]'); title('Figure C.2 x[n]');
  97. snapnow;
  98. figure
  99. stem (n, filt(x(n),4));
  100.  
  101. xlabel('n'); ylabel('x[n] Max Filtered at N =4'); title('Figure C.2 x[n] Max Filtered at N=4');
  102. figure
  103. stem (n, filt(x(n),8));
  104. xlabel('n'); ylabel('x[n] Max Filtered at N =8'); title('Figure C.2 x[n] Max Filtered at N=8');
  105. figure
  106. stem (n, filt(x(n),12));
  107. xlabel('n'); ylabel('x[n] Max Filtered at N =12'); title('Figure C.2 x[n] Max Filtered at N=12');
  108.  
  109.  
  110.  
  111. x = [-9 -6 -3 0 3 6 9];
  112. result = calc(x);
  113. energy = result(1)
  114. power = result(2)
  115.  
  116. function [y] = filt(x,N)
  117. M = length(x);
  118. x = x(:);
  119. x = [zeros(N-1,1);x];
  120. y = zeros(M,1);
  121. delta = @(n) (mod(n,1) == 0).*1.0.*(n == 0);
  122. n = (-10:10);
  123. figure;
  124. stem(n,delta(n-3));
  125. title('Figure A.1 I. delta[n-3]');xlabel('n');ylabel('delta[n-3]');
  126. % II. u[n+1]
  127. u = @(n) (mod(n,1) == 0).*1.0.*(n>=0);
  128. n = (-10:10);
  129. figure;
  130. stem(n,u(n+1));
  131. title('Figure A.1 II. u[n+1]');xlabel('n');ylabel('u[n+1]');
  132. % III.
  133. x = @(n) cos(n*pi/5).*u(n);
  134. figure;
  135. stem(n,x(n));
  136. title('Figure A.1 III. cos(n*pi/5).*u(n)');xlabel('n');ylabel('cos(n*pi/5).*u(n)');
  137. % IV.
  138. figure;
  139. stem(n,x(n-3));
  140. title('Figure A.1 IV. x1(n)');xlabel('n');ylabel('x(n-3)');
  141. % V.
  142. figure;
  143. stem(n,x(-1.0.*n));
  144. title('Figure A.1 V. x2(n)');xlabel('n');ylabel('x(-n)');
  145. % In x1(n), a shift to the right by 3 is performed and in x2(n), a
  146. % reflection in the y-axis is performed.
  147.  
  148.  
  149. % I.
  150. u = @(n) (mod(n,1) == 0).*1.0.*(n>=0);
  151. y = @(n) 5.*exp(-1.0.*n/8).*(u(n)-u(n-10));
  152. n = (-10:70);
  153. figure;
  154. stem(n,y(n));
  155. title('Figure A.2 I. y(n)');xlabel('n');ylabel('y(n)');
  156. % II.
  157. figure;
  158. stem(n,y(3.0.*n));
  159. title('Figure A.2 II. y1(n)');xlabel('n');ylabel('y(3n)');
  160. % III.
  161. figure;
  162. stem(n,y((1/3).*n));
  163. title('Figure A.2 III. y1(n)');xlabel('n');ylabel('y((1/3)n)');
  164. % In y1[n], y[n] is scaled down and compressed by a factor of 3. This is an
  165. % example of downsampling because sample points are lost. In y2[n], y[n] is
  166. % time scaled and stretched by a factor of 3, no sample points are lost.
  167.  
  168.  
  169. % I.
  170. t = (-10:0.1:70);
  171. uc = @(t) 1.0.*(t>=0);
  172. z = @(t) 5.*exp(-1.0.*t/8).*(uc(t)-uc(t-10));
  173. figure;
  174. plot(t,z(t));
  175. title('Figure A.3 Ia. z(t)');xlabel('t');ylabel('z(t)');
  176. y3 = @(t) z(t/3);
  177. figure;
  178. plot(t,y3(t));
  179. title('Figure A.3 Ib. y3(t)');xlabel('t');ylabel('y3(t)');
  180. figure;
  181. stem(n,y3(n));
  182. title('Figure A.3 Ic. y3(n)');xlabel('n');ylabel('y3(n)');
  183. % II.
  184. % Y2[n] obtained in part 2 III is not the same as as Y3[n] obtained here
  185. % because in part 2 III, the scaling is performed on a discrete function
  186. % and so sample points are lost due to down sampling. Y3[n] is a discrete
  187. % graph of a Y3(t), where the transformation is performed on the continuous
  188. % function. Here, no sample points are lost to down sampling.
  189.  
  190.  
  191. n = (0:13);
  192. y = [2000; zeros(length(n)-1,1)];
  193. for s = 2: length(n)-1,1;
  194. y(s) = 1.02.*y(s-1);
  195. end
  196. figure
  197. stem(n,y);
  198. title('Figure B.1 y(n)');xlabel('n');ylabel('y(n)');
  199.  
  200.  
  201. n = (0:13);
  202. y = [2000; zeros(length(n)-1,1)];
  203.  
  204. for s = 2: length(n)-1,1;
  205. y(s) = 1.02.*y(s-1) + 100.*s;
  206. end
  207. figure
  208. stem(n,y);
  209. title('Figure B.3 y(n)');xlabel('n');ylabel('y(n)');
  210.  
  211. delta=@(n)1.0*(n==0);
  212. x = @(n)cos((pi/5).*n) + delta(n-20) - delta(n-35);
  213. n =[0:1:44];
  214. figure
  215. stem (n, x(n));
  216. xlabel('n'); ylabel('x[n]'); title('Figure C.2 x[n]');
  217. snapnow;
  218. figure
  219. stem (n, filt(x(n),4));
  220.  
  221. xlabel('n'); ylabel('x[n] Max Filtered at N =4'); title('Figure C.2 x[n] Max Filtered at N=4');
  222. figure
  223. stem (n, filt(x(n),8));
  224. xlabel('n'); ylabel('x[n] Max Filtered at N =8'); title('Figure C.2 x[n] Max Filtered at N=8');
  225. figure
  226. stem (n, filt(x(n),12));
  227. xlabel('n'); ylabel('x[n] Max Filtered at N =12'); title('Figure C.2 x[n] Max Filtered at N=12');
  228.  
  229.  
  230.  
  231. x = [-9 -6 -3 0 3 6 9];
  232. result = calc(x);
  233. energy = result(1)
  234. power = result(2)
  235.  
  236. function [y] = filt(x,N)
  237. M = length(x);
  238. x = x(:);
  239. x = [zeros(N-1,1);x];
  240. y = zeros(M,1);
  241. for m = 1:M
  242. y(m) = max(x([m:m+(N-1)]));
  243. end
  244. end
  245.  
  246.  
  247. function o = calc(x)
  248. e = 0;
  249. for i = 1:length(x)
  250. e = e + abs(x(i))^2;
  251. end
  252. p = e/length(x);
  253. o = [e,p];
  254. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement