How can I find the maximum frequency component of an audio signal? I have used the following code so please tell me how can I point out the max freq component by the graph of abs(xfft)? If there is any other way to find it, do share it. Thanks!

18 views (last 30 days)
[y Fs]=wavread(filename); xfft=fft(y); plot(abs(xfft));

Answers (1)

Pratik
Pratik on 26 Jun 2024
Hi maham,
To find the maximum frequency component of an audio signal, you can follow these steps:
  1. Load the audio file and perform the Fast Fourier Transform (FFT).
  2. Compute the magnitude spectrum of the FFT.
  3. Identify the frequency corresponding to the maximum magnitude in the spectrum.
  4. Plot the magnitude spectrum and highlight the maximum frequency component.
Please refer to the following code snippet:
% Load the audio file
% 'audioread' reads the audio file 'filename.wav' and returns the audio data 'y' and the sampling frequency 'Fs'
[y, Fs] = audioread('filename.wav');
% Perform FFT
% 'fft' computes the Fast Fourier Transform of the audio signal 'y'
xfft = fft(y);
% Compute the magnitude spectrum
% 'abs' computes the magnitude of the complex FFT result to get the magnitude spectrum
magnitude_spectrum = abs(xfft);
% Create a frequency vector
% The length of the signal 'y' is stored in 'N'
N = length(y);
% The frequency vector is created, ranging from 0 to Fs-1, with N points
frequencies = (0:N-1)*(Fs/N);
% Find the index of the maximum magnitude
% 'max' finds the maximum value in the magnitude spectrum and its corresponding index
[max_magnitude, max_index] = max(magnitude_spectrum);
% Find the corresponding frequency
% The frequency corresponding to the maximum magnitude is found using the index 'max_index'
max_frequency = frequencies(max_index);
% Plot the magnitude spectrum
% 'figure' creates a new figure window
figure;
% 'plot' plots the magnitude spectrum against the frequency vector
plot(frequencies, magnitude_spectrum);
% 'xlabel' labels the x-axis
xlabel('Frequency (Hz)');
% 'ylabel' labels the y-axis
ylabel('Magnitude');
% 'title' adds a title to the plot
title('Magnitude Spectrum');
% 'grid on' adds a grid to the plot for better readability
grid on;
% Highlight the maximum frequency component
% 'hold on' allows adding more plots to the existing figure
hold on;
% 'plot' highlights the maximum frequency component with a red circle ('ro')
plot(max_frequency, max_magnitude, 'ro');
% 'text' adds a text annotation to the plot at the location of the maximum frequency component
text(max_frequency, max_magnitude, sprintf('Max Frequency: %.2f Hz', max_frequency), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
% 'hold off' releases the hold on the current figure
hold off;
% Display the maximum frequency
% 'disp' displays the maximum frequency component in the command window
disp(['The maximum frequency component is: ', num2str(max_frequency), ' Hz']);
I hope this helps!
  2 Comments
Saroosh
Saroosh on 28 Feb 2025
Hey Pratik,
I used your code to do the same thing with my audio file. It worked thank you. I also wanted to find which sound it is and when does it appear. With help of time graph I can find the time of its occurance but I am unable to find which sound it is. Could you help me out for this.
Walter Roberson
Walter Roberson on 28 Feb 2025
Suppose you have some loud drumming, and suppose the occasional high-hat is played. Suppose there is a point at which the high-hat is played at the same time as one of the loud drum tones. The combination of the two volumes together happens to produce peak loudness.
Now: how do you isolate which sound it was that produced peak loudness in the circumstances?
You might potentially isolate the high-hat, but the high-hat by itself is significantly less loud than the drum, so it would seem weird to say that the high-hat is the loudest sound -- indeed there might be other places where that high-hat played louder that did not happen to coincide with the drum.
You might potentially isolate the drum-beat, but again that one is not necessarily the loudest drum-beat: it is, in this scenario, the combination of the two sounds together that is loudest.
You kind of need to find the peak location and then widen your search a little to find the start and end of the drum beat that has the high-hat imposed on it... but how do you do that?

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!