Align two data sets

20 views (last 30 days)
EK
EK on 5 Aug 2023
Commented: EK on 6 Aug 2023
Hi,
I am trying to align two datasets log and data matrices (examples attached bellow data sezs of trials). The files have different sizes. Logfile is in ms and the data file is in frames. I need to align them together and downsample the log file to the size of the data file.
The columns in the logfile log the stimuli representation in time (an example, attached below ). The rows are time and the columns are events. The 3d column logs the ID and the duration of the trials. Trial 1=1, Trial2=2 etc. The trials in the log file start at the same time as in the datafile but they are a bit longer than in the datafile. I need to cut each trial at the end to the length of the trial's in the data file, downsample them to the size of the data file and concatenate them back. The trial length in the data file = size(datafile,1)./number of trials. At the moment, I am doing it using the simple code attached below which is limited to 10 trials. I would like to put it in the loop to run it automatically on the files with a large number of trials independent of the number of trials. Could anyone help with this?
  1 Comment
MarKf
MarKf on 5 Aug 2023
That is not well-written nor working, code and explanation. The unnecessairly long logfile.xlsx seems to have every millisecond logged, associated with the trial ID (or lack thereof for the first 7 ms it seems?), the trial time would be enough (like the variable Trial_IDS) but I guess you need something like that and you need to downsample it.
On failing line 9: tp2=size(datafile,1)./10;% length of the data for one trial I assume that's when your pseudocode starts, and that you mean to extract something from the variable Data, but not the size of the variable like you do in the code, but the trial length. That part is not clear. So not sure how much of the trial you need to cut, and if it's a different time duration for different tirals based on the info in datafile.xlsx.
As for downsampling then just take x(1:50:end,3:4) to get only 1sec/20 = 50ms matrix with tiral ID and time vector, but it won't capture the trial beginning at 7ms.

Sign in to comment.

Accepted Answer

Voss
Voss on 5 Aug 2023
Edited: Voss on 5 Aug 2023
x = readmatrix('logfile.xlsx');
Data = readmatrix('datafile.xlsx');
frame_rate = 20;
n = 1000/frame_rate;
x(x(:,3) == 0,:) = [];
[g,g_id] = findgroups(x(:,3));
trial_length = size(Data,1)/numel(g_id)*n;
Trials = splitapply(@(t)cut_and_downsample(t,trial_length,n),x,g);
Logfile_concatinated = vertcat(Trials{:});
function out = cut_and_downsample(in,len,n)
% out = {downsample(in(1:len,:),n)};
out = {in(1:n:len,:)};
end
  10 Comments
Voss
Voss on 6 Aug 2023
Doesn't frame_rate have to be the actual frame rate that the data was captured at, if you want the two to line up?
EK
EK on 6 Aug 2023
yes, the frame rate should be the actual one otherwise the alignment won't be precise. The example I sent is not the best one. If the frame rate is 20.04 and round it to 20 I do not see much difference on small data sets. Maybe there is a small shift in a few milliseconds that is not that critical. But if I have the frame rate let's say 17.5 or so it becomes a problem

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!