No BSD License  

Highlights from
crossing

4.05

4.0 | 20 ratings Rate this file 138 Downloads (last 30 days) File Size: 3.37 KB File ID: #2432

crossing

by Steffen Brueckner

 

25 Sep 2002 (Updated 28 Aug 2007)

Detect zero (or any other) level crossing of signals.

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

Detect the zero (or any other level) crossing of a given signal, either in discrete data points or using linear interpolation.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (23)
22 Jan 2003 Mike Price  
18 Sep 2003 Andre Frotta Muller

Very good!!!!

Thanks.

14 Feb 2006 Rajeeva Kumar  
13 Apr 2006 Zach Lewis

I found that with discrete data, it can be off 1 data point to the actual closest value (closest to 0). I've added this as a check
[CC,II] = min(abs([S(ind-1) S(ind) S(ind+1)]),[],2); %find closest value to zero around crossing
ind = ind+(II-2); %update indices

13 Jun 2006 Jonathan Kellogg

I found that if you wanted it to linearly interpolate between the t points that you give it, you need to go through the code and everywhere you see par.interpolation you need to change to just par or it breaks on the 'linear' string that put in. Otherwise works well for me.

01 Nov 2006 Alan Cumming

I found the same as Zach Lewis, it doesn't really find the correct point (it's one point off), and the interpolation doesn't work either (not only because the function specification line is wrong, either).

12 Mar 2007 Christian Rothleitner

In general a good algorithm. Just an improvement: First search for exact zeros. Otherwise you will count twice. Here the extension:
ind0 = find(S==0); %find amplitudes which are exactly zero
S1 = S(1:end-1) .* S(2:end);
ind1 = find(S1< 0);
ind_all_unsort = [ind0 ind1];
ind = sort(ind_all_unsort);%sorts values in ascending order
t0 = t(ind);

27 Jun 2007 Howard Fishman

For data far from 1, the eps test me not either not pass when you want to interpolate or pass when you shouldn't,

Change line 45 to
if abs(S(ind(ii))) > eps(S(ind(ii)))

Also change line 28 to
if nargin < 3 || isempty(level)

The condition of a peak (not a crossing) at exactly zero as described by Christian Rothleitner can be handled with the following

[t0, indx] = unique(t0);
ind = ind(indx);

30 Jan 2008 Sergio GM

It worked for me! Thanks.

18 Feb 2008 Islam Mohammed

if you need the number of crossing, NC.

NC = length(ind1) + length(ind0);

this is because if the signal cross zero exactly at the sample time it will not be calculated in ind1.

02 Apr 2008 Abhinav Jha

Is zero crossing, by definition, supposed to return all the indices at which the value of signal stays zero continually, or just the point at which zero is reached ? For eg if
a = [1 2 -1 0 0 0 1]
then should the return indices be
ind = [2 4 5 6],
or only
ind = [2 4].
Doesn't zero crossings mean the points at which zero is crossed ?

15 Apr 2008 Urs J

Hi

Search for zeros in a=[-1 1], and it will "crash" at line 99 because index is set to 0.

21 Jul 2008 René Truelsen

Excellent. Just what I was looking for. A function to find the zero-crossings of my LoG filtered signal

26 Aug 2008 Hartmut Wziontek

Nice source, saved some time for me.
However, I'd suggest to following patch to avoid indexes out of range:

*** crossing.m.orig Tue Aug 26 07:41:32 2008
--- crossing.m Tue Aug 26 13:38:27 2008
*************** end
*** 96,103 ****
  % Addition:
  % Some people like to get the data points closest to the zero crossing,
  % so we return these as well
! [CC,II] = min(abs([S(ind-1) ; S(ind) ; S(ind+1)]),[],1);
! ind2 = ind + (II-2); %update indices
  
  t0close = t(ind2);
! s0close = S(ind2);
\ No newline at end of file
--- 96,108 ----
  % Addition:
  % Some people like to get the data points closest to the zero crossing,
  % so we return these as well
! % --- hwz 2008-08-26
! % make it work for data starting/ending with a zero crossing!
! ind0 = find(ind > 1 & ind < length(S));
! [CC,II] = min(abs([S(ind(ind0)-1) ; S(ind(ind0)) ; S(ind(ind0)+1)]),[],1);
! II = ind0(II);
! ind2 = ind;
! ind2(ind0) = ind2(ind0) + (II-2); %update indices
  
  t0close = t(ind2);
! s0close = S(ind2);

04 May 2009 First Last  
10 Feb 2010 Nauman Qureshi  
21 Apr 2010 BD Knight

Following generates error, same as Urs J error:
crossing([8 .1 -3 -5],[],1)

05 May 2010 M. A. Hopcroft  
10 Feb 2011 Yan Cui  
12 Mar 2011 Paulo Emmanuel

Don't know what to do. I'm kinda new at this.

??? Subscript indices must either be real positive integers or logicals.

Error in ==> crossing at 99
[CC,II] = min(abs([S(ind-1) ; S(ind) ; S(ind+1)]),[],1);

Error in ==> AudioComp at 38
indexLeftZeroCross = crossing(leftFilteredData);

10 May 2011 Patrick Tamellini

Great, worked ok for me.

15 Sep 2011 Carlos Martinez

It doesn't work any more.

03 Jan 2012 Claire

doesn't seem to work

Index exceeds matrix dimensions.
Error in crossing (line 77)
t0 = t(ind);

Please login to add a comment or rating.
Updates
28 Aug 2007

Suggestions and bug reports in the file exchange reviews have finally been incorporated.

Tag Activity for this File
Tag Applied By Date/Time
zero crossing Steffen Brueckner 22 Oct 2008 06:52:13
signal processing Steffen Brueckner 22 Oct 2008 06:52:13
level crossing Steffen Brueckner 22 Oct 2008 06:52:13
detect zero Steffen Brueckner 22 Oct 2008 06:52:13
signal processing Aleksandar Granulic 10 Mar 2009 10:49:14
signal processing Kamini 17 Sep 2009 17:42:22
level crossing roberto madia 04 Nov 2009 08:16:20
detect zero Kamini 13 Nov 2009 16:34:55
detect zero Ilaria Milesi 29 Apr 2010 04:56:25
zero crossing Barry Foster 11 May 2010 07:14:07
detect zero Amir Khan 15 Nov 2011 07:06:06

Contact us at files@mathworks.com