spafe.fbanks.mel_fbanks#

spafe.fbanks.mel_fbanks.mel_filter_banks_helper(nfilts: int = 24, nfft: int = 512, fs: int = 16000, low_freq: float = 0, high_freq: Optional[float] = None, scale: Literal['ascendant', 'descendant', 'constant'] = 'constant', fb_type='mel', conversion_approach: Literal['Oshaghnessy', 'Lindsay'] = 'Oshaghnessy')[source]#

Compute Mel-filter banks.The filters are stored in the rows, the columns correspond to fft bins.

Parameters
  • nfilts (int) – the number of filters in the filter bank. (Default 20).

  • nfft (int) – the FFT size. (Default is 512).

  • fs (int) – sample rate/ sampling frequency of the signal. (Default 16000 Hz).

  • low_freq (float) – lowest band edge of mel filters. (Default 0 Hz).

  • high_freq (float) – highest band edge of mel filters. (Default samplerate/2).

  • scale (str) – monotonicity behavior of the filter banks. (Default is “constant”).

  • fb_type (str) – the filter banks type. (Default is “mel”)

  • conversion_approach (str) – mel scale conversion approach. (Default is “Oshaghnessy”).

Returns

  • (numpy.ndarray) : array of size nfilts * (nfft/2 + 1) containing filter bank. Each row holds 1 filter.

  • (numpy.ndarray) : array of center frequencies

Return type

(tuple)

Tip

  • scale : can take the following options [“constant”, “ascendant”, “descendant”].

  • fb_type : can take the following options [“mel”, “lin”].

  • conversion_approach : can take the following options [“Oshaghnessy”, “Beranek”, “Lindsay”]. Note that the use of different options than the default can lead to unexpected behavior/issues.

spafe.fbanks.mel_fbanks.mel_filter_banks(nfilts: int = 24, nfft: int = 512, fs: int = 16000, low_freq: float = 0, high_freq: Optional[float] = None, scale: Literal['ascendant', 'descendant', 'constant'] = 'constant', conversion_approach: Literal['Oshaghnessy', 'Lindsay'] = 'Oshaghnessy')[source]#

Compute Mel-filter banks.The filters are stored in the rows, the columns correspond to fft bins.

Parameters
  • nfilts (int) – the number of filters in the filter bank. (Default 20).

  • nfft (int) – the FFT size. (Default is 512).

  • fs (int) – sample rate/ sampling frequency of the signal. (Default 16000 Hz).

  • low_freq (float) – lowest band edge of mel filters. (Default 0 Hz).

  • high_freq (float) – highest band edge of mel filters. (Default samplerate/2).

  • scale (str) – monotonicity behavior of the filter banks. (Default is “constant”).

  • conversion_approach (str) – mel scale conversion approach. (Default is “Oshaghnessy”).

Returns

  • (numpy.ndarray) : array of size nfilts * (nfft/2 + 1) containing filter bank. Each row holds 1 filter.

  • (numpy.ndarray) : array of center frequencies

Return type

(tuple)

Tip

  • scale : can take the following options [“constant”, “ascendant”, “descendant”].

  • conversion_approach : can take the following options [“Oshaghnessy”, “Beranek”, “Lindsay”]. Note that the use of different options than the default can lead to unexpected behavior/issues.

Examples

import numpy as np
from spafe.utils.converters import mel2hz
from spafe.utils.vis import show_fbanks
from spafe.fbanks.mel_fbanks import mel_filter_banks

# init var
fs = 8000
nfilt = 7
nfft = 1024
low_freq = 0
high_freq = fs / 2

# compute freqs for xaxis
mhz_freqs = np.linspace(low_freq, high_freq, nfft //2+1)

for scale, label in [("constant", ""), ("ascendant", "Ascendant "), ("descendant", "Descendant ")]:
    # compute fbank
    mel_fbanks_mat, mel_freqs = mel_filter_banks(nfilts=nfilt,
                                                 nfft=nfft,
                                                 fs=fs,
                                                 low_freq=low_freq,
                                                 high_freq=high_freq,
                                                scale=scale)

    # visualize fbank
    show_fbanks(
        mel_fbanks_mat,
        [mel2hz(freq) for freq in mel_freqs],
        mhz_freqs,
        label + "Mel Filter Bank",
        ylabel="Weight",
        x1label="Frequency / Hz",
        x2label="Frequency / Mel",
        figsize=(14, 5),
        fb_type="mel")
../_images/mel_fbanks-1_00.png
../_images/mel_fbanks-1_01.png
../_images/mel_fbanks-1_02.png
spafe.fbanks.mel_fbanks.inverse_mel_filter_banks(nfilts: int = 24, nfft: int = 512, fs: int = 16000, low_freq: float = 0, high_freq: Optional[float] = None, scale: Literal['ascendant', 'descendant', 'constant'] = 'constant', conversion_approach: Literal['Oshaghnessy', 'Lindsay'] = 'Oshaghnessy')[source]#

Compute inverse Mel-filter banks. The filters are stored in the rows, the columns correspond to fft bins.

Parameters
  • nfilt (int) – the number of filters in the filter bank. (Default 20).

  • nfft (int) – the FFT size. (Default is 512).

  • fs (int) – sample rate/ sampling frequency of the signal. (Default 16000 Hz).

  • low_freq (float) – lowest band edge of mel filters. (Default 0 Hz).

  • high_freq (float) – highest band edge of mel filters. (Default samplerate/2).

  • scale (str) – monotonicity behavior of the filter banks. (Default is “constant”).

  • conversion_approach (str) – mel scale conversion approach. (Default is “Oshaghnessy”).

Returns

  • (numpy.ndarray) : array of size nfilts * (nfft/2 + 1) containing filter bank. Each row holds 1 filter.

  • (numpy.ndarray) : array of center frequencies

Return type

(tuple)

Tip

  • scale : can take the following options [“constant”, “ascendant”, “descendant”].

  • conversion_approach : can take the following options [“Oshaghnessy”, “Beranek”, “Lindsay”]. Note that the use of different options than the default can lead to unexpected behavior/issues.

Examples

import numpy as np
from spafe.utils.converters import mel2hz
from spafe.utils.vis import show_fbanks
from spafe.fbanks.mel_fbanks import inverse_mel_filter_banks

# init var
fs = 8000
nfilt = 7
nfft = 1024
low_freq = 0
high_freq = fs / 2

# compute freqs for xaxis
mhz_freqs = np.linspace(low_freq, high_freq, nfft //2+1)

for scale, label in [("constant", ""), ("ascendant", "Ascendant "), ("descendant", "Descendant ")]:
    # compute fbank
    imel_fbanks_mat, imel_freqs = inverse_mel_filter_banks(nfilts=nfilt,
                                                           nfft=nfft,
                                                           fs=fs,
                                                           low_freq=low_freq,
                                                           high_freq=high_freq,
                                                           scale=scale)

    # visualize fbank
    show_fbanks(
        imel_fbanks_mat,
        [mel2hz(freq) for freq in imel_freqs],
        mhz_freqs,
        label + "Inverse Mel Filter Bank",
        ylabel="Weight",
        x1label="Frequency / Hz",
        x2label="Frequency / Mel",
        figsize=(14, 5),
        fb_type="mel")
../_images/mel_fbanks-2_00.png
../_images/mel_fbanks-2_01.png
../_images/mel_fbanks-2_02.png