So, here is the problem.
I have a wav file (attached), one tuts piano sound, and I analyzed it. Using this.
[wave,fs] = audioread('file01.wav');
n=length(wave)-1;
figure
t=0:1/fs:n/fs;
subplot(3,1,1), plot(t,wave)
subplot(3,1,1), xlabel('Time (Second)')
subplot(3,1,1), ylabel('Amplitude')
f=0:fs/n:fs;
wavefft=abs(fft(wave));
subplot(3,1,2), plot(f,wavefft)
subplot(3,1,2), xlabel('Frekuency (Hz)')
subplot(3,1,2), ylabel('Magnitude')
[max_value, index] = max(wavefft(:));
subplot(3,1,3), plot(f,wavefft)
subplot(3,1,3), axis([0 index 0 max_value])
The output is like this.
When I search max value using
[max_value, index] = max(wavefft(:));
I got this. Surely the peak would be on the right side, right?
When I inspect it, the max_value is in index 1111 Hz.
But when I zoom in manually, it's on index 740 Hz.
Did I do something wrong?

1 commentaire

Star Strider
Star Strider le 12 Juin 2014
Seems you did everything correctly. The data in ‘subplot(3,1,3)’ is correct.
Explore the documentation on fft to understand why. (See the documentation on fftshift to understand the reason subplot(3,1,2) is misleading you.)

Connectez-vous pour commenter.

 Réponse acceptée

Image Analyst
Image Analyst le 12 Juin 2014

0 votes

Index 1111 is not at 1111 Hz necessarily. From the plot above, it looks like it's at about 750 Hz. But you msight want to use fftshift to shift the ends to the middle.

4 commentaires

Edo Christianto
Edo Christianto le 12 Juin 2014
Modifié(e) : Edo Christianto le 12 Juin 2014
Thanks for the answer. I think I got the gist of it. Shift the ends to the middle. Incorporating fftshift to my script, from
wavefft=abs(fft(wave));
to
wavefft=fftshift(abs((fft(wave))));
what do I get is
Then, how can I know that this particular piano tuts is 750Hz based from this fftshift?
Star Strider
Star Strider le 12 Juin 2014
You actually don’t need to. You did everything correctly in subplot(3,1,3) in your original Question.
Use those data and that plot.
Image Analyst
Image Analyst le 12 Juin 2014
If you shift the spectrum to put the 0 frequency at the center, you need to subtract ~2.25 * 10^4 (actually x(floor(length(x)/2)) I believe) from your x axis so that 0 shows up at the middle, with the middle index of the array you're plotting.
Edo Christianto
Edo Christianto le 12 Juin 2014
Modifié(e) : Edo Christianto le 12 Juin 2014
Ahh... Actually I'm so confused right now... If you don't mind, I would ask again about this in the near future.
Anyway, thanks for all the help, kind Sirs/Mesdames.
(Note for future reference.)

Connectez-vous pour commenter.

Plus de réponses (1)

Malcolm Hawksford
Malcolm Hawksford le 19 Déc 2017

0 votes

Lets assume you have a stereo .wav file and simply want to find the peak amplitude and sample index (time domain) in each channel. Assume the file is called Cohen.wav then this works for me: [p q]=max(abs(audioread('Cohen.wav'))); p gives the peak values and q the corresponding indices.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by