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
References
- https://en.wikipedia.org/wiki/Zero-crossing_rate
- Kim, H.-G., Moreau, N., & Sikora, T. (2006). MPEG-7 audio and beyond: Audio content indexing and retrieval. John Wiley & Sons.
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)