Search code examples
matlabwindowfilteringdata-processing

Applying a Rolling Window on Overlapping Time Measurements


I have data recorded from a device (250-Hz sampling device, ~100K measurements total so about 400 seconds worth). I want to create (overlapping) windows of 512 (~2 seconds) or 1024 (~4 seconds) measurements each, perhaps in ~0.5 second intervals. I'm using Matlab.

Is there a particular way to accomplish this goal? I imagine I want to use a filter to accomplish this goal? Would it change if I wanted to apply, say, a Hann window as well?

Thanks for the help!


Solution

  • Let your data be a row vector. You can use blockproc if you:

    • choose the "blockSize" argument as [1 S], where S is window step;
    • use the 'BorderSize' option to set borders [0 B]. Window length will be S+2*B;
    • set the 'TrimBorder' property to false;
    • define an appropriate function to apply to each window data.

    For example, say you a length-5 window with step 3, and you want the sum of all elements in each window:

    >> x = 1:12;
    >> y = blockproc(x, [1 3], @(t) sum(t.data), 'Bordersize', [0 1], 'TrimBorder', false)
    y =
        10    25    40    42
    

    where

    %// 0+1+2+3+4 = 10
    %// 3+4+5+6+7 = 25
    %// 6+7+8+9+10 = 40
    %// 9+10+11+12+0 = 42
    

    Note the zero padding at the initial and final windows. To avoid that, include an adequate amount of initial zeros in the data, choose data size appropriately, and discard initial and final output values:

    >> x = 1:14;
    >> y = blockproc([0 0 x], [1 3], @(t) sum(t.data), 'Bordersize', [0 1], 'TrimBorder', false)
    y =
         3    15    30    45    60    27
    

    where

    %// 1+2+3+4+5 = 15
    %// 4+5+6+7+8 = 30
    %// 7+8+9+10+11 = 45
    %// 10+11+12+13+14 = 60