Guest User

Untitled

a guest
Jan 18th, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.00 KB | None | 0 0
  1. % Problem 1: Real-Time Fast Convolution using Overlap-Add Technique
  2. %
  3. % The idea is instead of performing convolution in the time domain, which is slow and processor intensive,
  4. % we can take the Fast Fourier Transform of both the input X[n] and the filter H[n] perform convolution
  5. % in the frequency domain, which is simply multiplication, take the inverse fast fourier transform of
  6. % the result and be finished.
  7.  
  8. % Overlap Add Method (see wikipedia)
  9. % M = 7 - filter size
  10. % N = 10 - sample size
  11. % L = N + M - 1 = 16 - FFT matrix size
  12. % overlaps by (M - 1)  6 samples for each DFT evaluation
  13.  
  14.  
  15. % h[n] from fdatool
  16. % 0.0567    0.1560    0.2394    0.2719    0.2394    0.1560    0.0567
  17.  
  18.  
  19. h(1) = 0.0567;
  20. h(2) = 0.1560;
  21. h(3) = 0.2394;
  22. h(4) = 0.2719;
  23. h(5) = 0.2394;
  24. h(6) = 0.1560;
  25. h(7) = 0.0567;
  26.  
  27. % get first 30 samples of function with sample rate of 8khz
  28. pi = 3.141592;
  29. for i=1:30
  30.     t = i * (2 * pi / 8000);
  31.     x(i) = sin(2 * pi * 800 * t) + sin(2 * pi * 1400 * t);
  32. end
  33.  
  34. % this is what the answer should look like
  35. x_conv = conv(h,x);
  36.  
  37. subplot(2,1,1), stem(x);
  38. xlabel 'X[n] input'
  39. subplot(2,1,2), stem(h);
  40. xlabel 'H[n] filter'
  41.  
  42. pause
  43.  
  44.  
  45. % zero pad to 36
  46. x(31) = 0;
  47. x(32) = 0;
  48. x(33) = 0;
  49. x(34) = 0;
  50. x(35) = 0;
  51. x(36) = 0;
  52.  
  53. % zero pad impulse response
  54. for i=8:16
  55.     h(i) = 0;
  56. end
  57.  
  58.  
  59.  
  60. % Get 3 buffers of ten samples
  61. for(i = 1:30)
  62.     if (i <= 10)
  63.         x1(i) = x(i);
  64.     end
  65.     if (i > 10 && i <= 20)
  66.         x2(i-10) = x(i);
  67.     end
  68.     if (i > 20 && i <= 30)
  69.         x3(i-20) = x(i);
  70.     end
  71. end
  72.  
  73. % zero pad for 16 point DFT
  74. for(i = 11:16)
  75.     x1(i) = 0;
  76.     x2(i) = 0;
  77.     x3(i) = 0;
  78. end
  79.  
  80.  
  81.  
  82.  
  83. % calculate omega
  84. N=16;
  85. for i = 1:power(N - 1, 2) + 1
  86.     omega(i) = power( exp(-2 * pi * sqrt(-1) / N), i - 1);
  87. end
  88.  
  89. % populate DFT matrix with omega -- 9x9 matrix for N=10
  90. for(i=1:N)
  91.     for(j=1:N)
  92.         matrix(i,j) = omega( (i-1) * (j-1) + 1 );
  93.     end
  94. end
  95.  
  96.  
  97.  
  98.  
  99. %y1 = x1 * matrix;
  100. %y2 = x2 * matrix;
  101. %y3 = x3 * matrix;
  102. y1 = fft(x1);
  103. y2 = fft(x2);
  104. y3 = fft(x3);
  105.  
  106. % So we have the DFT of X[n], do the same for H[n]
  107. %impulse = h * matrix;
  108. impulse = fft(h);
  109.  
  110.  
  111. % perform filter in frequency domain
  112. for(i = 1:16)
  113.     y1_filtered(i) = y1(i) * impulse(i);
  114.     y2_filtered(i) = y2(i) * impulse(i);
  115.     y3_filtered(i) = y3(i) * impulse(i);
  116. end
  117.  
  118.  
  119. % Inverse is just the complex conjugate of each matrix entry eg: positive sqrt(-1) becomes negative sqrt(-1) and vice versa
  120. inverse_matrix = conj(matrix);
  121.  
  122. x1_filtered = ifft(y1_filtered);
  123. x2_filtered = ifft(y2_filtered);
  124. x3_filtered = ifft(y3_filtered);
  125.  
  126. %x1_filtered = y1_filtered * inverse_matrix;
  127. %x2_filtered = y2_filtered * inverse_matrix;
  128. %x3_filtered = y3_filtered * inverse_matrix;
  129.  
  130. % zero pad
  131. x1 = [x1_filtered zeros(1, 20)]
  132. x2 = [zeros(1, 10) x2_filtered zeros(1, 10)]
  133. x3 = [zeros(1, 20) x3_filtered]
  134.  
  135. % recombine into single output with overlap adding
  136. for(i = 1:36)
  137.         x_filtered(i) = x1(i) + x2(i) + x3(i);
  138. end
  139.  
  140.  
  141. subplot(2,1,1), stem(x_filtered);
  142. xlabel 'filtered time domain result'
  143. subplot(2,1,2), stem(x_conv);
  144. xlabel 'direct convolution'
  145. clear all
  146. pause
Add Comment
Please, Sign In to add comment