Resize and align the data matrix based on the stimuli ID

1 visualización (últimos 30 días)
EK
EK el 26 de Nov. de 2023
Comentada: Voss el 26 de Nov. de 2023
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?

Respuesta aceptada

Voss
Voss el 26 de Nov. de 2023
Editada: Voss el 26 de Nov. de 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
result = 10×4502
1.0000 -0.0002 -0.0002 -0.0005 0.0002 -0.0002 0.0005 -0.0005 -0.0002 0.0002 0.0008 0.0002 0.0002 0.0005 0.0005 -0.0002 0.0002 0.0002 0.0002 0.0005 -0.0005 0.0008 -0.0005 -0.0002 0.0005 0.0008 -0.0002 -0.0002 0.0002 -0.0002 2.0000 0.0002 -0.0002 0.0002 0.0002 -0.0002 0.0002 -0.0005 -0.0005 -0.0002 0.0005 -0.0002 0.0002 0.0002 0.0005 -0.0002 0.0002 0.0002 0.0002 -0.0002 0.0002 -0.0002 -0.0002 0.0008 0.0002 0.0002 -0.0005 0.0002 -0.0002 0.0002 3.0000 -0.0002 -0.0002 -0.0002 -0.0005 0.0005 -0.0005 0.0005 -0.0002 0.0002 0.0002 0.0005 0.0002 0.0002 0.0002 0.0002 0.0005 -0.0002 0.0002 -0.0002 -0.0005 0.0002 0.0002 -0.0005 0.0005 0.0005 -0.0005 0.0002 0.0002 -0.0002 4.0000 -0.0002 -0.0005 0.0005 -0.0002 -0.0002 0.0002 0.0002 0.0002 -0.0008 0.0002 0.0005 -0.0005 0.0005 0.0005 -0.0008 -0.0008 -0.0002 -0.0005 0.0002 -0.0002 0.0002 0.0008 -0.0002 -0.0002 0.0002 0.0005 0.0002 -0.0002 0.0002 5.0000 0.0005 0.0002 -0.0002 0.0008 -0.0002 0.0005 -0.0002 -0.0005 0.0002 0.0002 0.0002 0.0002 -0.0002 0.0005 0.0002 -0.0002 -0.0002 0.0002 0.0005 -0.0005 -0.0002 -0.0002 0.0005 0.0005 -0.0002 -0.0005 0.0002 -0.0002 -0.0002 6.0000 0.0002 -0.0002 0.0002 0.0002 -0.0002 0.0005 -0.0002 0.0002 0.0005 0.0005 -0.0002 0.0002 -0.0002 0.0008 0.0005 0.0002 0.0002 -0.0002 -0.0005 0.0002 -0.0002 0.0002 0.0008 0.0002 -0.0005 0.0005 0.0002 0.0002 0.0002 7.0000 0.0002 0.0002 -0.0002 0.0002 0.0005 0.0002 0.0002 0.0002 0.0002 -0.0002 -0.0002 0.0005 -0.0002 0.0002 0.0005 0.0002 -0.0005 0.0005 0.0002 0.0005 -0.0002 -0.0002 -0.0005 -0.0002 0.0002 0.0005 0.0002 0.0002 0.0002 8.0000 0.0005 -0.0005 0.0002 0.0005 0.0002 0.0002 -0.0002 -0.0002 -0.0002 0.0002 0.0005 0.0005 0.0005 -0.0005 0.0005 -0.0002 -0.0002 -0.0002 0.0008 0.0005 0.0002 -0.0005 -0.0002 0.0008 -0.0002 -0.0002 -0.0005 -0.0002 0.0005 9.0000 -0.0002 -0.0002 -0.0005 0.0005 0.0002 0.0002 -0.0002 0.0002 0.0002 0.0005 -0.0005 -0.0005 0.0002 0.0005 0.0005 0.0005 -0.0002 0.0002 0.0005 0.0005 -0.0002 0.0008 -0.0002 0.0002 0.0002 -0.0005 0.0002 -0.0008 -0.0002 10.0000 0.0005 -0.0002 0.0005 0.0002 0.0002 0.0005 0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 -0.0002 0.0002 0.0002 0.0005 -0.0005 -0.0002 0.0002 0.0005 -0.0002 -0.0005 -0.0002 0.0002 -0.0002 0.0002 0.0002 0.0002
% 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
  2 comentarios
EK
EK el 26 de Nov. de 2023
Thank you so much! You are amazing !!!
Voss
Voss el 26 de Nov. de 2023
You're welcome!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by