Advertisement
STANAANDREY

Untitled

Nov 20th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. % Define center frequencies
  2. center_freqs = [31.5, 63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000];
  3. num_filters = length(center_freqs);
  4.  
  5. % Set the sample frequency
  6. Fs = 16000;
  7.  
  8. % Define the quality factor (Q)
  9. Q = 5;
  10.  
  11. % Calculate normalized cutoff frequencies
  12. normalized_cutoffs = center_freqs / (Fs / 2 * Q);
  13.  
  14. % Initialize the figure
  15. figure;
  16.  
  17. % Loop through each center frequency and create filters
  18. for i = 1:num_filters
  19. % Calculate cutoff frequencies
  20. cutoff_low = normalized_cutoffs(i) / sqrt(2);
  21. cutoff_high = normalized_cutoffs(i) * sqrt(2);
  22.  
  23. % Design filter using fir1
  24. b = fir1(30, [cutoff_low, cutoff_high], 'bandpass');
  25.  
  26. % Get frequency response using freqz
  27. [H, Freq] = freqz(b, 1, 1024, Fs);
  28.  
  29. % Plot the magnitude components of the filter
  30. subplot(2, 1, 1);
  31. plot(Freq, abs(H), 'DisplayName', sprintf('Center Frequency: %g Hz', center_freqs(i)));
  32. hold on;
  33.  
  34. % Plot the filter coefficients
  35. subplot(2, 1, 2);
  36. plot(b, 'DisplayName', sprintf('Center Frequency: %g Hz', center_freqs(i)));
  37. hold on;
  38. end
  39.  
  40. % Configure subplot 1
  41. subplot(2, 1, 1);
  42. title('Magnitude Components of Filters');
  43. xlabel('Frequency (Hz)');
  44. ylabel('Magnitude');
  45. legend;
  46. grid on;
  47.  
  48. % Configure subplot 2
  49. subplot(2, 1, 2);
  50. title('Filter Coefficients');
  51. xlabel('Coefficient Index');
  52. ylabel('Coefficient Value');
  53. legend;
  54. grid on;
  55.  
  56. % Initialize a new figure for the sum of frequency responses
  57. figure;
  58. sum_response = zeros(size(H));
  59.  
  60. % Loop through each center frequency and add frequency responses
  61. for i = 1:num_filters
  62. % Design filter using fir1
  63. b = fir1(30, [normalized_cutoffs(i) / sqrt(2), normalized_cutoffs(i) * sqrt(2)], 'bandpass');
  64.  
  65. % Get frequency response using freqz
  66. [H, Freq] = freqz(b, 1, 1024, Fs);
  67.  
  68. % Add frequency response to the sum
  69. sum_response = sum_response + abs(H);
  70. end
  71.  
  72. % Plot the sum of frequency responses
  73. plot(Freq, sum_response, 'LineWidth', 2);
  74. title('Sum of Frequency Responses');
  75. xlabel('Frequency (Hz)');
  76. ylabel('Magnitude');
  77. grid on;
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement