MATLAB: How to take average of specific portions of data with overlap

averagedataemgmeanmoving averagewindow

Hi all,
I have a question regarding the EMG data I am working on. I have attached a file containing a sample data Excel sheet. What I need to do is compute the average of several "windows" for each data set, while also accounting for overlapped sections.For example, Window Size 1 = 1500 points & Overlap Size 1=750 points I then need the mean of each window as follows: [From 1 to 1500] then [750-2250] then [1500 – 3000] etc.
What is the best way to do this? I'm thinking some kind of for loop, but I don't know how exactly to account for overlap. Thank you so much in advance!
Below is the code I have right now to accomplish this. I need a more efficient way to implement this logic into my data set so that I can repeat it easily for various window sizes and overlaps
.
clc;
clear;
[d]=xlsread('Flex1.xls', 'Flex1', 'D5:D10000');
[t]=xlsread('Flex1.xls', 'Flex1', 'A5:A10000');
mean_d_1_1 = mean(d(1:1500))
mean_d_1_2 = mean(d(750:2250))
mean_d_1_3 = mean(d(1500:3000))
mean_d_1_4 = mean(d(2250:3750))
mean_d_1_5 = mean(d(3000:4500))
mean_d_1_6 = mean(d(3750:5250))
mean_d_1_7 = mean(d(4500:6000))
mean_d_1_8 = mean(d(5250:6750))
mean_d_1_9 = mean(d(6000:7500))
mean_d_1_10 = mean(d(6750:8250))
mean_d_1_11 = mean(d(7500:8694))
% code
end

Best Answer

First comment: don't store values in variables with numbered names like that. It just makes a mess, and that's why arrays were invented.
So instead of mean_d_1_1, mean_d_1_2, etc., you can create a single array named mean_d and access specific elements: mean_d(1,1), mean_d(1,2), etc.
As for your loop, just look at your code and see which pieces are incrementing...
  • the first index (1, 750, 1500, 2250)
  • the second index (1500, 2250, 3000)
So:
d = rand(10000,1);
nslide = 750;
nwin = 1500;
n = ceil(length(d)/nslide);
mean_d = zeros(n,1);
for ii = 1:n
idx1 = nslide*(ii-1) + 1;
idx2 = min(nslide*ii,length(d));
mean_d(ii) = mean(d(idx1:idx2));
end