Skip to content

Algorithms | latest | zero-crossing-rate

Zero-crossing rate

The zero-crossing rate (ZCR) is the rate at which a signal changes from positive to zero to negative or from negative to zero to positive.

The ZCR is computed by counting the zero crossings and converting it to a rate in Hz using the sampling rate fs.

ZCR=fsNi=1N1|p(x[i])p(x[i1])|p(x)={1x00x<0

References

Code

INFO

The following snippet is written in a generic and unoptimized manner. The code aims to be comprehensible to programmers familiar with various programming languages and may not represent the most efficient or idiomatic Python practices. Please refer to implementations for optimized implementations in different programming languages.

py
import numpy as np


def zero_crossing_rate(signal: np.ndarray, samplerate: float) -> int:
    if len(signal) == 0:
        return 0
    crossings = 0
    was_positive = signal[0] >= 0
    for value in signal[1:]:
        is_positive = value >= 0
        if was_positive != is_positive:
            crossings += 1
        was_positive = is_positive
    return samplerate * crossings / len(signal)
Run in playground