Thread Subject:
Zero cross detection

Subject: Zero cross detection

From: Dragomir Deltchev

Date: 27 Apr, 2003 18:32:19

Message: 1 of 13

Hello everyone,


I would like to find the cross point (the time when) of a signal
curve with the time axis. Does someone from you have an idea of how
this could be done? Thanks in advance


Dragomir

Subject: Zero cross detection

From: Jocke

Date: 28 Apr, 2003 02:08:28

Message: 2 of 13

Something like this...


t=0:0.0001:0.05; % time vector
x=sin(2*pi*100*t); % signal vector, f=100 Hz


zerIdx=[];
for i=1:length(x)-1
   if ((x(i)>0 && x(i+1)<0) || (x(i)<0 && x(i+1)>0))
      zerIdx(end+1)=i; % save index of zero-crossing
   end
end


t(zerIdx) % display timepoints of zero-crossings


/Jocke :-)

Subject: Zero cross detection

From: Dragomir Deltchev

Date: 28 Apr, 2003 07:20:29

Message: 3 of 13

Jocke wrote:
>
>
> Something like this...
>
> t=0:0.0001:0.05; % time vector
> x=sin(2*pi*100*t); % signal vector, f=100 Hz
>
> zerIdx=[];
> for i=1:length(x)-1
> if ((x(i)>0 && x(i+1)<0) || (x(i)<0 && x(i+1)>0))
> zerIdx(end+1)=i; % save index of zero-crossing
> end
> end
>
> t(zerIdx) % display timepoints of zero-crossings
>
> /Jocke :-)
>
Thank you for this reply Jocke,


My Situation is a bit different. What i have is a signal from
Simulink in form of a curve. Probably i should do some sort of
interpolation at the beginning ?

Subject: Zero cross detection

From: Alex

Date: 28 Apr, 2003 13:52:51

Message: 4 of 13

Don't know much about Simulink, but this may be helpful:

t=0:0.0001:0.05; % time vector
x=sin(2*pi*100*t); % signal vector, f=100 Hz

signum = sign(x) % get sign of data
signum(x==0) = 1; % set sign of exact data zeros to positiv
t(diff(signum)~=0) % get zero crossings by diff ~= 0

-Alex.


Dragomir Deltchev schrieb:
> Jocke wrote:
>
>>
>>Something like this...
>>
>>t=0:0.0001:0.05; % time vector
>>x=sin(2*pi*100*t); % signal vector, f=100 Hz
>>
>>zerIdx=[];
>>for i=1:length(x)-1
>>if ((x(i)>0 && x(i+1)<0) || (x(i)<0 && x(i+1)>0))
>>zerIdx(end+1)=i; % save index of zero-crossing
>>end
>>end
>>
>>t(zerIdx) % display timepoints of zero-crossings
>>
>>/Jocke :-)
>>
>
> Thank you for this reply Jocke,
>
>
> My Situation is a bit different. What i have is a signal from
> Simulink in form of a curve. Probably i should do some sort of
> interpolation at the beginning ?

Subject: Zero cross detection

From: Jocke

Date: 28 Apr, 2003 09:25:24

Message: 5 of 13

Dragomir Deltchev wrote:
> ...
> Probably i should do some sort of
> interpolation at the beginning ?


Yes, you have to interpolate to increase the accuracy of the
timepoint of the zero-crossing.
Or, you could increase your sample rate to increase the time-accuracy.


/Jocke

Subject: Zero cross detection

From: Dragomir Deltchev

Date: 28 Apr, 2003 11:14:50

Message: 6 of 13

Jocke wrote:
>
>
> Dragomir Deltchev wrote:
>> ...
>> Probably i should do some sort of
>> interpolation at the beginning ?
>
> Yes, you have to interpolate to increase the accuracy of the
> timepoint of the zero-crossing.
> Or, you could increase your sample rate to increase the
time-accuracy.
>
> /Jocke
>
Hi again Jocke,


