# How do I count the number of times zero is being crossed by a signal?

5 views (last 30 days)
Rajesh Siraskar on 8 Jan 2020
Commented: Rajesh Siraskar on 10 Jan 2020
Hi,
I am trying to build a control system and I want to count the oscillations in a time-span of say 100 seconds.
Can I count the number of times a signal crosses zero (probably using MATLAB's zer-crossing detection algo.) to count the oscillations?
I want it to happen within Simulink - is there any other better way? I just want a count say 10, that tells me there were 10 oscillations.

Fangjun Jiang on 8 Jan 2020
If you have the DSP toolbox, there is a Zero-Crossing counter block. If not, you can compare the signal with zero, which creates a square wave signal. Use the rising and falling edge to trig an triggered subsystem, inside which adding a Counter block.

Rajesh Siraskar on 9 Jan 2020
Fangjun - just a quick observation.
While this works stand-alone - do you know how to make it work in mixed signal cases?
I am actually building a Reinforcement Learning based control system and I am trying to penalize for oscillations and hence the need to count oscillations
When I put this solution into the whole scheme of things, I get an error. But thanks for the solution.
On the other hand if set the simulation as continous time then I get this error
Caused by:
Error due to multiple causes.
Data type mismatch. The internal rule determines the output of
'Stiction_TF_Valve_RL_VR_V11/RL Sub-System/Compute Rewards/Gain' to be a signal of data type
'ufix32_En1000'. However, it is driving a signal of data type 'double'.
An error occurred while propagating data type 'uint16' from 'Stiction_TF_Valve_RL_VR_V11/RL
Fangjun Jiang on 9 Jan 2020
I believe continuous solver should also work. You just need to add DataTypeConversion blocks to deal with those data type mismatches.
Rajesh Siraskar on 10 Jan 2020
Make sense - will try. Thanks a lot for your guidance.