File Exchange

## Zero Crossing Rate

version 1.0.0.0 (1.24 KB) by Jose R Zapata

### Jose R Zapata (view profile)

Calculates the Zero Crossing Rate for a array of values, work for vector and matrix

Updated 03 Jun 2011

Zero Crossing Rate
Works for vector and matrix
Vectorized function,very fast

if x is a Vector returns the zero crossing rate of the vector
Ex: x = [1 2 -3 4 5 -6 -2 -6 2];
y = ZCR(x) -> y = 0.444

if x is a matrix returns a row vector with the zero crossing rate of
the columns values

By: Jose Ricardo Zapata
2010

### Cite As

Jose R Zapata (2020). Zero Crossing Rate (https://www.mathworks.com/matlabcentral/fileexchange/31663-zero-crossing-rate), MATLAB Central File Exchange. Retrieved .

Rownaq Tahia

Drew

### Drew (view profile)

very neat, thanks! if x is a matrix, and number of cols > number of rows, length(x) is incorrect. here's a bugfix for that, with some additional asserts. :)

function y = zcr(x)
assert( ismatrix(x) );

[M,N] = size(x);

if isvector(x)
L = length(x);
else
assert( M>=2 && N>=2 );
L = M;
end
assert( L >= 1 );

y = sum(abs(diff(x>0))) / L;

end

Steven

### Steven (view profile)

Thank you for this post. Very efficient.
I have an addition, if you replace x>0, by x>threshold, you can use a threshold for hysteresis. So for example if threshold == 10, a crossing will only be detected if the signal went from -10 to +10 or +10 to -10.

RawDeal