How do I take average of specific portions of data with overlap?

8 views (last 30 days)
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

Accepted Answer

Kelly Kearney
Kelly Kearney on 23 Jan 2017
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
  3 Comments
Kelly Kearney
Kelly Kearney on 24 Jan 2017
Edited: Kelly Kearney on 24 Jan 2017
You're right, I wrote that out too quickly and used the wrong equation for idx2. Easy way to check this is to do some simple debugging; for example, print out the values of idx1 and idx2:
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));
fprintf('%d %d\n', idx1, idx2);
end
Results:
1 750
751 1500
1501 2250
2251 3000
3001 3750
3751 4500
4501 5250
5251 6000
6001 6750
6751 7500
7501 8250
8251 9000
9001 9750
9751 10000
Oops, that's not right. I was using the slide size and not the window size. Let's try again:
for ii = 1:n
idx1 = nslide*(ii-1) + 1;
idx2 = min(idx1 + nwin - 1, length(d));
mean_d(ii) = mean(d(idx1:idx2));
fprintf('%d %d\n', idx1, idx2);
end
Results:
1 1500
751 2250
1501 3000
2251 3750
3001 4500
3751 5250
4501 6000
5251 6750
6001 7500
6751 8250
7501 9000
8251 9750
9001 10000
9751 10000
Much better. Though note that it's still not quite identical to the way you typed it out in your example... in that one, your first window was 1500 long and all the rest were 1501. If you really want it that way, you'll need to adjust the idx1 equation accordingly.
Anthony
Anthony on 25 Jan 2017
Hi,
This is perfect! Thank you very much for the help! I appreciate it greatly.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!