Find zero crossings in discrete data set

PyAstronomy.pyaC.zerocross1d(x, y, getIndices=False)

Find the zero crossing points in 1d data.

Find the zero crossing events in a discrete data set. Linear interpolation is used to determine the actual locations of the zero crossing between two data points showing a change in sign. Data point which are zero are counted in as zero crossings if a sign change occurs across them. Note that the first and last data point will not be considered whether or not they are zero.

x, yarrays

Ordinate and abscissa data values.

getIndicesboolean, optional

If True, also the indicies of the points preceding the zero crossing event will be returned. Defeualt is False.


The locations of the zero crossing events determined by linear interpolation on the data.

indicesarray, optional

The indices of the points preceding the zero crossing events. Only returned if getIndices is set True.


import numpy as np
import matplotlib.pylab as plt
from PyAstronomy import pyaC

# Generate some 'data'
x = np.arange(100.)**2
y = np.sin(x)

# Set the last data point to zero.
# It will not be counted as a zero crossing!
y[-1] = 0

# Set point to zero. This will be counted as a
# zero crossing
y[10] = 0.0

# Get coordinates and indices of zero crossings
xc, xi = pyaC.zerocross1d(x, y, getIndices=True)

# Plot the data
plt.plot(x, y, 'b.-')
# Add black points where the zero line is crossed
plt.plot(xc, np.zeros(len(xc)), 'kp')
# Add green points at data points preceding an actual
# zero crossing.
plt.plot(x[xi], y[xi], 'gp')