Segmenting Data around an event

18 次查看(过去 30 天)
S
S 2024-11-12,15:23
回答: Voss 2024-11-12,16:49
I am trying to segment data to epoch it around an active event, but keep having issues. Currently I am finding every index where states.Active is true, but this ends up giving me segments that are one sample off from each other. I want to segment the data by finding the indices where state.Active changes from 0 to 1 so the rising edge. Below is what I have. Thank you for your time!
%segment data
fs = 240;
sampleDuration = 0.8;
numSamplesToCollect = round(sampleDuration * fs);
% Identify flashing
flashingIndices = find(state.Active == 1);
% Store collected segments
collectedSegments = cell(length(flashingIndices), 1);
% Segment data per channel per fs around flash
for i = 1:length(flashingIndices)
% Get the index of the flash
flashIndex = flashingIndices(i);
% Define start and end indices for segment
startIdx = flashIndex - round(numSamplesToCollect / 2);
endIdx = flashIndex + round(numSamplesToCollect / 2) - 1; % -1 to include the endpoint
% Ensure indices are within bounds
if startIdx < 1
startIdx = 1; % Avoid negative index
end
if endIdx > length(signal)
endIdx = length(signal); % Avoid exceeding signal length
end
% Collect the segment
collectedSegments{i} = signal(startIdx:endIdx);
end
% Check the result
disp(collectedSegments);
  1 个评论
Image Analyst
Image Analyst 2024-11-12,15:32
I'd have to debug it, since I can't do it just by looking at your code. But you didn't attach any data.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

请先登录,再进行评论。

回答(1 个)

Voss
Voss 2024-11-12,16:49
Active = [1 0 0 1 1 1 0 0 1]
Active = 1×9
1 0 0 1 1 1 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
flashIndex = strfind(Active,[0 1])+1 % index of the 1 at the rising edge
flashIndex = 1×2
4 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
If you want to consider a 1 that starts the signal to be a "rising edge":
flashIndex = strfind([0 Active],[0 1]) % index of the 1 at the rising edge
flashIndex = 1×3
1 4 9
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

标签

Community Treasure Hunt

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

Start Hunting!

Translated by