Resize and align the data matrix based on the stimuli ID
1 view (last 30 days)
Show older comments
Hi,
I have matrices with 2 column like in the file attached below (log). The 1st column logs an analog signal of stimuli in time. Each stimulus is a 2 waves (values <0) that are separated with short time interval. As in the ifigure attached. The second column is the data column. I would like to cut traces of the data (column 2) 500 rows before each stimulus till 4000 rows after the stimulus, rotate and save them in the separate matrix there rows are repetition of the stimuli /trials and columns log for data in time. An example attach below. Can anyone help with it?
0 Comments
Accepted Answer
Voss
on 26 Nov 2023
Edited: Voss
on 26 Nov 2023
M = readmatrix('log.csv');
% --- first, identify where the stimuli start and end ---
% find indices where 1st column of M has magnitude greater than 0.01:
big_idx = find(abs(M(:,1)) > 1e-2);
% logical indices where there are more than 1000 samples between values
% whose magnitude is greater than 0.01. these are the gaps between stimuli:
is_big_gap = diff(big_idx) > 1000;
% start and end index of each stimulus:
start_idx = [big_idx(1); big_idx([false; is_big_gap])]-1;
end_idx = [big_idx([is_big_gap; false]); big_idx(end)]+1;
% plotting:
figure();
ax = gca();
hold on
plot(M(:,1))
xline(start_idx,'g')
xline(end_idx,'r')
title('All Stimuli')
% plot each stimulus up-close:
ch = get(ax,'Children');
n = numel(start_idx);
f = figure('Units','pixels','Position',[10 10 750 1200]);
mtl = tiledlayout(f,(n+mod(n,2))/2,2,'TileSpacing','loose');
for ii = 1:n
tl = tiledlayout(mtl,2,2,'TileSpacing','tight');
tl.Layout.Tile = ii;
newax = nexttile(tl,[1 2]);
copyobj(ch,newax);
xlim(newax,[start_idx(ii)-1000, end_idx(ii)+1000]);
newax.XAxis.Exponent = 0;
newax.YAxis.Exponent = 0;
title(sprintf('Stimulus %d',ii))
newax = nexttile(tl);
copyobj(ch,newax);
xlim(newax,[start_idx(ii)-10, start_idx(ii)+10]);
newax.XAxis.Exponent = 0;
newax.YAxis.Exponent = 0;
title('Start')
newax = nexttile(tl);
copyobj(ch,newax);
xlim(newax,[end_idx(ii)-10, end_idx(ii)+10]);
newax.XAxis.Exponent = 0;
newax.YAxis.Exponent = 0;
title('End')
end
% --- second, put the data together in the desired form ---
% take a region starting at 500 samples before each stimulus start
% and ending at 4000 samples after each stimulus start, and put
% the data from those regions together in a matrix as in example.csv:
start_offset = 500;
end_offset = 4000;
N_stimuli = numel(start_idx);
result = zeros(N_stimuli,start_offset+end_offset+2);
result(:,1) = 1:N_stimuli;
for ii = 1:N_stimuli
result(ii,2:end) = M(start_idx(ii)-start_offset:start_idx(ii)+end_offset,1);
end
result
% plot from result matrix, to verify:
figure()
for ii = 1:size(result,1)
subplot(5,2,ii)
plot(result(ii,2:end))
title(sprintf('Stimulus %d',ii))
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!