Skip to content

Commit

Permalink
Merge pull request #17 from sp-nitech/root_pol
Browse files Browse the repository at this point in the history
Add root_pol
  • Loading branch information
takenori-y committed Jan 31, 2023
2 parents 72fcde4 + 04a4862 commit e3c3ddf
Show file tree
Hide file tree
Showing 37 changed files with 305 additions and 106 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
max-parallel: 4
matrix:
python-version: [3.8]
pytorch-version: [1.10.0, 1.13.0]
pytorch-version: [1.10.0, 1.13.1]

steps:
- uses: actions/checkout@v3
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ diffsptk
[![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/0.5.0/)
[![Downloads](https://pepy.tech/badge/diffsptk)](https://pepy.tech/project/diffsptk)
[![Python Version](https://img.shields.io/pypi/pyversions/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
[![PyTorch Version](https://img.shields.io/badge/pytorch-1.10.0%20%7C%201.13.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
[![PyTorch Version](https://img.shields.io/badge/pytorch-1.10.0%20%7C%201.13.1-orange.svg)](https://pypi.python.org/pypi/diffsptk)
[![PyPI Version](https://img.shields.io/pypi/v/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
[![Codecov](https://codecov.io/gh/sp-nitech/diffsptk/branch/master/graph/badge.svg)](https://app.codecov.io/gh/sp-nitech/diffsptk)
[![License](https://img.shields.io/github/license/sp-nitech/diffsptk.svg)](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
Expand Down
1 change: 1 addition & 0 deletions diffsptk/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
from .quantize import UniformQuantization
from .rmse import RootMeanSquaredError
from .rmse import RootMeanSquaredError as RMSE
from .root_pol import DurandKernerMethod
from .smcep import SecondOrderAllPassMelCepstralAnalysis
from .snr import SignalToNoiseRatio
from .snr import SignalToNoiseRatio as SNR
Expand Down
6 changes: 3 additions & 3 deletions diffsptk/core/b2mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
import torch.nn as nn

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


class MLSADigitalFilterCoefficientsToMelCepstrum(nn.Module):
Expand All @@ -42,10 +42,10 @@ def __init__(self, cep_order, alpha):
assert abs(alpha) < 1

# Make transform matrix.
A = np.eye(cep_order + 1, dtype=default_dtype())
A = np.eye(cep_order + 1)
np.fill_diagonal(A[:, 1:], alpha)

self.register_buffer("A", torch.from_numpy(A).t())
self.register_buffer("A", numpy_to_torch(A.T))

def forward(self, b):
"""Convert MLSA filter coefficients to mel-cepstrum.
Expand Down
6 changes: 3 additions & 3 deletions diffsptk/core/c2ndps.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
import torch.nn as nn

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


class CepstrumToNegativeDerivativeOfPhaseSpectrum(nn.Module):
Expand All @@ -45,10 +45,10 @@ def __init__(self, cep_order, fft_length):
assert 2 <= self.fft_length
assert cep_order <= half_fft_length

ramp = np.arange(cep_order + 1, dtype=default_dtype()) * 0.5
ramp = np.arange(cep_order + 1) * 0.5
if cep_order == half_fft_length:
ramp[-1] *= 2
self.register_buffer("ramp", torch.from_numpy(ramp))
self.register_buffer("ramp", numpy_to_torch(ramp))

def forward(self, c):
"""Convert cepstrum to NDPS.
Expand Down
5 changes: 2 additions & 3 deletions diffsptk/core/cqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import torch
import torch.nn as nn

from ..misc.utils import default_complex_dtype
from ..misc.utils import next_power_of_two
from ..misc.utils import numpy_to_torch
from .frame import Frame


Expand Down Expand Up @@ -83,8 +83,7 @@ def __init__(

spectral_kernels = np.fft.fft(temporal_kernels, axis=-1) / fft_length
assert np.all(spectral_kernels.imag == 0)
spectral_kernels = spectral_kernels.astype(default_complex_dtype())
self.register_buffer("kernel", torch.from_numpy(spectral_kernels).t())
self.register_buffer("kernel", numpy_to_torch(spectral_kernels.T))

self.frame = Frame(fft_length, frame_period)

Expand Down
6 changes: 3 additions & 3 deletions diffsptk/core/dct.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import torch
import torch.nn as nn

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


def make_dct_matrix(L):
W = np.empty((L, L), dtype=default_dtype())
W = np.empty((L, L))
n = (np.arange(L) + 0.5) * (np.pi / L)
c = np.sqrt(2 / L)
for k in range(L):
Expand All @@ -48,7 +48,7 @@ def __init__(self, dct_length):
assert 1 <= dct_length

W = make_dct_matrix(dct_length)
self.register_buffer("W", torch.from_numpy(W))
self.register_buffer("W", numpy_to_torch(W))

def forward(self, x):
"""Apply DCT to input.
Expand Down
7 changes: 3 additions & 4 deletions diffsptk/core/delta.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
# ------------------------------------------------------------------------ #

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


def make_window(seed, static_out=True):
Expand Down Expand Up @@ -78,7 +77,7 @@ def make_window(seed, static_out=True):
if 3 <= len(seed):
raise NotImplementedError

window = np.asarray(window, dtype=default_dtype())
window = np.asarray(window)
return window


Expand All @@ -103,7 +102,7 @@ def __init__(self, seed=[[-0.5, 0, 0.5], [1, -2, 1]], static_out=True):
window = make_window(seed, static_out=static_out)
width = window.shape[-1]
window = np.reshape(window, (-1, 1, width, 1))
self.register_buffer("window", torch.from_numpy(window))
self.register_buffer("window", numpy_to_torch(window))

# Make padding module to be compatible with SPTK.
self.pad = nn.ReplicationPad2d((0, 0, (width - 1) // 2, (width - 1) // 2))
Expand Down
7 changes: 3 additions & 4 deletions diffsptk/core/dfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
# ------------------------------------------------------------------------ #

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


class InfiniteImpulseResponseDigitalFilter(nn.Module):
Expand Down Expand Up @@ -48,7 +47,7 @@ def __init__(self, b=[1], a=[1], impulse_response_length=None):
assert 1 <= impulse_response_length

d = np.zeros(max(len(b), len(a)))
h = np.empty(impulse_response_length, dtype=default_dtype())
h = np.empty(impulse_response_length)

a0 = a[0]
a1 = np.asarray(a[1:])
Expand All @@ -66,7 +65,7 @@ def __init__(self, b=[1], a=[1], impulse_response_length=None):
h[t] = y

h = h.reshape(1, 1, -1)
self.register_buffer("h", torch.from_numpy(h).flip(-1))
self.register_buffer("h", numpy_to_torch(h).flip(-1))

self.pad = nn.ConstantPad1d((impulse_response_length - 1, 0), 0)

Expand Down
9 changes: 4 additions & 5 deletions diffsptk/core/excite.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ def forward(self, p):
p[tmp_mask] = torch.roll(p, 1, dims=-1)[tmp_mask]

# Interpolate pitch.
d = p.dim()
if d == 2 or d == 3:
p = p.transpose(-1, -2)
if p.dim() != 1:
p = p.transpose(-2, -1)
p = self.linear_intpl(p)
if d == 2 or d == 3:
p = p.transpose(-1, -2)
if p.dim() != 1:
p = p.transpose(-2, -1)
p *= mask

# Compute phase.
Expand Down
6 changes: 3 additions & 3 deletions diffsptk/core/fbank.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
import torch.nn as nn

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


def hz_to_mel(x):
Expand Down Expand Up @@ -109,7 +109,7 @@ def __init__(
lower_channel_map = [np.argmax((freq >= m) > 0) for m in mel]

diff = freq - np.insert(freq[:-1], 0, mel_min)
weights = np.zeros((fft_length // 2 + 1, n_channel), dtype=default_dtype())
weights = np.zeros((fft_length // 2 + 1, n_channel))
for i, k in enumerate(seed):
m = lower_channel_map[i]
w = (freq[max(0, m)] - mel[i]) / diff[max(0, m)]
Expand All @@ -118,7 +118,7 @@ def __init__(
if m < n_channel:
weights[k, m] += 1 - w

self.register_buffer("H", torch.from_numpy(weights))
self.register_buffer("H", numpy_to_torch(weights))

def forward(self, x):
"""Apply mel-filter banks to STFT.
Expand Down
6 changes: 3 additions & 3 deletions diffsptk/core/freqt.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import torch
import torch.nn as nn

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


class FrequencyTransform(nn.Module):
Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self, in_order, out_order, alpha):
L2 = out_order + 1

# Make transform matrix.
A = np.zeros((L2, L1), dtype=default_dtype())
A = np.zeros((L2, L1))
A[0, :] = alpha ** np.arange(L1)
if 1 < L2 and 1 < L1:
A[1, 1:] = alpha ** np.arange(L1 - 1) * np.arange(1, L1) * beta
Expand All @@ -60,7 +60,7 @@ def __init__(self, in_order, out_order, alpha):
j1 = j - 1
A[i, j] = A[i1, j1] + alpha * (A[i, j1] - A[i1, j])

self.register_buffer("A", torch.from_numpy(A).t())
self.register_buffer("A", numpy_to_torch(A.T))

def forward(self, c1):
"""Perform frequency transform.
Expand Down
6 changes: 3 additions & 3 deletions diffsptk/core/grpdelay.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import torch.nn as nn
import torch.nn.functional as F

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


class GroupDelay(nn.Module):
Expand Down Expand Up @@ -50,8 +50,8 @@ def __init__(self, fft_length, alpha=1, gamma=1):
assert 0 < self.alpha
assert 0 < self.gamma

ramp = np.arange(self.fft_length, dtype=default_dtype())
self.register_buffer("ramp", torch.from_numpy(ramp))
ramp = np.arange(self.fft_length)
self.register_buffer("ramp", numpy_to_torch(ramp))

def forward(self, b, a=None):
"""Compute group delay.
Expand Down
5 changes: 3 additions & 2 deletions diffsptk/core/idct.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import torch
import torch.nn as nn

from ..misc.utils import numpy_to_torch
from .dct import make_dct_matrix


Expand All @@ -36,8 +37,8 @@ def __init__(self, dct_length):

assert 1 <= dct_length

W = make_dct_matrix(dct_length).T
self.register_buffer("W", torch.from_numpy(W))
W = make_dct_matrix(dct_length)
self.register_buffer("W", numpy_to_torch(W.T))

def forward(self, y):
"""Apply inverse DCT to input.
Expand Down
12 changes: 6 additions & 6 deletions diffsptk/core/ipqmf.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
# ------------------------------------------------------------------------ #

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

from ..misc.utils import numpy_to_torch
from .pqmf import make_filter_banks


Expand Down Expand Up @@ -50,7 +50,7 @@ def __init__(self, n_band, filter_order, alpha=100):
filters = make_filter_banks(n_band, filter_order, "synthesis", alpha=alpha)
filters = np.expand_dims(filters, 0)
filters = np.flip(filters, 2).copy()
self.register_buffer("filters", torch.from_numpy(filters))
self.register_buffer("filters", numpy_to_torch(filters))

# Make padding module.
if filter_order % 2 == 0:
Expand All @@ -64,15 +64,15 @@ def __init__(self, n_band, filter_order, alpha=100):
nn.ConstantPad1d((delay_left, 0), 0), nn.ReplicationPad1d((0, delay_right))
)

def forward(self, y, keep_dims=True):
def forward(self, y, keepdim=True):
"""Reconstruct waveform from subband waveforms.
Parameters
----------
y : Tensor [shape=(B, K, T) or (K, T)]
Subband waveforms.
keep_dims : bool [scalar]
keepdim : bool [scalar]
If True, the output shape is (B, 1, T) instead (B, T).
Returns
Expand All @@ -87,7 +87,7 @@ def forward(self, y, keep_dims=True):
tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000])
>>> pqmf = diffsptk.PQMF(2, 10)
>>> ipqmf = diffsptk.IPQMF(2, 10)
>>> x2 = ipqmf(pmqf(x), keep_dims=False)
>>> x2 = ipqmf(pmqf(x), keepdim=False)
>>> x2
tensor([[[8.1887e-04, 2.4754e-01, 5.0066e-01, 7.4732e-01, 9.9419e-01]]])
Expand All @@ -97,6 +97,6 @@ def forward(self, y, keep_dims=True):
assert y.dim() == 3, "Input must be 3D tensor"

x = F.conv1d(self.pad(y), self.filters)
if not keep_dims:
if not keepdim:
x = x.squeeze(1)
return x
2 changes: 1 addition & 1 deletion diffsptk/core/iulaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@ def forward(self, y):
tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000])
"""
y2 = torch.abs(y) / self.abs_max
y2 = y.abs() / self.abs_max
x = self.const * torch.sign(y) * (torch.pow(1 + self.mu, y2) - 1)
return x
7 changes: 3 additions & 4 deletions diffsptk/core/linear_intpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
# ------------------------------------------------------------------------ #

import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F

from ..misc.utils import default_dtype
from ..misc.utils import numpy_to_torch


class LinearInterpolation(nn.Module):
Expand All @@ -41,12 +40,12 @@ def __init__(self, upsampling_factor):
assert 1 <= upsampling_factor

# Make upsampling filter.
w = np.linspace(1, 0, upsampling_factor + 1, dtype=default_dtype())[:-1]
w = np.linspace(1, 0, upsampling_factor + 1)[:-1]
upsampling_filter = np.stack((w, 1 - w), axis=1)
upsampling_filter = np.expand_dims(
upsampling_filter, (1, 3)
) # (Out, In, Height, Width)
self.register_buffer("upsampling_filter", torch.from_numpy(upsampling_filter))
self.register_buffer("upsampling_filter", numpy_to_torch(upsampling_filter))

# Make padding module.
self.pad = nn.ReplicationPad2d((0, 0, 0, 1))
Expand Down
Loading

0 comments on commit e3c3ddf

Please sign in to comment.