How can I find the first zero crossing of a signal in Simulink?

3 views (last 30 days)
I have a Simulink model that acquires data in real time using release R2022b.
I would like to have a "control signal" that only activates when a signal reaches zero for the first time. It must not activate when it reaches zero at later times.
How can I do that?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Feb 2023
One way to implement that detection system consists of three stages:
  1. Detecting when the signal changes signs.
  2. A counter that increases with every zero-value detection.
  3. A comparison block that compares the counter to the value "1".
The screenshots below show an example of this implementation:
Stage 1 is a combination of a "Memory" block, which outputs the value of its input at the previous time step, a multiplication block, and a comparison to zero. The output of the multiplication block will only be zero or negative when the original signal (the cosine) changes signs, and the output of the comparison block ("<=") will be a value of "1" in those cases.
Stage 2 
is a counter implemented through a "Triggered subsystem". This subsystem is activated by the comparison block. The count is stored inside a "Memory" block, and every time the subsystem is activated, the input to the memory is increased by one. The model is shown below.
Stage 3 is simply a comparison to the value "1". The counter from stage 2 will keep increasing as the signal crosses zero many times, and this third stage filters it to only detect the first zero crossing. Note that the signal drops back to zero after the second zero crossing. If you would like the output signal to persist at the value "1" for the rest of the simulation, consider adding another "triggered subsystem" activated by the output of the comparison block ("=="). 
The scope visualization is shown below:
Les Beckham
Les Beckham on 3 Feb 2023
How odd. I thought I had used that approach in the past to "latch" a signal.

Sign in to comment.

More Answers (0)


Find more on General Applications 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!

Translated by