I solve the problem by means of:
1) Transfer the signal to Matlab Workspace
2) t0=intern1(DataPoints,Time,0)

Subject: Zero cross detection

From: Jeff Klaus

Date: 29 Apr, 2003 12:53:43

Message: 7 of 13

Hello:


Lets say you save your signal from your simulink simulation to a
workspace variable x. You should also save the simulation time in a
vector t using the clock block in the simulink library. Then an
efficient way to find the zero crossings would be to use thefind
function of Matlab as follows:


i=1:length(x)-1;
k=find ((x(i)>0 & x(i+1)<0) | (x(i)<0 & x(i+1)>0));


the vector k has the indices to the array x where a zero crossing
occured. That is, Tcross=t(k). This should be much faster than
looping through the waveform to find the crossing indices.You can now
use the indices k and the original signal and time vectors (x,t) to
interpolate to find the exact zero crossing location.


Another handy way of doing this directly in simulink is to use the
hit crossing block in the simulink discontinuities library.


Jeff Klaus


Dragomir Deltchev wrote:
>
>
> Jocke wrote:
>>
>>
>> Dragomir Deltchev wrote:
>>> ...
>>> Probably i should do some sort of
>>> interpolation at the beginning ?
>>
>> Yes, you have to interpolate to increase the accuracy of the
>> timepoint of the zero-crossing.
>> Or, you could increase your sample rate to increase the
> time-accuracy.
>>
>> /Jocke
>>
> Hi again Jocke,
>
> I solve the problem by means of:
> 1) Transfer the signal to Matlab Workspace
> 2) t0=intern1(DataPoints,Time,0)
>

Subject: Zero cross detection

From: Lars Gregersen

Date: 3 May, 2003 12:56:47

Message: 8 of 13

On Mon, 28 Apr 2003 07:20:29 -0400, "Dragomir Deltchev"
<dragomir.deltchev@ifl.uni-karlsruhe.de> wrote:

>My Situation is a bit different. What i have is a signal from
>Simulink in form of a curve. Probably i should do some sort of
>interpolation at the beginning ?

If you're using Simulink then make the solver make the work for you.

Simulink is actually very good at detecting when a signal crosses a
certain value. You can use the Hit Crossing block for this. If you
use this block you'll see that Simulink calculates the exact point in
time where you have zero-crossing. If you're interested in other
signals at these points in time thay are also available without
interpolation.

See the help for the Hit Crossing block for more information and be
sure to click on the topic "Zero Crossing Detection"!

  Lars

Lars Gregersen
COMSOL A/S
http://www.comsol.dk

Subject: Zero cross detection

From: Dragomir Deltchev

Date: 3 May, 2003 07:00:22

Message: 9 of 13

Thank you for your reply Lars,


Well i saw that block and it do detects the cross point, but i dont
know how to export the result to the Matlab workspace in a form of
variable. I mean only this point. What you suppose is exactly what i
need. Actually i need this point to get results from other curves in
this time. Could you give me some more clues of how to do this and
how to get the results without interpolation.
Lars Gregersen wrote:
>
>
> On Mon, 28 Apr 2003 07:20:29 -0400, "Dragomir Deltchev"
> <dragomir.deltchev@ifl.uni-karlsruhe.de> wrote:
>
>>My Situation is a bit different. What i have is a signal from
>>Simulink in form of a curve. Probably i should do some sort of
>>interpolation at the beginning ?
>
> If you're using Simulink then make the solver make the work for you.
>
> Simulink is actually very good at detecting when a signal crosses a
> certain value. You can use the Hit Crossing block for this. If you
> use this block you'll see that Simulink calculates the exact point
in
> time where you have zero-crossing. If you're interested in other
> signals at these points in time thay are also available without
> interpolation.
>
> See the help for the Hit Crossing block for more information and be
> sure to click on the topic "Zero Crossing Detection"!
>
> Lars
>
> Lars Gregersen
> COMSOL A/S
> <http://www.comsol.dk>
>

Subject: Zero cross detection

