FeatureExtractors 를 위한 유틸리티
이 페이지는 오디오 FeatureExtractor
가 단시간 푸리에 변환(Short Time Fourier Transform) 또는 로그 멜 스펙트로그램(log mel spectrogram)과 같은 일반적인 알고리즘을 사용하여 원시 오디오에서 특수한 특성을 계산하는 데 사용할 수 있는 유틸리티 함수들을 나열합니다.
이 함수들 대부분은 라이브러리 내 오디오 처리 코드를 연구할 때에만 유용합니다.
오디오 변환
transformers.audio_utils.hertz_to_mel
< source >( freq: typing.Union[float, numpy.ndarray] mel_scale: str = 'htk' ) → float
or np.ndarray
Convert frequency from hertz to mels.
transformers.audio_utils.mel_to_hertz
< source >( mels: typing.Union[float, numpy.ndarray] mel_scale: str = 'htk' ) → float
or np.ndarray
Convert frequency from mels to hertz.
transformers.audio_utils.mel_filter_bank
< source >( num_frequency_bins: int num_mel_filters: int min_frequency: float max_frequency: float sampling_rate: int norm: typing.Optional[str] = None mel_scale: str = 'htk' triangularize_in_mel_space: bool = False ) → np.ndarray
of shape (num_frequency_bins
, num_mel_filters
)
Parameters
- num_frequency_bins (
int
) — Number of frequencies used to compute the spectrogram (should be the same as instft
). - num_mel_filters (
int
) — Number of mel filters to generate. - min_frequency (
float
) — Lowest frequency of interest in Hz. - max_frequency (
float
) — Highest frequency of interest in Hz. This should not exceedsampling_rate / 2
. - sampling_rate (
int
) — Sample rate of the audio waveform. - norm (
str
, optional) — If"slaney"
, divide the triangular mel weights by the width of the mel band (area normalization). - mel_scale (
str
, optional, defaults to"htk"
) — The mel frequency scale to use,"htk"
,"kaldi"
or"slaney"
. - triangularize_in_mel_space (
bool
, optional, defaults toFalse
) — If this option is enabled, the triangular filter is applied in mel space rather than frequency space. This should be set totrue
in order to get the same results astorchaudio
when computing mel filters.
Returns
np.ndarray
of shape (num_frequency_bins
, num_mel_filters
)
Triangular filter bank matrix. This is a projection matrix to go from a spectrogram to a mel spectrogram.
Creates a frequency bin conversion matrix used to obtain a mel spectrogram. This is called a mel filter bank, and various implementation exist, which differ in the number of filters, the shape of the filters, the way the filters are spaced, the bandwidth of the filters, and the manner in which the spectrum is warped. The goal of these features is to approximate the non-linear human perception of the variation in pitch with respect to the frequency.
Different banks of mel filters were introduced in the literature. The following variations are supported:
- MFCC FB-20: introduced in 1980 by Davis and Mermelstein, it assumes a sampling frequency of 10 kHz and a speech
bandwidth of
[0, 4600]
Hz. - MFCC FB-24 HTK: from the Cambridge HMM Toolkit (HTK) (1995) uses a filter bank of 24 filters for a speech
bandwidth of
[0, 8000]
Hz. This assumes sampling rate ≥ 16 kHz. - MFCC FB-40: from the Auditory Toolbox for MATLAB written by Slaney in 1998, assumes a sampling rate of 16 kHz and
speech bandwidth of
[133, 6854]
Hz. This version also includes area normalization. - HFCC-E FB-29 (Human Factor Cepstral Coefficients) of Skowronski and Harris (2004), assumes a sampling rate of
12.5 kHz and speech bandwidth of
[0, 6250]
Hz.
This code is adapted from torchaudio and librosa. Note that the default parameters of torchaudio’s
melscale_fbanks
implement the "htk"
filters while librosa uses the "slaney"
implementation.
Finds the best FFT input size for a given window_length
. This function takes a given window length and, if not
already a power of two, rounds it up to the next power or two.
The FFT algorithm works fastest when the length of the input is a power of two, which may be larger than the size of the window or analysis frame. For example, if the window is 400 samples, using an FFT input size of 512 samples is more optimal than an FFT size of 400 samples. Using a larger FFT size does not affect the detected frequencies, it simply gives a higher frequency resolution (i.e. the frequency bins are smaller).
transformers.audio_utils.window_function
< source >( window_length: int name: str = 'hann' periodic: bool = True frame_length: typing.Optional[int] = None center: bool = True )
Parameters
- window_length (
int
) — The length of the window in samples. - name (
str
, optional, defaults to"hann"
) — The name of the window function. - periodic (
bool
, optional, defaults toTrue
) — Whether the window is periodic or symmetric. - frame_length (
int
, optional) — The length of the analysis frames in samples. Provide a value forframe_length
if the window is smaller than the frame length, so that it will be zero-padded. - center (
bool
, optional, defaults toTrue
) — Whether to center the window inside the FFT buffer. Only used whenframe_length
is provided.
Returns an array containing the specified window. This window is intended to be used with stft
.
The following window types are supported:
"boxcar"
: a rectangular window"hamming"
: the Hamming window"hann"
: the Hann window"povey"
: the Povey window
transformers.audio_utils.spectrogram
< source >( waveform: ndarray window: ndarray frame_length: int hop_length: int fft_length: typing.Optional[int] = None power: typing.Optional[float] = 1.0 center: bool = True pad_mode: str = 'reflect' onesided: bool = True preemphasis: typing.Optional[float] = None mel_filters: typing.Optional[numpy.ndarray] = None mel_floor: float = 1e-10 log_mel: typing.Optional[str] = None reference: float = 1.0 min_value: float = 1e-10 db_range: typing.Optional[float] = None remove_dc_offset: typing.Optional[bool] = None dtype: dtype = <class 'numpy.float32'> )
Parameters
- waveform (
np.ndarray
of shape(length,)
) — The input waveform. This must be a single real-valued, mono waveform. - window (
np.ndarray
of shape(frame_length,)
) — The windowing function to apply, including zero-padding if necessary. The actual window length may be shorter thanframe_length
, but we’re assuming the array has already been zero-padded. - frame_length (
int
) — The length of the analysis frames in samples. With librosa this is always equal tofft_length
but we also allow smaller sizes. - hop_length (
int
) — The stride between successive analysis frames in samples. - fft_length (
int
, optional) — The size of the FFT buffer in samples. This determines how many frequency bins the spectrogram will have. For optimal speed, this should be a power of two. IfNone
, usesframe_length
. - power (
float
, optional, defaults to 1.0) — If 1.0, returns the amplitude spectrogram. If 2.0, returns the power spectrogram. IfNone
, returns complex numbers. - center (
bool
, optional, defaults toTrue
) — Whether to pad the waveform so that framet
is centered around timet * hop_length
. IfFalse
, framet
will start at timet * hop_length
. - pad_mode (
str
, optional, defaults to"reflect"
) — Padding mode used whencenter
isTrue
. Possible values are:"constant"
(pad with zeros),"edge"
(pad with edge values),"reflect"
(pads with mirrored values). - onesided (
bool
, optional, defaults toTrue
) — If True, only computes the positive frequencies and returns a spectrogram containingfft_length // 2 + 1
frequency bins. If False, also computes the negative frequencies and returnsfft_length
frequency bins. - preemphasis (
float
, optional) — Coefficient for a low-pass filter that applies pre-emphasis before the DFT. - mel_filters (
np.ndarray
of shape(num_freq_bins, num_mel_filters)
, optional) — The mel filter bank. If supplied, applies a this filter bank to create a mel spectrogram. - mel_floor (
float
, optional, defaults to 1e-10) — Minimum value of mel frequency banks. - log_mel (
str
, optional) — How to convert the spectrogram to log scale. Possible options are:None
(don’t convert),"log"
(take the natural logarithm)"log10"
(take the base-10 logarithm),"dB"
(convert to decibels). Can only be used whenpower
is notNone
. - reference (
float
, optional, defaults to 1.0) — Sets the input spectrogram value that corresponds to 0 dB. For example, usenp.max(spectrogram)
to set the loudest part to 0 dB. Must be greater than zero. - min_value (
float
, optional, defaults to1e-10
) — The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid takinglog(0)
. For a power spectrogram, the default of1e-10
corresponds to a minimum of -100 dB. For an amplitude spectrogram, the value1e-5
corresponds to -100 dB. Must be greater than zero. - db_range (
float
, optional) — Sets the maximum dynamic range in decibels. For example, ifdb_range = 80
, the difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero. - remove_dc_offset (
bool
, optional) — Subtract mean from waveform on each frame, applied before pre-emphasis. This should be set totrue
in order to get the same results astorchaudio.compliance.kaldi.fbank
when computing mel filters. - dtype (
np.dtype
, optional, defaults tonp.float32
) — Data type of the spectrogram tensor. Ifpower
is None, this argument is ignored and the dtype will benp.complex64
.
Calculates a spectrogram over one waveform using the Short-Time Fourier Transform.
This function can create the following kinds of spectrograms:
- amplitude spectrogram (
power = 1.0
) - power spectrogram (
power = 2.0
) - complex-valued spectrogram (
power = None
) - log spectrogram (use
log_mel
argument) - mel spectrogram (provide
mel_filters
) - log-mel spectrogram (provide
mel_filters
andlog_mel
)
How this works:
- The input waveform is split into frames of size
frame_length
that are partially overlapping by `frame_length- hop_length` samples.
- Each frame is multiplied by the window and placed into a buffer of size
fft_length
. - The DFT is taken of each windowed frame.
- The results are stacked into a spectrogram.
We make a distinction between the following “blocks” of sample data, each of which may have a different lengths:
- The analysis frame. This is the size of the time slices that the input waveform is split into.
- The window. Each analysis frame is multiplied by the window to avoid spectral leakage.
- The FFT input buffer. The length of this determines how many frequency bins are in the spectrogram.
In this implementation, the window is assumed to be zero-padded to have the same size as the analysis frame. A
padded window can be obtained from window_function()
. The FFT input buffer may be larger than the analysis frame,
typically the next power of two.
Note: This function is not optimized for speed yet. It should be mostly compatible with librosa.stft
and
torchaudio.functional.transforms.Spectrogram
, although it is more flexible due to the different ways spectrograms
can be constructed.
transformers.audio_utils.power_to_db
< source >( spectrogram: ndarray reference: float = 1.0 min_value: float = 1e-10 db_range: typing.Optional[float] = None ) → np.ndarray
Parameters
- spectrogram (
np.ndarray
) — The input power (mel) spectrogram. Note that a power spectrogram has the amplitudes squared! - reference (
float
, optional, defaults to 1.0) — Sets the input spectrogram value that corresponds to 0 dB. For example, usenp.max(spectrogram)
to set the loudest part to 0 dB. Must be greater than zero. - min_value (
float
, optional, defaults to1e-10
) — The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid takinglog(0)
. The default of1e-10
corresponds to a minimum of -100 dB. Must be greater than zero. - db_range (
float
, optional) — Sets the maximum dynamic range in decibels. For example, ifdb_range = 80
, the difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
Returns
np.ndarray
the spectrogram in decibels
Converts a power spectrogram to the decibel scale. This computes 10 * log10(spectrogram / reference)
, using basic
logarithm properties for numerical stability.
The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. This means that large variations in energy may not sound all that different if the sound is loud to begin with. This compression operation makes the (mel) spectrogram features match more closely what humans actually hear.
Based on the implementation of librosa.power_to_db
.
transformers.audio_utils.amplitude_to_db
< source >( spectrogram: ndarray reference: float = 1.0 min_value: float = 1e-05 db_range: typing.Optional[float] = None ) → np.ndarray
Parameters
- spectrogram (
np.ndarray
) — The input amplitude (mel) spectrogram. - reference (
float
, optional, defaults to 1.0) — Sets the input spectrogram value that corresponds to 0 dB. For example, usenp.max(spectrogram)
to set the loudest part to 0 dB. Must be greater than zero. - min_value (
float
, optional, defaults to1e-5
) — The spectrogram will be clipped to this minimum value before conversion to decibels, to avoid takinglog(0)
. The default of1e-5
corresponds to a minimum of -100 dB. Must be greater than zero. - db_range (
float
, optional) — Sets the maximum dynamic range in decibels. For example, ifdb_range = 80
, the difference between the peak value and the smallest value will never be more than 80 dB. Must be greater than zero.
Returns
np.ndarray
the spectrogram in decibels
Converts an amplitude spectrogram to the decibel scale. This computes 20 * log10(spectrogram / reference)
, using
basic logarithm properties for numerical stability.
The motivation behind applying the log function on the (mel) spectrogram is that humans do not hear loudness on a linear scale. Generally to double the perceived volume of a sound we need to put 8 times as much energy into it. This means that large variations in energy may not sound all that different if the sound is loud to begin with. This compression operation makes the (mel) spectrogram features match more closely what humans actually hear.