Efficient Zero-Crossing Detection Code for Continuous Sine Waves in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter elecz
  • Start date Start date
  • Tags Tags
    Detection
Click For Summary

Discussion Overview

The discussion centers around detecting zero-crossings in a continuous sine wave using MATLAB. Participants explore coding approaches, the characteristics of the sine wave, and the mathematical formulation necessary for zero-crossing detection.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Exploratory

Main Points Raised

  • One participant requests a simple MATLAB code to detect zero-crossings in a continuous sine wave.
  • Another participant inquires about the characteristics of the sine wave, specifically whether it includes noise or harmonics.
  • A participant clarifies that the sine wave in question is simple and free from noise or harmonics.
  • One participant provides the analytic expression for the sine wave, asking for the coefficients involved.
  • A later reply specifies the values of the coefficients: ϕ=0, w=2πf (where f=1000), B=0, and A=1, with the time interval 0
  • Another participant suggests solving the equation sin(2π1000t) = 0 within the specified time constraints, providing a formula for the zero-crossing times.
  • A participant shares a code snippet (not in MATLAB) that records zero crossings, detailing its structure and logic for detecting crossings.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the sine wave being discussed, but there is no consensus on the best approach to implement zero-crossing detection in MATLAB, as different coding examples and methods are presented.

Contextual Notes

The discussion includes various assumptions about the sine wave parameters and the coding environment. The mathematical steps for zero-crossing detection are not fully resolved, and the provided code is not in MATLAB, which may limit its applicability.

elecz
Messages
17
Reaction score
0
I want to detect all zero-crossings in a sine wave through matlab. Kindly tell me about a simple code which detects all zero-crossings and stores them in an array. The sine wave is continuous time signal, not discrete...
 
Physics news on Phys.org
Tell us about the "sine" wave. How is it generated, does it have noise or harmonics?
 
Its a simple sine wave without any noise or harmonics
 
A sine wave is given as the following analytic expression:

<br /> y = A \, \sin{(\omega t - \phi)} + B<br />

Do you know the coefficients A, B, \omega, \phi from your example?
 
ϕ=0 and w=2*pi*f; f=1000; B=0 and A=1; 0<t<5
 
Well, you need to solve the equation:
<br /> \sin{(2 \pi 1000 t)} = 0, 0 &lt; t &lt; 5<br />
which means:
<br /> t = \frac{n}{2000}<br />
where n is an integer that must be in a particular interval so that the constraints on t are met.
 
This is a simple code (but not Matlab) that records all zero crossings (of both signs) in array "xcross" to a timing accuracy of 1 microsecond.

PROGRAM xcrossing
DIM xcross(100000,3)
OPTION NOLET
f=1000
w=2*pi*f
dt=1e-6
t=-dt/2
y=sin(w*t)
N=0
DO
t=t+dt
yold=y
y=sin(w*t)
IF yold=0 then yold=1E-12 ! eliminates divide by zero problem
IF y/yold<0 then
N=N+1
xcross(N,1)=N
xcross(N,2)=t-dt/2
xcross(N,3)=sgn(y)
END IF
LOOP while t<1
FOR N=1 to 30 ! sample array printout
PRINT xcross(N,1),xcross(N,2),xcross(N,3)
NEXT N
END
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K