MATLAB Answers

Averaging data with slightly different time values

12 views (last 30 days)
Alex Boote
Alex Boote on 16 Aug 2018
Answered: Peter Perkins on 24 Aug 2018
Last week I got data from an experiment with three repeats, and unlike all of the examples in previous experience the times that the samples were collected are slightly different, as an example:
t_1 = [0 1 2 3 4 5], x_1 = [0 10 20 30 40 50]
t_2 = [0.1 1.1 2.1 3.2 4.2 5.4], x_2 = [0 9 19 29 38 40 51]
t_3 = [0 0.9 2 3.1 3.9 4.8], x_3 = [0 11 21 31 39 48]
For the sake of analyzing the data I would like to reduce this data to one set.
I thought of a couple of options: taking time intervals and averaging the data within that interval, or maybe interpolating the data for each data set onto fixed times and then averaging those values. Either of these seem fine, with the latter being preferable. However, the real data set is extensive and I was wondering if there was an easy way of summarizing this data? Currently the only way i could solve it would be with a tonne of if and for statements which would be very tedious, probably wouldn't save much time and may not even work.
Any help would be greatly appriciated!

  0 Comments

Sign in to comment.

Accepted Answer

Stephan
Stephan on 16 Aug 2018
Edited: Stephan on 16 Aug 2018
Hi,
a further approach would be not to manipulate data, but use all the Information the data contains and make a fit function with least suqared minimized coefficients:
t_1 = [0 1 2 3 4 5];
x_1 = [0 10 20 30 40 50];
t_2 = [0.1 1.1 2.1 3.2 4.2 5.4];
x_2 = [0 9 19 29 38 51];
t_3 = [0 0.9 2 3.1 3.9 4.8];
x_3 = [0 11 21 31 39 48];
% combine all data in one set and sort this set by time
A(:,1) = horzcat(t_1,t_2,t_3);
A(:,2) = horzcat(x_1,x_2,x_3);
sortrows(A,1)
% plot the data
plot(A(:,1),A(:,2),'*r')
hold on
% Solve least squares problem
x0 = [1 1];
x_data = A(:,1);
sol = lsqcurvefit(@myfun,x0,A(:,1),A(:,2));
% show results
disp(['y = ', num2str(sol(1)), ' * ', ' x + ' , num2str(sol(2))])
x_new = 0:6;
y_new = sol(1) .* x_new + sol(2);
plot(x_new,y_new)
hold off
% target function to find coefficients
function F = myfun(x,xdata)
F = x(1)*xdata + x(2);
end
This gives you a least squares fit of all the data without manipulating them:
Best regards
Stephan

  0 Comments

Sign in to comment.

More Answers (2)

Eduard Reitmann
Eduard Reitmann on 16 Aug 2018
I noticed that your t_2 and x_2 variables are not the same length. Once you fixed that, this should work:
t = linspace(0,max([t_1 t_2 t_3]),11);
x(:,1) = interp1(t_1,x_1,t);
x(:,2) = interp1(t_2,x_2,t);
x(:,3) = interp1(t_3,x_3,t);
xm = mean(x,2);
The first line can be modified to change the number of averaging points and interval. You can plot the average like this:
plot(t_1,x_1,'o',t_2,x_2,'o',t_3,x_3,'o',t,xm,'r')

  0 Comments

Sign in to comment.


Peter Perkins
Peter Perkins on 24 Aug 2018
Stephan's is an interesting and powerful approach. Alex, what you described in your question is a good deal simpler. Tak a look at timetables, and in particular, their synchronize method. It's a one-liner.
>> t_1 = [0 1 2 3 4 5]';
>> x_1 = [0 10 20 30 40 50]';
>> tt1 = timetable(seconds(t_1),x_1);
>> t_2 = [0.1 1.1 2.1 3.2 4.2 5.4]';
>> x_2 = [0 9 19 29 38 51]'; % I deleted 40
>> tt2 = timetable(seconds(t_2),x_2);
>> t_3 = [0 0.9 2 3.1 3.9 4.8]';
>> x_3 = [0 11 21 31 39 48]';
>> tt3 = timetable(seconds(t_3),x_3);
>> tt = synchronize(tt1,tt2,tt3,'first','spline')
tt =
6×3 timetable
Time x_1 x_2 x_3
_____ ___ ________ ______
0 sec 0 -0.74839 0
1 sec 10 8.0136 12.007
2 sec 20 18.027 21
3 sec 30 27.241 30.041
4 sec 40 36.129 40.018
5 sec 50 46.241 49.894

  0 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!