0

I am trying to use MATLAB to import a WAV file and create the type of diagram shown below. I am basically trying to pull frequency information and plot it according to decibels. Here is the code I am working with, but it doesn't seem to pull the frequency information correctly:

[x fs]=wavread('filename.wav');
dt=1/fs;%% time interval

X=fft(x);
df=1/(length(x)*dt); %% frequency interval
f=(1:length(X))*df;%% frequency vector

%% frequency domain plot, freq in hertz
figure
plot(f,abs(X))

Frequency:Decibel Diagram

Please help! Thank you!

1 Answers1

1

In your code "X" contains the waveform information, not the frequency information. To get the frequency information of the soundfile you could use the FFT function. I use this (more elaborate, but still simple) code for what you want to do:

[FileName,PathName]=uigetfile('*.wav');
[y, Fs, nbits] = wavread(fullfile(PathName,FileName));
length_y=length(y);

NFFT = 2^nextpow2(length_y); % Next power of 2 from length of y
fft_y=fft(y,NFFT)/length_y;
f = Fs/2*linspace(0,1,NFFT/2+1);

semilogy(f,abs(fft_y(1:length(f))));
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')

I hope this is useful to you. The plot will not be in steps like the one you have shown, but that can also be achieved - using the "stairs" plot function.

  • This is very helpful. I made some modifications and changed semilogy to loglog. How can I change the amplitude spectrum to decibels? –  Dec 29 '13 at 18:10
  • How you change amplitude to dB depends on "what kind of dB" it is! if you are talking sound pressure level, the dB level is calculated as 20*log(p/p_ref), where p is the pressure amplitude in Pascal, and p_ref is 20 micro Pascal... if you are dealing with some kind of power level it would be 10*log... Maybe see http://en.wikipedia.org/wiki/Decibel – shkristensen Jan 08 '14 at 11:59
  • If it is still relevant... assuming your signal is a linear representation of sound pressure (recorded in wave format using a microfone), you can normalize the plot to the right dB level by adding/subtracting the difference to a reference signal, that you know the level of.... – shkristensen Oct 27 '14 at 12:52