ertk.preprocessing.spectrogram.spectrogram

ertk.preprocessing.spectrogram.spectrogram(audio: ndarray, sr: float, kind: str = 'mel', pre_emphasis: float = 0, window_size: float = 0.025, window_shift: float = 0.01, win_length_samp: int | None = None, hop_length_samp: int | None = None, n_fft: int = 2048, n_mels: int = 128, htk_mel: bool = False, n_chroma: int = 12, clip_db: float | None = None, fmin: float = 0, fmax: float | None = 8000, power: int = 2, to_log: str | None = 'db', mel_norm: str = 'slaney')

General purpose spectrogram pipeline. Calculates spectrogram with optional pre-emphasis, clipping, mel/chroma transform, power and conversion to dB.

Parameters:
audio: np.ndarray

Audio data. This should be a 1D array, or a 2D array with shape (1, n_samples).

sr: int

Sample rate.

kind: str

The kind of spectrogram to calculate. “mel” calculates a mel spectrogram, whereas “stft” just uses linear frequency.

pre_emphasis: float

Amount of pre-emphasis to apply. Default is 0 (no pre-emphasis).

window_size: float

Size of window in seconds. Default is 0.025 (25 ms).

window_shift: float

Amount window moves each frame, in seconds. Default is 0.01 (10 ms).

win_length_samp: int, optional

The window length in samples. This overrides window_size if given.

hop_length_samp: int, optional

The hop size in samples. This overrides window_shift if given.

n_fft: int, optional

Number of FFT bins. Default is to use the next power of 2 greater than window_size * sr.

n_mels: int

Number of mel bands.

htk_mel: bool,

Use HTK formula for mel calculation.

n_chroma: int

Number of chroma bands.

clip_db: float, optional

Whether to clip noise floor at a given level in dB below maximum. Default is None which does no clipping. This is only used if to_db is True.

fmin: float

Minimum frequency for mel bands. Default is 0.

fmax: float, optional

Maximum frequency for mel bands. Default is 8000.

power: int

Raise spectrogram magnitudes to this power. Default is 2 which is the usual power spectrogram.

to_log: str

Whether to convert spectrogram to logarithmic domain. Default is db which converts to dB units. If to_log=='log' then the natual logarithm is taken. Note that this argument is mainly only useful if power == 2.

mel_norm: str

Normalisation to apply to mel filters. Default is “slaney” as in librosa.

Returns:
melspec: np.ndarray

An array of shape (n_frames, n_mels) containing mel spectrogram. If power=2 this is the power spectrogram, and if to_db is True then this is in dB units.