Sunday, March 18, 2018

Feature Extraction using pyAudio

How to find zero crossing rate using pyAudio

That's a lot of unnecessary multiplication. Using a Boolean comparison and running it through np.diff will probably be faster:

zero_crosses = np.nonzero(np.diff(audioData > 0)))[0]

What this is doing:

  1. creates a boolean array of where the signal is above 0 (audioData > 0)
  2. does a pairwise difference (np.diff) so locations of zero crossings become 1 (rising) and -1 (falling)
  3. picks the index of the array where those nonzero values are (np.nonzero).

Then if you want the number of crossings, you can just take zero_crosses.size.

As a bonus you have the timings of all the crosses so you can do things like a histogram that shows where more crosses are happening in your time history.


Source: https://stackoverflow.com/questions/44319374/how-to-calculate-zero-crossing-rate-with-pyaudio-stream-data