I recently posed a question on how to implement an algorithm which adaptively changes a threshold in real time, so that a time series reaches that threshold every N seconds. I was told that if my time series has a constant time interval (it does), I could take the absolute value, reverse sort it, and find the index in the array that gives me the average time resolution I want. I implemented it in MATLAB in this manner:
x = abs(timeseries); % Get the absolute value
x = flipud(sort(x)); % Reverse sort
N = length(x); % Size of the time series
idx = round(N/goal_time); % Find the right index
threshold = x(idx); % Set the threshold
Where goal time is the average time I want to detect a 'hit' (timeseries > threshold). My problem is two fold. I can't tell if the algorithm is not accurate enough, or if my data is too noisy (when using it, the average time I get hits doesn't really approximate my goal very well). And secondly, how would I modify this algorith to calculate a 'hit' time, where a hit is defined as the time series dipping below a threshold?