MATLAB Answers

Synchronize sensor data from multiple sensors

37 views (last 30 days)
I have sensor data from four inertial sensors. When collecting data the the .csv files for each sensor have a slightly different amount of rows as some measurements are missed. Now I want to synchronize the four dataframes into one big dataframe that contains the data from all four sensors and is synchronized.
The data looks something like this
Sensor 1 =
SampleTimeFine FreeAcc_X_S1
______________ __________
1 2.0
23 4.0
36 6.0
47 8.0
53 10.0
Sensor 2 =
SampleTimeFine FreeAcc_X_S2
______________ __________
1 3.0
36 5.0
47 6.0
53 2.0
Sensor 3 =
SampleTimeFine FreeAcc_X_S3
______________ __________
1 1.0
23 5.0
47 4.0
53 9.0
Sensor 4 =
SampleTimeFine FreeAcc_X_S4
______________ __________
1 6.0
23 5.0
36 6.0
47 7.0
53 8.0
I want to end up like this:
Sensor 1 =
SampleTimeFine FreeAcc_X_S1
______________ __________
1 2.0
23 4.0
36 6.0
47 8.0
53 10.0
Sensor 2 =
SampleTimeFine FreeAcc_X_S2
______________ __________
1 3.0
23 4.0
36 5.0
47 6.0
53 2.0
Sensor 3 =
SampleTimeFine FreeAcc_X_S3
______________ __________
1 1.0
23 5.0
36 4.5
47 4.0
53 9.0
Sensor 4 =
SampleTimeFine FreeAcc_X_S4
______________ __________
1 6.0
23 5.0
36 6.0
47 7.0
53 8.0
As you can see a new row is added in both table 2 and table 3. The FreeAcc_X is interpolated for these new rows. In the end I want to add these tables into one bigger table. It should look like this:
Sensor_all =
SampleTimeFine FreeAcc_X_S1 FreeAcc_X_S2 FreeAcc_X_S3 FreeAcc_X_S4
______________ __________ __________ __________ __________
1 2.0 3.0 1.0 6.0
23 4.0 4.0 5.0 5.0
36 6.0 5.0 4.5 6.0
47 8.0 6.0 4.0 7.0
53 10.0 2.0 9.0 8.0
What is an easy way to do this?
  1 Comment
Adam Danz
Adam Danz on 20 May 2021
The solution is similar to the solution I shared in your previous question but we didn't hear back from you on that thread. Instead of using innerjoin you'll use a similar function.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 22 May 2021
If the first columns are not actulal times, as would otherwise be used with datetime or duration, just interpolate.
Considering three of them —
S1 = [ 1 2.0
23 4.0
36 6.0
47 8.0
53 10.0];
S2 = [ 1 3.0
36 5.0
47 6.0
53 2.0];
S3 = [ 1 1.0
23 5.0
47 4.0
53 9.0];
Sc = {S1; S2; S3}; % Vertically Concatenate Matrices Into Cell Array
Scm = cell2mat(Sc); % Convert To (Nx2) Matrix To Get Unique Times
S_Time = unique(Scm(:,1)); % Unique Times
for k = 1:numel(Sc)
Si(:,k) = interp1(Sc{k}(:,1),Sc{k}(:,2),S_Time); % Interpolate
end
Snames = compose('Sensor_%02d',1:numel(Sc)); % Create Variable Names
Combined = [table(S_Time) array2table(Si)]; % Create Result Table
Combined.Properties.VariableNames = {'SampleTimeFine',Snames{:}}
Combined = 5×4 table
SampleTimeFine Sensor_01 Sensor_02 Sensor_03 ______________ _________ _________ _________ 1 2 3 1 23 4 4.2571 5 36 6 5 4.4583 47 8 6 4 53 10 2 9
This could easily be expanded to the rest of the matrices by including them in ‘Sc’., and differeing Use whatever interpolation method works best. See the interp1 documentation for details.
.
  4 Comments
Star Strider
Star Strider on 4 Jun 2021
In the original problem, the sample times were montonically increasing. This time, they are not.
After doing a bit of experimenting, change the order of the matrices in ‘Sc’ assignment to:
Sc = {S1; S3; S2};
and the order is as you would like it —
S1 = [ 1 2.0
23 4.0
36 6.0
45 8.2
47 8.0
2 10.0];
S2 = [ 1 3.0
36 5.0
47 6.0
2 2.0
6 3.0];
S3 = [ 1 1.0
23 5.0
47 4.0
2 9.0
4 10.0];
Sc = {S1; S3; S2}; % Vertically Concatenate Matrices Into Cell Array
Scm = cell2mat(Sc); % Convert To (Nx2) Matrix To Get Unique Times
S_Time = unique(Scm(:,1),'stable'); % Unique Times
for k = 1:numel(Sc)
Si(:,k) = interp1(Sc{k}(:,1),Sc{k}(:,2),S_Time); % Interpolate
end
Snames = compose('Sensor_%02d',1:numel(Sc)); % Create Variable Names
Combined = [table(S_Time) array2table(Si)]; % Create Result Table
Combined.Properties.VariableNames = {'SampleTimeFine',Snames{:}}
Combined = 8×4 table
SampleTimeFine Sensor_01 Sensor_02 Sensor_03 ______________ _________ _________ _________ 1 2 1 3 23 4 5 4.1333 36 6 4.4583 5 45 8.2 4.0833 5.8182 47 8 4 6 2 10 9 2 4 9.4286 10 2.5 6 8.8571 9.4737 3
That most likely has to do with ‘2’ being common to all of them, while ‘4’ and ‘6’ are not, so specifying the 'stable' sort order means that the order of the matrices themselves in ‘Sc’ is important.
.

Sign in to comment.

More Answers (1)

Mohammad Sami
Mohammad Sami on 20 May 2021
The easiest way to do this is to import your data as timetables and then use the built in synchronize function. Details and examples are in the documentation.
https://www.mathworks.com/help/matlab/ref/timetable.synchronize.html
  2 Comments
Mohammad Sami
Mohammad Sami on 22 May 2021
If a sample time fine value across different sensor is considered equal, you can use this as a time variable for this purpose.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!