Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
Torchaudio
Commits
2f62e573
Unverified
Commit
2f62e573
authored
Jul 25, 2019
by
jamarshon
Committed by
GitHub
Jul 25, 2019
Browse files
A quick update of docstrings of kaldi.py and transforms.py (#163)
parent
51401084
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
120 additions
and
118 deletions
+120
-118
torchaudio/compliance/kaldi.py
torchaudio/compliance/kaldi.py
+115
-112
torchaudio/transforms.py
torchaudio/transforms.py
+5
-6
No files found.
torchaudio/compliance/kaldi.py
View file @
2f62e573
...
@@ -31,25 +31,25 @@ WINDOWS = [HAMMING, HANNING, POVEY, RECTANGULAR, BLACKMAN]
...
@@ -31,25 +31,25 @@ WINDOWS = [HAMMING, HANNING, POVEY, RECTANGULAR, BLACKMAN]
def
_next_power_of_2
(
x
):
def
_next_power_of_2
(
x
):
"""
Returns the smallest power of 2 that is greater than x
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
,
window_size
,
window_shift
,
snip_edges
):
"""
Given a waveform (1D tensor of size num_samples), it returns a 2D tensor (m, window_size)
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.
representing how the window is shifted along the waveform. Each row is a frame.
Input
s:
Arg
s:
sig (
Tensor): Tensor of size num_samples
waveform (torch.
Tensor): Tensor of size
`
num_samples
`
window_size (int): Frame length
window_size (int): Frame length
window_shift (int): Frame shift
window_shift (int): Frame shift
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
snip_edges (bool): 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
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.
depends only on the frame_shift, and we reflect the data at the ends.
Output
:
Returns
:
Tensor: 2D tensor of size (m, window_size) where each row is a frame
torch.
Tensor: 2D tensor of size (m,
`
window_size
`
) where each row is a frame
"""
"""
assert
waveform
.
dim
()
==
1
assert
waveform
.
dim
()
==
1
num_samples
=
waveform
.
size
(
0
)
num_samples
=
waveform
.
size
(
0
)
...
@@ -79,7 +79,7 @@ def _get_strided(waveform, window_size, window_shift, snip_edges):
...
@@ -79,7 +79,7 @@ def _get_strided(waveform, window_size, window_shift, snip_edges):
def
_feature_window_function
(
window_type
,
window_size
,
blackman_coeff
):
def
_feature_window_function
(
window_type
,
window_size
,
blackman_coeff
):
"""
Returns a window function with the given type and size
r
"""Returns a window function with the given type and size
"""
"""
if
window_type
==
HANNING
:
if
window_type
==
HANNING
:
return
torch
.
hann_window
(
window_size
,
periodic
=
False
)
return
torch
.
hann_window
(
window_size
,
periodic
=
False
)
...
@@ -101,7 +101,7 @@ def _feature_window_function(window_type, window_size, blackman_coeff):
...
@@ -101,7 +101,7 @@ def _feature_window_function(window_type, window_size, blackman_coeff):
def
_get_log_energy
(
strided_input
,
epsilon
,
energy_floor
):
def
_get_log_energy
(
strided_input
,
epsilon
,
energy_floor
):
"""
Returns the log energy of size (m) for a strided_input (m,*)
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)
log_energy
=
torch
.
max
(
strided_input
.
pow
(
2
).
sum
(
1
),
epsilon
).
log
()
# size (m)
if
energy_floor
==
0.0
:
if
energy_floor
==
0.0
:
...
@@ -111,11 +111,11 @@ def _get_log_energy(strided_input, epsilon, energy_floor):
...
@@ -111,11 +111,11 @@ def _get_log_energy(strided_input, epsilon, energy_floor):
torch
.
tensor
(
math
.
log
(
energy_floor
),
dtype
=
torch
.
get_default_dtype
()))
torch
.
tensor
(
math
.
log
(
energy_floor
),
dtype
=
torch
.
get_default_dtype
()))
def
_get_waveform_and_window_properties
(
sig
,
channel
,
sample_frequency
,
frame_shift
,
def
_get_waveform_and_window_properties
(
waveform
,
channel
,
sample_frequency
,
frame_shift
,
frame_length
,
round_to_power_of_two
,
preemphasis_coefficient
):
frame_length
,
round_to_power_of_two
,
preemphasis_coefficient
):
"""Gets the waveform and window properties
r
"""Gets the waveform and window properties
"""
"""
waveform
=
sig
[
max
(
channel
,
0
),
:]
# size (n)
waveform
=
waveform
[
max
(
channel
,
0
),
:]
# size (n)
window_shift
=
int
(
sample_frequency
*
frame_shift
*
MILLISECONDS_TO_SECONDS
)
window_shift
=
int
(
sample_frequency
*
frame_shift
*
MILLISECONDS_TO_SECONDS
)
window_size
=
int
(
sample_frequency
*
frame_length
*
MILLISECONDS_TO_SECONDS
)
window_size
=
int
(
sample_frequency
*
frame_length
*
MILLISECONDS_TO_SECONDS
)
padded_window_size
=
_next_power_of_2
(
window_size
)
if
round_to_power_of_two
else
window_size
padded_window_size
=
_next_power_of_2
(
window_size
)
if
round_to_power_of_two
else
window_size
...
@@ -131,10 +131,11 @@ def _get_waveform_and_window_properties(sig, channel, sample_frequency, frame_sh
...
@@ -131,10 +131,11 @@ def _get_waveform_and_window_properties(sig, channel, sample_frequency, frame_sh
def
_get_window
(
waveform
,
padded_window_size
,
window_size
,
window_shift
,
window_type
,
blackman_coeff
,
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
):
snip_edges
,
raw_energy
,
energy_floor
,
dither
,
remove_dc_offset
,
preemphasis_coefficient
):
"""Gets a window and its log energy
r
"""Gets a window and its log energy
Outputs:
strided_input (Tensor): size (m, padded_window_size)
Returns:
signal_log_energy (Tensor): size (m)
strided_input (torch.Tensor): size (m, `padded_window_size`)
signal_log_energy (torch.Tensor): size (m)
"""
"""
# size (m, window_size)
# size (m, window_size)
strided_input
=
_get_strided
(
waveform
,
window_size
,
window_shift
,
snip_edges
)
strided_input
=
_get_strided
(
waveform
,
window_size
,
window_shift
,
snip_edges
)
...
@@ -180,7 +181,7 @@ def _get_window(waveform, padded_window_size, window_size, window_shift, window_
...
@@ -180,7 +181,7 @@ def _get_window(waveform, padded_window_size, window_size, window_shift, window_
def
spectrogram
(
def
spectrogram
(
sig
,
blackman_coeff
=
0.42
,
channel
=-
1
,
dither
=
1.0
,
energy_floor
=
0.0
,
waveform
,
blackman_coeff
=
0.42
,
channel
=-
1
,
dither
=
1.0
,
energy_floor
=
0.0
,
frame_length
=
25.0
,
frame_shift
=
10.0
,
min_duration
=
0.0
,
frame_length
=
25.0
,
frame_shift
=
10.0
,
min_duration
=
0.0
,
preemphasis_coefficient
=
0.97
,
raw_energy
=
True
,
remove_dc_offset
=
True
,
preemphasis_coefficient
=
0.97
,
raw_energy
=
True
,
remove_dc_offset
=
True
,
round_to_power_of_two
=
True
,
sample_frequency
=
16000.0
,
snip_edges
=
True
,
round_to_power_of_two
=
True
,
sample_frequency
=
16000.0
,
snip_edges
=
True
,
...
@@ -189,37 +190,37 @@ def spectrogram(
...
@@ -189,37 +190,37 @@ def spectrogram(
compute-spectrogram-feats.
compute-spectrogram-feats.
Args:
Args:
sig (
Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
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. (
d
efault
=
0.42)
blackman_coeff (float): Constant coefficient for generalized Blackman window. (
D
efault
:
0.42)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (
d
efault
=
-1)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (
D
efault
:
-1)
dither (float): Dithering constant (0.0 means no dither). If you turn this off, you should set
dither (float): 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 (
d
efault
=
1.0)
the energy_floor option, e.g. to 1.0 or 0.1 (
D
efault
:
1.0)
energy_floor (float): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
energy_floor (float): 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
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(). (
d
efault
=
0.0)
individual spectrogram elements is fixed at std::numeric_limits<float>::epsilon(). (
D
efault
:
0.0)
frame_length (float): Frame length in milliseconds (
d
efault
=
25.0)
frame_length (float): Frame length in milliseconds (
D
efault
:
25.0)
frame_shift (float): Frame shift in milliseconds (
d
efault
=
10.0)
frame_shift (float): Frame shift in milliseconds (
D
efault
:
10.0)
min_duration (float): Minimum duration of segments to process (in seconds). (
d
efault
=
0.0)
min_duration (float): Minimum duration of segments to process (in seconds). (
D
efault
:
0.0)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (
d
efault
=
0.97)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (
D
efault
:
0.97)
raw_energy (bool): If True, compute energy before preemphasis and windowing (
d
efault
=
True)
raw_energy (bool): If True, compute energy before preemphasis and windowing (
D
efault
:
True)
remove_dc_offset: Subtract mean from waveform on each frame (
d
efault
=
True)
remove_dc_offset: Subtract mean from waveform on each frame (
D
efault
:
True)
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
to FFT. (
d
efault
=
True)
to FFT. (
D
efault
:
True)
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
specified there) (
d
efault
=
16000.0)
specified there) (
D
efault
:
16000.0)
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
snip_edges (bool): 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
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. (
d
efault
=
True)
depends only on the frame_shift, and we reflect the data at the ends. (
D
efault
:
True)
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
it this way. (
d
efault
=
False)
it this way. (
D
efault
:
False)
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (
d
efault
=
'povey')
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (
D
efault
:
'povey')
Returns:
Returns:
Tensor:
a
spectrogram identical to what Kaldi would output. The shape is
torch.
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
(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
(
waveform
,
window_shift
,
window_size
,
padded_window_size
=
_get_waveform_and_window_properties
(
sig
,
channel
,
sample_frequency
,
frame_shift
,
frame_length
,
round_to_power_of_two
,
preemphasis_coefficient
)
waveform
,
channel
,
sample_frequency
,
frame_shift
,
frame_length
,
round_to_power_of_two
,
preemphasis_coefficient
)
if
len
(
waveform
)
<
min_duration
*
sample_frequency
:
if
len
(
waveform
)
<
min_duration
*
sample_frequency
:
# signal is too short
# signal is too short
...
@@ -263,8 +264,7 @@ def mel_scale(freq):
...
@@ -263,8 +264,7 @@ def mel_scale(freq):
def
vtln_warp_freq
(
vtln_low_cutoff
,
vtln_high_cutoff
,
low_freq
,
high_freq
,
def
vtln_warp_freq
(
vtln_low_cutoff
,
vtln_high_cutoff
,
low_freq
,
high_freq
,
vtln_warp_factor
,
freq
):
vtln_warp_factor
,
freq
):
"""
r
"""This computes a VTLN warping function that is not the same as HTK's one,
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
but has similar inputs (this function has the advantage of never producing
empty bins).
empty bins).
...
@@ -289,15 +289,16 @@ def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
...
@@ -289,15 +289,16 @@ def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
frequency) is at l, then min(l, F(l)) == vtln_low_cutoff
frequency) is at l, then min(l, F(l)) == vtln_low_cutoff
This implies that l = vtln_low_cutoff / min(1, 1/vtln_warp_factor)
This implies that l = vtln_low_cutoff / min(1, 1/vtln_warp_factor)
= vtln_low_cutoff * max(1, vtln_warp_factor)
= vtln_low_cutoff * max(1, vtln_warp_factor)
Input
s:
Arg
s:
vtln_low_cutoff (float):
l
ower frequency cutoffs for VTLN
vtln_low_cutoff (float):
L
ower frequency cutoffs for VTLN
vtln_high_cutoff (float):
u
pper frequency cutoffs for VTLN
vtln_high_cutoff (float):
U
pper frequency cutoffs for VTLN
low_freq (float):
l
ower frequency cutoffs in mel computation
low_freq (float):
L
ower frequency cutoffs in mel computation
high_freq (float):
u
pper frequency cutoffs in mel computation
high_freq (float):
U
pper frequency cutoffs in mel computation
vtln_warp_factor (float): Vtln warp factor
vtln_warp_factor (float): Vtln warp factor
freq (Tensor): given frequency in Hz
freq (torch.Tensor): given frequency in Hz
Outputs:
Tensor: freq after vtln warp
Returns:
torch.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_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]'
assert
vtln_high_cutoff
<
high_freq
,
'be sure to set the vtln_high option lower than high_freq [or negative]'
...
@@ -332,16 +333,17 @@ def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
...
@@ -332,16 +333,17 @@ def vtln_warp_freq(vtln_low_cutoff, vtln_high_cutoff, low_freq, high_freq,
def
vtln_warp_mel_freq
(
vtln_low_cutoff
,
vtln_high_cutoff
,
low_freq
,
high_freq
,
def
vtln_warp_mel_freq
(
vtln_low_cutoff
,
vtln_high_cutoff
,
low_freq
,
high_freq
,
vtln_warp_factor
,
mel_freq
):
vtln_warp_factor
,
mel_freq
):
"""
r
"""
Input
s:
Arg
s:
vtln_low_cutoff (float):
l
ower frequency cutoffs for VTLN
vtln_low_cutoff (float):
L
ower frequency cutoffs for VTLN
vtln_high_cutoff (float):
u
pper frequency cutoffs for VTLN
vtln_high_cutoff (float):
U
pper frequency cutoffs for VTLN
low_freq (float):
l
ower frequency cutoffs in mel computation
low_freq (float):
L
ower frequency cutoffs in mel computation
high_freq (float):
u
pper frequency cutoffs in mel computation
high_freq (float):
U
pper frequency cutoffs in mel computation
vtln_warp_factor (float): Vtln warp factor
vtln_warp_factor (float): Vtln warp factor
mel_freq (Tensor): given frequency in Mel
mel_freq (torch.Tensor): Given frequency in Mel
Outputs:
Tensor: mel_freq after vtln warp
Returns:
torch.Tensor: `mel_freq` after vtln warp
"""
"""
return
mel_scale
(
vtln_warp_freq
(
vtln_low_cutoff
,
vtln_high_cutoff
,
low_freq
,
high_freq
,
return
mel_scale
(
vtln_warp_freq
(
vtln_low_cutoff
,
vtln_high_cutoff
,
low_freq
,
high_freq
,
vtln_warp_factor
,
inverse_mel_scale
(
mel_freq
)))
vtln_warp_factor
,
inverse_mel_scale
(
mel_freq
)))
...
@@ -351,9 +353,10 @@ def get_mel_banks(num_bins, window_length_padded, sample_freq,
...
@@ -351,9 +353,10 @@ def get_mel_banks(num_bins, window_length_padded, sample_freq,
low_freq
,
high_freq
,
vtln_low
,
vtln_high
,
vtln_warp_factor
):
low_freq
,
high_freq
,
vtln_low
,
vtln_high
,
vtln_warp_factor
):
# type: (int, int, float, float, float, float, float)
# type: (int, int, float, float, float, float, float)
"""
"""
Outputs:
Returns:
bins (Tensor): melbank of size (num_bins, num_fft_bins)
Tuple[torch.Tensor, torch.Tensor]: The tuple consists of `bins` (which is
center_freqs (Tensor): center frequencies of bins of size (num_bins)
Melbank of size (`num_bins`, `num_fft_bins`)) and `center_freqs` (which is
Center frequencies of bins of size (`num_bins`)).
"""
"""
assert
num_bins
>
3
,
'Must have at least 3 mel bins'
assert
num_bins
>
3
,
'Must have at least 3 mel bins'
assert
window_length_padded
%
2
==
0
assert
window_length_padded
%
2
==
0
...
@@ -416,59 +419,59 @@ def get_mel_banks(num_bins, window_length_padded, sample_freq,
...
@@ -416,59 +419,59 @@ def get_mel_banks(num_bins, window_length_padded, sample_freq,
def
fbank
(
def
fbank
(
sig
,
blackman_coeff
=
0.42
,
channel
=-
1
,
dither
=
1.0
,
energy_floor
=
0.0
,
waveform
,
blackman_coeff
=
0.42
,
channel
=-
1
,
dither
=
1.0
,
energy_floor
=
0.0
,
frame_length
=
25.0
,
frame_shift
=
10.0
,
high_freq
=
0.0
,
htk_compat
=
False
,
low_freq
=
20.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
,
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
,
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
,
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'
):
vtln_high
=-
500.0
,
vtln_low
=
100.0
,
vtln_warp
=
1.0
,
window_type
=
POVEY
):
r
"""Create a fbank from a raw audio signal. This matches the input/output of Kaldi's
r
"""Create a fbank from a raw audio signal. This matches the input/output of Kaldi's
compute-fbank-feats.
compute-fbank-feats.
Args:
Args:
sig (
Tensor): Tensor of audio of size (c, n) where c is in the range [0,2)
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. (
d
efault
=
0.42)
blackman_coeff (float): Constant coefficient for generalized Blackman window. (
D
efault
:
0.42)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (
d
efault
=
-1)
channel (int): Channel to extract (-1 -> expect mono, 0 -> left, 1 -> right) (
D
efault
:
-1)
dither (float): Dithering constant (0.0 means no dither). If you turn this off, you should set
dither (float): 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 (
d
efault
=
1.0)
the energy_floor option, e.g. to 1.0 or 0.1 (
D
efault
:
1.0)
energy_floor (float): Floor on energy (absolute, not relative) in Spectrogram computation. Caution:
energy_floor (float): 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
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(). (
d
efault
=
0.0)
individual spectrogram elements is fixed at std::numeric_limits<float>::epsilon(). (
D
efault
:
0.0)
frame_length (float): Frame length in milliseconds (
d
efault
=
25.0)
frame_length (float): Frame length in milliseconds (
D
efault
:
25.0)
frame_shift (float): Frame shift in milliseconds (
d
efault
=
10.0)
frame_shift (float): Frame shift in milliseconds (
D
efault
:
10.0)
high_freq (float): High cutoff frequency for mel bins (if <= 0, offset from Nyquist) (
d
efault
=
0.0)
high_freq (float): High cutoff frequency for mel bins (if <= 0, offset from Nyquist) (
D
efault
:
0.0)
htk_compat (bool): If true, put energy last. Warning: not sufficient to get HTK compatible features (need
htk_compat (bool): If true, put energy last. Warning: not sufficient to get HTK compatible features (need
to change other parameters). (
d
efault
=
False)
to change other parameters). (
D
efault
:
False)
low_freq (float): Low cutoff frequency for mel bins (
d
efault
=
20.0)
low_freq (float): Low cutoff frequency for mel bins (
D
efault
:
20.0)
min_duration (float): Minimum duration of segments to process (in seconds). (
d
efault
=
0.0)
min_duration (float): Minimum duration of segments to process (in seconds). (
D
efault
:
0.0)
num_mel_bins (int): Number of triangular mel-frequency bins (
d
efault
=
23)
num_mel_bins (int): Number of triangular mel-frequency bins (
D
efault
:
23)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (
d
efault
=
0.97)
preemphasis_coefficient (float): Coefficient for use in signal preemphasis (
D
efault
:
0.97)
raw_energy (bool): If True, compute energy before preemphasis and windowing (
d
efault
=
True)
raw_energy (bool): If True, compute energy before preemphasis and windowing (
D
efault
:
True)
remove_dc_offset: Subtract mean from waveform on each frame (
d
efault
=
True)
remove_dc_offset: Subtract mean from waveform on each frame (
D
efault
:
True)
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
round_to_power_of_two (bool): If True, round window size to power of two by zero-padding input
to FFT. (
d
efault
=
True)
to FFT. (
D
efault
:
True)
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
sample_frequency (float): Waveform data sample frequency (must match the waveform file, if
specified there) (
d
efault
=
16000.0)
specified there) (
D
efault
:
16000.0)
snip_edges (bool): If True, end effects will be handled by outputting only frames that completely fit
snip_edges (bool): 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
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. (
d
efault
=
True)
depends only on the frame_shift, and we reflect the data at the ends. (
D
efault
:
True)
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
subtract_mean (bool): Subtract mean of each feature file [CMS]; not recommended to do
it this way. (
d
efault
=
False)
it this way. (
D
efault
:
False)
use_energy (bool): Add an extra dimension with energy to the FBANK output. (
d
efault
=
False)
use_energy (bool): Add an extra dimension with energy to the FBANK output. (
D
efault
:
False)
use_log_fbank (bool):If true, produce log-filterbank, else produce linear. (
d
efault
=
True)
use_log_fbank (bool):If true, produce log-filterbank, else produce linear. (
D
efault
:
True)
use_power (bool): If true, use power, else use magnitude. (
d
efault
=
True)
use_power (bool): If true, use power, else use magnitude. (
D
efault
:
True)
vtln_high (float): High inflection point in piecewise linear VTLN warping function (if
vtln_high (float): High inflection point in piecewise linear VTLN warping function (if
negative, offset from high-mel-freq (
d
efault
=
-500.0)
negative, offset from high-mel-freq (
D
efault
:
-500.0)
vtln_low (float): Low inflection point in piecewise linear VTLN warping function (
float, d
efault
=
100.0)
vtln_low (float): Low inflection point in piecewise linear VTLN warping function (
D
efault
:
100.0)
vtln_warp (float): Vtln warp factor (only applicable if vtln_map not specified) (
float, d
efault
=
1.0)
vtln_warp (float): Vtln warp factor (only applicable if vtln_map not specified) (
D
efault
:
1.0)
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (
d
efault
=
'povey')
window_type (str): Type of window ('hamming'|'hanning'|'povey'|'rectangular'|'blackman') (
D
efault
:
'povey')
Returns:
Returns:
Tensor:
a
fbank identical to what Kaldi would output. The shape is (m, `num_mel_bins` + `use_energy`)
torch.
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
where m is calculated in _get_strided
"""
"""
waveform
,
window_shift
,
window_size
,
padded_window_size
=
_get_waveform_and_window_properties
(
waveform
,
window_shift
,
window_size
,
padded_window_size
=
_get_waveform_and_window_properties
(
sig
,
channel
,
sample_frequency
,
frame_shift
,
frame_length
,
round_to_power_of_two
,
preemphasis_coefficient
)
waveform
,
channel
,
sample_frequency
,
frame_shift
,
frame_length
,
round_to_power_of_two
,
preemphasis_coefficient
)
if
len
(
waveform
)
<
min_duration
*
sample_frequency
:
if
len
(
waveform
)
<
min_duration
*
sample_frequency
:
# signal is too short
# signal is too short
...
@@ -517,7 +520,7 @@ def fbank(
...
@@ -517,7 +520,7 @@ def fbank(
def
_get_LR_indices_and_weights
(
orig_freq
,
new_freq
,
output_samples_in_unit
,
window_width
,
def
_get_LR_indices_and_weights
(
orig_freq
,
new_freq
,
output_samples_in_unit
,
window_width
,
lowpass_cutoff
,
lowpass_filter_width
):
lowpass_cutoff
,
lowpass_filter_width
):
"""Based on LinearResample::SetIndexesAndWeights where it retrieves the weights for
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
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
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
frequency of `new_freq`). It uses sinc/bandlimited interpolation to upsample/downsample
...
@@ -548,20 +551,20 @@ def _get_LR_indices_and_weights(orig_freq, new_freq, output_samples_in_unit, win
...
@@ -548,20 +551,20 @@ def _get_LR_indices_and_weights(orig_freq, new_freq, output_samples_in_unit, win
[1] Chapter 16: Windowed-Sinc Filters, https://www.dspguide.com/ch16/1.htm
[1] Chapter 16: Windowed-Sinc Filters, https://www.dspguide.com/ch16/1.htm
Args:
Args:
orig_freq (float):
t
he original frequency of the signal
orig_freq (float):
T
he original frequency of the signal
new_freq (float):
t
he desired frequency
new_freq (float):
T
he desired frequency
output_samples_in_unit (int):
t
he number of output samples in the smallest repeating unit:
output_samples_in_unit (int):
T
he number of output samples in the smallest repeating unit:
num_samp_out = new_freq / Gcd(orig_freq, new_freq)
num_samp_out = new_freq / Gcd(orig_freq, new_freq)
window_width (float):
t
he width of the window which is nonzero
window_width (float):
T
he width of the window which is nonzero
lowpass_cutoff (float):
t
he filter cutoff in Hz. The filter cutoff needs to be less
lowpass_cutoff (float):
T
he filter cutoff in Hz. The filter cutoff needs to be less
than samp_rate_in_hz/2 and less than samp_rate_out_hz/2.
than samp_rate_in_hz/2 and less than samp_rate_out_hz/2.
lowpass_filter_width (int):
c
ontrols the sharpness of the filter, more == sharper but less
lowpass_filter_width (int):
C
ontrols the sharpness of the filter, more == sharper but less
efficient. We suggest around 4 to 10 for normal use
efficient. We suggest around 4 to 10 for normal use
Returns:
Returns:
min_input_index (
Tensor
)
:
the minimum indices where
the
w
in
dow is valid. size (output_samples_in_unit)
Tuple[torch.Tensor, torch.
Tensor
]
:
A tuple of `min_input_index` (which is
the
m
in
imum indices
w
eights (Tensor): the
weights
which
correspond with min_input_index. size (
w
here the window is valid, size (`output_samples_in_unit`)) and `
weights
` (
which
is the weights
output_samples_in_unit, max_weight_width
)
which correspond with min_input_index, size (`
output_samples_in_unit
`
,
`
max_weight_width
`)).
"""
"""
assert
lowpass_cutoff
<
min
(
orig_freq
,
new_freq
)
/
2
assert
lowpass_cutoff
<
min
(
orig_freq
,
new_freq
)
/
2
output_t
=
torch
.
arange
(
0
,
output_samples_in_unit
,
dtype
=
torch
.
get_default_dtype
())
/
new_freq
output_t
=
torch
.
arange
(
0
,
output_samples_in_unit
,
dtype
=
torch
.
get_default_dtype
())
/
new_freq
...
@@ -601,18 +604,18 @@ def _lcm(a, b):
...
@@ -601,18 +604,18 @@ def _lcm(a, b):
def
_get_num_LR_output_samples
(
input_num_samp
,
samp_rate_in
,
samp_rate_out
):
def
_get_num_LR_output_samples
(
input_num_samp
,
samp_rate_in
,
samp_rate_out
):
"""
Based on LinearResample::GetNumOutputSamples. LinearResample (LR) means that
r
"""Based on LinearResample::GetNumOutputSamples. LinearResample (LR) means that
the output signal is at linearly spaced intervals (i.e the output signal has a
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
frequency of `new_freq`). It uses sinc/bandlimited interpolation to upsample/downsample
the signal.
the signal.
Args:
Args:
input_num_samp (int):
t
he number of samples in the input
input_num_samp (int):
T
he number of samples in the input
samp_rate_in (float):
t
he original frequency of the signal
samp_rate_in (float):
T
he original frequency of the signal
samp_rate_out (float):
t
he desired frequency
samp_rate_out (float):
T
he desired frequency
Returns:
Returns:
int:
t
he number of output samples
int:
T
he number of output samples
"""
"""
# For exact computation, we measure time in "ticks" of 1.0 / tick_freq,
# For exact computation, we measure time in "ticks" of 1.0 / tick_freq,
# where tick_freq is the least common multiple of samp_rate_in and
# where tick_freq is the least common multiple of samp_rate_in and
...
@@ -644,8 +647,8 @@ def _get_num_LR_output_samples(input_num_samp, samp_rate_in, samp_rate_out):
...
@@ -644,8 +647,8 @@ def _get_num_LR_output_samples(input_num_samp, samp_rate_in, samp_rate_out):
return
num_output_samp
return
num_output_samp
def
resample_waveform
(
wave
,
orig_freq
,
new_freq
,
lowpass_filter_width
=
6
):
def
resample_waveform
(
wave
form
,
orig_freq
,
new_freq
,
lowpass_filter_width
=
6
):
r
"""Resamples the wave at the new frequency. This matches Kaldi's OfflineFeatureTpl ResampleWaveform
r
"""Resamples the wave
form
at the new frequency. This matches Kaldi's OfflineFeatureTpl ResampleWaveform
which uses a LinearResample (resample a signal at linearly spaced intervals to upsample/downsample
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
a signal). 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
the output signal has a frequency of `new_freq`). It uses sinc/bandlimited interpolation to
...
@@ -655,16 +658,16 @@ def resample_waveform(wave, orig_freq, new_freq, lowpass_filter_width=6):
...
@@ -655,16 +658,16 @@ def resample_waveform(wave, orig_freq, new_freq, lowpass_filter_width=6):
https://github.com/kaldi-asr/kaldi/blob/master/src/feat/resample.h#L56
https://github.com/kaldi-asr/kaldi/blob/master/src/feat/resample.h#L56
Args:
Args:
wave
(
Tensor):
t
he input signal of size (c, n)
wave
form (torch.
Tensor):
T
he input signal of size (c, n)
orig_freq (float):
t
he original frequency of the signal
orig_freq (float):
T
he original frequency of the signal
new_freq (float):
t
he desired frequency
new_freq (float):
T
he desired frequency
lowpass_filter_width (int):
c
ontrols the sharpness of the filter, more == sharper
lowpass_filter_width (int):
C
ontrols the sharpness of the filter, more == sharper
but less efficient. We suggest around 4 to 10 for normal use
but less efficient. We suggest around 4 to 10 for normal use
. (Default: 6)
Returns:
Returns:
Tensor:
t
he signal at the new frequency
torch.
Tensor:
T
he signal at the new frequency
"""
"""
assert
wave
.
dim
()
==
2
assert
wave
form
.
dim
()
==
2
assert
orig_freq
>
0.0
and
new_freq
>
0.0
assert
orig_freq
>
0.0
and
new_freq
>
0.0
min_freq
=
min
(
orig_freq
,
new_freq
)
min_freq
=
min
(
orig_freq
,
new_freq
)
...
@@ -687,13 +690,13 @@ def resample_waveform(wave, orig_freq, new_freq, lowpass_filter_width=6):
...
@@ -687,13 +690,13 @@ def resample_waveform(wave, orig_freq, new_freq, lowpass_filter_width=6):
# doing a conv1d for that specific weight.
# doing a conv1d for that specific weight.
conv_stride
=
input_samples_in_unit
conv_stride
=
input_samples_in_unit
conv_transpose_stride
=
output_samples_in_unit
conv_transpose_stride
=
output_samples_in_unit
num_channels
,
wave_len
=
wave
.
size
()
num_channels
,
wave_len
=
wave
form
.
size
()
window_size
=
weights
.
size
(
1
)
window_size
=
weights
.
size
(
1
)
tot_output_samp
=
_get_num_LR_output_samples
(
wave_len
,
orig_freq
,
new_freq
)
tot_output_samp
=
_get_num_LR_output_samples
(
wave_len
,
orig_freq
,
new_freq
)
output
=
torch
.
zeros
((
num_channels
,
tot_output_samp
))
output
=
torch
.
zeros
((
num_channels
,
tot_output_samp
))
eye
=
torch
.
eye
(
num_channels
).
unsqueeze
(
2
)
# size (num_channels, num_channels, 1)
eye
=
torch
.
eye
(
num_channels
).
unsqueeze
(
2
)
# size (num_channels, num_channels, 1)
for
i
in
range
(
first_indices
.
size
(
0
)):
for
i
in
range
(
first_indices
.
size
(
0
)):
wave_to_conv
=
wave
wave_to_conv
=
wave
form
first_index
=
int
(
first_indices
[
i
].
item
())
first_index
=
int
(
first_indices
[
i
].
item
())
if
first_index
>=
0
:
if
first_index
>=
0
:
# trim the signal as the filter will not be applied before the first_index
# trim the signal as the filter will not be applied before the first_index
...
...
torchaudio/transforms.py
View file @
2f62e573
...
@@ -275,7 +275,7 @@ class MuLawEncoding(torch.jit.ScriptModule):
...
@@ -275,7 +275,7 @@ class MuLawEncoding(torch.jit.ScriptModule):
returns a signal encoded with values from 0 to quantization_channels - 1
returns a signal encoded with values from 0 to quantization_channels - 1
Args:
Args:
quantization_channels (int): Number of channels
. d
efault: 256
quantization_channels (int): Number of channels
(D
efault: 256
)
"""
"""
__constants__
=
[
'quantization_channels'
]
__constants__
=
[
'quantization_channels'
]
...
@@ -303,7 +303,7 @@ class MuLawDecoding(torch.jit.ScriptModule):
...
@@ -303,7 +303,7 @@ class MuLawDecoding(torch.jit.ScriptModule):
and returns a signal scaled between -1 and 1.
and returns a signal scaled between -1 and 1.
Args:
Args:
quantization_channels (int): Number of channels
. d
efault: 256
quantization_channels (int): Number of channels
(D
efault: 256
)
"""
"""
__constants__
=
[
'quantization_channels'
]
__constants__
=
[
'quantization_channels'
]
...
@@ -328,10 +328,9 @@ class Resample(torch.nn.Module):
...
@@ -328,10 +328,9 @@ class Resample(torch.nn.Module):
be given.
be given.
Args:
Args:
orig_freq (float): the original frequency of the signal
orig_freq (float): The original frequency of the signal
new_freq (float): the desired frequency
new_freq (float): The desired frequency
resampling_method (str): the resampling method (Default: 'kaldi' which uses
resampling_method (str): The resampling method (Default: 'sinc_interpolation')
sinc interpolation)
"""
"""
def
__init__
(
self
,
orig_freq
,
new_freq
,
resampling_method
=
'sinc_interpolation'
):
def
__init__
(
self
,
orig_freq
,
new_freq
,
resampling_method
=
'sinc_interpolation'
):
super
(
Resample
,
self
).
__init__
()
super
(
Resample
,
self
).
__init__
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment