How do I get the fft from the audio wav file?

Hi guys, I am new to MATLAB. I need help on how to get FFT plot from an audio file that i have?

 Réponse acceptée

Walter Roberson
Walter Roberson le 30 Sep 2018

1 vote

[data, fs] = audioread('YourFile.wav');
data_fft = fft(data);
plot(abs(data_fft(:,1)))

If you want better markings such as frequency on the fft plot, then see the examples in the fft documentation to see how to construct the frequency graph.

3 commentaires

Aidil AA
Aidil AA le 7 Oct 2018
Modifié(e) : Walter Roberson le 7 Oct 2018
Hi Walter Roberson,
[data,fs]=audioread('1khz test1.wav');
data_fft = fft(data);
plot(abs(data_fft(:,1)));
title('1khz-fft');
I tested the code above on a pure 1khz sound file, I got the graph shown in the attachment. Shouldn't I just get one spike instead of two? Am I doing something wrong? Many thanks, cheers.
No. fft() is the two-sided fast fourier transform, returning both "negative" time and positive time. The positive time is the first half of the output and the negative time is the second half of the output. fftshift() would move the data to be symmetric around time 0.
Most people are only interested in positive time, so they mostly only plot the first half of the data. See the example plotting "Single-Sided Amplitude Spectrum of X(t)" at https://www.mathworks.com/help/matlab/ref/fft.html
Aidil AA
Aidil AA le 7 Oct 2018
Modifié(e) : Walter Roberson le 8 Juil 2024
Oh I get it now, thank you. So what should I add to the code so that it will only plot the first half of the data?
[data,fs]=audioread('1khz test1.wav');
data_fft = fft(data);
plot(abs(data_fft(:,1)));
title('1khz-fft');

Connectez-vous pour commenter.

Plus de réponses (2)

Emin ORUÇ
Emin ORUÇ le 29 Oct 2019

0 votes

Hi,how can we shift the signal to the left or right and then return to the time domain?

1 commentaire

For your purposes, what does it mean to shift the signal to the left or right?
To some extent, increasing or decreasing the "bin number" of data within an fft result corresponds to frequency shifting; however this idea tends to break down when you think about negative time or think about the frequencies between 0 and the shift.

Connectez-vous pour commenter.

Frantz Bouchereau
Frantz Bouchereau le 10 Déc 2024

0 votes

Here is a popular MATLAB doc page that explains the relationship between FFT and true power spectra: Power Spectral Density Estimates Using FFT. USe this to scale the FFT to obtain true power values.

Catégories

En savoir plus sur Fourier Analysis and Filtering dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by