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

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.
Oscilltions.PNG

 Akzeptierte Antwort

Fangjun Jiang
Fangjun Jiang am 8 Jan. 2020

0 Stimmen

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.

8 Kommentare

Rajesh Siraskar
Rajesh Siraskar am 8 Jan. 2020
Bearbeitet: Rajesh Siraskar am 8 Jan. 2020
Hi Fangjun,
I tried but it didn't work. In the Simulink model below the counter should have counted 9 crossings, but it shows '0'.
If I use a constant array it detects correctly. What am I doing wrong when I use a waveform? I need to be able to detect from real time waveforms
ZeroCrossingQuestion-2.PNG
Yes. Click the Help button from the parameter dialog of the block.
Open the example model from the Help document, it seems that this "Zero Crossing" block is not couting the zero-crossings in the time domain as the example shows it can count the zero-crossings when the Stop time is set to 0.
Construct your own model using the second approach. I've tried it and it counted the correct zero-crossings. You just need to set the output data type as uint16.
Hi Fangjun,
Appreciate your help and guidance.
I had looked at the help page and the examples. I am relative beginner to MATLAB/Simulink and hence need help in understanding
>> Construct your own model using the second approach. I've tried it and it counted the correct zero-crossings. You just need to set the output data type as uint16
  1. How do I specifiy stop-time = 0 for a signal builder waveform?
  2. Also I couldn't find where I set the output data type as unit16.
Can you please help point where I do this?
This should do it. Set the solver to be discrete at 0.01 step.
Annotation 2020-01-08 154006.jpg
Thank you Fangjun.
It worked. Actually there was one more setting I had to set and then it worked - I had to set the Solver Type to "Fixed-Type" from the default "Variable-type"
Your solution is brilliant. Thank you once again.
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.
I believe continuous solver should also work. You just need to add DataTypeConversion blocks to deal with those data type mismatches.
Make sense - will try. Thanks a lot for your guidance.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

hossein
hossein am 2 Feb. 2022
Bearbeitet: hossein am 2 Feb. 2022

0 Stimmen

You can count the number of times the product of the signal and the shifted signal becomse negative. The below formula gives the number of crossings I guess. I am not sure if this is bug free.
"Sig" is your signal data vector here.
sum(sign((Sig.*[Sig(2:end),Sig(end)]))<0)

Produkte

Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by