From: Lars Gregersen

Date: 4 May, 2003 11:09:53

Message: 10 of 13

On Sat, 3 May 2003 07:00:22 -0400, "Dragomir Deltchev"
<dragomir.deltchev@ifl.uni-karlsruhe.de> wrote:

>Thank you for your reply Lars,
>
>
>Well i saw that block and it do detects the cross point, but i dont
>know how to export the result to the Matlab workspace in a form of
>variable. I mean only this point. What you suppose is exactly what i
>need. Actually i need this point to get results from other curves in
>this time. Could you give me some more clues of how to do this and
>how to get the results without interpolation.

I assume you have read the documentation I referred to.

If Simulink detects the zero-crossing you will get all you outputs and
states calculated at the time of the zero-crossing. You can export
your signals from Simulink using the Out block or the To Workspace
block.

All you need now is the time where you have the zero-crossing. Use the
Hit Crossing block for this. It will output true at the time of the
crossing. Simply output this value as well to your Matlab workspace
and you're done.

  Lars

Lars Gregersen
COMSOL A/S
http://www.comsol.dk

Subject: Zero cross detection

From: Titus Edelhofer

Date: 5 May, 2003 14:52:52

Message: 11 of 13

Hi Dragomir,
if you are only interested in this specific point, connect the output
of the hit crossing block to an enabled-subsystem. In this subsystem
just connect the clock-signal (from "Sources") with an To-Workspace
block.
The Output of the hit-crossing is only one when hitting the corresponding
value, so the enabled subsystem is only executed during these points
(and only these points are in the Workspace-variable afterwards).

Titus


"Dragomir Deltchev" <dragomir.deltchev@ifl.uni-karlsruhe.de> schrieb im
Newsbeitrag news:eebd047.7@WebX.raydaftYaTP...
> Thank you for your reply Lars,
>
>
> Well i saw that block and it do detects the cross point, but i dont
> know how to export the result to the Matlab workspace in a form of
> variable. I mean only this point. What you suppose is exactly what i
> need. Actually i need this point to get results from other curves in
> this time. Could you give me some more clues of how to do this and
> how to get the results without interpolation.
> Lars Gregersen wrote:
> >
> >
> > On Mon, 28 Apr 2003 07:20:29 -0400, "Dragomir Deltchev"
> > <dragomir.deltchev@ifl.uni-karlsruhe.de> wrote:
> >
> >>My Situation is a bit different. What i have is a signal from
> >>Simulink in form of a curve. Probably i should do some sort of
> >>interpolation at the beginning ?
> >
> > If you're using Simulink then make the solver make the work for you.
> >
> > Simulink is actually very good at detecting when a signal crosses a
> > certain value. You can use the Hit Crossing block for this. If you
> > use this block you'll see that Simulink calculates the exact point
> in
> > time where you have zero-crossing. If you're interested in other
> > signals at these points in time thay are also available without
> > interpolation.
> >
> > See the help for the Hit Crossing block for more information and be
> > sure to click on the topic "Zero Crossing Detection"!
> >
> > Lars
> >
> > Lars Gregersen
> > COMSOL A/S
> > <http://www.comsol.dk>
> >

Subject: Zero cross detection

From: Kyle

Date: 1 May, 2013 22:44:10

Message: 12 of 13

[0; diff(sign(x))]>1 % this detects if your signal just went above 0
[0; diff(sign(x))]<-1 % this detects if your signal just went below 0

Subject: Zero cross detection

From: reza

Date: 1 May, 2013 23:24:04

Message: 13 of 13

On Wednesday, May 1, 2013 6:44:10 PM UTC-4, Kyle wrote:
> [0; diff(sign(x))]>1 % this detects if your signal just went above 0
>
> [0; diff(sign(x))]<-1 % this detects if your signal just went below 0

or you can detect it using this:

>> find(x(1:end-1).*x(2:end) <= 0) + 1

drawback: it marks a zero-crossing where the signal is 0 from one time period to the next.

/reza

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us