Advertisement
STANAANDREY

apd sapt 8 assig1

Nov 15th, 2023
1,616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.76 KB | None | 0 0
  1. % Design 10 filters with center frequencies and a common quality factor
  2. center_frequencies = [31.5, 63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000];
  3. quality_factor = 10;
  4.  
  5. % Sample rate (you may adjust this based on your specific application)
  6. Fs = 44100;
  7.  
  8. % Normalize center frequencies
  9. normalized_center_frequencies = center_frequencies / (Fs / 2);
  10.  
  11. % Calculate cutoff frequencies for each filter
  12. cutoff_frequencies = normalized_center_frequencies / quality_factor;
  13.  
  14. % Initialize figure for plotting
  15. figure;
  16.  
  17. % Design and plot each filter
  18. for i = 1:length(center_frequencies)
  19.     % Design filter using fir1
  20.     filter_order = 50;  % Adjust as needed
  21.     b = fir1(filter_order, [cutoff_frequencies(i) cutoff_frequencies(i) + 2/quality_factor], 'bandpass');
  22.    
  23.     % Get frequency response using freqz
  24.     [H, F] = freqz(b, 1, 1024, Fs);
  25.    
  26.     % Plot individual filter response
  27.     subplot(2, 1, 1);
  28.     plot(F, abs(H), 'DisplayName', sprintf('Center Frequency = %d Hz', center_frequencies(i)));
  29.     hold on;
  30.    
  31.     % Plot cumulative frequency response
  32.     subplot(2, 1, 2);
  33.     plot(F, abs(H), 'DisplayName', sprintf('Center Frequency = %d Hz', center_frequencies(i)));
  34.     hold on;
  35. end
  36.  
  37. % Finish plotting individual filter response
  38. subplot(2, 1, 1);
  39. title('Individual Filter Responses');
  40. xlabel('Frequency (Hz)');
  41. ylabel('Magnitude');
  42. legend;
  43. grid on;
  44.  
  45. % Finish plotting cumulative frequency response
  46. subplot(2, 1, 2);
  47. title('Sum of Filter Responses');
  48. xlabel('Frequency (Hz)');
  49. ylabel('Magnitude');
  50. legend;
  51. grid on;
  52.  
  53. % Display final figure
  54. hold off;
  55.  
  56. % Save the figure if needed
  57. saveas(gcf, 'filter_responses.png');
  58.  
  59. % Optionally, you can write the filter coefficients to a file for later use
  60. % dlmwrite('filter_coefficients.txt', b);
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement