Resize and align the data matrix based on the stimuli ID

4 ビュー (過去 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 Center および File ExchangeWeather and Atmospheric Science についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by