Search code examples
matlabsignal-processingwindowing

Number of overlapping windows of a given length


I'm writing a code in Matlab with a simple windowing function in order to apply a simple overlap and add algorithm to my input signal.

So far this is what I have written:

[s_a,Fs] = audioread('a.wav');
frame_dur = 0.04; %length of my window in time
frame_stride = 0.01; %shift of every single window in time

frame_len = round(frame_dur * Fs); 
frame_step = round(frame_stride*Fs);

win = hamming(frame_len);

The window overlapping is given by shift in time instead of a percentage value of its length (so every 10ms I have a window that ends 40ms later.

How do i calculate the number of windows in my signal?

I found this solution but i do not have the overlap r. Can I find my number of windows starting from the data I have?


Solution

  • Let us assume that n is the number of samples in your audio file:

    n=numel(s_a);
    

    When processing your data frame wise (with each window) you would do the follwoing

    for frame=1:frame_step:(n-(frame_len-1))
    
        Tmp=s_a(frame:frame+frame_len-1).*win;
        %do something with tmp
    
    end
    

    So you can see that the amount of windows that fits into your data can be calculated in the following way:

    num_win=numel(1:frame_step:(n-(frame_len-1)));
    

    Assuming the worst case that n is not a multiple of frame_len you can calculate it like:

    num_win=floor((n-(frame_len))/frame_step)+1;