Unverified Commit 0f8fa5f8 authored by Tomás Osório's avatar Tomás Osório Committed by GitHub
Browse files

Inline typing Compliance Kaldi (#497)

* first stage

* add inline typing

* Fix typing and Flake8
parent a91cae7c
from typing import Tuple
import math
import random
import torch
import torchaudio
from torch import Tensor
__all__ = [
'get_mel_banks',
......@@ -31,18 +33,18 @@ BLACKMAN = 'blackman'
WINDOWS = [HAMMING, HANNING, POVEY, RECTANGULAR, BLACKMAN]
def _next_power_of_2(x):
def _next_power_of_2(x: int) -> int:
r"""Returns the smallest power of 2 that is greater than x
"""
return 1 if x == 0 else 2**(x - 1).bit_length()
return 1 if x == 0 else 2 ** (x - 1).bit_length()
def _get_strided(waveform, window_size, window_shift, snip_edges):
def _get_strided(waveform: Tensor, window_size: int, window_shift: int, snip_edges: bool) -> Tensor:
r"""Given a waveform (1D tensor of size ``num_samples``), it returns a 2D tensor (m, ``window_size``)
representing how the window is shifted along the waveform. Each row is a frame.
Args:
waveform (torch.Tensor): Tensor of size ``num_samples``
waveform (Tensor): Tensor of size ``num_samples``
window_size (int): Frame length
window_shift (int): Frame shift
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
......@@ -50,7 +52,7 @@ def _get_strided(waveform, window_size, window_shift, snip_edges):
depends only on the frame_shift, and we reflect the data at the ends.
Returns:
torch.Tensor: 2D tensor of size (m, ``window_size``) where each row is a frame
Tensor: 2D tensor of size (m, ``window_size``) where each row is a frame
"""
assert waveform.dim() == 1
num_samples = waveform.size(0)
......@@ -79,7 +81,9 @@ def _get_strided(waveform, window_size, window_shift, snip_edges):
return waveform.as_strided(sizes, strides)
def _feature_window_function(window_type, window_size, blackman_coeff):
def _feature_window_function(window_type: str,
window_size: int,
blackman_coeff: float) -> Tensor:
r"""Returns a window function with the given type and size
"""
if window_type == HANNING:
......@@ -95,13 +99,15 @@ def _feature_window_function(window_type, window_size, blackman_coeff):
a = 2 * math.pi / (window_size - 1)
window_function = torch.arange(window_size)
# can't use torch.blackman_window as they use different coefficients
return blackman_coeff - 0.5 * torch.cos(a * window_function) + \
(0.5 - blackman_coeff) * torch.cos(2 * a * window_function)
return (blackman_coeff - 0.5 * torch.cos(a * window_function) +
(0.5 - blackman_coeff) * torch.cos(2 * a * window_function))
else:
raise Exception('Invalid window type ' + window_type)
def _get_log_energy(strided_input, epsilon, energy_floor):
def _get_log_energy(strided_input: Tensor,
epsilon: Tensor,
energy_floor: float) -> Tensor:
r"""Returns the log energy of size (m) for a strided_input (m,*)
"""
log_energy = torch.max(strided_input.pow(2).sum(1), epsilon).log() # size (m)
......@@ -112,8 +118,13 @@ def _get_log_energy(strided_input, epsilon, energy_floor):
torch.tensor(math.log(energy_floor)))
def _get_waveform_and_window_properties(waveform, channel, sample_frequency, frame_shift,
frame_length, round_to_power_of_two, preemphasis_coefficient):
def _get_waveform_and_window_properties(waveform: Tensor,
channel: int,
sample_frequency: float,
frame_shift: float,
frame_length: float,
round_to_power_of_two: bool,
preemphasis_coefficient: float) -> Tuple[Tensor, int, int, int]:
r"""Gets the waveform and window properties
"""
channel = max(channel, 0)
......@@ -125,20 +136,29 @@ def _get_waveform_and_window_properties(waveform, channel, sample_frequency, fra
assert 2 <= window_size <= len(waveform), ('choose a window size %d that is [2, %d]' % (window_size, len(waveform)))
assert 0 < window_shift, '`window_shift` must be greater than 0'
assert padded_window_size % 2 == 0, 'the padded ' \
'`window_size` must be divisible by two. use `round_to_power_of_two` or change `frame_length`'
assert padded_window_size % 2 == 0, 'the padded `window_size` must be divisible by two.' \
' use `round_to_power_of_two` or change `frame_length`'
assert 0. <= preemphasis_coefficient <= 1.0, '`preemphasis_coefficient` must be between [0,1]'
assert sample_frequency > 0, '`sample_frequency` must be greater than zero'
return waveform, window_shift, window_size, padded_window_size
def _get_window(waveform, padded_window_size, window_size, window_shift, window_type, blackman_coeff,
snip_edges, raw_energy, energy_floor, dither, remove_dc_offset, preemphasis_coefficient):
def _get_window(waveform: Tensor,
padded_window_size: int,
window_size: int,
window_shift: int,
window_type: str,
blackman_coeff: float,
snip_edges: bool,
raw_energy: bool,
energy_floor: float,
dither: float,
remove_dc_offset: bool,
preemphasis_coefficient: float) -> Tuple[Tensor, Tensor]:
r"""Gets a window and its log energy
Returns:
strided_input (torch.Tensor): size (m, ``padded_window_size``)
signal_log_energy (torch.Tensor): size (m)
(Tensor, Tensor): strided_input of size (m, ``padded_window_size``) and signal_log_energy of size (m)
"""
# size (m, window_size)
strided_input = _get_strided(waveform, window_size, window_shift, snip_edges)
......@@ -183,7 +203,7 @@ def _get_window(waveform, padded_window_size, window_size, window_shift, window_
return strided_input, signal_log_energy
def _subtract_column_mean(tensor, subtract_mean):
def _subtract_column_mean(tensor: Tensor, subtract_mean: bool) -> Tensor:
# subtracts the column mean of the tensor size (m, n) if subtract_mean=True
# it returns size (m, n)
if subtract_mean:
......@@ -192,43 +212,54 @@ def _subtract_column_mean(tensor, subtract_mean):
return tensor
def spectrogram(
waveform, blackman_coeff=0.42, channel=-1, dither=0.0, energy_floor=1.0,
frame_length=25.0, frame_shift=10.0, min_duration=0.0,
preemphasis_coefficient=0.97, raw_energy=True, remove_dc_offset=True,
round_to_power_of_two=True, sample_frequency=16000.0, snip_edges=True,
subtract_mean=False, window_type=POVEY):
def spectrogram(waveform: Tensor,
blackman_coeff: float = 0.42,
channel: int = -1,
dither: float = 0.0,
energy_floor: float = 1.0,
frame_length: float = 25.0,
frame_shift: float = 10.0,
min_duration: float = 0.0,
preemphasis_coefficient: float = 0.97,
raw_energy: bool = True,
remove_dc_offset: bool = True,
round_to_power_of_two: bool = True,
sample_frequency: float = 16000.0,
snip_edges: bool = True,
subtract_mean: bool = False,
window_type: str = POVEY) -> Tensor:
r"""Create a spectrogram from a raw audio signal. This matches the input/output of Kaldi's
compute-spectrogram-feats.
Args:
waveform (torch.Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
blackman_coeff (float): Constant coefficient for generalized Blackman window. (Default: ``0.42``)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (Default: ``-1``)
dither (float): Dithering constant (0.0 means no dither). If you turn this off, you should set
waveform (Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
blackman_coeff (float, optional): Constant coefficient for generalized Blackman window. (Default: ``0.42``)
channel (int, optional): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (Default: ``-1``)
dither (float, optional): Dithering constant (0.0 means no dither). If you turn this off, you should set
the energy_floor option, e.g. to 1.0 or 0.1 (Default: ``0.0``)
energy_floor (float): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
energy_floor (float, optional): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
this floor is applied to the zeroth component, representing the total signal energy. The floor on the
individual spectrogram elements is fixed at std::numeric_limits<float>::epsilon(). (Default: ``1.0``)
frame_length (float): Frame length in milliseconds (Default: ``25.0``)
frame_shift (float): Frame shift in milliseconds (Default: ``10.0``)
min_duration (float): Minimum duration of segments to process (in seconds). (Default: ``0.0``)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (Default: ``0.97``)
raw_energy (bool): If True, compute energy before preemphasis and windowing (Default: ``True``)
remove_dc_offset: Subtract mean from waveform on each frame (Default: ``True``)
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
frame_length (float, optional): Frame length in milliseconds (Default: ``25.0``)
frame_shift (float, optional): Frame shift in milliseconds (Default: ``10.0``)
min_duration (float, optional): Minimum duration of segments to process (in seconds). (Default: ``0.0``)
preemphasis_coefficient (float, optional): Coefficient for use in signal preemphasis (Default: ``0.97``)
raw_energy (bool, optional): If True, compute energy before preemphasis and windowing (Default: ``True``)
remove_dc_offset (bool, optional): Subtract mean from waveform on each frame (Default: ``True``)
round_to_power_of_two (bool, optional): If True, round window size to power of two by zero-padding input
to FFT. (Default: ``True``)
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
sample_frequency (float, optional): Waveform data sample frequency (must match the waveform file, if
specified there) (Default: ``16000.0``)
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
snip_edges (bool, optional): If True, end effects will be handled by outputting only frames that completely fit
in the file, and the number of frames depends on the frame_length. If False, the number of frames
depends only on the frame_shift, and we reflect the data at the ends. (Default: ``True``)
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
subtract_mean (bool, optional): Subtract mean of each feature file [CMS]; not recommended to do
it this way. (Default: ``False``)
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (Default: ``'povey'``)
window_type (str, optional): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman')
(Default: ``'povey'``)
Returns:
torch.Tensor: A spectrogram identical to what Kaldi would output. The shape is
Tensor: A spectrogram identical to what Kaldi would output. The shape is
(m, ``padded_window_size // 2 + 1``) where m is calculated in _get_strided
"""
waveform, window_shift, window_size, padded_window_size = _get_waveform_and_window_properties(
......@@ -253,26 +284,28 @@ def spectrogram(
return power_spectrum
def inverse_mel_scale_scalar(mel_freq):
# type: (float) -> float
def inverse_mel_scale_scalar(mel_freq: float) -> float:
return 700.0 * (math.exp(mel_freq / 1127.0) - 1.0)
def inverse_mel_scale(mel_freq):
def inverse_mel_scale(mel_freq: Tensor) -> Tensor:
return 700.0 * ((mel_freq / 1127.0).exp() - 1.0)
def mel_scale_scalar(freq):
# type: (float) -> float
def mel_scale_scalar(freq: float) -> float:
return 1127.0 * math.log(1.0 + freq / 700.0)
def mel_scale(freq):
def mel_scale(freq: Tensor) -> Tensor:
return 1127.0 * (1.0 + freq / 700.0).log()
def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
vtln_warp_factor, freq):
def vtln_warp_freq(vtln_low_cutoff: float,
vtln_high_cutoff: float,
low_freq: float,
high_freq: float,
vtln_warp_factor: float,
freq: Tensor) -> Tensor:
r"""This computes a VTLN warping function that is not the same as HTK's one,
but has similar inputs (this function has the advantage of never producing
empty bins).
......@@ -304,10 +337,10 @@ def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
low_freq (float): Lower frequency cutoffs in mel computation
high_freq (float): Upper frequency cutoffs in mel computation
vtln_warp_factor (float): Vtln warp factor
freq (torch.Tensor): given frequency in Hz
freq (Tensor): given frequency in Hz
Returns:
torch.Tensor: Freq after vtln warp
Tensor: Freq after vtln warp
"""
assert vtln_low_cutoff > low_freq, 'be sure to set the vtln_low option higher than low_freq'
assert vtln_high_cutoff < high_freq, 'be sure to set the vtln_high option lower than high_freq [or negative]'
......@@ -340,8 +373,11 @@ def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
return res
def vtln_warp_mel_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
vtln_warp_factor, mel_freq):
def vtln_warp_mel_freq(vtln_low_cutoff: float,
vtln_high_cutoff: float,
low_freq, high_freq: float,
vtln_warp_factor: float,
mel_freq: Tensor) -> Tensor:
r"""
Args:
vtln_low_cutoff (float): Lower frequency cutoffs for VTLN
......@@ -349,21 +385,26 @@ def vtln_warp_mel_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
low_freq (float): Lower frequency cutoffs in mel computation
high_freq (float): Upper frequency cutoffs in mel computation
vtln_warp_factor (float): Vtln warp factor
mel_freq (torch.Tensor): Given frequency in Mel
mel_freq (Tensor): Given frequency in Mel
Returns:
torch.Tensor: ``mel_freq`` after vtln warp
Tensor: ``mel_freq`` after vtln warp
"""
return mel_scale(vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
vtln_warp_factor, inverse_mel_scale(mel_freq)))
def get_mel_banks(num_bins, window_length_padded, sample_freq,
low_freq, high_freq, vtln_low, vtln_high, vtln_warp_factor):
# type: (int, int, float, float, float, float, float)
def get_mel_banks(num_bins: int,
window_length_padded: int,
sample_freq: float,
low_freq: float,
high_freq: float,
vtln_low: float,
vtln_high: float,
vtln_warp_factor: float) -> Tuple[Tensor, Tensor]:
"""
Returns:
Tuple[torch.Tensor, torch.Tensor]: The tuple consists of ``bins`` (which is
(Tensor, Tensor): The tuple consists of ``bins`` (which is
melbank of size (``num_bins``, ``num_fft_bins``)) and ``center_freqs`` (which is
center frequencies of bins of size (``num_bins``)).
"""
......@@ -393,7 +434,7 @@ def get_mel_banks(num_bins, window_length_padded, sample_freq,
assert vtln_warp_factor == 1.0 or ((low_freq < vtln_low < high_freq) and
(0.0 < vtln_high < high_freq) and (vtln_low < vtln_high)), \
('Bad values in options: vtln-low %f and vtln-high %f, versus low-freq %f and high-freq %f' %
(vtln_low, vtln_high, low_freq, high_freq))
(vtln_low, vtln_high, low_freq, high_freq))
bin = torch.arange(num_bins).unsqueeze(1)
left_mel = mel_low_freq + bin * mel_freq_delta # size(num_bins, 1)
......@@ -427,56 +468,77 @@ def get_mel_banks(num_bins, window_length_padded, sample_freq,
return bins, center_freqs
def fbank(
waveform, blackman_coeff=0.42, channel=-1, dither=0.0, energy_floor=1.0,
frame_length=25.0, frame_shift=10.0, high_freq=0.0, htk_compat=False, low_freq=20.0,
min_duration=0.0, num_mel_bins=23, preemphasis_coefficient=0.97, raw_energy=True,
remove_dc_offset=True, round_to_power_of_two=True, sample_frequency=16000.0,
snip_edges=True, subtract_mean=False, use_energy=False, use_log_fbank=True, use_power=True,
vtln_high=-500.0, vtln_low=100.0, vtln_warp=1.0, window_type=POVEY):
def fbank(waveform: Tensor,
blackman_coeff: float = 0.42,
channel: int = -1,
dither: float = 0.0,
energy_floor: float = 1.0,
frame_length: float = 25.0,
frame_shift: float = 10.0,
high_freq: float = 0.0,
htk_compat: bool = False,
low_freq: float = 20.0,
min_duration: float = 0.0,
num_mel_bins: int = 23,
preemphasis_coefficient: float = 0.97,
raw_energy: bool = True,
remove_dc_offset: bool = True,
round_to_power_of_two: bool = True,
sample_frequency: float = 16000.0,
snip_edges: bool = True,
subtract_mean: bool = False,
use_energy: bool = False,
use_log_fbank: bool = True,
use_power: bool = True,
vtln_high: float = -500.0,
vtln_low: float = 100.0,
vtln_warp: float = 1.0,
window_type: str = POVEY) -> Tensor:
r"""Create a fbank from a raw audio signal. This matches the input/output of Kaldi's
compute-fbank-feats.
Args:
waveform (torch.Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
blackman_coeff (float): Constant coefficient for generalized Blackman window. (Default: ``0.42``)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (Default: ``-1``)
dither (float): Dithering constant (0.0 means no dither). If you turn this off, you should set
waveform (Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
blackman_coeff (float, optional): Constant coefficient for generalized Blackman window. (Default: ``0.42``)
channel (int, optional): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (Default: ``-1``)
dither (float, optional): Dithering constant (0.0 means no dither). If you turn this off, you should set
the energy_floor option, e.g. to 1.0 or 0.1 (Default: ``0.0``)
energy_floor (float): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
energy_floor (float, optional): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
this floor is applied to the zeroth component, representing the total signal energy. The floor on the
individual spectrogram elements is fixed at std::numeric_limits<float>::epsilon(). (Default: ``1.0``)
frame_length (float): Frame length in milliseconds (Default: ``25.0``)
frame_shift (float): Frame shift in milliseconds (Default: ``10.0``)
high_freq (float): High cutoff frequency for mel bins (if <= 0, offset from Nyquist) (Default: ``0.0``)
htk_compat (bool): If true, put energy last. Warning: not sufficient to get HTK compatible features (need
to change other parameters). (Default: ``False``)
low_freq (float): Low cutoff frequency for mel bins (Default: ``20.0``)
min_duration (float): Minimum duration of segments to process (in seconds). (Default: ``0.0``)
num_mel_bins (int): Number of triangular mel-frequency bins (Default: ``23``)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (Default: ``0.97``)
raw_energy (bool): If True, compute energy before preemphasis and windowing (Default: ``True``)
remove_dc_offset: Subtract mean from waveform on each frame (Default: ``True``)
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
frame_length (float, optional): Frame length in milliseconds (Default: ``25.0``)
frame_shift (float, optional): Frame shift in milliseconds (Default: ``10.0``)
high_freq (float, optional): High cutoff frequency for mel bins (if <= 0, offset from Nyquist)
(Default: ``0.0``)
htk_compat (bool, optional): If true, put energy last. Warning: not sufficient to get HTK compatible features
(need to change other parameters). (Default: ``False``)
low_freq (float, optional): Low cutoff frequency for mel bins (Default: ``20.0``)
min_duration (float, optional): Minimum duration of segments to process (in seconds). (Default: ``0.0``)
num_mel_bins (int, optional): Number of triangular mel-frequency bins (Default: ``23``)
preemphasis_coefficient (float, optional): Coefficient for use in signal preemphasis (Default: ``0.97``)
raw_energy (bool, optional): If True, compute energy before preemphasis and windowing (Default: ``True``)
remove_dc_offset (bool, optional): Subtract mean from waveform on each frame (Default: ``True``)
round_to_power_of_two (bool, optional): If True, round window size to power of two by zero-padding input
to FFT. (Default: ``True``)
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
sample_frequency (float, optional): Waveform data sample frequency (must match the waveform file, if
specified there) (Default: ``16000.0``)
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
snip_edges (bool, optional): If True, end effects will be handled by outputting only frames that completely fit
in the file, and the number of frames depends on the frame_length. If False, the number of frames
depends only on the frame_shift, and we reflect the data at the ends. (Default: ``True``)
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
subtract_mean (bool, optional): Subtract mean of each feature file [CMS]; not recommended to do
it this way. (Default: ``False``)
use_energy (bool): Add an extra dimension with energy to the FBANK output. (Default: ``False``)
use_log_fbank (bool):If true, produce log-filterbank, else produce linear. (Default: ``True``)
use_power (bool): If true, use power, else use magnitude. (Default: ``True``)
vtln_high (float): High inflection point in piecewise linear VTLN warping function (if
use_energy (bool, optional): Add an extra dimension with energy to the FBANK output. (Default: ``False``)
use_log_fbank (bool, optional):If true, produce log-filterbank, else produce linear. (Default: ``True``)
use_power (bool, optional): If true, use power, else use magnitude. (Default: ``True``)
vtln_high (float, optional): High inflection point in piecewise linear VTLN warping function (if
negative, offset from high-mel-freq (Default: ``-500.0``)
vtln_low (float): Low inflection point in piecewise linear VTLN warping function (Default: ``100.0``)
vtln_warp (float): Vtln warp factor (only applicable if vtln_map not specified) (Default: ``1.0``)
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (Default: ``'povey'``)
vtln_low (float, optional): Low inflection point in piecewise linear VTLN warping function (Default: ``100.0``)
vtln_warp (float, optional): Vtln warp factor (only applicable if vtln_map not specified) (Default: ``1.0``)
window_type (str, optional): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman')
(Default: ``'povey'``)
Returns:
torch.Tensor: A fbank identical to what Kaldi would output. The shape is (m, ``num_mel_bins + use_energy``)
Tensor: A fbank identical to what Kaldi would output. The shape is (m, ``num_mel_bins + use_energy``)
where m is calculated in _get_strided
"""
waveform, window_shift, window_size, padded_window_size = _get_waveform_and_window_properties(
......@@ -524,7 +586,7 @@ def fbank(
return mel_energies
def _get_dct_matrix(num_ceps, num_mel_bins):
def _get_dct_matrix(num_ceps: int, num_mel_bins: int) -> Tensor:
# returns a dct matrix of size (num_mel_bins, num_ceps)
# size (num_mel_bins, num_mel_bins)
dct_matrix = torchaudio.functional.create_dct(num_mel_bins, num_mel_bins, 'ortho')
......@@ -537,7 +599,7 @@ def _get_dct_matrix(num_ceps, num_mel_bins):
return dct_matrix
def _get_lifter_coeffs(num_ceps, cepstral_lifter):
def _get_lifter_coeffs(num_ceps: int, cepstral_lifter: float) -> Tensor:
# returns size (num_ceps)
# Compute liftering coefficients (scaling on cepstral coeffs)
# coeffs are numbered slightly differently from HTK: the zeroth index is C0, which is not affected.
......@@ -546,55 +608,77 @@ def _get_lifter_coeffs(num_ceps, cepstral_lifter):
def mfcc(
waveform, blackman_coeff=0.42, cepstral_lifter=22.0, channel=-1, dither=0.0,
energy_floor=1.0, frame_length=25.0, frame_shift=10.0, high_freq=0.0, htk_compat=False,
low_freq=20.0, num_ceps=13, min_duration=0.0, num_mel_bins=23, preemphasis_coefficient=0.97,
raw_energy=True, remove_dc_offset=True, round_to_power_of_two=True,
sample_frequency=16000.0, snip_edges=True, subtract_mean=False, use_energy=False,
vtln_high=-500.0, vtln_low=100.0, vtln_warp=1.0, window_type=POVEY):
waveform: Tensor,
blackman_coeff: float = 0.42,
cepstral_lifter: float = 22.0,
channel: int = -1,
dither: float = 0.0,
energy_floor: float = 1.0,
frame_length: float = 25.0,
frame_shift: float = 10.0,
high_freq: float = 0.0,
htk_compat: bool = False,
low_freq: float = 20.0,
num_ceps: int = 13,
min_duration: float = 0.0,
num_mel_bins: int = 23,
preemphasis_coefficient: float = 0.97,
raw_energy: bool = True,
remove_dc_offset: bool = True,
round_to_power_of_two: bool = True,
sample_frequency: float = 16000.0,
snip_edges: bool = True,
subtract_mean: bool = False,
use_energy: bool = False,
vtln_high: float = -500.0,
vtln_low: float = 100.0,
vtln_warp: float = 1.0,
window_type: str = POVEY) -> Tensor:
r"""Create a mfcc from a raw audio signal. This matches the input/output of Kaldi's
compute-mfcc-feats.
Args:
waveform (torch.Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
blackman_coeff (float): Constant coefficient for generalized Blackman window. (Default: ``0.42``)
cepstral_lifter (float): Constant that controls scaling of MFCCs (Default: ``22.0``)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (Default: ``-1``)
dither (float): Dithering constant (0.0 means no dither). If you turn this off, you should set
waveform (Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
blackman_coeff (float, optional): Constant coefficient for generalized Blackman window. (Default: ``0.42``)
cepstral_lifter (float, optional): Constant that controls scaling of MFCCs (Default: ``22.0``)
channel (int, optional): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (Default: ``-1``)
dither (float, optional): Dithering constant (0.0 means no dither). If you turn this off, you should set
the energy_floor option, e.g. to 1.0 or 0.1 (Default: ``0.0``)
energy_floor (float): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
energy_floor (float, optional): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
this floor is applied to the zeroth component, representing the total signal energy. The floor on the
individual spectrogram elements is fixed at std::numeric_limits<float>::epsilon(). (Default: ``1.0``)
frame_length (float): Frame length in milliseconds (Default: ``25.0``)
frame_shift (float): Frame shift in milliseconds (Default: ``10.0``)
high_freq (float): High cutoff frequency for mel bins (if <= 0, offset from Nyquist) (Default: ``0.0``)
htk_compat (bool): If true, put energy last. Warning: not sufficient to get HTK compatible features (need
to change other parameters). (Default: ``False``)
low_freq (float): Low cutoff frequency for mel bins (Default: ``20.0``)
num_ceps (int): Number of cepstra in MFCC computation (including C0) (Default: ``13``)
min_duration (float): Minimum duration of segments to process (in seconds). (Default: ``0.0``)
num_mel_bins (int): Number of triangular mel-frequency bins (Default: ``23``)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (Default: ``0.97``)
raw_energy (bool): If True, compute energy before preemphasis and windowing (Default: ``True``)
remove_dc_offset: Subtract mean from waveform on each frame (Default: ``True``)
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
frame_length (float, optional): Frame length in milliseconds (Default: ``25.0``)
frame_shift (float, optional): Frame shift in milliseconds (Default: ``10.0``)
high_freq (float, optional): High cutoff frequency for mel bins (if <= 0, offset from Nyquist)
(Default: ``0.0``)
htk_compat (bool, optional): If true, put energy last. Warning: not sufficient to get HTK compatible
features (need to change other parameters). (Default: ``False``)
low_freq (float, optional): Low cutoff frequency for mel bins (Default: ``20.0``)
num_ceps (int, optional): Number of cepstra in MFCC computation (including C0) (Default: ``13``)
min_duration (float, optional): Minimum duration of segments to process (in seconds). (Default: ``0.0``)
num_mel_bins (int, optional): Number of triangular mel-frequency bins (Default: ``23``)
preemphasis_coefficient (float, optional): Coefficient for use in signal preemphasis (Default: ``0.97``)
raw_energy (bool, optional): If True, compute energy before preemphasis and windowing (Default: ``True``)
remove_dc_offset (bool, optional): Subtract mean from waveform on each frame (Default: ``True``)
round_to_power_of_two (bool, optional): If True, round window size to power of two by zero-padding input
to FFT. (Default: ``True``)
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
sample_frequency (float, optional): Waveform data sample frequency (must match the waveform file, if
specified there) (Default: ``16000.0``)
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
snip_edges (bool, optional): If True, end effects will be handled by outputting only frames that completely fit
in the file, and the number of frames depends on the frame_length. If False, the number of frames
depends only on the frame_shift, and we reflect the data at the ends. (Default: ``True``)
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
subtract_mean (bool, optional): Subtract mean of each feature file [CMS]; not recommended to do
it this way. (Default: ``False``)
use_energy (bool): Add an extra dimension with energy to the FBANK output. (Default: ``False``)
vtln_high (float): High inflection point in piecewise linear VTLN warping function (if
use_energy (bool, optional): Add an extra dimension with energy to the FBANK output. (Default: ``False``)
vtln_high (float, optional): High inflection point in piecewise linear VTLN warping function (if
negative, offset from high-mel-freq (Default: ``-500.0``)
vtln_low (float): Low inflection point in piecewise linear VTLN warping function (Default: ``100.0``)
vtln_warp (float): Vtln warp factor (only applicable if vtln_map not specified) (Default: ``1.0``)
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (Default: ``'povey'``)
vtln_low (float, optional): Low inflection point in piecewise linear VTLN warping function (Default: ``100.0``)
vtln_warp (float, optional): Vtln warp factor (only applicable if vtln_map not specified) (Default: ``1.0``)
window_type (str, optional): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman')
(Default: ``"povey"``)
Returns:
torch.Tensor: A mfcc identical to what Kaldi would output. The shape is (m, ``num_ceps``)
Tensor: A mfcc identical to what Kaldi would output. The shape is (m, ``num_ceps``)
where m is calculated in _get_strided
"""
assert num_ceps <= num_mel_bins, 'num_ceps cannot be larger than num_mel_bins: %d vs %d' % (num_ceps, num_mel_bins)
......@@ -648,9 +732,12 @@ def mfcc(
return feature
def _get_LR_indices_and_weights(orig_freq, new_freq, output_samples_in_unit, window_width,
lowpass_cutoff, lowpass_filter_width):
# type: (float, float, int, float, float, int) -> Tuple[Tensor, Tensor]
def _get_LR_indices_and_weights(orig_freq: float,
new_freq: float,
output_samples_in_unit: int,
window_width: float,
lowpass_cutoff: float,
lowpass_filter_width: int) -> Tuple[Tensor, Tensor]:
r"""Based on LinearResample::SetIndexesAndWeights where it retrieves the weights for
resampling as well as the indices in which they are valid. LinearResample (LR) means
that the output signal is at linearly spaced intervals (i.e the output signal has a
......@@ -693,7 +780,7 @@ def _get_LR_indices_and_weights(orig_freq, new_freq, output_samples_in_unit, win
efficient. We suggest around 4 to 10 for normal use
Returns:
Tuple[torch.Tensor, torch.Tensor]: A tuple of ``min_input_index`` (which is the minimum indices
(Tensor, Tensor): A tuple of ``min_input_index`` (which is the minimum indices
where the window is valid, size (``output_samples_in_unit``)) and ``weights`` (which is the weights
which correspond with min_input_index, size (``output_samples_in_unit``, ``max_weight_width``)).
"""
......@@ -716,7 +803,7 @@ def _get_LR_indices_and_weights(orig_freq, new_freq, output_samples_in_unit, win
inside_window_indices = delta_t.abs().lt(window_width)
# raised-cosine (Hanning) window with width `window_width`
weights[inside_window_indices] = 0.5 * (1 + torch.cos(2 * math.pi * lowpass_cutoff /
lowpass_filter_width * delta_t[inside_window_indices]))
lowpass_filter_width * delta_t[inside_window_indices]))
t_eq_zero_indices = delta_t.eq(0.0)
t_not_eq_zero_indices = ~t_eq_zero_indices
......@@ -730,13 +817,13 @@ def _get_LR_indices_and_weights(orig_freq, new_freq, output_samples_in_unit, win
return min_input_index, weights
def _lcm(a, b):
# type: (int, int) -> int
def _lcm(a: int, b: int) -> int:
return abs(a * b) // math.gcd(a, b)
def _get_num_LR_output_samples(input_num_samp, samp_rate_in, samp_rate_out):
# type: (int, float, float) -> int
def _get_num_LR_output_samples(input_num_samp: int,
samp_rate_in: float,
samp_rate_out: float) -> int:
r"""Based on LinearResample::GetNumOutputSamples. LinearResample (LR) means that
the output signal is at linearly spaced intervals (i.e the output signal has a
frequency of ``new_freq``). It uses sinc/bandlimited interpolation to upsample/downsample
......@@ -780,8 +867,10 @@ def _get_num_LR_output_samples(input_num_samp, samp_rate_in, samp_rate_out):
return num_output_samp
def resample_waveform(waveform, orig_freq, new_freq, lowpass_filter_width=6):
# type: (Tensor, float, float, int) -> Tensor
def resample_waveform(waveform: Tensor,
orig_freq: float,
new_freq: float,
lowpass_filter_width: int = 6) -> Tensor:
r"""Resamples the waveform at the new frequency. This matches Kaldi's OfflineFeatureTpl ResampleWaveform
which uses a LinearResample (resample a signal at linearly spaced intervals to upsample/downsample
a signal). LinearResample (LR) means that the output signal is at linearly spaced intervals (i.e
......@@ -792,14 +881,14 @@ def resample_waveform(waveform, orig_freq, new_freq, lowpass_filter_width=6):
https://github.com/kaldi-asr/kaldi/blob/master/src/feat/resample.h#L56
Args:
waveform (torch.Tensor): The input signal of size (c, n)
waveform (Tensor): The input signal of size (c, n)
orig_freq (float): The original frequency of the signal
new_freq (float): The desired frequency
lowpass_filter_width (int): Controls the sharpness of the filter, more == sharper
lowpass_filter_width (int, optional): Controls the sharpness of the filter, more == sharper
but less efficient. We suggest around 4 to 10 for normal use. (Default: ``6``)
Returns:
torch.Tensor: The waveform at the new frequency
Tensor: The waveform at the new frequency
"""
assert waveform.dim() == 2
assert orig_freq > 0.0 and new_freq > 0.0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment