Thursday, October 22, 2009

Compute windowed block FFT of audio data

When we start to process the audio data, we cut it to a lot of blocks. There are some overlaps between blocks. If we know the size of a block and how blocks overlap, we can get the total block number and the block information (start and end information). MATLAB code:

% wav is the audio data
% blocksize, let's say 1024
% n: if = 16, means 16 blocks overlap together
% that right-shifts pretty slowly
% blocks: start/end of every block
% ex: blocks(1)=1, blocks(2)=65, blocks(3)=129

function blocks = wavblocks(wav, blocksize, n)

blocks_num = floor((length(wav)/blocksize-1)*n);
blocks = floor((0:blocks_num)*(blocksize/n)+1);

If we want to get the FFT of windowed blocks,

blocks = wavblocks(snd, windowsize, overlap);
numblocks = length(blocks);

minfreq = 200; %specific freq range
maxfreq = 20000;
fs = 8000; % sampling rate

% block2fft func is in last post, snd is audio data
% t is the fft power of a block within specific freq range
t = length(block2fft(snd(blocks(1) : blocks(1) + windowsize - 1),
fs, minfreq, maxfreq));
p = zeros(tl, numblocks);

hann_window(1:windowsize, 1) = 0.54 + 0.46*
cos(2*pi*(0:windowsize-1)/(windowsize-1));

%p(:,i) is the fft power of a windowed block
for (i = 1:numblocks)
p(:, i) = block2fft(snd(blocks(i) : blocks(i) + windowsize
- 1).*hann_window', fs, minfreq, maxfreq);
end

No comments:

Post a Comment