How to Extract ROI on a signal with additional rows before and after threshold
Mostrar comentarios más antiguos
I have data with the following details :
a) Three columns (Time, Signal 1, Signal 2) and 950000 rows.
b) My Threshold (Th) = 0.5
c) RT (Raising Threshold) = FT (Falling Threshold) = Th
I would like to extract the ROI between RT and FT for both signals simultaneously, but I also would like to include 300 rows Before RT (BRT) and 300 rows After FT (AFT).
Until now, I could extract only the values that are greater than my Threshold for S1 (Signal 1), but it was not enough, as it does not include S2 (Signal 2) and BRT and AFT either.
If someone could help, I would greatly appreciate it.
Thank you in advance.
Respuesta aceptada
Más respuestas (2)
Revised to match the figure and new understanding of intent to isolate one transient and select ROI around it -- the second trace is immaterial in setting the ROI limits; however, one will have to somehow ensure the ROI pre- and post-trigger intervals are wide enough to capture the entire ROI for the second trace as well -- in the later figure there appears to be a transient before S2 settles down to its final SS value that isn't captured in the shown ROI after S1 has returned to its baseline. This is a different kind of search than thresholding...
But, for the Q? as asked, it's pretty simple
PreTrigNo=300;
PostTrigNo=300;
tmp=int8(A(:,2)>Th); % convert to logical, save memory with int8
ix1=find(tmp,1)-PreTrigNo; % +ive crossing (first "1"); subtract pretrigger
ix2=find(tmp,1,'last')+PostTrigNo; % -ive crossing (+1, -1) add posttrigger
S=A(ix1:ix2,:); % select all the array between
clear tmp
To be completely robust, one should check that the result of the find operations are >=1 or <=size(A,1) BEFORE trying to address the S array with them; that's where the previous failed because there were no elements that matched for the second trace if it looked like the example figure.
6 comentarios
dpb
el 4 de Ag. de 2022
There's a misplaced parenthesis in the second find to look at the end -- and you caught the missing comma in the indexing expression in the first...
% first location >Th in either column 2 or 3 less the pre-trigger count
ix1=min(find(A(:,2)>Th,1),find(A(:,3)>Th,1))-PreTrigNo;
% last location >Th in either column 2 or 3 less the pre-trigger count
ix2=max(find(A(:,2)<Th,1),find(A(:,3)<Th,1),'last')+PostTrigNo;
But, on reflection the last won't do what you're wanting -- it'll pick the very last point in the trace <Th, not the first that drops below it after the first.
Needs some more refinement to the logic. Before just trying something willy-nilly, it would help a whole lot to have an idea of what the signal looks like -- is it uniformly going to be above the threshold once it triggers and then drop again and stay down below the threshold once does?
dpb
el 4 de Ag. de 2022
Yeah, knowing that general shape does help -- shouldn't need terribly complicated logic.
See the updated Answer -- it's sill "air code" which means untested, but the idea should be clear...
I'll be unavailable rest of the day today, so you'll have to debug anything yourself...shouldn't be tough to do presuming you have sufficient memory (the only reason for the int8() for the logic variable to save memory)
I was very rushed the other day and didn't look at the figure carefully enough -- in fact, the second trace in the above doesn't cross the threshold and the ROI is based ONLY on the first signal -- I thought the intent/need was to isolate two transients at different triggering times -- remove the tests for the second channel entirely and all should be resolved.
I edited the Answer to match the problem...
2 comentarios
"I need to Extract S2 the same as S1 only between the cursor A and B. ..."
Well, yes. That's EXACTLY what the revised Answer code will do -- excepting it was based on the original figure and Q? that didn't point out that the signal repeats.
Instead of the above with the 'first' and 'last' options, you'll need to either
- apply the above iteratively with the starting point for each successive iteration after the end point for the previous, or
- revert to the previous use of sign and find all crossings from -ive to +ive and then iterate over that collection.
The basic idea works as in the above code...if you want actual debugged code, attach a .mat file with the first few cycles of your data.
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
