Advertisement
NullChips

Updated MATLab Code

Mar 5th, 2021
984
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.21 KB | None | 0 0
  1. samplingRate = 48000;
  2. width = 550;
  3. cutoff = 1500;
  4. ripple = -80;
  5. A = -ripple;
  6.  
  7. transitionWidthNorm = 2 * pi * width / samplingRate;
  8. cutoffNorm = cutoff / samplingRate;
  9.  
  10.  
  11.  
  12. nTemp = (A - 7.95) / (2.285 * transitionWidthNorm);
  13. N = ceil(nTemp);
  14.  
  15. if(mod(N, 2) == 0)
  16.     N = N + 1;
  17. end
  18.  
  19.  
  20. beta = 0.1102 * (A - 8.7);
  21.  
  22.  
  23.  
  24. %Working out window starts here.
  25. hN = (N - 1) / 2;
  26. n= -hN : hN;
  27.  
  28.  
  29. window = kaiser(N, beta)';
  30. response = 2 * cutoffNorm * sin(2 * pi * cutoffNorm * n)./(n * 2 * pi * cutoffNorm);
  31. response(hN+1) = 2 * cutoffNorm;
  32.  
  33.  
  34. windowedResponse = response.*window;
  35.  
  36. %Working out window ends here.
  37.  
  38. lpAddress = (2^ceil(log2(N)));
  39.  
  40. lowpassFile = fopen('D:\Windows Folders\Desktop\LowPassCoef.asm', 'w');
  41. fprintf(lowpassFile, '\torg\tx:%d\n\n', lpAddress);
  42. fprintf(lowpassFile, 'NTAPS\tEQU\t%d\n\n', N);
  43.  
  44. for c = 1 : N
  45.     fprintf(lowpassFile, 'lpcoef_%d\tdc\t%1.12f\n', c, windowedResponse(c));
  46. end
  47. fclose(lowpassFile);
  48.  
  49. %Start working out high pass filter here.
  50.  
  51. transitionWidthNorm = 2 * pi * width / samplingRate;
  52. cutoffNorm = cutoff / samplingRate;
  53.  
  54.  
  55.  
  56. nTemp = (A - 7.95) / (2.285 * transitionWidthNorm);
  57. N = ceil(nTemp);
  58.  
  59. if(mod(N, 2) == 0)
  60.     N = N + 1;
  61. end
  62.  
  63.  
  64. beta = 0.1102 * (A - 8.7);
  65.  
  66.  
  67.  
  68. %Working out window starts here.
  69. hN = (N - 1) / 2;
  70. n= -hN : hN;
  71.  
  72.  
  73. window = kaiser(N, beta)';
  74. response = -2 * cutoffNorm * sin(2 * pi * cutoffNorm * n)./(n * 2 * pi * cutoffNorm);
  75. response(hN+1) = 1 - (2 * cutoffNorm);
  76.  
  77.  
  78. windowedResponse = response.*window;
  79.  
  80. %Working out window ends here.
  81.  
  82. %Plot graphs of high pass filter.
  83. figure(1)
  84. subplot(3, 1, 1), plot(n, response);
  85. subplot(3, 1, 2), plot(n, window);
  86. subplot(3, 1, 3), plot(n, windowedResponse);
  87. [h1,w] = freqz(response,1,4096,samplingRate);
  88. [h2,w] = freqz(windowedResponse,1,4096,samplingRate);
  89. figure(2)
  90. subplot(2,1,1)
  91. plot(w,20*log10(abs(h1)),w,20*log10(abs(h2)))
  92. subplot(2,1,2)
  93. plot(w,unwrap(angle(h1)),w,unwrap(angle(h2)))
  94.  
  95. hpAddress = (2^ceil(log2(N))) * 2;
  96.  
  97. highpassFile = fopen('D:\Windows Folders\Desktop\HighPassCoef.asm', 'w');
  98.  
  99. fprintf(highpassFile, '\torg\tx:%d\n\n', hpAddress);
  100. for c = 1 : N
  101.     fprintf(highpassFile, 'hpcoef_%d\tdc\t%1.12f\n', c, windowedResponse(c));
  102. end
  103. fclose(highpassFile);
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement