MATLAB Answers

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

5 views (last 30 days)
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.


Sign in to comment.

Accepted Answer

Fangjun Jiang
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.


Show 5 older comments
Rajesh Siraskar
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 using rl.env.SimulinkEnvWithAgent/simWrapper (line 325)
Error due to multiple causes.
Error using Simulink.SimulationInput/sim
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'.
Error using Simulink.SimulationInput/sim
An error occurred while propagating data type 'uint16' from 'Stiction_TF_Valve_RL_VR_V11/RL
Sub-System/Compute Rewards/Add', output port 1.
Fangjun Jiang
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.

Sign in to comment.

More Answers (0)

Sign in to answer this question.




Translated by