Clear Filters
Clear Filters

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!

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

Answers (1)

Pratik
Pratik on 26 Jun 2024 at 9:04
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!

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!