Advertisement
STANAANDREY

apd week 8 assig 2

Nov 15th, 2023
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.74 KB | None | 0 0
  1. inputFile = 'input.wav';
  2. gains = [0, 3, -6, 9, 0, -12, 6, 0, -9, 12]; % Example gains in dB
  3. outputFile = 'output.wav';
  4.  
  5. equalizer(inputFile, gains, outputFile);
  6.  
  7.  
  8. function equalizer(inputFile, gains, outputFile)
  9.     % Step 1: Read the input wavefile
  10.     [x, fs] = audioread(inputFile);
  11.  
  12.     % Step 2: Plot the magnitude components of the frequency spectrum
  13.     plotSpectrum(x, fs, 'Input Spectrum');
  14.  
  15.     % Step 3: Create filters
  16.     centerFreqs = [31.5, 63, 125, 250, 500, 1000, 2000, 4000, 8000, 16000];
  17.     filters = cell(1, 10);
  18.  
  19.     for i = 1:10
  20.         f1 = centerFreqs(i) / (fs/2);
  21.         f2 = centerFreqs(i+1) / (fs/2);
  22.         filters{i} = fir1(100, [f1 f2], 'band');
  23.     end
  24.  
  25.     % Step 4: Apply filters to signal components
  26.     filteredSignals = cell(1, 10);
  27.  
  28.     for i = 1:10
  29.         filteredSignals{i} = filter(filters{i}, 1, x);
  30.     end
  31.  
  32.     % Step 5: Apply gain values and reconstruct the signal
  33.     for i = 1:10
  34.         filteredSignals{i} = filteredSignals{i} * 10^(gains(i)/20);
  35.     end
  36.  
  37.     y = sum(cell2mat(filteredSignals), 2);
  38.  
  39.     % Step 6: Plot the magnitude components of the frequency spectrum for the resulting audio signal
  40.     plotSpectrum(y, fs, 'Output Spectrum');
  41.  
  42.     % Step 7: Write the resulting audio signal to the output wavefile
  43.     audiowrite(outputFile, y, fs);
  44. end
  45.  
  46. function plotSpectrum(x, fs, titleText)
  47.     N = length(x);
  48.     f = (-N/2:N/2-1) * fs/N;
  49.  
  50.     figure;
  51.     subplot(2, 1, 1);
  52.     plot(f, fftshift(abs(fft(x))));
  53.     title('Magnitude Spectrum - Before Equalization');
  54.     xlabel('Frequency (Hz)');
  55.     ylabel('Magnitude');
  56.  
  57.     subplot(2, 1, 2);
  58.     plot(f, fftshift(abs(fft(x))));
  59.     title(titleText);
  60.     xlabel('Frequency (Hz)');
  61.     ylabel('Magnitude');
  62. end
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement