MCPcopy Create free account
hub / github.com/ddbourgin/numpy-ml / mel_spectrogram

Function mel_spectrogram

numpy_ml/preprocessing/dsp.py:523–605  ·  view source on GitHub ↗

Apply the Mel-filterbank to the power spectrum for a signal `x`. Notes ----- The Mel spectrogram is the projection of the power spectrum of the framed and windowed signal onto the basis set provided by the Mel filterbank. Parameters ---------- x : :py:class:`ndarra

(
    x,
    window_duration=0.025,
    stride_duration=0.01,
    mean_normalize=True,
    window="hamming",
    n_filters=20,
    center=True,
    alpha=0.95,
    fs=44000,
)

Source from the content-addressed store, hash-verified

521
522
523def mel_spectrogram(
524 x,
525 window_duration=0.025,
526 stride_duration=0.01,
527 mean_normalize=True,
528 window="hamming",
529 n_filters=20,
530 center=True,
531 alpha=0.95,
532 fs=44000,
533):
534 """
535 Apply the Mel-filterbank to the power spectrum for a signal `x`.
536
537 Notes
538 -----
539 The Mel spectrogram is the projection of the power spectrum of the framed
540 and windowed signal onto the basis set provided by the Mel filterbank.
541
542 Parameters
543 ----------
544 x : :py:class:`ndarray <numpy.ndarray>` of shape `(N,)`
545 A 1D signal consisting of N samples
546 window_duration : float
547 The duration of each frame / window (in seconds). Default is 0.025.
548 stride_duration : float
549 The duration of the hop between consecutive windows (in seconds).
550 Default is 0.01.
551 mean_normalize : bool
552 Whether to subtract the coefficient means from the final filter values
553 to improve the signal-to-noise ratio. Default is True.
554 window : {'hamming', 'hann', 'blackman_harris'}
555 The windowing function to apply to the signal before FFT. Default is
556 'hamming'.
557 n_filters : int
558 The number of mel filters to include in the filterbank. Default is 20.
559 center : bool
560 Whether to the `k` th frame of the signal should *begin* at index ``x[k *
561 stride_len]`` (center = False) or be *centered* at ``x[k * stride_len]``
562 (center = True). Default is False.
563 alpha : float in [0, 1)
564 The coefficient for the preemphasis filter. A value of 0 corresponds to
565 no filtering. Default is 0.95.
566 fs : int
567 The sample rate/frequency for the signal. Default is 44000.
568
569 Returns
570 -------
571 filter_energies : :py:class:`ndarray <numpy.ndarray>` of shape `(G, n_filters)`
572 The (possibly mean_normalized) power for each filter in the Mel
573 filterbank (i.e., the Mel spectrogram). Rows correspond to frames,
574 columns to filters
575 energy_per_frame : :py:class:`ndarray <numpy.ndarray>` of shape `(G,)`
576 The total energy in each frame of the signal
577 """
578 eps = np.finfo(float).eps
579 window_fn = WindowInitializer()(window)
580

Callers 1

mfccFunction · 0.85

Calls 5

WindowInitializerClass · 0.85
preemphasisFunction · 0.85
to_framesFunction · 0.85
power_spectrumFunction · 0.85
mel_filterbankFunction · 0.85

Tested by

no test coverage detected