Synchronize sensor data from multiple sensors

조회 수: 22 (최근 30일)
Dylan den Hartog
Dylan den Hartog 2021년 5월 19일
댓글: Star Strider 2021년 6월 4일
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
Adam Danz
Adam Danz 2021년 5월 20일
편집: Adam Danz 2021년 5월 20일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2021년 5월 22일
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
Dylan den Hartog
Dylan den Hartog 2021년 6월 4일
편집: Dylan den Hartog 2021년 6월 4일
My problem changed slightly. Lets say these are the matrices now:
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]
In the end I want to get this:
Combined = 5×4 table
SampleTimeFine Sensor_01 Sensor_02 Sensor_03
______________ _________ _________ _________
1 2 3 1
23 4 number 5
36 6 5 number
45 8.2 number number
47 8 6 4
2 10 2 9
4 number number 10
6 number 3 number
I do not want the SampleTimeFine numbers to be ordered from low to high when using the unique function. So I used the same code but with 'stable' in S_Time = unique(Scm(:,1),'stable');
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),'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{:}}
And I got this:
Combined =
8×4 table
SampleTimeFine Sensor_01 Sensor_02 Sensor_03
______________ ________________ ________________ ________________
1 2 3 1
23 4 4.13333333333333 5
36 6 5 4.45833333333333
45 8.2 5.81818181818182 4.08333333333333
47 8 6 4
2 10 2 9
6 8.85714285714286 3 9.47368421052632
4 9.42857142857143 2.5 10
It is almost perfect except that the row with SampleTimeFine i = 6 should come after the row with SampleTimeFine i = 4. So like this
SampleTimeFine Sensor_01 Sensor_02 Sensor_03
______________ ________________ ________________ ________________
1 2 3 1
23 4 4.13333333333333 5
36 6 5 4.45833333333333
45 8.2 5.81818181818182 4.08333333333333
47 8 6 4
2 10 2 9
4 number 2.5 10
6 number 3 number
Star Strider
Star Strider 2021년 6월 4일
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.
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2021년 5월 20일
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
Dylan den Hartog
Dylan den Hartog 2021년 5월 20일
The problem is that I want to synchronize the data with the SampleTimeFine which are the sensor measurement numbers. They are not times.
Mohammad Sami
Mohammad Sami 2021년 5월 22일
If a sample time fine value across different sensor is considered equal, you can use this as a time variable for this purpose.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by