Skip to content

Commit

Permalink
Merge pull request #36 from sp-nitech/update_mglsadf
Browse files Browse the repository at this point in the history
Update mglsadf
  • Loading branch information
takenori-y committed Apr 19, 2023
2 parents e0ab310 + cd0165b commit 367fbc5
Show file tree
Hide file tree
Showing 29 changed files with 467 additions and 213 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# project
*.egg-info/
build/
dist/
venv/

Expand All @@ -13,6 +11,7 @@ _build/

# tools
tools/SPTK/
tools/toml/

# misc
__pycache__/
Expand Down
35 changes: 20 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,16 @@ init:
pip install -e .

dev:
@if type virtualenv > /dev/null 2>&1; then \
test -d venv || virtualenv -p python$(PYTHON_VERSION) venv; \
else \
test -d venv || python$(PYTHON_VERSION) -m venv venv; \
fi
test -d venv || python$(PYTHON_VERSION) -m venv venv; \
. ./venv/bin/activate; pip install pip --upgrade; \
pip install torch==1.10.0+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html; \
pip install -e .[dev]

dist:
./venv/bin/python setup.py bdist_wheel
./venv/bin/twine check dist/*
. ./venv/bin/activate; python -m build --wheel; \
twine check dist/*

dist-clean:
@if [ -f ./venv/bin/python ]; then \
./venv/bin/python setup.py clean --all; \
fi
rm -rf dist

doc:
Expand Down Expand Up @@ -72,21 +65,33 @@ test:
. ./venv/bin/activate; export PATH=tools/SPTK/bin:$$PATH; \
python -m pytest -s --cov=./ --cov-report=xml $$module

test-clean:
rm -rf tests/__pycache__
rm -rf *.wav

tool:
cd tools; make

tool-clean:
cd tools; make clean

update:
@if [ ! -x tools/toml/toml ]; then \
echo ""; \
echo "Error: please install toml-cli"; \
echo ""; \
echo " make tool"; \
echo ""; \
exit 1; \
fi
./venv/bin/python -m pip install --upgrade pip
@for package in $$(cat setup.py | grep " " | sed "s/\s//g" | \
sed 's/"//g' | tr ",\n" " "); do \
@for package in $$(./tools/toml/toml get pyproject.toml project.optional-dependencies.dev | \
sed 's/"//g' | tr -d '[]' | tr , ' '); do \
./venv/bin/pip install --upgrade $$package; \
done

clean: dist-clean doc-clean tool-clean
rm -rf *.egg-info venv
clean: dist-clean doc-clean test-clean tool-clean
rm -rf venv
find . -name "__pycache__" -type d | xargs rm -rf

.PHONY: init dev dist dist-clean doc doc-clean check format test tool tool-clean update clean
.PHONY: init dev dist dist-clean doc doc-clean check format test test-clean tool tool-clean update clean
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mcep = diffsptk.MelCepstralAnalysis(cep_order=M, fft_length=n_fft, alpha=alpha,
mc = mcep(X)

# Reconstruct x.
mlsa = diffsptk.MLSA(filter_order=M, alpha=alpha, frame_period=fp, taylor_order=30)
mlsa = diffsptk.MLSA(filter_order=M, frame_period=fp, alpha=alpha, taylor_order=30)
x_hat = mlsa(mlsa(x, -mc), mc)

# Write reconstructed waveform.
Expand Down
13 changes: 13 additions & 0 deletions diffsptk/core/dct.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@


def make_dct_matrix(L):
"""Make DCT matrix.
Parameters
----------
L : int >= 1 [scalar]
DCT length, :math:`L`.
Returns
-------
W : ndarray [shape=(L, L)]
DCT matrix.
"""
W = np.empty((L, L))
n = (np.arange(L) + 0.5) * (np.pi / L)
c = np.sqrt(2 / L)
Expand Down
17 changes: 17 additions & 0 deletions diffsptk/core/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,23 @@


def make_window(seed, static_out=True):
"""Make window for delta computation.
Parameters
----------
seed : list[list[float]] or list[int]
Delta coefficients or width(s) of 1st (and 2nd) regression coefficients.
static_out : bool [scalar]
If True, make window for static component.
Returns
-------
window : ndarray [shape=(D+1, W)]
Window for delta computation, where D is the number of dynamic components and
W is the width of window.
"""
if not isinstance(seed, (tuple, list)):
raise ValueError("seed must be tuple or list")

Expand Down
3 changes: 1 addition & 2 deletions diffsptk/core/fbank.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ def hz_to_mel(x):


def sample_mel(n, fft_length, sample_rate):
hz = sample_rate * n / fft_length
return hz_to_mel(hz)
return hz_to_mel(sample_rate * n / fft_length)


class MelFilterBankAnalysis(nn.Module):
Expand Down
22 changes: 21 additions & 1 deletion diffsptk/core/freqt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,33 @@


def warp(omega, alpha, theta):
"""Warp frequency.
Parameters
----------
omega : float [0 <= omega <= 2pi]
Frequency.
alpha : float [-1 < alpha < 1]
Frequency warping factor, :math:`\\alpha`.
theta : float [0 <= theta <= 1]
Emphasis frequency, :math:`\\theta`.
Returns
-------
w : float
Warped frequency.
"""
x = omega - theta
y = omega + theta
return (
w = (
omega
+ np.arctan2(alpha * np.sin(x), 1 - alpha * np.cos(x))
+ np.arctan2(alpha * np.sin(y), 1 - alpha * np.cos(y))
)
return w


class SecondOrderAllPassFrequencyTransform(nn.Module):
Expand Down
55 changes: 11 additions & 44 deletions diffsptk/core/imglsadf.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,20 @@


class PseudoInverseMGLSADigitalFilter(nn.Module):
"""See `this page <https://sp-nitech.github.io/sptk/latest/main/imglsadf.html>`_
for details.
"""See :func:`~diffsptk.PseudoMGLSADigitalFilter` for details."""

Parameters
----------
filter_order : int >= 0 [scalar]
Order of filter coefficients, :math:`M`.
alpha : float [-1 < alpha < 1]
Frequency warping factor, :math:`\\alpha`.
gamma : float [-1 <= gamma <= 1]
Gamma, :math:`\\gamma`.
c : int >= 1 [scalar]
Number of stages.
frame_period : int >= 1 [scalar]
Frame period, :math:`P`.
ignore_gain : bool [scalar]
If True, perform filtering without gain.
phase : ['minimum', 'maximum', 'zero']
Filter type.
cascade : bool [scalar]
If True, use multi-stage FIR filter.
taylor_order : int >= 0 [scalar]
Order of Taylor series expansion (valid only if **cascade** is True).
ir_length : int >= 1 [scalar]
Length of impulse response (valid only if **cascade** is False).
n_fft : int >= 1 [scalar]
Number of FFT bins for conversion (valid only if **cascade** is False).
cep_order : int >= 0 [scalar]
Order of linear cepstrum (used to convert input to cepstrum).
"""

def __init__(self, filter_order, **kwargs):
def __init__(self, filter_order, frame_period, **kwargs):
super(PseudoInverseMGLSADigitalFilter, self).__init__()

self.mglsadf = PseudoMGLSADigitalFilter(filter_order, **kwargs)
# Change the default value of the order of Taylor series.
# This is because inverse filtering requires the large value.
if (
kwargs.get("mode", "multi-stage") == "multi-stage"
and "taylor_order" not in kwargs
):
kwargs["taylor_order"] = 40

self.mglsadf = PseudoMGLSADigitalFilter(filter_order, frame_period, **kwargs)

def forward(self, y, mc):
"""Apply an inverse MGLSA digital filter.
Expand Down
2 changes: 1 addition & 1 deletion diffsptk/core/istft.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


class InverseShortTermFourierTransform(nn.Module):
"""This is the opposite module to ShortTermFourierTransform.
"""This is the opposite module to :func:`~diffsptk.ShortTermFourierTransform`
Parameters
----------
Expand Down
36 changes: 35 additions & 1 deletion diffsptk/core/mgc2mgc.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,28 @@


class GeneralizedCepstrumToGeneralizedCepstrum(nn.Module):
def __init__(self, in_order, out_order, in_gamma, out_gamma, n_fft):
"""Generalized cepstral transformation module.
Parameters
----------
in_order : int >= 0 [scalar]
Order of input cepstrum, :math:`M_1`.
out_order : int >= 0 [scalar]
Order of output cepstrum, :math:`M_2`.
in_gamma : float [-1 <= in_gamma <= 1]
Input gamma, :math:`\\gamma_1`.
out_gamma : float [-1 <= out_gamma <= 1]
Output gamma, :math:`\\gamma_2`.
n_fft : int >> :math:`M_1, M_2` [scalar]
Number of FFT bins. Accurate conversion requires the large value.
"""

def __init__(self, in_order, out_order, in_gamma, out_gamma, n_fft=512):
super(GeneralizedCepstrumToGeneralizedCepstrum, self).__init__()

self.in_order = in_order
Expand All @@ -45,6 +66,19 @@ def __init__(self, in_order, out_order, in_gamma, out_gamma, n_fft):
assert max(self.in_order, self.out_order) + 1 < self.n_fft

def forward(self, c1):
"""Perform generalized cepstral transformation.
Parameters
----------
c1 : Tensor [shape=(..., M1+1)]
Input cepstrum.
Returns
-------
c2 : Tensor [shape=(..., M2+1)]
Output cepstrum.
"""
c01 = torch.cat((c1[..., :1] * 0, c1[..., 1:]), dim=-1)
C1 = torch.fft.fft(c01, n=self.n_fft)

Expand Down
4 changes: 3 additions & 1 deletion diffsptk/core/mgc2sp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MelGeneralizedCepstrumToSpectrum(nn.Module):
If True, assume gamma-multiplied cepstrum.
out_format : ['db', 'log-magnitude', 'magnitude', 'power', \
'cycle', 'radian', 'degree']
'cycle', 'radian', 'degree', 'complex']
Output format.
n_fft : int >> :math:`L` [scalar]
Expand Down Expand Up @@ -88,6 +88,8 @@ def __init__(
elif out_format == 6 or out_format == "degree":
c = 180 / math.pi
self.convert = lambda x: x.imag * c
elif out_format == "complex":
self.convert = lambda x: torch.polar(torch.exp(x.real), x.imag)
else:
raise ValueError(f"out_format {out_format} is not supported")

Expand Down
Loading

0 comments on commit 367fbc5

Please sign in to comment.