def power_to_db(spectrum: np.ndarray,
clip_below: float = None,
clip_above: float = None) -> np.ndarray:
"""
Convert a spectrogram to the Decibel scale.
Optionally, frequencies with amplitudes below or above a certain threshold can be clipped.
Parameters
----------
spectrum: numpy.ndarray
The spectrogram to convert
clip_below: float, optional
Clip frequencies below the specified amplitude in dB
clip_above: float, optional
Clip frequencies above the specified amplitude in dB
Returns
-------
numpy.ndarray
The spectrogram on the Decibel scale
"""
# there might be zeros, fix them to the lowest non-zero power in the spectrogram
epsilon = np.min(spectrum[np.where(spectrum > 0)])
sxx = np.where(spectrum > epsilon, spectrum, epsilon)
sxx = 10 * np.log10(sxx / np.max(sxx))
if clip_below is not None:
sxx = np.maximum(sxx, clip_below)
if clip_above is not None:
sxx = np.minimum(sxx, clip_above)
return sxx
评论列表
文章目录