Synchronize sensor data from multiple sensors
12 views (last 30 days)
Show older comments
Dylan den Hartog
on 19 May 2021
Commented: Star Strider
on 4 Jun 2021
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
Accepted Answer
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{:}}
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
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{:}}
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.
.
More Answers (1)
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
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.
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!