I am looking to some C algorithm(for MCU), where I have these signals : enter image description here

First graph you can see that there is an event in the middle and it goes back to normal, second and third there is some event at a certain point. Any of them can happen per session(= when I turn the device there will be 1 behavior)

I have to find real time when the event begin and ends, and it has to be FAST.

  1. All maximums in all 3 graphs are more or less the same (with 2% diff)
  2. I don't know the values before hand but I can calibrate the "no event" state.
  3. I need 1 algorithm to all of them.

I can do all sorts of basic stuff with averages and LPF, but I guess there is more clever and generic way to do so (?)

Something that can cover all cases and do what my eyes do - I can just tell where is the event.


You need to be sure about:

  1. Noise power is bounded
  2. The power of the signal step is higher than 4 times that noise power.

If the noise power is bounded and the change in the signal level is higher than 4 times that noise power (from the pictures it seems to be so, I mean, the step is more than twice the max value of noise), why not take the derivative of the signal? something like:

step = abs(x[n] - x[n-1]);
if (step > threshold)
  // Event has happened: 
  // 1. Take timer value
  event_time = get_time();
  // 2. Update the state machine
  state = (state == START_STATE) ? STOP_STATE : START_STATE;

Well, the get_timer() is specific to your MCU and there are better ways to implement a Finite State Machine, but that is the idea. It is fast and fulfills the requirements, I would not go further.

  • 1
    $\begingroup$ This may fail, becaus the signal is zigzag you might take the top of it right befor the jump and then the bottom, which may be close. $\endgroup$ – Curnelious Jun 4 '17 at 6:55
  • $\begingroup$ Could you assign some numbers to the top of it right before the jump and the bottom to give me a counterexample? $\endgroup$ – oxuf Jun 4 '17 at 11:23
  • $\begingroup$ @Curnelious : Oh, I see now what you mean, there was a 2 missed over there. Please, check my edit. $\endgroup$ – oxuf Jun 4 '17 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.