I want to calculate if a real-time signal pass some thresholds in the first step. In the first step, I want to detect if the real signal pass under those thresholds (in order to detect a peak in the signal). My Matlab code:
k=1;
t = 1;
l=1;
for i =1:length(sm) //sm my signal.
if (sm(i) > 0.25)
first(k) = i;
k = k+1;
if (sm(i) > 0.5)
second(t) = i;
t =t +1;
if (sm(i) > 0.75)
third(l) = i;
l = l+1;
end
end
end
end
Example:
![enter image description here][1]
I want to calculate the times that the signal pass over and under the three thresholds 0.25
, 0.5
, 0.75
and to return those windows. Basically the three main peaks that I have in my example.
Basically what am I trying to do is to use fastsmooth function and findpeaks.
signalSmoothed = fastsmooth(sm,50); plot(signalSmoothed)
[max_pk1 max_pk2] = findpeaks(signalSmoothed);
find(max_pk1 >0.5)
inversex = 1.01*max(signalSmoothed) - signalSmoothed;
[min_pk1 min_pk2] = findpeaks(inversex);
find(min_pk1 >0.5)
Which are the heuristics in order to take only the desired peaks? Moreover the depticted image is an offline example. Generally I want to perform the technique online.
EDIT: I wrongfully defined as peak my desired curve result which is the whole wave not just the max value.
Here is a solution to get the points where the signal
sm
passes the thresholds 0.25, 0.50 and 0.75. The points can be converted into windows inside the data-range and get stored inW
. Finally we can plot them easily in the same figure. Note that we need to do some checks in the local functiongetwindows
to handle special cases, for example when the window starts outside the data-range. The detection of windows inside another window is done in thegetwindowsspecial
-function.Here is the code:
This is the result:
To see that the handling works properly, we can plot the result when initialized with
rng(3)
:Note that the window of 0.25 and 0.50 would start out of bounds left and therefore is not present in the plotted windows.