MATLAB: How to find the time between when the animal_speed exceeded the threshold till when it dipped below the threshold (all instances when this happened)

findpeaksthresholdtime

The data set I am working with has 17 columns and over 10000 rows. Two particular columns are 'time' and 'animal_speed'. I am also using a Threshold of 0.10m/s.
To find all instances when the 'animal_speed' exceeds the Threshold, I use this:
>> [row, col, v] = find(data_set.animal_speed > 0.10);
which gives me all 'animal_speed' row numbers where the values are greater than 0.10.
Plotting the graph with 'time' and 'animal speed' and including the Threshold line (y=0.10) is not a problem.
However, how do I find the time between when the animal speed exceeded the threshold till when the speed dipped below the threshold (all instances when this happened)?
Thanks!

Best Answer

  • There are likely other solutions, but this seems to work:
    animal_speed = rand(1, 1E+4)/2; % Create Data

    time = [0:1E+4]/350; % Create Data
    Hspeed_log = animal_speed > 0.10; % Logical Index
    Lspeed_log = ~Hspeed_log;
    HspeedIdx = find(Hspeed_log);
    LspeedIdx = find(Lspeed_log);
    for k1 = 1:length(HspeedIdx)
    IdxMtx(k1,:) = [HspeedIdx(k1) LspeedIdx(find(LspeedIdx > HspeedIdx(k1), 1, 'first'))];
    end
    [~,ia] = unique(IdxMtx(:,2)); % Indices Of Unique ‘LspeedIdx’ Values In ‘IdxMtx’
    StartStopIdx = IdxMtx(ia,:); % Start-Stop Index Intervals
    StartStopTimes = time(StartStopIdx); % Start-Stop Times For Speed > 0.10