Skip to content

Commit 3bd430b

Browse files
authored
Merge pull request #154 from sp-nitech/minor_fix [skip ci]
Minor fix
2 parents 3b7e829 + 44cfcd6 commit 3bd430b

12 files changed

Lines changed: 61 additions & 54 deletions

File tree

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ jobs:
2121
torch: 2.3.1
2222
torchaudio: 2.3.1
2323
- python: "3.13"
24-
torch: 2.9.0
25-
torchaudio: 2.9.0
24+
torch: 2.9.1
25+
torchaudio: 2.9.1
2626

2727
steps:
2828
- name: Clone
29-
uses: actions/checkout@v5
29+
uses: actions/checkout@v6
3030

3131
- name: Python
3232
uses: actions/setup-python@v6

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![ClickPy](https://img.shields.io/badge/downloads-clickpy-yellow.svg)](https://clickpy.clickhouse.com/dashboard/diffsptk)
88
[![Advisor](https://snyk.io/advisor/python/diffsptk/badge.svg)](https://snyk.io/advisor/python/diffsptk)
99
[![Python Version](https://img.shields.io/pypi/pyversions/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
10-
[![PyTorch Version](https://img.shields.io/badge/pytorch-2.3.1%20%7C%202.9.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
10+
[![PyTorch Version](https://img.shields.io/badge/pytorch-2.3.1%20%7C%202.9.1-orange.svg)](https://pypi.python.org/pypi/diffsptk)
1111
[![PyPI Version](https://img.shields.io/pypi/v/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
1212
[![Codecov](https://codecov.io/gh/sp-nitech/diffsptk/branch/master/graph/badge.svg)](https://app.codecov.io/gh/sp-nitech/diffsptk)
1313
[![License](https://img.shields.io/github/license/sp-nitech/diffsptk.svg)](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)

diffsptk/functional.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def excite(
622622
voiced_region: str = "pulse",
623623
unvoiced_region: str = "gauss",
624624
polarity: str = "auto",
625-
init_phase: str = "zeros",
625+
init_phase: str | float = "zeros",
626626
) -> Tensor:
627627
"""Generate a simple excitation signal.
628628
@@ -638,14 +638,14 @@ def excite(
638638
'triangle', 'square']
639639
The type of voiced region.
640640
641-
unvoiced_region : ['zeros', 'gauss', 'uniform']
641+
unvoiced_region : ['zeros', 'gauss', 'm-sequence', 'uniform']
642642
The type of unvoiced region.
643643
644644
polarity : ['auto', 'unipolar', 'bipolar']
645645
The polarity.
646646
647-
init_phase : ['zeros', 'random']
648-
The initial phase.
647+
init_phase : ['zeros', 'random'] or float
648+
The initial phase in radians.
649649
650650
Returns
651651
-------
@@ -1692,7 +1692,8 @@ def levdur(r: Tensor, eps: float | None = None) -> Tensor:
16921692
The autocorrelation.
16931693
16941694
eps : float >= 0 or None
1695-
A small value to improve numerical stability.
1695+
A small value to improve numerical stability. If None, automatically set
1696+
based on the input data type.
16961697
16971698
Returns
16981699
-------
@@ -1735,7 +1736,8 @@ def lpc(x: Tensor, lpc_order: int, eps: float | None = None) -> Tensor:
17351736
The order of the LPC coefficients, :math:`M`.
17361737
17371738
eps : float >= 0 or None
1738-
A small value to improve numerical stability.
1739+
A small value to improve numerical stability. If None, automatically set
1740+
based on the input data type.
17391741
17401742
Returns
17411743
-------
@@ -2742,21 +2744,24 @@ def quantize(
27422744
)
27432745

27442746

2745-
def rlevdur(a: Tensor) -> Tensor:
2747+
def rlevdur(a: Tensor, n_fft: int = 1024) -> Tensor:
27462748
"""Solve a Yule-Walker linear system given the LPC coefficients.
27472749
27482750
Parameters
27492751
----------
27502752
a : Tensor [shape=(..., M+1)]
27512753
The gain and the LPC coefficients.
27522754
2755+
n_fft : int >> M
2756+
The number of FFT bins. Accurate conversion requires a large value.
2757+
27532758
Returns
27542759
-------
27552760
out : Tensor [shape=(..., M+1)]
27562761
The autocorrelation.
27572762
27582763
"""
2759-
return nn.ReverseLevinsonDurbin._func(a)
2764+
return nn.ReverseLevinsonDurbin._func(a, n_fft=n_fft)
27602765

27612766

27622767
def rmse(x: Tensor, y: Tensor, reduction: str = "mean") -> Tensor:

diffsptk/modules/excite.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class ExcitationGeneration(BaseFunctionalModule):
4545
polarity : ['auto', 'unipolar', 'bipolar']
4646
The polarity.
4747
48-
init_phase : ['zeros', 'random']
49-
The initial phase.
48+
init_phase : ['zeros', 'random'] or float
49+
The initial phase in radians.
5050
5151
"""
5252

@@ -57,7 +57,7 @@ def __init__(
5757
voiced_region: str = "pulse",
5858
unvoiced_region: str = "gauss",
5959
polarity: str = "auto",
60-
init_phase: str = "zeros",
60+
init_phase: str | float = "zeros",
6161
) -> None:
6262
super().__init__()
6363

@@ -108,7 +108,7 @@ def _precompute(
108108
voiced_region: str,
109109
unvoiced_region: str,
110110
polarity: str,
111-
init_phase: str,
111+
init_phase: str | float,
112112
) -> Precomputed:
113113
ExcitationGeneration._check(frame_period)
114114
return (frame_period, voiced_region, unvoiced_region, polarity, init_phase)
@@ -121,7 +121,7 @@ def _forward(
121121
voiced_region: str,
122122
unvoiced_region: str,
123123
polarity: str,
124-
init_phase: str,
124+
init_phase: str | float,
125125
) -> torch.Tensor:
126126
# Make mask represents voiced region.
127127
base_mask = torch.clip(p, min=0, max=1)
@@ -148,12 +148,15 @@ def _forward(
148148
s = torch.cumsum(q.double(), dim=-1)
149149
bias, _ = torch.cummax(s * ~mask, dim=-1)
150150
phase = (s - bias).to(p.dtype)
151-
if init_phase == "zeros":
152-
pass
153-
elif init_phase == "random":
154-
phase += torch.rand_like(p[..., :1])
151+
if isinstance(init_phase, str):
152+
if init_phase == "zeros":
153+
pass
154+
elif init_phase == "random":
155+
phase += torch.rand_like(p[..., :1])
156+
else:
157+
raise ValueError(f"init_phase {init_phase} is not supported.")
155158
else:
156-
raise ValueError(f"init_phase {init_phase} is not supported.")
159+
phase += init_phase / TAU
157160

158161
# Generate excitation signal using phase.
159162
if polarity == "auto":

diffsptk/modules/gmm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ def forward(
346346
nu = px / y.view(-1, 1)
347347
nm = nu * self.mu
348348
a = pxx - y.view(-1, 1) * (2 * nm - mm)
349+
a = torch.nan_to_num(a, nan=0.0, posinf=0.0, neginf=0.0)
349350
b = xi.view(-1, 1) * self.ubm_sigma.diagonal(dim1=-2, dim2=-1)
350351
c = xi.view(-1, 1) * (self.ubm_mu - self.mu) ** 2
351352
sigma = (a + b + c) * z.view(-1, 1)
@@ -368,6 +369,7 @@ def forward(
368369
nm = outer(nu, self.mu)
369370
mn = nm.transpose(-2, -1)
370371
a = pxx - y.view(-1, 1, 1) * (nm + mn - mm)
372+
a = torch.nan_to_num(a, nan=0.0, posinf=0.0, neginf=0.0)
371373
b = xi.view(-1, 1, 1) * self.ubm_sigma
372374
c = xi.view(-1, 1, 1) * outer(self.ubm_mu - self.mu)
373375
sigma = (a + b + c) * z.view(-1, 1, 1)

diffsptk/modules/levdur.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class LevinsonDurbin(BaseFunctionalModule):
3030
lpc_order : int >= 0
3131
The order of the LPC coefficients, :math:`M`.
3232
33-
eps : float >= 0
34-
A small value to improve numerical stability.
33+
eps : float >= 0 or None
34+
A small value to improve numerical stability. If None, automatically set
35+
based on the data type.
3536
3637
device : torch.device or None
3738
The device of this module.

diffsptk/modules/lpc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ class LinearPredictiveCodingAnalysis(BaseFunctionalModule):
3939
The order of the LPC coefficients, :math:`M`.
4040
4141
eps : float >= 0 or None
42-
A small value to improve numerical stability.
42+
A small value to improve numerical stability. If None, automatically set
43+
based on the data type.
4344
4445
device : torch.device or None
4546
The device of this module.

diffsptk/modules/mgc2mgc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ def _check(
169169
if 1 < abs(out_gamma):
170170
raise ValueError("out_gamma must be in [-1, 1].")
171171
if n_fft <= max(in_order, out_order) + 1:
172-
raise ValueError("n_fft must be much larger then order of cepstrum.")
172+
raise ValueError("n_fft must be much larger than order of cepstrum.")
173173
if 0 == in_gamma and in_mul:
174174
raise ValueError("Invalid combination of in_gamma and in_mul.")
175175

diffsptk/modules/rlevdur.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
# limitations under the License. #
1515
# ------------------------------------------------------------------------ #
1616

17+
import numpy as np
1718
import torch
18-
import torch.nn.functional as F
1919

2020
from ..typing import Precomputed
21-
from ..utils.private import check_size, filter_values, remove_gain
21+
from ..utils.private import check_size, filter_values, remove_gain, to
2222
from .base import BaseFunctionalModule
2323

2424

@@ -31,6 +31,9 @@ class ReverseLevinsonDurbin(BaseFunctionalModule):
3131
lpc_order : int >= 0
3232
The order of the LPC coefficients, :math:`M`.
3333
34+
n_fft : int >> M
35+
The number of FFT bins. Accurate conversion requires a large value.
36+
3437
device : torch.device or None
3538
The device of this module.
3639
@@ -42,6 +45,7 @@ class ReverseLevinsonDurbin(BaseFunctionalModule):
4245
def __init__(
4346
self,
4447
lpc_order: int,
48+
n_fft: int = 1024,
4549
device: torch.device | None = None,
4650
dtype: torch.dtype | None = None,
4751
) -> None:
@@ -50,7 +54,7 @@ def __init__(
5054
self.in_dim = lpc_order + 1
5155

5256
_, _, tensors = self._precompute(**filter_values(locals()))
53-
self.register_buffer("eye", tensors[0])
57+
self.register_buffer("phase_factors", tensors[0])
5458

5559
def forward(self, a: torch.Tensor) -> torch.Tensor:
5660
"""Solve a Yule-Walker linear system given the LPC coefficients.
@@ -95,39 +99,30 @@ def _takes_input_size() -> bool:
9599
return True
96100

97101
@staticmethod
98-
def _check(lpc_order: int) -> None:
102+
def _check(lpc_order: int, n_fft: int) -> None:
99103
if lpc_order < 0:
100104
raise ValueError("lpc_order must be non-negative.")
105+
if n_fft <= lpc_order + 1:
106+
raise ValueError("n_fft must be much larger than lpc_order.")
101107

102108
@staticmethod
103109
def _precompute(
104110
lpc_order: int,
111+
n_fft: int,
105112
device: torch.device | None,
106113
dtype: torch.dtype | None,
107114
) -> Precomputed:
108-
ReverseLevinsonDurbin._check(lpc_order)
109-
eye = torch.eye(lpc_order + 1, device=device, dtype=dtype)
110-
return None, None, (eye,)
115+
ReverseLevinsonDurbin._check(lpc_order, n_fft)
116+
n_freq = n_fft // 2 + 1
117+
omega = torch.linspace(0, np.pi, n_freq, device=device, dtype=torch.double)
118+
m = torch.arange(lpc_order + 1, device=device, dtype=torch.double)
119+
phase_factors = torch.exp(-1j * omega * m.unsqueeze(-1))
120+
return None, None, (to(phase_factors, dtype=dtype),)
111121

112122
@staticmethod
113-
def _forward(a: torch.Tensor, eye: torch.Tensor) -> torch.Tensor:
123+
def _forward(a: torch.Tensor, phase_factors: torch.Tensor) -> torch.Tensor:
114124
M = a.size(-1) - 1
115125
K, a = remove_gain(a, return_gain=True)
116-
117-
U = [a.flip(-1)]
118-
E = [K**2]
119-
for m in range(M):
120-
u0 = U[-1][..., :1]
121-
u1 = U[-1][..., 1 : M - m]
122-
t = 1 / (1 - u0**2)
123-
u = (u1 - u0 * u1.flip(-1)) * t
124-
u = F.pad(u, (0, m + 2))
125-
e = E[-1] * t
126-
U.append(u)
127-
E.append(e)
128-
U = torch.stack(U[::-1], dim=-1)
129-
E = torch.stack(E[::-1], dim=-1)
130-
131-
V = torch.linalg.solve_triangular(U, eye, upper=True, unitriangular=True)
132-
r = torch.matmul(V[..., :1].transpose(-2, -1) * E, V).squeeze(-2)
126+
A = torch.sum(a.unsqueeze(-1) * phase_factors, dim=-2)
127+
r = torch.fft.irfft((K / A.abs()) ** 2)[..., : M + 1]
133128
return r

diffsptk/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "3.4.0"
1+
__version__ = "3.4.1.dev0"

0 commit comments

Comments
 (0)