Thursday, 16 April 2009

Analyzing a .wav file

In the previous article we found how to create a simple audio music file. Here, we are going to analyze it, i.e., to find out the fundamental frequency and other parameters. The code given below plots the .wav file and it power spectrum.

%Code Starts here

[y, Fs] = wavread(file); % y is sound data, Fs is sample frequency.
t = (1:length(y))/Fs; % time

ind = find(t>0.1 & t<0.12); % set time duration for waveform plot
figure; subplot(1,2,1)
plot(t(ind),y(ind))
axis tight
title(['Waveform of ' file])

N = 2^12; % number of points to analyze
c = fft(y(1:N))/N; % compute fft of sound data
p = 2*abs( c(2:N/2)); % compute power at each frequency
f = (1:N/2-1)*Fs/N; % frequency corresponding to p

subplot(1,2,2)
semilogy(f,p)
axis([0 4000 10^-4 1])
title(['Power Spectrum of ' file])

%Code ends here

querrymail@gmail.com

3 comments:

Anonymous said...

eh i like the way u explained. something innovative..

Sukunath B A said...

Thank you

Anonymous said...

this doesn't actually... find the fundamental frequency. i see where the power spectrum peaks, but not. much. else useful.