Identify values above a threshold that are sustained for a time period
Show older comments
I have muscle activity values that I need to determine when the muscles are on (responding to a stimulus) and when they are off (done responding) so that I can perform area under the curve calculations to the activity during the response. Those values are for example: LOOCRMS = [7.52; 7.59; 8.45; 11.40; 12.97;13.33;15.16;17.37;18.14;18.17;18.10;17.91;17.87;17.49;15.91;14.56] with corresponding time values of LOOCRMSTIME = [0.0155;0.01581;0.01612;0.01643;0.01674;0.01705;0.01736;0.01767;0.01798;0.01829;0.0186;0.01891;0.01922;0.01953;0.01984;0.02015] in seconds.
I have a standard deviation calculation value of let's say TSD_LOOC1 = 15. I need to find the time when the LOOCRMS muscle activity value is above my threshold of TSD_LOOC1 and is also sustained for a time of 0.03 seconds to be sure that it's reacting to the stimulus. I also need to know when it turns back "off" meaning that it drops below and stays below for 0.039 seconds.
I have tried using the find function but have a hard time with the additional complexity of whether the muscle is on or off for the time duration. I realize that I did not include enough data to span the required time period, but in the interest of keeping my question brief I chose to only provide a small subset of the data.
6 Comments
jessupj
on 29 Apr 2020
Does your analysis need estimated times (that is to say, intervals based on some kind of interpolation of the data), or the intervals that correspond precisely to the measurement times?
Jessa Ward
on 29 Apr 2020
jessupj
on 29 Apr 2020
(i'm just fidding while some code complies...this is inelegant but maybe it points you in a good direction.)
you might try something like
CHANGPTS = find( abs( diff( LOOCRMS> TSD_LOOC1) )==1 )+1
which will identify the indices of the data where the value changes from one side of the threshold to anohter.
The '+1' here is because diff returns an N-1 length vector.
Then you can use your known sample frequency (0.00031) to determine how long the signal spends above/below the threshold. You might put an initialindex in there too to make accounting easier (and a terminal one later for the full series), and then check the time differnence between the start/end times:
CHANGEPTS=[1; CHANGEPTS]
diff(CHANGEPTS)*0.00031 %or you could sum over these if your sample frequency is irregular
ans =
0.00186 % seconds between first index and first value over threshold
0.00279 % seconds between first value over threshold and next value under it.
% ??? % seconds signal spends below threshold until next value at/above threshold
% & etc for a longer tiemseries
Image Analyst
on 29 Apr 2020
If she has the Image Processing Toolbox, there is a function in there that is specifically meant for this, as I show in my Answer below. Actually 2 functions that you can use bwareaopen() and bwareafilt(). We're still waiting for her to upload data that lasts longer than the 0.03 seconds though.
i haven't used any of the image processing toolboxes and don't have it installed. my timeseries work usually centers on irregularly sampled data, which (years ago circa 2003, maybe not the case now) weren't well handled by matlab signal (and presumably image) processing tools without re-sampling. do you know if they do now, without any internal downsampling?
Image Analyst
on 29 Apr 2020
If you don't have the Image Processing Toolbox, I would download the excellent RunLength utility from the File Exchange. That function gets the length of all runs of 1's in a signal. Then you can do some erasing of those elements once you know where they are.
Accepted Answer
More Answers (0)
Categories
Find more on Multirate Signal Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!