Advertisement
stiansjogren

Øving 6, Digsig

Oct 7th, 2014
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.71 KB | None | 0 0
  1. %% Øving 6, Digital signalbehandling, Henning Schei
  2. %
  3. %% Oppgave 1
  4.  
  5. % a)
  6. Nx= 28;
  7. f = linspace(0,1,10000);
  8. X = (1 - (power(0.9*exp(1j*2*pi*f), Nx)))./(1- (0.9*exp(1j*2*pi*f)));
  9. plot (f, abs(X));
  10. title 'Magnutide response'
  11. xlabel 'Frequency'
  12.  
  13. % b)
  14. n = 0:Nx-1;
  15. x = power(0.9,n);
  16. N = [Nx/4, Nx/2, Nx, 2*Nx ];
  17.  
  18. X1 = fft(x,N(1)); X2 = fft(x,N(2));
  19. X3 = fft(x,N(3)); X4 = fft(x, N(4));
  20.  
  21.  
  22.  
  23.  
  24. % d)
  25. figure
  26. subplot(2,2,1)
  27. hold on
  28. plot(f,abs(X));
  29. freq= linspace(0,1, length(X1));
  30. stem(freq,abs(X1));
  31. title 'Magnitude DTFT and DFT with length = 7'
  32.  
  33. subplot(2,2,2)
  34. hold on
  35. plot(f,abs(X));
  36. freq= linspace(0,1, length(X2));
  37. stem(freq,abs(X2));
  38. title 'Magnitude DTFT and DFT with length = 14'
  39.  
  40. subplot(2,2,3)
  41. plot(f,abs(X));
  42. hold on
  43. freq= linspace(0,1, length(X3));
  44. stem(freq,abs(X3));
  45. title 'Magnitude DTFT and DFT with length = 28'
  46.  
  47. subplot(2,2,4)
  48. plot(f,abs(X));
  49. hold on
  50. freq= linspace(0,1, length(X4));
  51. stem(freq,abs(X4));
  52. title 'Magnitude DTFT and DFT with length = 56'
  53.  
  54.  
  55.  
  56. %% Oppgave 2
  57. % a)
  58. clear all
  59. Nh = 9;
  60. Nx = 28;
  61. n  = 0:Nx-1;
  62. h  = ones(1,Nh);
  63. x  = power(0.9,n);
  64. y  = conv(x,h);
  65. Ny = 0:(length(x) + length(h) -2);
  66. figure
  67. stem(Ny,y)
  68. title 'Time domain convolution of x[n] and h[n]'
  69. xlabel 'n'
  70. ylabel 'y[n]'
  71.  
  72. % b)
  73. % Computing y[n] via the frecuency domain
  74. N  = (1/2)*(Nx + Nh -1);
  75. X  = fft(x, N); H = fft(h, N);
  76. Y  = X .* H;
  77. n  = 0:N-1;
  78. y  = ifft(Y, N);
  79. figure
  80. stem(n,y)
  81. Ny = Nx + Nh -1;
  82. % Plotting with different values of $N_y$;
  83. N_set = [Ny/4, Ny/2, Ny, 2*Ny];
  84. for i=1:length(N_set)
  85.     DFT_IFT(x,h, N_set(i));
  86. end
  87.  
  88.  
  89. % DFT-lengths must at lest be equal to the length of Ny to avoid time
  90. % domain alaiasing, as the plots show.
  91.  
  92.  
  93. clear all
  94. %% Oppgave 3
  95. % b)
  96.  
  97. f1 = 7/40; f2 = 9/40;
  98. n  = 0:100-1;
  99. x  = sin(2*pi*f1*n) + sin(2*pi*f2*n);
  100. N  = 1024;
  101. X  = fft(x,N);
  102. f  = linspace(0,0.5, length(X));
  103. plot(f,abs(X));
  104.  
  105. % Repeating with different segment lengths of n
  106. n_ = [1000, 30, 10];
  107. figure
  108. for i=1:3
  109.     n  = 0:n_(i) -1;
  110.     x  = sin(2*pi*f1*n) + sin(2*pi*f2*n);
  111.     N  = 1024;
  112.     X  = fft(x,N);
  113.     f  = linspace(0,0.5, length(X));
  114.     subplot(2,2,i)
  115.     plot(f,abs(X));
  116.     xlabel 'f'
  117.     title (['DFT of x[n] with length n = ' num2str(n_(i))])
  118.  
  119. end
  120. % c) Repeating b) with segment length N = 100 and different DFT lengths
  121. DFT_length=[256, 128];
  122. n= 0:100-1;
  123. N = DFT_length(1);
  124. x  = sin(2*pi*f1*n) + sin(2*pi*f2*n);
  125. X  = fft(x,N);
  126. f  = linspace(0,0.5, length(X));
  127. figure
  128. subplot(2,1,1)
  129. plot(f,abs(X));
  130. xlabel 'f'
  131. title 'DFT- length = 256'
  132. N = DFT_length(2);
  133. x  = sin(2*pi*f1*n) + sin(2*pi*f2*n);
  134. X  = fft(x,N);
  135. f  = linspace(0,0.5, length(X));
  136. subplot(2,1,2)
  137. plot(f,abs(X))
  138. xlabel 'f'
  139. title 'DFT- length = 128'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement