music.normalize_mono

music.normalize_mono(sonic_vector, remove_bias=True)[source]

Normalize a mono sonic vector.

The final array will have values only between -1 and 1.

Parameters:
sonic_vectorarray_like

A (nsamples,) shaped array.

remove_biasbool

If True (default), subtract the mean and divide by the larger of the two peaks, which preserves the waveform’s shape. If False, map [min, max] onto [-1, 1] affinely, which fills the range but stretches an asymmetric waveform. Either way the result is normalized; this chooses how.

Returns:
sndarray

A numpy array with values between -1 and 1.

Raises:
ValueError

If the sonic vector is empty. A zero duration renders zero samples throughout this package and the sequence operations carry one through as the identity it is, so an empty array arriving here means a duration computed as zero upstream. This is where an empty sound stops being something anyone can act on, so this is where it is said; numpy used to report it as a zero-size reduction, which names the line rather than the mistake.

Parameters:
  • sonic_vector (ArrayLike)

  • remove_bias (bool)

Return type:

NDArray[float64]

Examples

>>> normalize_mono([-1., -.5, 0., .5, 1.])  # already normalized
array([-1. , -0.5,  0. ,  0.5,  1. ])
>>> normalize_mono([0., 1., 2.])  # centred, then scaled
array([-1.,  0.,  1.])