Reputation: 543
I have a time series data or considering a real valued data of length N
. I want to create sub-blocks of length k
, which is the window length. The value of k
can be arbitrarily chosen. This creates problem since the window size is the same across the data. I want to store each subblock in an array. But I am stuck in creating sub-blocks of the data and to include a check so that the (mod(N, nseg))
nseg
must be divisible by the data length.
N = 512; %length of the time series
data = rand(N,1);
window_length = 30; %k
Nseg = floor(N/window_length) %Number of segments or blocks
Modified_Data = [mean(reshape(data,window_length,Nseg))]; %Throws error
Upvotes: 0
Views: 900
Reputation: 65430
If you have the Image Processing toolbox you could use im2col
to slide a specific block size over the entire time series. Each column of the output represents the data from one of those blocks.
values = im2col(data, [window_length 1], 'distinct');
Since it looks like you just want the mean over each block, you could also use blockproc
to do this.
means = blockproc(data, [window_length, 1], @(x)mean(x.data));
If you do not have the Image Processing Toolbox, you can instead use accumarray
to perform this task.
means = accumarray(floor((0:(N-1)).'/window_length) + 1, data, [], @mean);
If you want to discard any data that extends beyond a number which is divisible by window_length
, you can do this with something like the following:
data = data(1:(numel(data) - mod(numel(data), window_length)));
If you want overlapping data, you'll either want to use straight-up convolution (the preferred method)
means = conv(data(:), ones(5, 1)/5, 'same');
Or you can create overlapping blocks with im2col
by omitting the last input.
values = im2col(data, [window_length 1]);
means = mean(values 1);
Upvotes: 2
Reputation: 18484
If you have R2016a+, consider using the built-in movmean
function:
N = 512; %length of the time series
data = rand(N,1);
window_length = 30; %k
Modified_Data = movmean(data, window_length);
See the documentation for further details and other options.
Upvotes: 2
Reputation: 300
If I understand your question correctly, it's pretty straightforward:
filter(ones(N,1)/N,1,signal)
If you think about it filtering with [1/N 1/N 1/N...1/N] is exactly calculating the localized mean...
Upvotes: 0