Resize and align the data matrix based on the stimuli ID

조회 수: 1 (최근 30일)
EK
EK 2023년 11월 26일
댓글: Voss 2023년 11월 26일
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?

채택된 답변

Voss
Voss 2023년 11월 26일
편집: Voss 2023년 11월 26일
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
EK
EK 2023년 11월 26일
Thank you so much! You are amazing !!!
Voss
Voss 2023년 11월 26일
You're welcome!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by