## Wednesday, October 21, 2009

### Calculate the power of audio in frequency block

Here is the matlab code, it shows the power between frequency 200-2000. This is the power of all audio data instead of a small window block (like 256 samples):

>> [snd samplefreq] = wavread('crash.wav');>> num_samples = length(snd);>> minfreq = 200; maxfreq = 2000;>> f = fft(snd) / (num_samples/2);>> seconds = num_samples / samplefreq;>> low = floor(seconds * minfreq);>> high = floor (seconds * maxfreq);>> p = abs(f(low:high)) .^ 2;>> figure; plot(p);
If you want to show the fft result directly, use ‘fftshift’ to produce a more standard plot with the low frequencies in the center of plot:
plot(abs(f));plot(abs(fftshift(f)));

The x-axis is plotting the number of samples. It is more useful to plot the single-sided spectrum with frequency in x-axis. For example, we want to display 200-9000Hz spectrum:

Ns = length(p);plot([201:2*(9000-200)/Ns:9000],p(1:Ns/2-1)); %make sure the size are the same