spafe.fbanks.gammatone_fbanks#

spafe.fbanks.gammatone_fbanks.generate_center_frequencies(min_freq: float, max_freq: float, nfilts: int) ndarray[source]#

Compute center frequencies in the ERB scale.

Parameters
  • min_freq (float) – minimum frequency of the center frequencies’ domain.

  • max_freq (float) – maximum frequency of the center frequencies’ domain.

  • nfilts (int) – number of filters <=> number of center frequencies to compute.

Returns

array of center frequencies.

Return type

(numpy.ndarray)

spafe.fbanks.gammatone_fbanks.compute_gain(fcs: ndarray, B: ndarray, wT: ndarray, T: float) Tuple[ndarray, ndarray][source]#

Compute Gain and matrixify computation for speed purposes [Ellis-spectrogram].

Parameters
  • fcs (numpy.ndarray) – center frequencies in

  • B (numpy.ndarray) – bandwidths of the filters.

  • wT (numpy.ndarray) – corresponds to (omega)*T = 2*pi*freq*T.

  • T (float) – periode in seconds aka inverse of the sampling rate.

Returns

  • (numpy.ndarray) : a 2d numpy array representing the filter gains.

  • (numpy.ndarray) : a 2d array A used for final computations.

Return type

(tuple)

spafe.fbanks.gammatone_fbanks.gammatone_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', order: int = 4, conversion_approach: Literal['Glasberg'] = 'Glasberg')[source]#

Compute Gammatone-filter banks. The filters are stored in the rows, the columns correspond to fft bins [Ellis-spectrogram] and [Cusimano] .

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 is 16000 Hz).

  • low_freq (float) – lowest band edge of mel filters. (Default is 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”).

  • order (int) – order of the gammatone filter. (Default is 4).

  • conversion_approach (str) – erb scale conversion approach. (Default is “Glasberg”).

Returns

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

  • (numpy.ndarray) : array of center frequencies in Erb.

Return type

(tuple)

Tip

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

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

References

Ellis-spectrogram(1,2)

: Ellis, D.P.W. (2009). “Gammatone-like spectrograms”, web resource. http://www.ee.columbia.edu/~dpwe/resources/matlab/gammatonegram/

Cusimano

: Cusimano, M., gammatonegram, https://github.com/mcusi/gammatonegram/blob/master/gtg.py

Examples

import numpy as np
from spafe.utils.converters import erb2hz
from spafe.utils.vis import show_fbanks
from spafe.fbanks.gammatone_fbanks import gammatone_filter_banks

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

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

for scale, label in [("constant", ""), ("ascendant", "Ascendant "), ("descendant", "Descendant ")]:
    # gamma fbanks
    gamma_fbanks_mat, gamma_freqs = gammatone_filter_banks(nfilts=nfilts,
                                                           nfft=nfft,
                                                           fs=fs,
                                                           low_freq=low_freq,
                                                           high_freq=high_freq,
                                                           scale=scale,
                                                           order=4)
    # visualize filter bank
    show_fbanks(
        gamma_fbanks_mat,
        [erb2hz(freq) for freq in gamma_freqs],
        ghz_freqs,
        label + "Gamma Filter Bank",
        ylabel="Weight",
        x1label="Frequency / Hz",
        x2label="Frequency / erb",
        figsize=(14, 5),
        fb_type="gamma")
../_images/gammatone_fbanks-1_00.png
../_images/gammatone_fbanks-1_01.png
../_images/gammatone_fbanks-1_02.png