maad.sound.envelope

maad.sound.envelope(s, mode='fast', Nt=32)[source]

Calculate the envelope of a sound waveform (1d)

Parameters:
s1d ndarray of floats

Vector containing sound waveform

modestr, optional, default is fast
  • fastThe sound is first divided into frames (2d) using the

    function wave2timeframes(s), then the max of each frame gives a good approximation of the envelope.

  • Hilbertestimation of the envelope from the Hilbert transform.

    The method is slow

Ntinteger, optional, default is 32

Size of each frame. The largest, the highest is the approximation.

Returns:
env1d ndarray of floats

Envelope of the sound

References

[1]

Towsey, Michael (2013), Noise Removal from Waveforms and Spectrograms Derived from Natural Recordings of the Environment. Queensland University of Technology, Brisbane.

[2]

Towsey, Michael (2017),The calculation of acoustic indices derived from long-duration recordings of the natural environment. Queensland University of Technology, Brisbane.

Examples

>>> s,fs = maad.sound.load("../data/rock_savanna.wav")
>>> env_fast = maad.sound.envelope(s, mode='fast', Nt=32)
>>> env_fast
array([0.2300415 , 0.28643799, 0.24285889, ..., 0.3059082 , 0.20040894,
   0.26074219])
>>> env_hilbert = maad.sound.envelope(s, mode='hilbert')
>>> env_hilbert
array([0.06588196, 0.11301711, 0.09201435, ..., 0.18053983, 0.18351906,
   0.10258595])

Compute the time vector for the vector wave.

>>> import numpy as np
>>> t = np.arange(0,len(s),1)/fs

Compute the time vector for the vector env_fast.

>>> t_env_fast = np.arange(0,len(env_fast),1)*len(s)/fs/len(env_fast)

Plot 0.1s of the envelope and 0.1s of the abs(s).

>>> import matplotlib.pyplot as plt
>>> fig1, ax1 = plt.subplots(figsize=(10,4))
>>> ax1.plot(t[t<0.1], abs(s[t<0.1]), label='abs(s)', lw=0.7)
>>> ax1.plot(t[t<0.1], env_hilbert[t<0.1], label='env(s) - hilbert option', lw=0.7)
>>> ax1.plot(t_env_fast[t_env_fast<0.1], env_fast[t_env_fast<0.1], label='env(s) - fast option', lw=0.7)
>>> ax1.set_xlabel('Time [sec]')
>>> ax1.set_ylabel('Amplitude')
>>> ax1.legend()