Find zero crossings for a given 2D curve.

The code is very simple and needs only one line for basic results.
41 Downloads
Updated 21 Jul 2022

View License

PURPOSE:
Find zero crossings of a given 2D curve.
If the curve is noisy, you might consider to smooth it beforehand,
e.g. with https://mathworks.com/matlabcentral/fileexchange/66099
Full call:
ZeroX=FindZeroCrossSimple(x,y,Interpolate)
INPUTS:
1 x: x-values. Vector with numbers.
2 y: y-values. Vector with numbers.
Optional value:
You may omit it.
3 Interpolate: 0: x zero position is nearest sample left of zero crossing
1: x zero position is at nearest sample
2: linear interpolation(default)
Output:
Zerox: Vector with zero x-positions
Empty vector for no zeros at all.
Processing time on my PC for 10^6 samples and 3.4*10^5 zero crossings is:
0) x zero position left of zero crossing: 9 ms
1) x zero position is at nearest sample: 21 ms
2) linear interpolation (default): 21 ms
If you process always in the same manner, you can delete many code lines.
E.g. you want allways all x zero position left of zero crossing:
ZeroX=x(diff(sign(y))~=0);
Or you want allways all x zero position with linear interpolation:
Zindx = find(diff(sign(y)));
y1=y(Zindx);
ZeroX=(y1*(x(1)-x(2)))./(y(Zindx+1)-y1)+x(Zindx);
Or you want only ascending zero crosses with linear interpolation:
Zindx = find(diff(sign(y))>0);%ascending zero crossings.
% For desending: Zindx = find(diff(sign(y))<0);%descending zero crossings.
y1=y(Zindx);
ZeroX=(y1*(x(1)-x(2)))./(y(Zindx+1)-y1)+x(Zindx);

Cite As

Peter Seibold (2024). Find zero crossings for a given 2D curve. (https://www.mathworks.com/matlabcentral/fileexchange/114340-find-zero-crossings-for-a-given-2d-curve), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2022a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.0.1

Some changes in demo.

1.0.0