Plotting an average of multiple time series of different length

56 views (last 30 days)
I have 24 different time series where the index number is the elapsed time in miliseconds and the value is power. (e.g. 1069x1 double, 873x1 double, etc.)
I am trying to create a plot using the average of all 24 of the time series where the x-axis is 0 to 100% of the movement (which is a vertical jump) and the y-axis is power.
Plotting an individual time series with these parameters is simple, but I am struggling to find a way to get them all to an equal length and then average them. I am thinking it will require interpolation for the shorter time series, but again, not sure where to start. Below is roughly what I am trying to accomplish (disregard the fact that there are 3 lines).
Thank you for the help!

Accepted Answer

Dave B
Dave B on 29 Sep 2021
Edited: Dave B on 29 Sep 2021
I think you're saying that you want to resample these all so that they have the same number of samples, then take the average. For the simple interpolation solution you could just use interp1, specifying an arbitrary number of samples (maybe choosing the one with the most samples). (You might consider using resample for this which will apply anti-aliasing filters.)
y1 = smoothdata(rand(1069,1),'gaussian', 100);
y2 = smoothdata(rand(873,1),'gaussian',100);
y3 = smoothdata(rand(500,1),'gaussian',100);
y4 = smoothdata(rand(1000,1),'gaussian',100);
Y=nan(4,1000);
Y(1,:) = interp1(y1,linspace(1,numel(y1),1000));
Y(2,:) = interp1(y2,linspace(1,numel(y2),1000));
Y(3,:) = interp1(y3,linspace(1,numel(y3),1000));
Y(4,:) = interp1(y4,linspace(1,numel(y4),1000));
h=plot(linspace(0,100,1000),Y,'Color',[.7 .7 .7]);
hold on
hh=plot(linspace(0,100,1000),mean(Y),'k-','linewidth',2);
legend([h(1) hh],'Individual','Average')
  5 Comments
Dave B
Dave B on 30 Sep 2021
@Ryan Baker Happy it's working, you were totally clear just wanted to double-check. It sounds like you've got some messy data to start learning MATLAB with!
Here's some detail (more than you ever could have wanted) about 1-3:
  1. Mostly true. I wouldn't call it an empty matrix (because an empty matrix couldn't have dimensions 24 x 1000. You don't really have to do this step, but when you're about to keep tacking on rows or columns it's good to initialize. It's good to intialize your variables because your code will run faster and you won't get confused by running your code multiple times and finding things get weird. Sometimes people initialize with zeros, you could initializes with anything you want, but most of us prefer to initialize with NaN (which means 'Not a Number'). The bonus of this is that lots of stuff won't look right with NaN (like plots), so if you start off with NaN and then accidentally don't fill in a value, you're likely to notice (but less so with zeros).
  2. Absolutely right, I picked 1000 arbitrarily, it's a nice round number and you probably don't have more than 1000 pixels on your plot.
  3. interp1 is (in my opinion) one of the best things to know in MATLAB, I almost never use it now that I work at Mathworks but when I was a scientist it was my best friend. The way I think of interp1 is that it (normally) looks like: yi = interp1(x, y, xi); (Sorry I use different parameter names than our documentation, I like mine better!) Read this as, given some relationship between x and y, take some new x values, and figure out what the y values would be.
This is easier to explain with a plot:
x = [1 3 7 10];
y = [5 10 4 8];
plot(x, y, '-o', 'MarkerFaceColor', 'w', 'LineWidth', 2)
Now, if I asked you - what's the value of y when x is 2, you'd maybe grab a ruler, line it up with 2, hold your finger where it meets the line, and rotate it 90 degrees. That's what interp1 does:
xi = 2;
yi = interp1(x, y, xi);
plot(x,y,'-o','MarkerFaceColor','w', 'LineWidth', 2)
hold on
plot([2 2],[min(ylim) yi])
plot([min(xlim), 2],[yi yi])
text(1,yi,string(yi),'HorizontalAlignment','right','VerticalAlignment','middle')
The reason interp1 is awesome in MATLAB is because you can give it a boatload of points and it'll compute a boatload of y values (very quickly), which turns out to be useful for a lot of things!
Ticky sidenote: even though I told you interp1 is yi = interp1(x, y, xi); we called it with just two arguments. When you call interp1 with two arguments it's a shortcut for x being the indices of y (i.e. it's another way of writing yi = interp1(1:length(y), y, xi));
And remember that xi in our case was 1000 values between 1 and length(y)
So here's a demo of what that process looks like (with some smaller numbers):
figure;
tcl=tiledlayout(3,1);
nexttile
t=linspace(0,2*pi,24);
y=cos(t);
plot(y,'-o','MarkerFaceColor','w','LineWidth',2)
title('Original','24 values')
nexttile
xi=linspace(1, length(y), 30);
plot(y,'-o','MarkerFaceColor','w','LineWidth',2)
xline(xi)
title('Ask interp1 for y','at 30 evenly spaced x values')
nexttile
yi=interp1(y,xi);
plot(xi,yi,'-o','MarkerFaceColor','w','LineWidth',2)
title('30 interpolated values between the beginning and end','Ready for averaging!')
axis(tcl.Children,'tight')
Hope that helps, sorry for the unnecessary detail. There's actually way more (because this is just the linear version of interp1...there are other options for how to interpolate, and there's an interp2, and an interpn, and a TriScatteredInterp...but it's so useful I always like to share. And also, pro tip, if in doubt with MATLAB, make some plots! Good luck @Ryan Baker!
Ryan Baker
Ryan Baker on 30 Sep 2021
Are you kidding?? That is the most succinct explanation for a MATLAB question I have ever had, online or in person! Thank you so much for taking the time to go through what each step means, and then to show an example! The combination of written AND visual explanation really make things clear :) I took some time to walk myself through the steps (I want to understand it before applying it) and feel far more comfortable than before with process and how to approach it mentally!
I am unfamiliar with the 'tiledlayout' feature (I think my 2018b version is too old, sadly. I'll use sublots!). Seeing how quickly and fluidly you were able to take a problem, find a solution, and explain it all in a way that a beginner can understand is really inspiring! It is super motivating @Dave B :) Thank you for all the help...I'll move forward with new confidence! You're wonderful!

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!