diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0f62341..99c11f2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,15 +21,15 @@ jobs: torch: 2.3.1 torchaudio: 2.3.1 - python: "3.13" - torch: 2.8.0 - torchaudio: 2.8.0 + torch: 2.9.0 + torchaudio: 2.9.0 steps: - name: Clone - uses: actions/checkout@v4 + uses: actions/checkout@v5 - name: Python - uses: actions/setup-python@v5 + uses: actions/setup-python@v6 with: python-version: ${{ matrix.python }} architecture: x64 @@ -46,7 +46,7 @@ jobs: run: make check - name: Test - run: make test OPT="--dtype double" + run: make test-all OPT="--dtype double" - name: Coverage uses: codecov/codecov-action@v5 diff --git a/Makefile b/Makefile index ad5ac95..39f73fb 100644 --- a/Makefile +++ b/Makefile @@ -61,11 +61,18 @@ format: tool ./tools/taplo/taplo fmt *.toml ./tools/yamlfmt/yamlfmt *.cff *.yml .github/workflows/*.yml +test-all: test-example test + test: tool - [ -n "$(MODULE)" ] && module=tests/test_$(MODULE).py || module=; \ + [ -n "$(MODULE)" ] && module="--no-cov tests/test_$(MODULE).py" || module=; \ . .venv/bin/activate && export PATH=./tools/SPTK/bin:$$PATH NUMBA_CUDA_LOW_OCCUPANCY_WARNINGS=0 PYLSTRAIGHT_DEBUG=1 && \ python -m pytest $$module $(OPT) +test-example: tool + [ -n "$(MODULE)" ] && module=modules/$(MODULE).py || module=; \ + . .venv/bin/activate && export NUMBA_CUDA_LOW_OCCUPANCY_WARNINGS=0 && \ + python -m pytest --doctest-modules --no-cov --ignore=diffsptk/third_party diffsptk/$$module + test-clean: rm -rf tests/__pycache__ rm -rf *.png *.wav diff --git a/README.md b/README.md index e06c756..c3f9e6e 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,12 @@ *diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework. -[![Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/3.3.1/) +[![Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/3.4.0/) [![Downloads](https://static.pepy.tech/badge/diffsptk)](https://pepy.tech/project/diffsptk) [![ClickPy](https://img.shields.io/badge/downloads-clickpy-yellow.svg)](https://clickpy.clickhouse.com/dashboard/diffsptk) +[![Advisor](https://snyk.io/advisor/python/diffsptk/badge.svg)](https://snyk.io/advisor/python/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-2.3.1%20%7C%202.8.0-orange.svg)](https://pypi.python.org/pypi/diffsptk) +[![PyTorch Version](https://img.shields.io/badge/pytorch-2.3.1%20%7C%202.9.0-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) @@ -20,7 +21,7 @@ ## Documentation -- Online [documentation](https://sp-nitech.github.io/diffsptk/3.3.1/) for the reference manual +- Online [documentation](https://sp-nitech.github.io/diffsptk/3.4.0/) for the reference manual - Conference [paper](https://www.isca-archive.org/ssw_2023/yoshimura23_ssw.html) on the ISCA Archive - Hands-on [tutorial](https://colab.research.google.com/drive/1xAoUKqXadvJXJ7RzN0OceB6y7q5i7Sn6?usp=drive_link) on Google Colab diff --git a/diffsptk/functional.py b/diffsptk/functional.py index 8e1d7ad..837f310 100644 --- a/diffsptk/functional.py +++ b/diffsptk/functional.py @@ -444,11 +444,12 @@ def dht(x: Tensor, dht_type: int = 2) -> Tensor: def drc( x: Tensor, + *, + sample_rate: int, threshold: float, ratio: float, attack_time: float, release_time: float, - sample_rate: int, makeup_gain: float = 0, abs_max: float = 1, ) -> Tensor: @@ -459,6 +460,9 @@ def drc( x : Tensor [shape=(..., T)] The input waveform. + sample_rate : int >= 1 + The sample rate in Hz. + threshold : float <= 0 The threshold in dB. @@ -471,9 +475,6 @@ def drc( release_time : float > 0 The release time in msec. - sample_rate : int >= 1 - The sample rate in Hz. - makeup_gain : float >= 0 The make-up gain in dB. @@ -571,36 +572,27 @@ def dtw( ) -def dtw_merge(x: Tensor, y: Tensor, indices: Tensor) -> tuple[Tensor, Tensor]: - """Align two vector sequences according to the given path. +def dtw_merge(x: Tensor, y: Tensor, indices: Tensor) -> Tensor: + """Merge two sequences according to the given indices. Parameters ---------- - x : Tensor [shape=(T1, ...)] + x : Tensor [shape=(T1, D) or (T1,)] The query vector sequence. - y : Tensor [shape=(T2, ...)] + y : Tensor [shape=(T2, D) or (T2,)] The reference vector sequence. indices : Tensor [shape=(T, 2)] - The indices of the path. + The indices of the viterbi path. Returns ------- - x_align : Tensor [shape=(T, ...)] - The aligned query vector sequence. - - y_align : Tensor [shape=(T, ...)] - The aligned reference vector sequence. + z : Tensor [shape=(T, 2D) or (T, 2)] + The merged vector sequence. """ - if x.dim() != y.dim(): - raise ValueError("x and y must have the same number of dimensions.") - if indices.dim() != 2 or indices.size(-1) != 2: - raise ValueError("The shape of indices must be (T, 2).") - x_align = x[indices[:, 0]] - y_align = y[indices[:, 1]] - return x_align, y_align + return nn.DynamicTimeWarping.merge(x=x, y=y, indices=indices) def entropy(p: Tensor, out_format: str = "nat") -> Tensor: diff --git a/diffsptk/modules/acorr.py b/diffsptk/modules/acorr.py index e9d3172..24d7565 100644 --- a/diffsptk/modules/acorr.py +++ b/diffsptk/modules/acorr.py @@ -62,8 +62,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) + >>> import diffsptk >>> acorr = diffsptk.Autocorrelation(5, 3) + >>> x = diffsptk.ramp(4) >>> r = acorr(x) >>> r tensor([30.0000, 20.0000, 11.0000, 4.0000]) diff --git a/diffsptk/modules/acr2csm.py b/diffsptk/modules/acr2csm.py index 734abae..32e15f0 100644 --- a/diffsptk/modules/acr2csm.py +++ b/diffsptk/modules/acr2csm.py @@ -73,14 +73,13 @@ def forward(self, r: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([ 0.0165, -2.3693, 0.1375, -0.2262, 1.3307]) + >>> import diffsptk >>> acorr = diffsptk.Autocorrelation(5, 3) >>> acr2csm = diffsptk.AutocorrelationToCompositeSinusoidalModelCoefficients(3) + >>> x = diffsptk.ramp(4) >>> c = acr2csm(acorr(x)) >>> c - tensor([0.9028, 2.5877, 3.8392, 3.6153]) + tensor([ 0.5261, 2.1403, 25.7668, 4.2332]) """ check_size(r.size(-1), self.in_dim, "dimension of autocorrelation") @@ -137,12 +136,6 @@ def _precompute( @staticmethod def _forward(r: torch.Tensor, C: torch.Tensor) -> torch.Tensor: - if r.dtype != torch.double or C.dtype != torch.double: - raise ValueError( - "Only double precision is supported " - f"(input: {r.dtype}, module: {C.dtype})." - ) - u = torch.matmul(r, C) u1, u2 = torch.tensor_split(u, 2, dim=-1) diff --git a/diffsptk/modules/alaw.py b/diffsptk/modules/alaw.py index 2c2c0ae..0f0b72c 100644 --- a/diffsptk/modules/alaw.py +++ b/diffsptk/modules/alaw.py @@ -57,8 +57,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) + >>> import diffsptk >>> alaw = diffsptk.ALawCompression(4) + >>> x = diffsptk.ramp(4) >>> y = alaw(x) >>> y tensor([0.0000, 2.9868, 3.4934, 3.7897, 4.0000]) diff --git a/diffsptk/modules/ap.py b/diffsptk/modules/ap.py index 0f1c232..d6694d3 100644 --- a/diffsptk/modules/ap.py +++ b/diffsptk/modules/ap.py @@ -138,15 +138,16 @@ def forward(self, x: torch.Tensor, f0: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.sin(1000, 80) - >>> pitch = diffsptk.Pitch(160, 8000, out_format="f0") + >>> import diffsptk + >>> pitch = diffsptk.Pitch(160, 16000, out_format="f0") + >>> aperiodicity = diffsptk.Aperiodicity(160, 16000, 1024) + >>> x = diffsptk.sin(2000, 80) >>> f0 = pitch(x) >>> f0.shape - torch.Size([7]) - >>> aperiodicity = diffsptk.Aperiodicity(160, 16000, 1024) + torch.Size([13]) >>> ap = aperiodicity(x, f0) >>> ap.shape - torch.Size([7, 513]) + torch.Size([13, 513]) """ d = x.dim() diff --git a/diffsptk/modules/b2mc.py b/diffsptk/modules/b2mc.py index d653064..c9534a7 100644 --- a/diffsptk/modules/b2mc.py +++ b/diffsptk/modules/b2mc.py @@ -77,12 +77,13 @@ def forward(self, b: torch.Tensor) -> torch.Tensor: Examples -------- - >>> b = diffsptk.ramp(4) + >>> import diffsptk >>> mc2b = diffsptk.MelCepstrumToMLSADigitalFilterCoefficients(4, 0.3) >>> b2mc = diffsptk.MLSADigitalFilterCoefficientsToMelCepstrum(4, 0.3) + >>> b = diffsptk.ramp(1, 5) >>> b2 = mc2b(b2mc(b)) >>> b2 - tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000]) + tensor([1.0000, 2.0000, 3.0000, 4.0000, 5.0000]) """ check_size(b.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/c2acr.py b/diffsptk/modules/c2acr.py index c9b76ad..4ba2b5b 100644 --- a/diffsptk/modules/c2acr.py +++ b/diffsptk/modules/c2acr.py @@ -61,13 +61,13 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c = diffsptk.nrand(4) - >>> c - tensor([-0.1751, 0.1950, -0.3211, 0.3523, -0.5453]) - >>> c2acr = diffsptk.CepstrumToAutocorrelation(4, 4, 16) + >>> import diffsptk + >>> import torch + >>> c2acr = diffsptk.CepstrumToAutocorrelation(4, 3) + >>> c = torch.tensor([0.5, -0.3, 0.2, -0.1, 0.05]) >>> r = c2acr(c) >>> r - tensor([ 1.0672, -0.0485, -0.1564, 0.2666, -0.4551]) + tensor([ 3.2404, -1.1920, 0.9037, -0.5838]) """ check_size(c.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/c2mpir.py b/diffsptk/modules/c2mpir.py index bd9e362..c87b4c9 100644 --- a/diffsptk/modules/c2mpir.py +++ b/diffsptk/modules/c2mpir.py @@ -61,11 +61,12 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c = diffsptk.ramp(3) - >>> c2mpir = diffsptk.CepstrumToMinimumPhaseImpulseResponse(3, 5) + >>> import diffsptk + >>> c2mpir = diffsptk.CepstrumToMinimumPhaseImpulseResponse(4, 5) + >>> c = torch.tensor([0.5, -0.3, 0.2, -0.1, 0.05]) >>> h = c2mpir(c) >>> h - tensor([1.0000, 1.0000, 2.5000, 5.1667, 6.0417]) + tensor([ 1.6487, -0.4946, 0.4039, -0.2712, 0.1803]) """ check_size(c.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/c2ndps.py b/diffsptk/modules/c2ndps.py index 6d40fa9..cb50ec6 100644 --- a/diffsptk/modules/c2ndps.py +++ b/diffsptk/modules/c2ndps.py @@ -75,8 +75,9 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c = diffsptk.ramp(4) + >>> import diffsptk >>> c2ndps = diffsptk.CepstrumToNegativeDerivativeOfPhaseSpectrum(4, 8) + >>> c = diffsptk.ramp(4) >>> n = c2ndps(c) >>> n tensor([ 30.0000, -21.6569, 12.0000, -10.3431, 10.0000]) diff --git a/diffsptk/modules/cdist.py b/diffsptk/modules/cdist.py index 8a79206..1372001 100644 --- a/diffsptk/modules/cdist.py +++ b/diffsptk/modules/cdist.py @@ -66,16 +66,14 @@ def forward(self, c1: torch.Tensor, c2: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c1 = diffsptk.nrand(2, 2) - tensor([[ 0.4296, 1.6517, -0.6022], - [-1.0464, -0.6088, -0.9274]]) - >>> c2 = diffsptk.nrand(2, 2) - tensor([[ 1.6441, -0.6962, -0.2524], - [ 0.9344, 0.3965, 1.1494]]) - >>> cdist = diffsptk.CepstralDistance() + >>> import diffsptk + >>> import torch + >>> cdist = diffsptk.CepstralDistance(reduction="none") + >>> c1 = torch.tensor([[ 0.4296, 1.6517], [-1.0464, -0.6088]]) + >>> c2 = torch.tensor([[ 1.6441, -0.6962], [ 0.9344, 0.3965]]) >>> distance = cdist(c1, c2) >>> distance - tensor(1.6551) + tensor([2.3479, 1.0053]) """ return self._forward(c1, c2, *self.values) diff --git a/diffsptk/modules/chroma.py b/diffsptk/modules/chroma.py index a3b64d0..7c64ec2 100644 --- a/diffsptk/modules/chroma.py +++ b/diffsptk/modules/chroma.py @@ -85,14 +85,15 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> sr = 16000 - >>> x = diffsptk.sin(500, period=sr/440) + >>> import diffsptk + >>> sample_rate = 16000 >>> stft = diffsptk.STFT( ... frame_length=512, frame_period=512, fft_length=512, window="hanning" ... ) >>> chroma = diffsptk.ChromaFilterBankAnalysis( - ... fft_length=512, n_channel=12, sample_rate=sr + ... fft_length=512, n_channel=12, sample_rate=sample_rate ... ) + >>> x = diffsptk.sin(500, period=sample_rate / 440) >>> y = chroma(stft(x)) >>> y tensor([[0.1146, 0.0574, 0.0457, 0.0468, 0.0519, 0.0789, 0.1736, 0.4285, 0.7416, diff --git a/diffsptk/modules/cqt.py b/diffsptk/modules/cqt.py index d11fe94..06f979f 100644 --- a/diffsptk/modules/cqt.py +++ b/diffsptk/modules/cqt.py @@ -274,11 +274,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.sin(99) - >>> cqt = diffsptk.CQT(100, 8000, n_bin=4) + >>> import diffsptk + >>> cqt = diffsptk.CQT(100, 8000, n_bin=4, res_type=None) + >>> x = diffsptk.sin(100 - 1) >>> c = cqt(x).abs() >>> c - tensor([[1.1259, 1.2069, 1.3008, 1.3885]]) + tensor([[1.1231, 1.2045, 1.2976, 1.3867]]) """ x = self.early_downsample(x) diff --git a/diffsptk/modules/csm2acr.py b/diffsptk/modules/csm2acr.py index c352f84..c5ba26c 100644 --- a/diffsptk/modules/csm2acr.py +++ b/diffsptk/modules/csm2acr.py @@ -71,16 +71,17 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) + >>> import diffsptk >>> acorr = diffsptk.Autocorrelation(5, 3) >>> acr2csm = diffsptk.AutocorrelationToCompositeSinusoidalModelCoefficients(3) >>> csm2acr = diffsptk.CompositeSinusoidalModelCoefficientsToAutocorrelation(3) + >>> x = diffsptk.ramp(4) >>> r = acorr(x) >>> r - tensor([ 8.8894, -0.1102, -4.1748, 0.7501]) + tensor([30.0000, 20.0000, 11.0000, 4.0000]) >>> r2 = csm2acr(acr2csm(r)) >>> r2 - tensor([ 8.8894, -0.1102, -4.1748, 0.7501]) + tensor([30.0000, 20.0000, 11.0000, 4.0000]) """ check_size(c.size(-1), self.in_dim, "dimension of autocorrelation") diff --git a/diffsptk/modules/dct.py b/diffsptk/modules/dct.py index cc4ebe0..b6c10c7 100644 --- a/diffsptk/modules/dct.py +++ b/diffsptk/modules/dct.py @@ -70,8 +70,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> dct = diffsptk.DCT(4) + >>> x = diffsptk.ramp(3) >>> y = dct(x) >>> y tensor([ 3.0000, -2.2304, 0.0000, -0.1585]) diff --git a/diffsptk/modules/decimate.py b/diffsptk/modules/decimate.py index 82e39aa..14f703f 100644 --- a/diffsptk/modules/decimate.py +++ b/diffsptk/modules/decimate.py @@ -58,8 +58,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(9) + >>> import diffsptk >>> decimate = diffsptk.Decimation(3, start=1) + >>> x = diffsptk.ramp(9) >>> y = decimate(x) >>> y tensor([1., 4., 7.]) diff --git a/diffsptk/modules/delay.py b/diffsptk/modules/delay.py index 5b4bb70..09a181f 100644 --- a/diffsptk/modules/delay.py +++ b/diffsptk/modules/delay.py @@ -59,11 +59,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 3) + >>> import diffsptk >>> delay = diffsptk.Delay(2) + >>> x = diffsptk.ramp(1, 3) >>> y = delay(x) >>> y tensor([0., 0., 1., 2., 3.]) + >>> delay = diffsptk.Delay(-2) + >>> y = delay(x) + >>> y + tensor([3.]) """ return self._forward(x, *self.values) diff --git a/diffsptk/modules/delta.py b/diffsptk/modules/delta.py index ca3ea84..5fa2dc7 100644 --- a/diffsptk/modules/delta.py +++ b/diffsptk/modules/delta.py @@ -72,13 +72,14 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> delta = diffsptk.Delta([[-0.5, 0], [0, 0, 0.5]]) >>> x = diffsptk.ramp(1, 8).view(1, -1, 2) >>> x tensor([[[1., 2.], [3., 4.], [5., 6.], [7., 8.]]]) - >>> delta = diffsptk.Delta([[-0.5, 0], [0, 0, 0.5]]) >>> y = delta(x) >>> y tensor([[[ 1.0000, 2.0000, -0.5000, -1.0000, 1.5000, 2.0000], diff --git a/diffsptk/modules/dequantize.py b/diffsptk/modules/dequantize.py index 9e79d74..b99a18e 100644 --- a/diffsptk/modules/dequantize.py +++ b/diffsptk/modules/dequantize.py @@ -61,11 +61,12 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> quantize = diffsptk.UniformQuantization(4, 2) + >>> dequantize = diffsptk.InverseUniformQuantization(4, 2) >>> x = diffsptk.ramp(-4, 4) >>> x tensor([-4., -3., -2., -1., 0., 1., 2., 3., 4.]) - >>> quantize = diffsptk.UniformQuantization(4, 2) - >>> dequantize = diffsptk.InverseUniformQuantization(4, 2) >>> x2 = dequantize(quantize(x)) >>> x2 tensor([-3., -3., -1., -1., 1., 1., 3., 3., 3.]) diff --git a/diffsptk/modules/df2.py b/diffsptk/modules/df2.py index 030bd0d..4190c0b 100644 --- a/diffsptk/modules/df2.py +++ b/diffsptk/modules/df2.py @@ -89,11 +89,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) - >>> df2 = diffsptk.SecondOrderDigitalFilter(16000, 100, 200, 1000, 50, 5) + >>> import diffsptk + >>> df2 = diffsptk.SecondOrderDigitalFilter(16000, 1000, 200) + >>> x = diffsptk.impulse(4) >>> y = df2(x) >>> y - tensor([0.0000, 1.0000, 2.0918, 3.4161, 5.1021]) + tensor([1.0000, 1.7766, 2.2319, 2.3227, 2.0633]) """ return self._forward(x, *self.layers) diff --git a/diffsptk/modules/dfs.py b/diffsptk/modules/dfs.py index a73d4be..8c0225e 100644 --- a/diffsptk/modules/dfs.py +++ b/diffsptk/modules/dfs.py @@ -89,8 +89,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) + >>> import diffsptk >>> dfs = diffsptk.IIR(b=[1, -0.97]) + >>> x = diffsptk.ramp(4) >>> y = dfs(x) >>> y tensor([0.0000, 1.0000, 1.0300, 1.0600, 1.0900]) diff --git a/diffsptk/modules/dht.py b/diffsptk/modules/dht.py index 76e6a2a..c5872f7 100644 --- a/diffsptk/modules/dht.py +++ b/diffsptk/modules/dht.py @@ -69,8 +69,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> dht = diffsptk.DHT(4) + >>> x = diffsptk.ramp(3) >>> y = dht(x) >>> y tensor([ 3.0000, -1.4142, -1.0000, -1.4142]) diff --git a/diffsptk/modules/drc.py b/diffsptk/modules/drc.py index 1bf6d9f..adcb301 100644 --- a/diffsptk/modules/drc.py +++ b/diffsptk/modules/drc.py @@ -29,6 +29,9 @@ class DynamicRangeCompression(BaseFunctionalModule): Parameters ---------- + sample_rate : int >= 1 + The sample rate in Hz. + threshold : float <= 0 The threshold in dB. @@ -41,9 +44,6 @@ class DynamicRangeCompression(BaseFunctionalModule): release_time : float > 0 The release time in msec. - sample_rate : int >= 1 - The sample rate in Hz. - makeup_gain : float >= 0 The make-up gain in dB. @@ -68,11 +68,12 @@ class DynamicRangeCompression(BaseFunctionalModule): def __init__( self, - threshold: float, - ratio: float, - attack_time: float, - release_time: float, + *, sample_rate: int, + threshold: float = -20, + ratio: float = 2, + attack_time: float = 1, + release_time: float = 500, makeup_gain: float = 0, abs_max: float = 1, learnable: bool = False, @@ -104,13 +105,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = torch.randn(16000) - >>> x.abs().max() - tensor(4.2224) - >>> drc = diffsptk.DynamicRangeCompression(-20, 4, 10, 100, 16000) + >>> import diffsptk + >>> drc = diffsptk.DynamicRangeCompression( + ... sample_rate=8000, threshold=-20, ratio=2, makeup_gain=10 + ... ) + >>> x = diffsptk.sin(8000) + >>> torch.var(x, correction=0) + tensor(0.5000) >>> y = drc(x) - >>> y.abs().max() - tensor(2.5779) + >>> torch.var(y, correction=0) + tensor(0.5651) """ return self._forward(x, *self.values, **self._buffers, **self._parameters) diff --git a/diffsptk/modules/dst.py b/diffsptk/modules/dst.py index b94bad1..a8a9dd1 100644 --- a/diffsptk/modules/dst.py +++ b/diffsptk/modules/dst.py @@ -69,8 +69,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> dst = diffsptk.DST(4) + >>> x = diffsptk.ramp(3) >>> y = dst(x) >>> y tensor([ 2.7716, -2.0000, 1.1481, -1.0000]) diff --git a/diffsptk/modules/dtw.py b/diffsptk/modules/dtw.py index b8d293b..18187a0 100644 --- a/diffsptk/modules/dtw.py +++ b/diffsptk/modules/dtw.py @@ -189,9 +189,10 @@ def forward( Examples -------- + >>> import diffsptk + >>> dtw = diffsptk.DynamicTimeWarping(p=1) >>> x = torch.tensor([1., 3., 6., 9.]) >>> y = torch.tensor([2., 3., 8., 8.]) - >>> dtw = diffsptk.DynamicTimeWarping(p=1) >>> distance, indices = dtw(x, y, return_indices=True) >>> distance tensor([0.8749]) @@ -326,3 +327,55 @@ def _forward( if return_indices: return distance, indices return distance + + @staticmethod + def merge( + x: torch.Tensor, + y: torch.Tensor, + indices: torch.Tensor, + ) -> torch.Tensor: + """Merge two sequences according to the given indices. + + Parameters + ---------- + x : Tensor [shape=(T1, D) or (T1,)] + The query vector sequence. + + y : Tensor [shape=(T2, D) or (T2,)] + The reference vector sequence. + + indices : Tensor [shape=(T, 2)] + The indices of the viterbi path. + + Returns + ------- + z : Tensor [shape=(T, 2D) or (T, 2)] + The merged vector sequence. + + Examples + -------- + >>> import diffsptk + >>> dtw = diffsptk.DynamicTimeWarping(p=1) + >>> x = torch.tensor([1., 3., 6., 9.]) + >>> y = torch.tensor([2., 3., 8., 8.]) + >>> _, indices = dtw(x, y, return_indices=True) + >>> z = dtw.merge(x, y, indices[0]) + >>> z + tensor([[1., 2.], + [3., 3.], + [6., 8.], + [9., 8.], + [9., 8.]]) + + """ + if x.dim() != y.dim(): + raise ValueError("x and y must have the same number of dimensions.") + if indices.dim() != 2 or indices.size(-1) != 2: + raise ValueError("The shape of indices must be (T, 2).") + x_expanded = x[indices[:, 0]] + y_expanded = y[indices[:, 1]] + if x.dim() == 1: + z = torch.stack([x_expanded, y_expanded], dim=-1) + else: + z = torch.cat([x_expanded, y_expanded], dim=-1) + return z diff --git a/diffsptk/modules/entropy.py b/diffsptk/modules/entropy.py index 06e2f1d..5c37dc4 100644 --- a/diffsptk/modules/entropy.py +++ b/diffsptk/modules/entropy.py @@ -54,10 +54,11 @@ def forward(self, p: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> entropy = diffsptk.Entropy("bit") >>> p = diffsptk.step(3) / 4 >>> p tensor([0.2500, 0.2500, 0.2500, 0.2500]) - >>> entropy = diffsptk.Entropy("bit") >>> h = entropy(p) >>> h tensor(2.) diff --git a/diffsptk/modules/excite.py b/diffsptk/modules/excite.py index 6aa0bec..e2df43e 100644 --- a/diffsptk/modules/excite.py +++ b/diffsptk/modules/excite.py @@ -78,8 +78,9 @@ def forward(self, p: torch.Tensor) -> torch.Tensor: Examples -------- - >>> p = torch.tensor([2.0, 3.0]) + >>> import diffsptk >>> excite = diffsptk.ExcitationGeneration(3) + >>> p = torch.tensor([2.0, 3.0]) >>> e = excite(p) >>> e tensor([1.4142, 0.0000, 1.6330, 0.0000, 0.0000, 1.7321]) diff --git a/diffsptk/modules/f0eval.py b/diffsptk/modules/f0eval.py index 96f06a8..518099e 100644 --- a/diffsptk/modules/f0eval.py +++ b/diffsptk/modules/f0eval.py @@ -61,6 +61,16 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: out : Tensor [shape=(...,) or scalar] The F0 metric. + Examples + -------- + >>> import diffsptk + >>> f0eval = diffsptk.F0Evaluation(out_format="f0-rmse-cent") + >>> x = torch.tensor([100, 200, 300, 0, 400]) + >>> y = torch.tensor([110, 180, 0, 290, 410]) + >>> error = f0eval(x, y) + >>> error + tensor(144.1353) + """ return self._forward(x, y, *self.values) diff --git a/diffsptk/modules/fbank.py b/diffsptk/modules/fbank.py index 590a621..9c7fa31 100644 --- a/diffsptk/modules/fbank.py +++ b/diffsptk/modules/fbank.py @@ -144,11 +144,12 @@ def forward( Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=32) >>> fbank = diffsptk.MelFilterBankAnalysis( ... fft_length=32, n_channel=4, sample_rate=8000 ... ) + >>> x = diffsptk.ramp(19) >>> y = fbank(stft(x)) >>> y tensor([[0.1214, 0.4825, 0.6072, 0.3589], diff --git a/diffsptk/modules/fftcep.py b/diffsptk/modules/fftcep.py index 0b861a9..5377b3f 100644 --- a/diffsptk/modules/fftcep.py +++ b/diffsptk/modules/fftcep.py @@ -71,13 +71,14 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=16) >>> fftcep = diffsptk.CepstralAnalysis(fft_length=16, cep_order=3) + >>> x = diffsptk.ramp(19) >>> c = fftcep(stft(x)) >>> c tensor([[-0.9663, 0.8190, -0.0932, -0.0152], - [-0.8539, 4.6173, -0.5496, -0.3207]]) + [-0.8540, 4.6173, -0.5496, -0.3206]]) """ check_size(x.size(-1), self.in_dim, "dimension of spectrum") diff --git a/diffsptk/modules/fftr.py b/diffsptk/modules/fftr.py index 5718d1a..dddf1a0 100644 --- a/diffsptk/modules/fftr.py +++ b/diffsptk/modules/fftr.py @@ -78,10 +78,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 3) - >>> x - tensor([1., 2., 3.]) + >>> import diffsptk >>> fftr = diffsptk.RealValuedFastFourierTransform(8, out_format="real") + >>> x = diffsptk.ramp(1, 3) >>> y = fftr(x) >>> y tensor([ 6.0000, 2.4142, -2.0000, -0.4142, 2.0000]) diff --git a/diffsptk/modules/flux.py b/diffsptk/modules/flux.py index 51c3baf..98f0b5f 100644 --- a/diffsptk/modules/flux.py +++ b/diffsptk/modules/flux.py @@ -61,12 +61,13 @@ def forward(self, x: torch.Tensor, y: torch.Tensor | None = None) -> torch.Tenso Examples -------- + >>> import diffsptk + >>> flux = diffsptk.Flux(reduction="none", norm=1) >>> x = diffsptk.ramp(5).view(3, 2) >>> x tensor([[0., 1.], [2., 3.], [4., 5.]]) - >>> flux = diffsptk.Flux(reduction="none", norm=1) >>> f = flux(x) >>> f tensor([4., 4.]) diff --git a/diffsptk/modules/frame.py b/diffsptk/modules/frame.py index bf5eaff..96d4cde 100644 --- a/diffsptk/modules/frame.py +++ b/diffsptk/modules/frame.py @@ -73,8 +73,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 9) + >>> import diffsptk >>> frame = diffsptk.Frame(5, 2) + >>> x = diffsptk.ramp(1, 9) >>> y = frame(x) >>> y tensor([[0., 0., 1., 2., 3.], diff --git a/diffsptk/modules/freqt.py b/diffsptk/modules/freqt.py index 26b257c..6e83c59 100644 --- a/diffsptk/modules/freqt.py +++ b/diffsptk/modules/freqt.py @@ -79,17 +79,18 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c1 = diffsptk.ramp(3) - >>> c1 - tensor([0., 1., 2., 3.]) + >>> import diffsptk >>> freqt = diffsptk.FrequencyTransform(3, 4, 0.02) + >>> ifreqt = diffsptk.FrequencyTransform(4, 3, -0.02) + >>> c1 = diffsptk.ramp(1, 4) + >>> c1 + tensor([1., 2., 3., 4.]) >>> c2 = freqt(c1) >>> c2 - tensor([ 0.0208, 1.0832, 2.1566, 2.9097, -0.1772]) - >>> freqt2 = diffsptk.FrequencyTransform(4, 3, -0.02) - >>> c3 = freqt2(c2) + tensor([ 1.0412, 2.1240, 3.1949, 3.8666, -0.2358]) + >>> c3 = ifreqt(c2) >>> c3 - tensor([-9.8953e-10, 1.0000e+00, 2.0000e+00, 3.0000e+00]) + tensor([1.0000, 2.0000, 3.0000, 4.0000]) """ check_size(c.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/freqt2.py b/diffsptk/modules/freqt2.py index 1d8e832..15b6d07 100644 --- a/diffsptk/modules/freqt2.py +++ b/diffsptk/modules/freqt2.py @@ -87,17 +87,15 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c1 = diffsptk.nrand(3) - >>> c1 - tensor([ 0.0304, 0.5849, -0.8668, -0.7278]) - >>> freqt2 = diffsptk.SecondOrderAllPassFrequencyTransform(3, 4, .1, .3) + >>> import diffsptk + >>> import torch + >>> freqt2 = diffsptk.SecondOrderAllPassFrequencyTransform( + ... 3, 4, alpha=0.1, theta=0.3 + ... ) + >>> c1 = torch.tensor([0.8, 0.6, -1.0, -0.5]) >>> c2 = freqt2(c1) >>> c2 - tensor([ 0.0682, 0.4790, -1.0168, -0.6026, 0.1094]) - >>> ifreqt2 = diffsptk.SecondOrderAllPassInverseFrequencyTransform(4, 3, .1, .3) - >>> c3 = ifreqt2(c2) - >>> c3 - tensor([ 0.0682, 0.4790, -1.0168, -0.6026, 0.1094]) + tensor([ 0.8388, 0.4785, -1.1090, -0.3654, 0.0682]) """ check_size(c.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/gammatone.py b/diffsptk/modules/gammatone.py index 94ce95d..82dd119 100644 --- a/diffsptk/modules/gammatone.py +++ b/diffsptk/modules/gammatone.py @@ -173,8 +173,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.impulse(15999) + >>> import diffsptk >>> gammatone = diffsptk.GammatoneFilterBankAnalysis(16000) + >>> x = diffsptk.impulse(16000 - 1) >>> y = gammatone(x) >>> y.shape torch.Size([1, 30, 16000]) diff --git a/diffsptk/modules/gmm.py b/diffsptk/modules/gmm.py index e8b85c6..0f6c8f7 100644 --- a/diffsptk/modules/gmm.py +++ b/diffsptk/modules/gmm.py @@ -266,22 +266,29 @@ def forward( Examples -------- - >>> x = diffsptk.nrand(10, 1) + >>> import diffsptk + >>> import torch >>> gmm = diffsptk.GMM(1, 2) + >>> x = torch.tensor([ + ... [-0.5, 0.3], [0.0, 0.7], [0.2, -0.1], [3.4, 2.0], [-2.8, 1.0], + ... [2.9, -3.0], [2.2, -2.5], [1.5, -1.6], [1.8, 0.5], [1.3, 0.0], + ... ]) + >>> gmm.warmup(x) >>> params, log_likelihood = gmm(x) >>> w, mu, sigma = params >>> w - tensor([0.1917, 0.8083]) + tensor([0.5471, 0.4529]) >>> mu - tensor([[ 1.2321, 0.2058], - [-0.1326, -0.7006]]) - >>> sigma - tensor([[[3.4010e-01, 0.0000e+00], - [0.0000e+00, 6.2351e-04]], - [[3.0944e-01, 0.0000e+00], - [0.0000e+00, 8.6096e-01]]]) + tensor([[-0.1507, 0.4112], + [ 2.3901, -1.0930]]) + >>> print(sigma) + tensor([[[2.1197, 0.0000], + [0.0000, 0.1536]], + + [[0.5578, -0.0000], + [-0.0000, 3.6378]]]) >>> log_likelihood - tensor(-19.5235) + tensor(-32.5925) """ x = to_dataloader(x, batch_size=self.batch_size) diff --git a/diffsptk/modules/gnorm.py b/diffsptk/modules/gnorm.py index a14806a..b8b9264 100644 --- a/diffsptk/modules/gnorm.py +++ b/diffsptk/modules/gnorm.py @@ -65,8 +65,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> gnorm = diffsptk.GeneralizedCepstrumGainNormalization(3, 0.5) >>> x = diffsptk.ramp(1, 4) - >>> gnorm = diffsptk.GeneralizedCepstrumGainNormalization(3, c=2) >>> y = gnorm(x) >>> y tensor([2.2500, 1.3333, 2.0000, 2.6667]) diff --git a/diffsptk/modules/griffin.py b/diffsptk/modules/griffin.py index 9dfd970..edcceae 100644 --- a/diffsptk/modules/griffin.py +++ b/diffsptk/modules/griffin.py @@ -128,15 +128,16 @@ def forward(self, y: torch.Tensor, out_length: int | None = None) -> torch.Tenso Examples -------- - >>> x = diffsptk.ramp(1, 3) - >>> x - tensor([1., 2., 3.]) + >>> import diffsptk >>> stft_params = {"frame_length": 3, "frame_period": 1, "fft_length": 8} >>> stft = diffsptk.STFT(**stft_params, out_format="power") >>> griffin = diffsptk.GriffinLim(**stft_params, n_iter=10, init_phase="zeros") - >>> y = griffin(stft(x), out_length=3) + >>> x = diffsptk.ramp(1, 3) + >>> x + tensor([1., 2., 3.]) + >>> y = griffin(stft(x), out_length=3).round() >>> y - tensor([ 1.0000, 2.0000, -3.0000]) + tensor([-1., -2., -3.]) """ return self._forward(y, out_length, *self.values, *self.layers) diff --git a/diffsptk/modules/grpdelay.py b/diffsptk/modules/grpdelay.py index 0d1a370..56303d6 100644 --- a/diffsptk/modules/grpdelay.py +++ b/diffsptk/modules/grpdelay.py @@ -78,8 +78,9 @@ def forward( Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> grpdelay = diffsptk.GroupDelay(8) + >>> x = diffsptk.ramp(3) >>> g = grpdelay(x) >>> g tensor([2.3333, 2.4278, 3.0000, 3.9252, 3.0000]) diff --git a/diffsptk/modules/hilbert.py b/diffsptk/modules/hilbert.py index 3935f26..7e37848 100644 --- a/diffsptk/modules/hilbert.py +++ b/diffsptk/modules/hilbert.py @@ -68,15 +68,14 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(3) - >>> x - tensor([ 1.1809, -0.2834, -0.4169, 0.3883]) + >>> import diffsptk >>> hilbert = diffsptk.HilbertTransform(4) + >>> x = diffsptk.ramp(3) >>> z = hilbert(x) >>> z.real - tensor([ 1.1809, -0.2834, -0.4169, 0.3883]) + tensor([0., 1., 2., 3.]) >>> z.imag - tensor([ 0.3358, 0.7989, -0.3358, -0.7989]) + tensor([ 1., -1., -1., 1.]) """ return self._forward(x, *self.values, **self._buffers) diff --git a/diffsptk/modules/hilbert2.py b/diffsptk/modules/hilbert2.py index acc077e..79e60c7 100644 --- a/diffsptk/modules/hilbert2.py +++ b/diffsptk/modules/hilbert2.py @@ -69,15 +69,14 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(3) - >>> x - tensor([[ 1.1809, -0.2834, -0.4169, 0.3883]]) + >>> import diffsptk >>> hilbert2 = diffsptk.TwoDimensionalHilbertTransform((1, 4)) + >>> x = diffsptk.ramp(3).unsqueeze(0) >>> z = hilbert2(x) >>> z.real - tensor([[ 1.1809, -0.2834, -0.4169, 0.3883]]) + tensor([[0., 1., 2., 3.]]) >>> z.imag - tensor([[ 0.3358, 0.7989, -0.3358, -0.7989]]) + tensor([[ 1., -1., -1., 1.]]) """ return self._forward(x, *self.values, **self._buffers) diff --git a/diffsptk/modules/histogram.py b/diffsptk/modules/histogram.py index 544fead..46bc324 100644 --- a/diffsptk/modules/histogram.py +++ b/diffsptk/modules/histogram.py @@ -88,8 +88,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(9) + >>> import diffsptk >>> histogram = diffsptk.Histogram(n_bin=4, lower_bound=0, upper_bound=9) + >>> x = diffsptk.ramp(9) >>> h = histogram(x) >>> h tensor([2.5000, 2.0000, 2.0000, 2.5000]) diff --git a/diffsptk/modules/ialaw.py b/diffsptk/modules/ialaw.py index aaef419..296c695 100644 --- a/diffsptk/modules/ialaw.py +++ b/diffsptk/modules/ialaw.py @@ -58,9 +58,10 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) + >>> import diffsptk >>> alaw = diffsptk.ALawCompression(4) >>> ialaw = diffsptk.ALawExpansion(4) + >>> x = diffsptk.ramp(4) >>> x2 = ialaw(alaw(x)) >>> x2 tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000]) diff --git a/diffsptk/modules/ica.py b/diffsptk/modules/ica.py index 666e287..cc1944e 100644 --- a/diffsptk/modules/ica.py +++ b/diffsptk/modules/ica.py @@ -81,8 +81,8 @@ def __init__( ) -> None: super().__init__() - if order <= 0: - raise ValueError("order must be positive.") + if order < 0: + raise ValueError("order must be non-negative.") if order + 1 < n_comp: raise ValueError("n_comp must be less than or equal to input dimension.") if n_iter <= 0: @@ -129,6 +129,25 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: W : Tensor [shape=(K, K)] The separating matrix. + Examples + -------- + >>> import diffsptk + >>> ica = diffsptk.IndependentComponentAnalysis(order=1, n_comp=2, n_iter=10) + >>> x = diffsptk.ramp(1, 6).view(-1, 2) + >>> x + tensor([[1., 2.], + [3., 4.], + [5., 6.]]) + >>> W = ica(x) + >>> W + tensor([[ 0.9928, 0.0292], + [-0.0844, 2.8666]]) + >>> s = ica.transform(x) + >>> s + tensor([[ 1.2169, -0.0138], + [ 0.0000, 0.0000], + [-1.2169, 0.0138]]) + """ x = to_dataloader(x, self.batch_size) device = self.W.device diff --git a/diffsptk/modules/icqt.py b/diffsptk/modules/icqt.py index cbc019a..66504ff 100644 --- a/diffsptk/modules/icqt.py +++ b/diffsptk/modules/icqt.py @@ -240,9 +240,12 @@ def forward(self, c: torch.Tensor, out_length: int | None = None) -> torch.Tenso Examples -------- - >>> x = diffsptk.sin(99) + >>> import diffsptk >>> cqt = diffsptk.CQT(100, 8000, n_bin=4) >>> icqt = diffsptk.ICQT(100, 8000, n_bin=4) + >>> x = diffsptk.sin(100 - 1) + >>> x.shape + torch.Size([100]) >>> y = icqt(cqt(x), out_length=x.size(0)) >>> y.shape torch.Size([100]) diff --git a/diffsptk/modules/idct.py b/diffsptk/modules/idct.py index 436050d..3d413f7 100644 --- a/diffsptk/modules/idct.py +++ b/diffsptk/modules/idct.py @@ -71,12 +71,13 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> dct = diffsptk.DCT(4) >>> idct = diffsptk.IDCT(4) + >>> x = diffsptk.ramp(1, 4) >>> x2 = idct(dct(x)) >>> x2 - tensor([-4.4703e-08, 1.0000e+00, 2.0000e+00, 3.0000e+00]) + tensor([1.0000, 2.0000, 3.0000, 4.0000]) """ check_size(y.size(-1), self.in_dim, "dimension of input") diff --git a/diffsptk/modules/idht.py b/diffsptk/modules/idht.py index c2cd27d..271f048 100644 --- a/diffsptk/modules/idht.py +++ b/diffsptk/modules/idht.py @@ -70,12 +70,13 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> dht = diffsptk.DHT(4) >>> idht = diffsptk.IDHT(4) - >>> x2 = idht(dht(x)) + >>> x = diffsptk.ramp(1, 4) + >>> x2 = idht(dht(x)).round() >>> x2 - tensor([5.9605e-08, 1.0000e+00, 2.0000e+00, 3.0000e+00]) + tensor([1., 2., 3., 4.]) """ check_size(y.size(-1), self.in_dim, "dimension of input") diff --git a/diffsptk/modules/idst.py b/diffsptk/modules/idst.py index 13c9a0a..937885a 100644 --- a/diffsptk/modules/idst.py +++ b/diffsptk/modules/idst.py @@ -70,12 +70,13 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> dst = diffsptk.DST(4) >>> idst = diffsptk.IDST(4) + >>> x = diffsptk.ramp(1, 4) >>> x2 = idst(dst(x)) >>> x2 - tensor([1.1921e-07, 1.0000e+00, 2.0000e+00, 3.0000e+00]) + tensor([1.0000, 2.0000, 3.0000, 4.0000]) """ check_size(y.size(-1), self.in_dim, "dimension of input") diff --git a/diffsptk/modules/ifbank.py b/diffsptk/modules/ifbank.py index f7d79f8..d78655d 100644 --- a/diffsptk/modules/ifbank.py +++ b/diffsptk/modules/ifbank.py @@ -111,17 +111,18 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=32) - >>> X = stft(x) - >>> X.shape - torch.Size([2, 17]) >>> fbank = diffsptk.MelFilterBankAnalysis( ... fft_length=32, n_channel=4, sample_rate=8000 ... ) >>> ifbank = diffsptk.InverseMelFilterBankAnalysis( ... fft_length=32, n_channel=4, sample_rate=8000 ... ) + >>> x = diffsptk.ramp(19) + >>> X = stft(x) + >>> X.shape + torch.Size([2, 17]) >>> X2 = ifbank(fbank(X)) >>> X2.shape torch.Size([2, 17]) diff --git a/diffsptk/modules/ifftr.py b/diffsptk/modules/ifftr.py index e078e74..4721255 100644 --- a/diffsptk/modules/ifftr.py +++ b/diffsptk/modules/ifftr.py @@ -79,11 +79,10 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 3) - >>> x - tensor([1., 2., 3.]) + >>> import diffsptk >>> fftr = diffsptk.RealValuedFastFourierTransform(8) >>> ifftr = diffsptk.RealValuedInverseFastFourierTransform(8, 3) + >>> x = diffsptk.ramp(1, 3) >>> x2 = ifftr(fftr(x)) >>> x2 tensor([1., 2., 3.]) diff --git a/diffsptk/modules/ifreqt2.py b/diffsptk/modules/ifreqt2.py index e905baf..f7060c5 100644 --- a/diffsptk/modules/ifreqt2.py +++ b/diffsptk/modules/ifreqt2.py @@ -88,17 +88,18 @@ def forward(self, c: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c1 = diffsptk.nrand(3) - >>> c1 - tensor([ 0.0304, 0.5849, -0.8668, -0.7278]) - >>> freqt2 = diffsptk.SecondOrderAllPassFrequencyTransform(3, 4, .1, .3) - >>> c2 = freqt2(c1) + >>> import diffsptk + >>> import torch + >>> freqt2 = diffsptk.SecondOrderAllPassFrequencyTransform( + ... 3, 4, alpha=0.05, theta=0.3 + ... ) + >>> ifreqt2 = diffsptk.SecondOrderAllPassInverseFrequencyTransform( + ... 4, 3, alpha=0.05, theta=0.3 + ... ) + >>> c1 = torch.tensor([0.8, 0.6, -1.0, -0.5]) + >>> c2 = ifreqt2(freqt2(c1)) >>> c2 - tensor([ 0.0682, 0.4790, -1.0168, -0.6026, 0.1094]) - >>> ifreqt2 = diffsptk.SecondOrderAllPassInverseFrequencyTransform(4, 3, .1, .3) - >>> c3 = ifreqt2(c2) - >>> c3 - tensor([ 0.0682, 0.4790, -1.0168, -0.6026, 0.1094]) + tensor([ 0.8000, 0.6000, -1.0000, -0.5000]) """ check_size(c.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/igammatone.py b/diffsptk/modules/igammatone.py index 0cf6104..268ffb8 100644 --- a/diffsptk/modules/igammatone.py +++ b/diffsptk/modules/igammatone.py @@ -179,11 +179,12 @@ def forward( Examples -------- - >>> x = diffsptk.impulse(15999) - >>> x[:5] - tensor([1., 0., 0., 0., 0.]) + >>> import diffsptk >>> f = diffsptk.GammatoneFilterBankAnalysis(16000) >>> g = diffsptk.GammatoneFilterBankSynthesis(16000) + >>> x = diffsptk.impulse(16000 - 1) + >>> x[:5] + tensor([1., 0., 0., 0., 0.]) >>> y = g(f(x)).squeeze() >>> y[:5] tensor([ 0.8349, 0.0682, -0.1085, 0.0559, -0.0947]) diff --git a/diffsptk/modules/ignorm.py b/diffsptk/modules/ignorm.py index 9548f2b..8be4a20 100644 --- a/diffsptk/modules/ignorm.py +++ b/diffsptk/modules/ignorm.py @@ -67,9 +67,10 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> gnorm = diffsptk.GeneralizedCepstrumGainNormalization(3, 0.5) + >>> ignorm = diffsptk.GeneralizedCepstrumInverseGainNormalization(3, 0.5) >>> x = diffsptk.ramp(1, 4) - >>> gnorm = diffsptk.GeneralizedCepstrumGainNormalization(3, c=2) - >>> ignorm = diffsptk.GeneralizedCepstrumInverseGainNormalization(3, c=2) >>> x2 = ignorm(gnorm(x)) >>> x2 tensor([1., 2., 3., 4.]) diff --git a/diffsptk/modules/imdct.py b/diffsptk/modules/imdct.py index 9efb096..d3b0e49 100644 --- a/diffsptk/modules/imdct.py +++ b/diffsptk/modules/imdct.py @@ -82,15 +82,14 @@ def forward(self, y: torch.Tensor, out_length: int | None = None) -> torch.Tenso Examples -------- - >>> x = diffsptk.ramp(3) - >>> x - tensor([0., 1., 2., 3.]) + >>> import diffsptk >>> mdct_params = {"frame_length": 4, "window": "vorbis"} >>> mdct = diffsptk.MDCT(**mdct_params) >>> imdct = diffsptk.IMDCT(**mdct_params) + >>> x = diffsptk.ramp(1, 4) >>> y = imdct(mdct(x)) >>> y - tensor([1.0431e-07, 1.0000e+00, 2.0000e+00, 3.0000e+00]) + tensor([1.0000, 2.0000, 3.0000, 4.0000]) """ return self._forward(y, out_length, *self.values, *self.layers) diff --git a/diffsptk/modules/imdst.py b/diffsptk/modules/imdst.py index 27da12f..9b9a571 100644 --- a/diffsptk/modules/imdst.py +++ b/diffsptk/modules/imdst.py @@ -78,15 +78,14 @@ def forward(self, y: torch.Tensor, out_length: int | None = None) -> torch.Tenso Examples -------- - >>> x = diffsptk.ramp(3) - >>> x - tensor([0., 1., 2., 3.]) + >>> import diffsptk >>> mdst_params = {"frame_length": 4} >>> mdst = diffsptk.MDST(**mdst_params) >>> imdst = diffsptk.IMDST(**mdst_params) + >>> x = diffsptk.ramp(1, 4) >>> y = imdst(mdst(x)) >>> y - tensor([-8.9407e-08, 1.0000e+00, 2.0000e+00, 3.0000e+00]) + tensor([1.0000, 2.0000, 3.0000, 4.0000]) """ return self._forward(y, out_length, *self.values, *self.layers) diff --git a/diffsptk/modules/imglsadf.py b/diffsptk/modules/imglsadf.py index 61bc79b..072b6c2 100644 --- a/diffsptk/modules/imglsadf.py +++ b/diffsptk/modules/imglsadf.py @@ -49,16 +49,14 @@ def forward(self, y: torch.Tensor, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> M = 4 + >>> import diffsptk + >>> import torch + >>> imglsadf = diffsptk.IMLSA(1, frame_period=2) >>> y = diffsptk.step(3) - >>> mc = diffsptk.nrand(2, M) - >>> mc - tensor([[ 0.8457, 1.5812, 0.1379, 1.6558, 1.4591], - [-1.3714, -0.9669, -1.2025, -1.3683, -0.2352]]) - >>> imglsadf = diffsptk.IMLSA(M, frame_period=2) - >>> x = imglsadf(y.view(1, -1), mc.view(1, 2, M + 1)) + >>> mc = torch.tensor([[0.3, 0.5], [-0.2, 0.1]]) + >>> x = imglsadf(y, mc) >>> x - tensor([[ 0.4293, 1.0592, 7.9349, 14.9794]]) + tensor([0.7408, 0.6659, 1.1176, 1.1048]) """ x = self.mglsadf(y, -mc) diff --git a/diffsptk/modules/imsvq.py b/diffsptk/modules/imsvq.py index 06448ff..7fbedae 100644 --- a/diffsptk/modules/imsvq.py +++ b/diffsptk/modules/imsvq.py @@ -51,13 +51,13 @@ def forward(self, indices: torch.Tensor, codebooks: torch.Tensor) -> torch.Tenso Examples -------- - >>> msvq = diffsptk.MultiStageVectorQuantization(4, 3, 2) + >>> import diffsptk + >>> msvq = diffsptk.MultiStageVectorQuantization(4, 3, n_stage=2) >>> imsvq = diffsptk.InverseMultiStageVectorQuantization() - >>> indices = torch.tensor([[0, 1], [1, 0]]) + >>> indices = torch.tensor([[0, 1], [2, 1]]) >>> xq = imsvq(indices, msvq.codebooks) - >>> xq - tensor([[-0.8029, -0.1674, 0.5697, 0.9734, 0.1920], - [ 0.0720, -1.0491, -0.4491, -0.2043, -0.3582]]) + >>> xq.shape + torch.Size([2, 5]) """ target_shape = list(indices.shape[:-1]) diff --git a/diffsptk/modules/interpolate.py b/diffsptk/modules/interpolate.py index 1d6db35..f423f89 100644 --- a/diffsptk/modules/interpolate.py +++ b/diffsptk/modules/interpolate.py @@ -59,8 +59,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 3) + >>> import diffsptk >>> interpolate = diffsptk.Interpolation(3, start=1) + >>> x = diffsptk.ramp(1, 3) >>> y = interpolate(x) >>> y tensor([0., 1., 0., 0., 2., 0., 0., 3., 0., 0.]) diff --git a/diffsptk/modules/ipnorm.py b/diffsptk/modules/ipnorm.py index e9399c2..6c39dbb 100644 --- a/diffsptk/modules/ipnorm.py +++ b/diffsptk/modules/ipnorm.py @@ -54,9 +54,10 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 4) + >>> import diffsptk >>> pnorm = diffsptk.MelCepstrumPowerNormalization(3, alpha=0.1) >>> ipnorm = diffsptk.MelCepstrumInversePowerNormalization(3) + >>> x = diffsptk.ramp(1, 4) >>> y = ipnorm(pnorm(x)) >>> y tensor([1., 2., 3., 4.]) diff --git a/diffsptk/modules/ipqmf.py b/diffsptk/modules/ipqmf.py index f51adc3..19af802 100644 --- a/diffsptk/modules/ipqmf.py +++ b/diffsptk/modules/ipqmf.py @@ -120,14 +120,13 @@ def forward(self, y: torch.Tensor, keepdim: bool = True) -> torch.Tensor: Examples -------- - >>> x = torch.arange(0, 1, 0.25) - >>> x - tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000]) + >>> import diffsptk >>> pqmf = diffsptk.PQMF(2, 10) >>> ipqmf = diffsptk.IPQMF(2, 10) - >>> x2 = ipqmf(pmqf(x), keepdim=False) - >>> x2 - tensor([[[8.1887e-04, 2.4754e-01, 5.0066e-01, 7.4732e-01, 9.9419e-01]]]) + >>> x = diffsptk.ramp(1, 4) + >>> x2 = ipqmf(pqmf(x)) + >>> x2.squeeze() + tensor([0.5372, 1.9768, 2.9893, 3.9759]) """ if y.dim() == 2: diff --git a/diffsptk/modules/is2par.py b/diffsptk/modules/is2par.py index 9951b97..ae0caf8 100644 --- a/diffsptk/modules/is2par.py +++ b/diffsptk/modules/is2par.py @@ -53,8 +53,9 @@ def forward(self, s: torch.Tensor) -> torch.Tensor: Examples -------- - >>> s = diffsptk.ramp(1, 4) * 0.1 + >>> import diffsptk >>> is2par = diffsptk.InverseSineToParcorCoefficients(3) + >>> s = diffsptk.ramp(1, 4) * 0.1 >>> k = is2par(s) >>> k tensor([0.1000, 0.3090, 0.4540, 0.5878]) diff --git a/diffsptk/modules/istft.py b/diffsptk/modules/istft.py index 7e2b915..6ebdd43 100644 --- a/diffsptk/modules/istft.py +++ b/diffsptk/modules/istft.py @@ -104,12 +104,11 @@ def forward(self, y: torch.Tensor, out_length: int | None = None) -> torch.Tenso Examples -------- - >>> x = diffsptk.ramp(1, 3) - >>> x - tensor([1., 2., 3.]) + >>> import diffsptk >>> stft_params = {"frame_length": 3, "frame_period": 1, "fft_length": 8} >>> stft = diffsptk.STFT(**stft_params, out_format="complex") >>> istft = diffsptk.ISTFT(**stft_params) + >>> x = diffsptk.ramp(1, 3) >>> y = istft(stft(x), out_length=3) >>> y tensor([1., 2., 3.]) diff --git a/diffsptk/modules/iulaw.py b/diffsptk/modules/iulaw.py index a5f9551..95b928c 100644 --- a/diffsptk/modules/iulaw.py +++ b/diffsptk/modules/iulaw.py @@ -56,9 +56,10 @@ def forward(self, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) + >>> import diffsptk >>> ulaw = diffsptk.MuLawCompression(4) >>> iulaw = diffsptk.MuLawExpansion(4) + >>> x = diffsptk.ramp(4) >>> x2 = iulaw(ulaw(x)) >>> x2 tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000]) diff --git a/diffsptk/modules/ivq.py b/diffsptk/modules/ivq.py index eca285f..c97d578 100644 --- a/diffsptk/modules/ivq.py +++ b/diffsptk/modules/ivq.py @@ -51,15 +51,13 @@ def forward(self, indices: torch.Tensor, codebook: torch.Tensor) -> torch.Tensor Examples -------- - >>> vq = diffsptk.VectorQuantization(4, 2) + >>> import diffsptk + >>> vq = diffsptk.VectorQuantization(4, 3) >>> ivq = diffsptk.InverseVectorQuantization() - >>> indices = torch.tensor([[0, 1], [1, 0]]) + >>> indices = torch.tensor([0, 1, 2, 1]) >>> xq = ivq(indices, vq.codebook) - >>> xq - tensor([[[ 1.9246, -1.4498, -0.9959, 0.3809, 0.0478], - [-0.3040, 0.0568, 0.0113, -0.7284, -0.5103]], - [[-0.3040, 0.0568, 0.0113, -0.7284, -0.5103], - [ 1.9246, -1.4498, -0.9959, 0.3809, 0.0478]]]) + >>> xq.shape + torch.Size([4, 5]) """ target_shape = list(indices.shape) diff --git a/diffsptk/modules/lar2par.py b/diffsptk/modules/lar2par.py index 930aff8..8338172 100644 --- a/diffsptk/modules/lar2par.py +++ b/diffsptk/modules/lar2par.py @@ -54,8 +54,9 @@ def forward(self, g: torch.Tensor) -> torch.Tensor: Examples -------- - >>> g = diffsptk.ramp(1, 4) * 0.1 + >>> import diffsptk >>> lar2par = diffsptk.LogAreaRatioToParcorCoefficients(3) + >>> g = diffsptk.ramp(1, 4) * 0.1 >>> k = lar2par(g) >>> k tensor([0.1000, 0.0997, 0.1489, 0.1974]) diff --git a/diffsptk/modules/lbg.py b/diffsptk/modules/lbg.py index 65732b4..f0bf03a 100644 --- a/diffsptk/modules/lbg.py +++ b/diffsptk/modules/lbg.py @@ -170,16 +170,21 @@ def forward( Examples -------- - >>> x = diffsptk.nrand(10, 0) - >>> lbg = diffsptk.LBG(0, 2) + >>> import diffsptk + >>> import torch + >>> lbg = diffsptk.LBG(1, 2) + >>> x = torch.tensor([ + ... [-0.5, 0.3], [0.0, 0.7], [0.2, -0.1], [3.4, 2.0], [-2.8, 1.0], + ... [2.9, -3.0], [2.2, -2.5], [1.5, -1.6], [1.8, 0.5], [1.3, 0.0], + ... ]) >>> codebook, indices, distance = lbg(x, return_indices=True) >>> codebook - tensor([[-0.5277], - [ 0.6747]]) + tensor([[ 1.6250, 0.8000], + [ 0.5833, -0.9833]]) >>> indices - tensor([0, 0, 0, 1, 0, 1, 1, 1, 1, 0]) + tensor([1, 0, 1, 0, 1, 1, 1, 1, 0, 0]) >>> distance - tensor(0.2331) + tensor(4.2804) """ x = to_dataloader(x, self.batch_size) @@ -337,14 +342,6 @@ def transform(self, x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: indices : Tensor [shape=(T,)] The codebook indices. - Examples - -------- - >>> lbg = diffsptk.LBG(0, 2) - >>> torch.save(lbg.state_dict(), "lbg.pt") - >>> lbg.load_state_dict(torch.load("lbg.pt")) - >>> x = diffsptk.nrand(10, 0) - >>> xq, indices = lbg.transform(x) - """ xq, indices, _ = self.vq(x) return xq, indices diff --git a/diffsptk/modules/levdur.py b/diffsptk/modules/levdur.py index d415268..dfae782 100644 --- a/diffsptk/modules/levdur.py +++ b/diffsptk/modules/levdur.py @@ -70,14 +70,13 @@ def forward(self, r: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([ 0.8226, -0.0284, -0.5715, 0.2127, 0.1217]) + >>> import diffsptk >>> acorr = diffsptk.Autocorrelation(5, 2) >>> levdur = diffsptk.LevinsonDurbin(2) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> a = levdur(acorr(x)) >>> a - tensor([0.8726, 0.1475, 0.5270]) + tensor([ 0.5054, -0.8140, 0.1193]) """ check_size(r.size(-1), self.in_dim, "dimension of autocorrelation") diff --git a/diffsptk/modules/linear_intpl.py b/diffsptk/modules/linear_intpl.py index 4b3a8ad..d81513c 100644 --- a/diffsptk/modules/linear_intpl.py +++ b/diffsptk/modules/linear_intpl.py @@ -55,10 +55,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> linear_intpl = diffsptk.LinearInterpolation(2) >>> x = diffsptk.ramp(2) >>> x tensor([0., 1., 2.]) - >>> linear_intpl = diffsptk.LinearInterpolation(2) >>> y = linear_intpl(x) >>> y tensor([0.0000, 0.5000, 1.0000, 1.5000, 2.0000, 2.0000]) diff --git a/diffsptk/modules/lpc.py b/diffsptk/modules/lpc.py index 39ce337..db1617c 100644 --- a/diffsptk/modules/lpc.py +++ b/diffsptk/modules/lpc.py @@ -77,12 +77,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - tensor([ 0.8226, -0.0284, -0.5715, 0.2127, 0.1217]) + >>> import diffsptk >>> lpc = diffsptk.LPC(5, 2) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> a = lpc(x) >>> a - tensor([0.8726, 0.1475, 0.5270]) + tensor([ 0.5054, -0.8140, 0.1193]) """ return self._forward(x, *self.layers) diff --git a/diffsptk/modules/lpc2lsp.py b/diffsptk/modules/lpc2lsp.py index 8c8bdba..3337072 100644 --- a/diffsptk/modules/lpc2lsp.py +++ b/diffsptk/modules/lpc2lsp.py @@ -87,17 +87,14 @@ def forward(self, a: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([-1.5326, 1.0875, -1.5925, 0.6913, 1.6217]) - >>> lpc = diffsptk.LPC(3, 5) + >>> import diffsptk + >>> lpc = diffsptk.LPC(5, 2) + >>> lpc2lsp = diffsptk.LinearPredictiveCoefficientsToLineSpectralPairs(2) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> a = lpc(x) - >>> a - tensor([ 2.7969, 0.3908, 0.0458, -0.0859]) - >>> lpc2lsp = diffsptk.LinearPredictiveCoefficientsToLineSpectralPairs(3) >>> w = lpc2lsp(a) >>> w - tensor([2.7969, 0.9037, 1.8114, 2.4514]) + tensor([0.5054, 0.5598, 1.6042]) """ check_size(a.size(-1), self.in_dim, "dimension of LPC") diff --git a/diffsptk/modules/lpc2par.py b/diffsptk/modules/lpc2par.py index 3a00d2b..9b74d99 100644 --- a/diffsptk/modules/lpc2par.py +++ b/diffsptk/modules/lpc2par.py @@ -62,17 +62,14 @@ def forward(self, a: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([ 0.7829, -0.2028, 1.6912, 0.1454, 0.4861]) - >>> lpc = diffsptk.LPC(5, 3) + >>> import diffsptk + >>> lpc = diffsptk.LPC(5, 2) + >>> lpc2par = diffsptk.LinearPredictiveCoefficientsToParcorCoefficients(2) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> a = lpc(x) - >>> a - tensor([ 1.6036, 0.0573, -0.5615, -0.0638]) - >>> lpc2par = diffsptk.LinearPredictiveCoefficientsToParcorCoefficients(3) >>> k = lpc2par(a) >>> k - tensor([ 1.6036, 0.0491, -0.5601, -0.0638]) + tensor([ 0.5054, -0.7273, 0.1193]) """ check_size(a.size(-1), self.in_dim, "dimension of LPC") diff --git a/diffsptk/modules/lpccheck.py b/diffsptk/modules/lpccheck.py index 6a50a32..48cd31f 100644 --- a/diffsptk/modules/lpccheck.py +++ b/diffsptk/modules/lpccheck.py @@ -66,15 +66,15 @@ def forward(self, a: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - tensor([-0.9966, -0.2970, -0.2173, 0.0594, 0.5831]) - >>> lpc = diffsptk.LPC(3, 5) - >>> a = lpc(x) - >>> a - tensor([ 1.1528, -0.2613, -0.0274, 0.1778]) - >>> lpccheck = diffsptk.LinearPredictiveCoefficientsStabilityCheck(3) + >>> import diffsptk + >>> import torch + >>> lpccheck = diffsptk.LinearPredictiveCoefficientsStabilityCheck( + ... 4, warn_type="ignore" + ... ) + >>> a = torch.tensor([1.0, -2.5, 2.8, -1.5, 0.4]) >>> a2 = lpccheck(a) - tensor([ 1.1528, -0.2613, -0.0274, 0.1778]) + >>> a2 + tensor([ 1.0000, -2.4825, 2.7743, -1.4930, 0.4000]) """ check_size(a.size(-1), self.in_dim, "dimension of LPC") diff --git a/diffsptk/modules/lsp2lpc.py b/diffsptk/modules/lsp2lpc.py index 99ffa76..1729c05 100644 --- a/diffsptk/modules/lsp2lpc.py +++ b/diffsptk/modules/lsp2lpc.py @@ -81,8 +81,9 @@ def forward(self, w: torch.Tensor) -> torch.Tensor: Examples -------- - >>> w = diffsptk.ramp(3) + >>> import diffsptk >>> lsp2lpc = diffsptk.LineSpectralPairsToLinearPredictiveCoefficients(3) + >>> w = diffsptk.ramp(3) >>> a = lsp2lpc(w) >>> a tensor([ 0.0000, 0.8658, -0.0698, 0.0335]) diff --git a/diffsptk/modules/lsp2sp.py b/diffsptk/modules/lsp2sp.py index 8656cdd..e2ffe29 100644 --- a/diffsptk/modules/lsp2sp.py +++ b/diffsptk/modules/lsp2sp.py @@ -99,17 +99,16 @@ def forward(self, w: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([ 2.1110, -1.4767, 1.2490, 2.4201, 1.5429]) - >>> lpc = diffsptk.LPC(3, 5) + >>> import diffsptk + >>> lpc = diffsptk.LPC(5, 2) + >>> lpc2lsp = diffsptk.LinearPredictiveCoefficientsToLineSpectralPairs(2) + >>> lsp2sp = diffsptk.LineSpectralPairsToSpectrum(2, 8) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> a = lpc(x) - >>> lpc2lsp = diffsptk.LinearPredictiveCoefficientsToLineSpectralPairs(3) >>> w = lpc2lsp(a) - >>> lsp2sp = diffsptk.LineSpectralPairsToSpectrum(3, 8) >>> sp = lsp2sp(w) >>> sp - tensor([31.3541, 13.7932, 14.7454, 16.9510, 10.4759]) + tensor([2.7407, 0.6577, 0.1776, 0.0861, 0.0683]) """ check_size(w.size(-1), self.in_dim, "dimension of LSP") diff --git a/diffsptk/modules/lspcheck.py b/diffsptk/modules/lspcheck.py index 88aa9c9..7a31c01 100644 --- a/diffsptk/modules/lspcheck.py +++ b/diffsptk/modules/lspcheck.py @@ -67,11 +67,15 @@ def forward(self, w: torch.Tensor) -> torch.Tensor: Examples -------- - >>> w1 = torch.tensor([0, 0, 1]) * torch.pi - >>> lspcheck = diffsptk.LineSpectralPairsStabilityCheck(2, rate=0.01) + >>> import diffsptk + >>> import torch + >>> w1 = torch.tensor([1.0 / torch.pi, 0.0, 0.0, 0.5, 1.0]) * torch.pi + >>> lspcheck = diffsptk.LineSpectralPairsStabilityCheck( + ... 4, rate=0.01, n_iter=10, warn_type="ignore" + ... ) >>> w2 = lspcheck(w1) >>> w2 - tensor([0.0000, 0.0105, 3.1311]) + tensor([1.0000, 0.0063, 0.0126, 1.5708, 3.1353]) """ check_size(w.size(-1), self.in_dim, "dimension of LSP") diff --git a/diffsptk/modules/magic_intpl.py b/diffsptk/modules/magic_intpl.py index 61eb1ce..d279df0 100644 --- a/diffsptk/modules/magic_intpl.py +++ b/diffsptk/modules/magic_intpl.py @@ -53,10 +53,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> magic_intpl = diffsptk.MagicNumberInterpolation(0) >>> x = torch.tensor([0, 1, 2, 0, 4, 0]).float() >>> x tensor([0., 1., 2., 0., 4., 0.]) - >>> magic_intpl = diffsptk.MagicNumberInterpolation(0) >>> y = magic_intpl(x) >>> y tensor([1., 1., 2., 3., 4., 4.]) diff --git a/diffsptk/modules/mc2b.py b/diffsptk/modules/mc2b.py index 0999423..7698a29 100644 --- a/diffsptk/modules/mc2b.py +++ b/diffsptk/modules/mc2b.py @@ -77,8 +77,9 @@ def forward(self, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> mc = diffsptk.ramp(4) + >>> import diffsptk >>> mc2b = diffsptk.MelCepstrumToMLSADigitalFilterCoefficients(4, 0.3) + >>> mc = diffsptk.ramp(4) >>> b = mc2b(mc) >>> b tensor([-0.1686, 0.5620, 1.4600, 1.8000, 4.0000]) diff --git a/diffsptk/modules/mcep.py b/diffsptk/modules/mcep.py index fbc29a1..f6af827 100644 --- a/diffsptk/modules/mcep.py +++ b/diffsptk/modules/mcep.py @@ -92,15 +92,16 @@ def forward(self, x: torch.Tensor): Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=16) >>> mcep = diffsptk.MelCepstralAnalysis( ... fft_length=16, cep_order=3, alpha=0.1, n_iter=1 ... ) + >>> x = diffsptk.ramp(19) >>> mc = mcep(stft(x)) >>> mc tensor([[-0.8851, 0.7917, -0.1737, 0.0175], - [-0.3522, 4.4222, -1.0882, -0.0511]]) + [-0.3522, 4.4222, -1.0882, -0.0510]]) """ check_size(x.size(-1), self.in_dim, "dimension of spectrum") diff --git a/diffsptk/modules/mcpf.py b/diffsptk/modules/mcpf.py index 3a21c53..6f9ec65 100644 --- a/diffsptk/modules/mcpf.py +++ b/diffsptk/modules/mcpf.py @@ -94,17 +94,19 @@ def forward(self, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> X = diffsptk.nrand(4).square() - >>> X - tensor([0.2725, 2.5650, 0.3552, 0.3757, 0.1904]) - >>> mcep = diffsptk.MelCepstralAnalysis(3, 8, 0.1) + >>> import diffsptk + >>> import torch + >>> mcep = diffsptk.MelCepstralAnalysis( + ... fft_length=8, cep_order=3, alpha=0.1, n_iter=1 + ... ) >>> mcpf = diffsptk.MelCepstrumPostfiltering(3, 0.1, 0.2) + >>> X = torch.tensor([1.2, 0.9, 0.2, 0.1, 0.3]) >>> mc1 = mcep(X) >>> mc1 - tensor([-0.2819, 0.3486, -0.2487, -0.3600]) + tensor([-0.4989, 0.5941, 0.1558, -0.2130]) >>> mc2 = mcpf(mc1) >>> mc2 - tensor([-0.3256, 0.3486, -0.2984, -0.4320]) + tensor([-0.5097, 0.5941, 0.1869, -0.2556]) """ return self._forward(mc, *self.layers, **self._buffers) diff --git a/diffsptk/modules/mdct.py b/diffsptk/modules/mdct.py index 863f79d..98d2c4b 100644 --- a/diffsptk/modules/mdct.py +++ b/diffsptk/modules/mdct.py @@ -81,15 +81,14 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) - >>> x - tensor([0., 1., 2., 3.]) + >>> import diffsptk >>> mdct = diffsptk.MDCT(frame_length=4) + >>> x = diffsptk.ramp(3) >>> y = mdct(x) >>> y - tensor([[-0.3536, -0.1464], - [-3.1213, -0.2929], - [-0.7678, 1.8536]]) + tensor([[-0.5000, -0.2071], + [-4.4142, -0.4142], + [-1.0858, 2.6213]]) """ return self._forward(x, *self.values, *self.layers) diff --git a/diffsptk/modules/mdst.py b/diffsptk/modules/mdst.py index 614bad4..de9bcd0 100644 --- a/diffsptk/modules/mdst.py +++ b/diffsptk/modules/mdst.py @@ -75,10 +75,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) - >>> x - tensor([0., 1., 2., 3.]) + >>> import diffsptk >>> mdst = diffsptk.MDST(frame_length=4) + >>> x = diffsptk.ramp(3) >>> y = mdst(x) >>> y tensor([[-0.2071, -0.5000], diff --git a/diffsptk/modules/mfcc.py b/diffsptk/modules/mfcc.py index 8ebc918..64b1fbb 100644 --- a/diffsptk/modules/mfcc.py +++ b/diffsptk/modules/mfcc.py @@ -133,15 +133,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=32) >>> mfcc = diffsptk.MFCC( ... fft_length=32, mfcc_order=4, n_channel=8, sample_rate=8000 ... ) + >>> x = diffsptk.ramp(19) >>> y = mfcc(stft(x)) - >>> y - tensor([[-7.7745e-03, -1.4447e-02, 1.6157e-02, 1.1069e-03], - [ 2.8049e+00, -1.6257e+00, -2.3566e-02, 1.2804e-01]]) + >>> y.round(decimals=3) + tensor([[-0.3400, -0.6580, -0.0210, -0.1940], + [ 4.5530, -3.2100, 1.3190, -0.9300]]) """ return self._forward(x, *self.values, *self.layers, **self._buffers) diff --git a/diffsptk/modules/mgc2mgc.py b/diffsptk/modules/mgc2mgc.py index d809871..0c66946 100644 --- a/diffsptk/modules/mgc2mgc.py +++ b/diffsptk/modules/mgc2mgc.py @@ -118,8 +118,9 @@ def forward(self, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c1 = diffsptk.ramp(3) + >>> import diffsptk >>> mgc2mgc = diffsptk.MelGeneralizedCepstrumToMelGeneralizedCepstrum(3, 4, 0.1) + >>> c1 = diffsptk.ramp(3) >>> c2 = mgc2mgc(c1) >>> c2 tensor([-0.0830, 0.6831, 1.1464, 3.1334, 0.9063]) diff --git a/diffsptk/modules/mgc2sp.py b/diffsptk/modules/mgc2sp.py index 22da0bd..2a4dd08 100644 --- a/diffsptk/modules/mgc2sp.py +++ b/diffsptk/modules/mgc2sp.py @@ -101,18 +101,18 @@ def forward(self, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=16) - >>> mcep = diffsptk.MelCepstralAnalysis(3, 16, 0.1, n_iter=1) + >>> mcep = diffsptk.MelCepstralAnalysis( + ... fft_length=16, cep_order=3, alpha=0.1, n_iter=1 + ... ) + >>> mc2sp = diffsptk.MelGeneralizedCepstrumToSpectrum(3, 8, alpha=0.1) + >>> x = diffsptk.ramp(19) >>> mc = mcep(stft(x)) - >>> mc - tensor([[-0.8851, 0.7917, -0.1737, 0.0175], - [-0.3522, 4.4222, -1.0882, -0.0511]]) - >>> mc2sp = diffsptk.MelGeneralizedCepstrumToSpectrum(3, 8, 0.1) >>> sp = mc2sp(mc) >>> sp tensor([[6.0634e-01, 4.6702e-01, 1.7489e-01, 4.4821e-02, 2.3869e-02], - [3.5677e+02, 1.9435e+02, 6.0078e-01, 2.4278e-04, 8.8537e-06]]) + [3.5678e+02, 1.9434e+02, 6.0081e-01, 2.4274e-04, 8.8505e-06]]) """ check_size(mc.size(-1), self.in_dim, "dimension of cepstrum") diff --git a/diffsptk/modules/mgcep.py b/diffsptk/modules/mgcep.py index f93b96f..7b854f8 100644 --- a/diffsptk/modules/mgcep.py +++ b/diffsptk/modules/mgcep.py @@ -158,15 +158,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=16) >>> mgcep = diffsptk.MelGeneralizedCepstralAnalysis( - ... fft_length=16, cep_order=3, alpha=0.1, n_iter=1 + ... fft_length=16, cep_order=3, alpha=0.1, gamma=0.0, n_iter=1 ... ) + >>> x = diffsptk.ramp(19) >>> mc = mgcep(stft(x)) >>> mc tensor([[-0.8851, 0.7917, -0.1737, 0.0175], - [-0.3522, 4.4222, -1.0882, -0.0511]]) + [-0.3522, 4.4222, -1.0882, -0.0510]]) """ if self.gamma == 0: diff --git a/diffsptk/modules/mglsadf.py b/diffsptk/modules/mglsadf.py index 3121aaf..38caffc 100644 --- a/diffsptk/modules/mglsadf.py +++ b/diffsptk/modules/mglsadf.py @@ -205,16 +205,14 @@ def forward(self, x: torch.Tensor, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> M = 4 - >>> x = diffsptk.step(3) - >>> mc = diffsptk.nrand(2, M) - >>> mc - tensor([[-0.9134, -0.5774, -0.4567, 0.7423, -0.5782], - [ 0.6904, 0.5175, 0.8765, 0.1677, 2.4624]]) - >>> mglsadf = diffsptk.MLSA(M, frame_period=2) - >>> y = mglsadf(x.view(1, -1), mc.view(1, 2, M + 1)) - >>> y - tensor([[0.4011, 0.8760, 3.5677, 4.8725]]) + >>> import diffsptk + >>> import torch + >>> mglsadf = diffsptk.MLSA(1, frame_period=2) + >>> y = diffsptk.step(3) + >>> mc = torch.tensor([[0.3, 0.5], [-0.2, 0.1]]) + >>> x = mglsadf(y, mc) + >>> x + tensor([1.3499, 1.3667, 0.9129, 0.9051]) """ check_size(mc.size(-1), sum(self.split_sections), "dimension of mel-cepstrum") diff --git a/diffsptk/modules/mlpg.py b/diffsptk/modules/mlpg.py index 7beacff..052562a 100644 --- a/diffsptk/modules/mlpg.py +++ b/diffsptk/modules/mlpg.py @@ -74,6 +74,7 @@ def forward(self, u: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk >>> x = diffsptk.ramp(1, 8).view(1, -1, 2) >>> x tensor([[[1., 2.], diff --git a/diffsptk/modules/mlsacheck.py b/diffsptk/modules/mlsacheck.py index 9aad21c..afe378b 100644 --- a/diffsptk/modules/mlsacheck.py +++ b/diffsptk/modules/mlsacheck.py @@ -108,10 +108,12 @@ def forward(self, mc: torch.Tensor) -> torch.Tensor: Examples -------- - >>> c1 = diffsptk.nrand(4, stdv=10) - >>> c1 - tensor([ 1.8963, 7.6629, 4.4804, 8.0669, -1.2768]) - >>> mlsacheck = diffsptk.MLSADigitalFilterStabilityCheck(4, warn_type="ignore") + >>> import diffsptk + >>> import torch + >>> mlsacheck = diffsptk.MLSADigitalFilterStabilityCheck( + ... cep_order=4, alpha=0.1, warn_type="ignore" + ... ) + >>> c1 = torch.tensor([1.8963, 7.6629, 4.4804, 8.0669, -1.2768]) >>> c2 = mlsacheck(c1) >>> c2 tensor([ 1.3336, 1.7537, 1.0254, 1.8462, -0.2922]) diff --git a/diffsptk/modules/mpir2c.py b/diffsptk/modules/mpir2c.py index ff02a13..680266b 100644 --- a/diffsptk/modules/mpir2c.py +++ b/diffsptk/modules/mpir2c.py @@ -61,8 +61,9 @@ def forward(self, h: torch.Tensor) -> torch.Tensor: Examples -------- - >>> h = diffsptk.ramp(4, 0, -1) + >>> import diffsptk >>> mpir2c = diffsptk.MinimumPhaseImpulseResponseToCepstrum(5, 3) + >>> h = diffsptk.ramp(4, 0, -1) >>> c = mpir2c(h) >>> c tensor([1.3863, 0.7500, 0.2188, 0.0156]) diff --git a/diffsptk/modules/msvq.py b/diffsptk/modules/msvq.py index e48c547..cbbb39d 100644 --- a/diffsptk/modules/msvq.py +++ b/diffsptk/modules/msvq.py @@ -103,15 +103,18 @@ def forward( Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([-0.5206, 1.0048, -0.3370, 1.3364, -0.2933]) + >>> import diffsptk >>> msvq = diffsptk.MultiStageVectorQuantization(4, 3, 2).eval() - >>> xq, indices, _ = msvq(x) - >>> xq - tensor([-0.4561, 0.9835, -0.3787, -0.1488, -0.8025]) - >>> indices - tensor([0, 2]) + >>> x = diffsptk.nrand(4) + >>> x.shape + torch.Size([5]) + >>> xq, indices, losses = msvq(x) + >>> xq.shape + torch.Size([5]) + >>> indices.shape + torch.Size([2]) + >>> losses.shape + torch.Size([2]) """ if codebooks is not None: diff --git a/diffsptk/modules/ndps2c.py b/diffsptk/modules/ndps2c.py index 9216cbb..dffa6a7 100644 --- a/diffsptk/modules/ndps2c.py +++ b/diffsptk/modules/ndps2c.py @@ -75,8 +75,9 @@ def forward(self, n: torch.Tensor) -> torch.Tensor: Examples -------- - >>> n = diffsptk.ramp(4) + >>> import diffsptk >>> ndps2c = diffsptk.NegativeDerivativeOfPhaseSpectrumToCepstrum(8, 4) + >>> n = diffsptk.ramp(4) >>> c = ndps2c(n) >>> c tensor([ 0.0000, -1.7071, 0.0000, -0.0976, 0.0000]) diff --git a/diffsptk/modules/nmf.py b/diffsptk/modules/nmf.py index cfe02aa..8f13fad 100644 --- a/diffsptk/modules/nmf.py +++ b/diffsptk/modules/nmf.py @@ -175,8 +175,11 @@ def forward( Examples -------- + >>> import diffsptk + >>> nmf = diffsptk.NMF(10, 3, 2, n_iter=10) >>> x = diffsptk.nrand(10, 3) ** 2 - >>> nmf = diffsptk.NMF(10, 3, 2) + >>> x.shape + torch.Size([10, 4]) >>> (U, H), _ = nmf(x) >>> U.shape torch.Size([10, 2]) diff --git a/diffsptk/modules/norm0.py b/diffsptk/modules/norm0.py index a3dd155..53caaef 100644 --- a/diffsptk/modules/norm0.py +++ b/diffsptk/modules/norm0.py @@ -54,8 +54,11 @@ def forward(self, a: torch.Tensor) -> torch.Tensor: Examples -------- - >>> a = diffsptk.ramp(4, 1, -1) + >>> import diffsptk >>> norm0 = diffsptk.AllPoleToAllZeroDigitalFilterCoefficients(3) + >>> a = diffsptk.ramp(4, 1, -1) + >>> a + tensor([4., 3., 2., 1.]) >>> b = norm0(a) >>> b tensor([0.2500, 0.7500, 0.5000, 0.2500]) diff --git a/diffsptk/modules/oband.py b/diffsptk/modules/oband.py index f3ca62e..9b04cf6 100644 --- a/diffsptk/modules/oband.py +++ b/diffsptk/modules/oband.py @@ -165,10 +165,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> oband = diffsptk.FractionalOctaveBandAnalysis(16000) >>> x = diffsptk.ramp(0, 1, 0.25) >>> x tensor([0.0000, 0.2500, 0.5000, 0.7500, 1.0000]) - >>> oband = diffsptk.FractionalOctaveBandAnalysis(16000) >>> y = oband(x) >>> y.shape torch.Size([1, 9, 5]) diff --git a/diffsptk/modules/par2is.py b/diffsptk/modules/par2is.py index 946a545..8eb4221 100644 --- a/diffsptk/modules/par2is.py +++ b/diffsptk/modules/par2is.py @@ -53,9 +53,10 @@ def forward(self, k: torch.Tensor) -> torch.Tensor: Examples -------- - >>> k = diffsptk.ramp(1, 4) * 0.1 + >>> import diffsptk >>> par2is = diffsptk.ParcorCoefficientsToInverseSine(3) >>> is2par = diffsptk.InverseSineToParcorCoefficients(3) + >>> k = diffsptk.ramp(1, 4) * 0.1 >>> k2 = is2par(par2is(k)) >>> k2 tensor([0.1000, 0.2000, 0.3000, 0.4000]) diff --git a/diffsptk/modules/par2lar.py b/diffsptk/modules/par2lar.py index 92b5f2d..b71bab7 100644 --- a/diffsptk/modules/par2lar.py +++ b/diffsptk/modules/par2lar.py @@ -56,9 +56,10 @@ def forward(self, k: torch.Tensor) -> torch.Tensor: Examples -------- - >>> k = diffsptk.ramp(1, 4) * 0.1 + >>> import diffsptk >>> par2lar = diffsptk.ParcorCoefficientsToLogAreaRatio(3) >>> lar2par = diffsptk.LogAreaRatioToParcorCoefficients(3) + >>> k = diffsptk.ramp(1, 4) * 0.1 >>> k2 = lar2par(par2lar(k)) >>> k2 tensor([0.1000, 0.2000, 0.3000, 0.4000]) diff --git a/diffsptk/modules/par2lpc.py b/diffsptk/modules/par2lpc.py index 091c151..0398f13 100644 --- a/diffsptk/modules/par2lpc.py +++ b/diffsptk/modules/par2lpc.py @@ -63,18 +63,17 @@ def forward(self, k: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([ 0.7829, -0.2028, 1.6912, 0.1454, 0.4861]) - >>> lpc = diffsptk.LPC(3, 5) + >>> import diffsptk + >>> lpc = diffsptk.LPC(5, 2) + >>> lpc2par = diffsptk.LinearPredictiveCoefficientsToParcorCoefficients(2) + >>> par2lpc = diffsptk.ParcorCoefficientsToLinearPredictiveCoefficients(2) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> a = lpc(x) >>> a - tensor([ 1.6036, 0.0573, -0.5615, -0.0638]) - >>> lpc2par = diffsptk.LinearPredictiveCoefficientsToParcorCoefficients(3) - >>> par2lpc = diffsptk.ParcorCoefficientsToLinearPredictiveCoefficients(3) + tensor([ 0.5054, -0.8140, 0.1193]) >>> a2 = par2lpc(lpc2par(a)) >>> a2 - tensor([ 1.6036, 0.0573, -0.5615, -0.0638]) + tensor([ 0.5054, -0.8140, 0.1193]) """ check_size(k.size(-1), self.in_dim, "dimension of PARCOR") diff --git a/diffsptk/modules/pca.py b/diffsptk/modules/pca.py index b53359d..efed2d5 100644 --- a/diffsptk/modules/pca.py +++ b/diffsptk/modules/pca.py @@ -129,16 +129,19 @@ def forward( Examples -------- - >>> x = diffsptk.nrand(10, 3) - >>> x.size() - torch.Size([10, 4]) - >>> pca = diffsptk.PCA(3, 3) + >>> import diffsptk + >>> import torch + >>> pca = diffsptk.PCA(1, 1) + >>> x = torch.tensor([ + ... [-0.5, 0.3], [0.0, 0.7], [0.2, -0.1], [3.4, 2.0], [-2.8, 1.0], + ... [2.9, -3.0], [2.2, -2.5], [1.5, -1.6], [1.8, 0.5], [1.3, 0.0], + ... ]) >>> s, _, _ = pca(x) >>> s - tensor([1.3465, 0.7497, 0.4447]) + tensor([3.6372]) >>> y = pca.transform(x) - >>> y.size() - torch.Size([10, 3]) + >>> y.shape + torch.Size([10, 1]) """ x = to_dataloader(x, self.batch_size) diff --git a/diffsptk/modules/phase.py b/diffsptk/modules/phase.py index 55bf309..ec0a5d5 100644 --- a/diffsptk/modules/phase.py +++ b/diffsptk/modules/phase.py @@ -60,8 +60,9 @@ def forward( Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> phase = diffsptk.Phase(8) + >>> x = diffsptk.ramp(3) >>> p = phase(x) >>> p tensor([ 0.0000, -0.5907, 0.7500, -0.1687, 1.0000]) diff --git a/diffsptk/modules/pitch.py b/diffsptk/modules/pitch.py index fc51562..ca03a99 100644 --- a/diffsptk/modules/pitch.py +++ b/diffsptk/modules/pitch.py @@ -129,8 +129,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.sin(1000, 80) + >>> import diffsptk >>> pitch = diffsptk.Pitch(160, 8000, out_format="f0") + >>> x = diffsptk.sin(1000, 80) >>> y = pitch(x) >>> y tensor([ 0.0000, 99.7280, 99.7676, 99.8334, 99.8162, 100.1602, 0.0000]) @@ -267,6 +268,11 @@ def __init__( def forward(self, x: torch.Tensor, embed: bool = True) -> torch.Tensor: x = self.resample(x) + if x.size(-1) < self.torchcrepe.WINDOW_SIZE // 2: + raise ValueError( + f"Input length must be greater than {self.torchcrepe.WINDOW_SIZE // 2}" + f" at {self.torchcrepe.SAMPLE_RATE} Hz." + ) x = self.frame(x) x = x / torch.clip(x.std(dim=-1, keepdim=True), min=1e-10) @@ -355,6 +361,11 @@ def __init__( def forward(self, x: torch.Tensor) -> torch.Tensor: x = self.resample(x) + if x.size(-1) <= self.penn.WINDOW_SIZE // 2: + raise ValueError( + f"Input length must be greater than {self.penn.WINDOW_SIZE // 2}" + f" at {self.penn.SAMPLE_RATE} Hz." + ) frames = self.frame(x) target_shape = frames.shape[:-1] + (self.penn.PITCH_BINS,) org_dtype = torch.get_default_dtype() diff --git a/diffsptk/modules/pitch_spec.py b/diffsptk/modules/pitch_spec.py index 5b2a8fc..c2bb00d 100644 --- a/diffsptk/modules/pitch_spec.py +++ b/diffsptk/modules/pitch_spec.py @@ -135,12 +135,13 @@ def forward(self, x: torch.Tensor, f0: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.sin(1000, 80) + >>> import diffsptk >>> pitch = diffsptk.Pitch(160, 8000, out_format="f0") + >>> pitch_spec = diffsptk.PitchAdaptiveSpectralAnalysis(160, 8000, 1024) + >>> x = diffsptk.sin(1000, 80) >>> f0 = pitch(x) >>> f0.shape torch.Size([7]) - >>> pitch_spec = diffsptk.PitchAdaptiveSpectralAnalysis(160, 8000, 1024) >>> sp = pitch_spec(x, f0) >>> sp.shape torch.Size([7, 513]) diff --git a/diffsptk/modules/plp.py b/diffsptk/modules/plp.py index 3ec44aa..28843f5 100644 --- a/diffsptk/modules/plp.py +++ b/diffsptk/modules/plp.py @@ -149,11 +149,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=32) >>> plp = diffsptk.PLP( - ... fft_length=32, mfcc_order=4, n_channel=8, sample_rate=8000 + ... fft_length=32, plp_order=4, n_channel=8, sample_rate=8000 ... ) + >>> x = diffsptk.ramp(19) >>> y = plp(stft(x)) >>> y tensor([[-0.2896, -0.2356, -0.0586, -0.0387], diff --git a/diffsptk/modules/pnorm.py b/diffsptk/modules/pnorm.py index 9a45566..44b197a 100644 --- a/diffsptk/modules/pnorm.py +++ b/diffsptk/modules/pnorm.py @@ -77,11 +77,12 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(1, 4) + >>> import diffsptk >>> pnorm = diffsptk.MelCepstrumPowerNormalization(3, alpha=0.1) + >>> x = diffsptk.ramp(1, 4) >>> y = pnorm(x) >>> y - tensor([ 8.2942, -7.2942, 2.0000, 3.0000, 4.0000]) + tensor([16.5884, -7.2942, 2.0000, 3.0000, 4.0000]) """ return self._forward(x, *self.layers) diff --git a/diffsptk/modules/pol_root.py b/diffsptk/modules/pol_root.py index 888442d..73bb5b2 100644 --- a/diffsptk/modules/pol_root.py +++ b/diffsptk/modules/pol_root.py @@ -71,8 +71,10 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> import torch + >>> pol_root = diffsptk.RootsToPolynomial(3) >>> x = torch.tensor([3, 4, -1]) - >>> pol_root = diffsptk.RootsToPolynomial(x.size(-1)) >>> a = pol_root(x) >>> a tensor([ 1, -6, 5, 12]) diff --git a/diffsptk/modules/poledf.py b/diffsptk/modules/poledf.py index d51e817..83835d8 100644 --- a/diffsptk/modules/poledf.py +++ b/diffsptk/modules/poledf.py @@ -72,12 +72,13 @@ def forward(self, x: torch.Tensor, a: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> poledf = diffsptk.AllPoleDigitalFilter(0, 1) >>> x = diffsptk.step(4) >>> a = diffsptk.ramp(4) - >>> poledf = diffsptk.AllPoleDigitalFilter(0, 1) >>> y = poledf(x, a.view(-1, 1)) >>> y - tensor([[0., 1., 2., 3., 4.]]) + tensor([0., 1., 2., 3., 4.]) """ check_size(a.size(-1), self.in_dim, "dimension of LPC coefficients") diff --git a/diffsptk/modules/pqmf.py b/diffsptk/modules/pqmf.py index 794bc82..16c2a23 100644 --- a/diffsptk/modules/pqmf.py +++ b/diffsptk/modules/pqmf.py @@ -238,9 +238,10 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(0, 1, 0.25) + >>> import diffsptk >>> pqmf = diffsptk.PQMF(2, 10) - >>> y = pmqf(x) + >>> x = diffsptk.ramp(0, 1, 0.25) + >>> y = pqmf(x) >>> y tensor([[[ 0.1605, 0.4266, 0.6927, 0.9199, 1.0302], [-0.0775, -0.0493, -0.0211, -0.0318, 0.0743]]]) diff --git a/diffsptk/modules/quantize.py b/diffsptk/modules/quantize.py index 3f29fab..cd0b6d1 100644 --- a/diffsptk/modules/quantize.py +++ b/diffsptk/modules/quantize.py @@ -80,8 +80,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(-4, 4) + >>> import diffsptk >>> quantize = diffsptk.UniformQuantization(4, 2) + >>> x = diffsptk.ramp(-4, 4) >>> y = quantize(x).int() >>> y tensor([0, 0, 1, 1, 2, 2, 3, 3, 3], dtype=torch.int32) diff --git a/diffsptk/modules/rlevdur.py b/diffsptk/modules/rlevdur.py index cdce9be..07160cf 100644 --- a/diffsptk/modules/rlevdur.py +++ b/diffsptk/modules/rlevdur.py @@ -67,16 +67,17 @@ def forward(self, a: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) + >>> import diffsptk >>> acorr = diffsptk.Autocorrelation(5, 2) >>> levdur = diffsptk.LevinsonDurbin(2) >>> rlevdur = diffsptk.ReverseLevinsonDurbin(2) + >>> x = diffsptk.ramp(1, 5) * 0.1 >>> r = acorr(x) >>> r - tensor([ 5.8784, 0.8978, -2.0951]) + tensor([0.5500, 0.4000, 0.2600]) >>> r2 = rlevdur(levdur(r)) >>> r2 - tensor([ 5.8784, 0.8978, -2.0951]) + tensor([0.5500, 0.4000, 0.2600]) """ check_size(a.size(-1), self.in_dim, "dimension of LPC coefficients") diff --git a/diffsptk/modules/rmse.py b/diffsptk/modules/rmse.py index 7b5d980..64153f8 100644 --- a/diffsptk/modules/rmse.py +++ b/diffsptk/modules/rmse.py @@ -55,16 +55,14 @@ def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([-0.5945, -0.2401, 0.8633, -0.6464, 0.4515]) - >>> y = diffsptk.nrand(4) - >>> y - tensor([-0.4025, 0.9367, 1.1299, 3.1986, -0.2832]) - >>> rmse = diffsptk.RootMeanSquaredError() + >>> import diffsptk + >>> import torch + >>> rmse = diffsptk.RootMeanSquareError() + >>> x = torch.tensor([0.1, -0.2, 0.3, -0.4, 0.5]) + >>> y = torch.tensor([-0.4, 0.9, 1.1, 3.2, -0.3]) >>> e = rmse(x, y) >>> e - tensor(1.8340) + tensor(1.7720) """ return self._forward(x, y, *self.values) diff --git a/diffsptk/modules/root_pol.py b/diffsptk/modules/root_pol.py index 8c2a92f..7df691b 100644 --- a/diffsptk/modules/root_pol.py +++ b/diffsptk/modules/root_pol.py @@ -76,11 +76,12 @@ def forward(self, a: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> root_pol = diffsptk.PolynomialToRoots(2) >>> a = torch.tensor([3, 4, 5]) - >>> root_pol = diffsptk.PolynomialToRoots(a.size(-1) - 1) >>> x = root_pol(a) >>> x - tensor([[-0.6667+1.1055j, -0.6667-1.1055j]]) + tensor([-0.6667+1.1055j, -0.6667-1.1055j]) """ check_size(a.size(-1), self.in_dim, "order of polynomial") diff --git a/diffsptk/modules/smcep.py b/diffsptk/modules/smcep.py index 3e7ff4a..26089ec 100644 --- a/diffsptk/modules/smcep.py +++ b/diffsptk/modules/smcep.py @@ -101,15 +101,16 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(19) + >>> import diffsptk >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=16) >>> smcep = diffsptk.SecondOrderAllPassMelCepstralAnalysis( - ... fft_length=16, cep_order=3, alpha=0.1, n_iter=1 + ... fft_length=16, cep_order=3, alpha=0.1, theta=0.1, n_iter=1 ... ) + >>> x = diffsptk.ramp(19) >>> mc = smcep(stft(x)) >>> mc - tensor([[-0.8851, 0.7917, -0.1737, 0.0175], - [-0.3523, 4.4223, -1.0883, -0.0510]]) + tensor([[-0.8889, 0.7930, -0.1700, 0.0159], + [-0.3738, 4.4321, -1.0657, -0.0642]]) """ check_size(x.size(-1), self.in_dim, "dimension of spectrum") diff --git a/diffsptk/modules/snr.py b/diffsptk/modules/snr.py index c1a0eb4..ba85537 100644 --- a/diffsptk/modules/snr.py +++ b/diffsptk/modules/snr.py @@ -70,16 +70,14 @@ def forward(self, s: torch.Tensor, sn: torch.Tensor) -> torch.Tensor: Examples -------- - >>> s = diffsptk.nrand(4) - >>> s - tensor([-0.5804, -0.8002, -0.0645, 0.6101, 0.4396]) - >>> n = diffsptk.nrand(4) * 0.1 - >>> n - tensor([ 0.0854, 0.0485, -0.0826, 0.1455, 0.0257]) + >>> import diffsptk + >>> import torch >>> snr = diffsptk.SignalToNoiseRatio(full=True) + >>> s = torch.tensor([0.1, -0.2, 0.3, -0.4, 0.5]) + >>> n = s * 0.1 >>> y = snr(s, s + n) >>> y - tensor(16.0614) + tensor(20.0000) """ return self._forward(s, sn, *self.values) diff --git a/diffsptk/modules/spec.py b/diffsptk/modules/spec.py index 18cce89..8415d9a 100644 --- a/diffsptk/modules/spec.py +++ b/diffsptk/modules/spec.py @@ -82,10 +82,11 @@ def forward( Examples -------- + >>> import diffsptk + >>> spec = diffsptk.Spectrum(8) >>> x = diffsptk.ramp(1, 3) >>> x tensor([1., 2., 3.]) - >>> spec = diffsptk.Spectrum(8) >>> y = spec(x) >>> y tensor([36.0000, 25.3137, 8.0000, 2.6863, 4.0000]) diff --git a/diffsptk/modules/stft.py b/diffsptk/modules/stft.py index 5c35eea..e2e9f1e 100644 --- a/diffsptk/modules/stft.py +++ b/diffsptk/modules/stft.py @@ -124,10 +124,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> stft = diffsptk.STFT(frame_length=3, frame_period=1, fft_length=8) >>> x = diffsptk.ramp(1, 3) >>> x tensor([1., 2., 3.]) - >>> stft = diffsptk.STFT(frame_length=3, frame_period=1, fft_length=8) >>> y = stft(x) >>> y tensor([[1.0000, 1.0000, 1.0000, 1.0000, 1.0000], diff --git a/diffsptk/modules/ulaw.py b/diffsptk/modules/ulaw.py index 6d51da3..018e08b 100644 --- a/diffsptk/modules/ulaw.py +++ b/diffsptk/modules/ulaw.py @@ -57,8 +57,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(4) + >>> import diffsptk >>> ulaw = diffsptk.MuLawCompression(4) + >>> x = diffsptk.ramp(4) >>> y = ulaw(x) >>> y tensor([0.0000, 3.0084, 3.5028, 3.7934, 4.0000]) diff --git a/diffsptk/modules/unframe.py b/diffsptk/modules/unframe.py index 01ae9ac..5e24853 100644 --- a/diffsptk/modules/unframe.py +++ b/diffsptk/modules/unframe.py @@ -102,8 +102,10 @@ def forward(self, y: torch.Tensor, out_length: int | None = None) -> torch.Tenso Examples -------- - >>> x = diffsptk.ramp(1, 9) + >>> import diffsptk >>> frame = diffsptk.Frame(5, 2) + >>> unframe = diffsptk.Unframe(5, 2) + >>> x = diffsptk.ramp(1, 9) >>> y = frame(x) >>> y tensor([[0., 0., 1., 2., 3.], @@ -111,7 +113,6 @@ def forward(self, y: torch.Tensor, out_length: int | None = None) -> torch.Tenso [3., 4., 5., 6., 7.], [5., 6., 7., 8., 9.], [7., 8., 9., 0., 0.]]) - >>> unframe = diffsptk.Unframe(5, 2) >>> z = unframe(y, out_length=x.size(0)) >>> z tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.]) diff --git a/diffsptk/modules/vq.py b/diffsptk/modules/vq.py index 0ff2c9f..7987be2 100644 --- a/diffsptk/modules/vq.py +++ b/diffsptk/modules/vq.py @@ -97,13 +97,14 @@ def forward( Examples -------- - >>> x = diffsptk.nrand(4) - >>> x - tensor([ 0.7947, 0.1007, 1.2290, -0.5019, 1.5552]) + >>> import diffsptk >>> vq = diffsptk.VectorQuantization(4, 2).eval() + >>> x = diffsptk.nrand(4) + >>> x.shape + torch.Size([5]) >>> xq, _, _ = vq(x) - >>> xq - tensor([0.3620, 0.2736, 0.7098, 0.7106, 0.6494] + >>> xq.shape + torch.Size([5]) """ if codebook is not None: diff --git a/diffsptk/modules/wht.py b/diffsptk/modules/wht.py index c8ad241..f212adb 100644 --- a/diffsptk/modules/wht.py +++ b/diffsptk/modules/wht.py @@ -76,8 +76,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.ramp(3) + >>> import diffsptk >>> wht = diffsptk.WHT(4) + >>> x = diffsptk.ramp(3) >>> y = wht(x) >>> y tensor([ 3., -1., -2., 0.]) diff --git a/diffsptk/modules/window.py b/diffsptk/modules/window.py index 6a542f7..3ca6950 100644 --- a/diffsptk/modules/window.py +++ b/diffsptk/modules/window.py @@ -96,8 +96,9 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = torch.ones(5) + >>> import diffsptk >>> window = diffsptk.Window(5, out_length=7, window="hamming", norm="none") + >>> x = diffsptk.step(4, value=1) >>> y = window(x) >>> y tensor([0.0800, 0.5400, 1.0000, 0.5400, 0.0800, 0.0000, 0.0000]) diff --git a/diffsptk/modules/world_synth.py b/diffsptk/modules/world_synth.py index 9c26386..c509944 100644 --- a/diffsptk/modules/world_synth.py +++ b/diffsptk/modules/world_synth.py @@ -116,7 +116,11 @@ def __init__( self.register_buffer("dc_remover", to(dc_remover, dtype=dtype)) def forward( - self, f0: torch.Tensor, ap: torch.Tensor, sp: torch.Tensor + self, + f0: torch.Tensor, + ap: torch.Tensor, + sp: torch.Tensor, + out_length: int | None = None, ) -> torch.Tensor: """Synthesize speech using WORLD vocoder. @@ -131,6 +135,9 @@ def forward( sp : Tensor [shape=(B, T/P, L/2+1) or (T/P, L/2+1)] The spectral envelope (power spectrum). + out_length : int > 0 or None + The length of the output waveform. + Returns ------- out : Tensor [shape=(B, T) or (T,)] @@ -138,17 +145,18 @@ def forward( Examples -------- - >>> x = diffsptk.sin(1000, 80) - >>> pitch = diffsptk.Pitch(160, 8000, out_format="f0") - >>> f0 = pitch(x) + >>> import diffsptk + >>> pitch = diffsptk.Pitch(160, 16000, out_format="f0") >>> aperiodicity = diffsptk.Aperiodicity(160, 16000, 1024) + >>> spec = diffsptk.PitchAdaptiveSpectralAnalysis(160, 16000, 1024) + >>> world_synth = diffsptk.WorldSynthesis(160, 16000, 1024) + >>> x = diffsptk.sin(2000 - 1, 80) + >>> f0 = pitch(x) >>> ap = aperiodicity(x, f0) - >>> pitch_spec = diffsptk.PitchAdaptiveSpectralAnalysis(160, 8000, 1024) - >>> sp = pitch_spec(x, f0) - >>> world_synth = diffsptk.WorldSynthesis(160, 8000, 1024) - >>> y = world_synth(f0, ap, sp) + >>> sp = spec(x, f0) + >>> y = world_synth(f0, ap, sp, out_length=x.size(0)) >>> y.shape - torch.Size([1120]) + torch.Size([2000]) """ is_batched_input = f0.ndim == 2 @@ -304,4 +312,6 @@ def forward( if not is_batched_input: y = y.squeeze(0) + if out_length is not None: + y = y[..., :out_length] return y diff --git a/diffsptk/modules/yingram.py b/diffsptk/modules/yingram.py index 114260d..462d167 100644 --- a/diffsptk/modules/yingram.py +++ b/diffsptk/modules/yingram.py @@ -96,9 +96,10 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(22050) + >>> import diffsptk >>> frame = diffsptk.Frame(2048, 441) >>> yingram = diffsptk.Yingram(2048) + >>> x = diffsptk.nrand(22050) >>> y = yingram(frame(x)) >>> y.shape torch.Size([51, 1580]) diff --git a/diffsptk/modules/zcross.py b/diffsptk/modules/zcross.py index 46b2ac4..5ddf34f 100644 --- a/diffsptk/modules/zcross.py +++ b/diffsptk/modules/zcross.py @@ -61,10 +61,10 @@ def forward(self, x: torch.Tensor) -> torch.Tensor: Examples -------- - >>> x = diffsptk.nrand(5) - >>> x - tensor([-0.2388, 0.3587, -0.6606, -0.6929, 0.5239, 0.4501]) + >>> import diffsptk + >>> import torch >>> zcross = diffsptk.ZeroCrossingAnalysis(3) + >>> x = torch.tensor([-0.2, 0.3, -0.5, -0.7, 0.4, 0.2]) >>> z = zcross(x) >>> z tensor([2., 1.]) diff --git a/diffsptk/modules/zerodf.py b/diffsptk/modules/zerodf.py index ae78df7..1772513 100644 --- a/diffsptk/modules/zerodf.py +++ b/diffsptk/modules/zerodf.py @@ -67,12 +67,13 @@ def forward(self, x: torch.Tensor, b: torch.Tensor) -> torch.Tensor: Examples -------- + >>> import diffsptk + >>> zerodf = diffsptk.AllZeroDigitalFilter(0, 1) >>> x = diffsptk.step(4) >>> b = diffsptk.ramp(4) - >>> zerodf = diffsptk.AllZeroDigitalFilter(0, 1) >>> y = zerodf(x, b.view(-1, 1)) >>> y - tensor([[0., 1., 2., 3., 4.]]) + tensor([0., 1., 2., 3., 4.]) """ check_size(b.size(-1), self.in_dim, "dimension of impulse response") diff --git a/diffsptk/signals.py b/diffsptk/signals.py index 4969d02..24dd6d9 100644 --- a/diffsptk/signals.py +++ b/diffsptk/signals.py @@ -40,6 +40,7 @@ def impulse(order: int, **kwargs) -> torch.Tensor: Examples -------- + >>> import diffsptk >>> x = diffsptk.impulse(4) >>> x tensor([1., 0., 0., 0., 0.]) @@ -73,6 +74,7 @@ def step(order: int, value: float = 1, **kwargs) -> torch.Tensor: Examples -------- + >>> import diffsptk >>> x = diffsptk.step(4, 2) >>> x tensor([2., 2., 2., 2., 2.]) @@ -115,6 +117,7 @@ def ramp( Examples -------- + >>> import diffsptk >>> x = diffsptk.ramp(4) >>> x tensor([0., 1., 2., 3., 4.]) @@ -165,6 +168,7 @@ def sin( Examples -------- + >>> import diffsptk >>> x = diffsptk.sin(4) >>> x tensor([ 0.0000, 0.9511, 0.5878, -0.5878, -0.9511]) @@ -208,6 +212,7 @@ def train( Examples -------- + >>> import diffsptk >>> x = diffsptk.train(5, 2.3) >>> x tensor([1.5166, 0.0000, 0.0000, 1.5166, 0.0000, 1.5166]) @@ -257,6 +262,7 @@ def mseq(*order: int, **kwargs) -> torch.Tensor: Examples -------- + >>> import diffsptk >>> x = diffsptk.mseq(4) >>> x tensor([-1., 1., -1., 1., -1.]) @@ -327,13 +333,13 @@ def nrand( Examples -------- + >>> import diffsptk >>> x = diffsptk.nrand(4) - >>> x - tensor([-0.8603, 0.6743, -0.9178, 1.5382, -0.2574]) + >>> x.shape + torch.Size([5]) >>> x = diffsptk.nrand(2, 4) - >>> x - tensor([[-0.2385, -0.0778, -0.0418, -1.6217, 0.1560], - [ 1.6646, 0.8429, 0.9357, -0.5123, 0.9571]]) + >>> x.shape + torch.Size([2, 5]) """ if var is not None: @@ -378,13 +384,13 @@ def rand(*order: int, a: float = 0, b: float = 1, **kwargs) -> torch.Tensor: Examples -------- + >>> import diffsptk >>> x = diffsptk.rand(4) - >>> x - tensor([0.3425, 0.5997, 0.2882, 0.3961, 0.9791]) + >>> x.shape + torch.Size([5]) >>> x = diffsptk.rand(2, 4) - >>> x - tensor([[0.6170, 0.3649, 0.9397, 0.2408, 0.1868], - [0.6527, 0.1400, 0.3955, 0.9999, 0.8298]]) + >>> x.shape + torch.Size([2, 5]) """ if b <= a: diff --git a/diffsptk/utils/public.py b/diffsptk/utils/public.py index 90af6c9..e419963 100644 --- a/diffsptk/utils/public.py +++ b/diffsptk/utils/public.py @@ -20,13 +20,13 @@ def get_alpha( - sr: int, mode: str = "hts", n_freq: int = 10, n_alpha: int = 100 + sample_rate: int, mode: str = "hts", n_freq: int = 10, n_alpha: int = 100 ) -> float: """Compute an appropriate frequency warping factor given the sample rate. Parameters ---------- - sr : int >= 1 + sample_rate : int >= 1 The sample rate in Hz. mode : ['hts', 'auto'] @@ -46,8 +46,8 @@ def get_alpha( Examples -------- - >>> _, sr = diffsptk.read("assets/data.wav") - >>> alpha = diffsptk.get_alpha(sr) + >>> import diffsptk + >>> alpha = diffsptk.get_alpha(sample_rate=16000, mode="hts") >>> alpha 0.42 @@ -95,9 +95,9 @@ def get_auto_alpha(sr, n_freq, n_alpha): return selected_alpha if mode == "hts": - alpha = get_hts_alpha(sr) + alpha = get_hts_alpha(sample_rate) elif mode == "auto": - alpha = get_auto_alpha(sr, n_freq, n_alpha) + alpha = get_auto_alpha(sample_rate, n_freq, n_alpha) else: raise ValueError("Only hts and auto are supported.") @@ -137,29 +137,30 @@ def read( x : Tensor [shape=(C, T) or (T, C) or (T,)]] The waveform. - sr : int + sample_rate : int The sample rate in Hz. Examples -------- - >>> x, sr = diffsptk.read("assets/data.wav") - >>> x - tensor([ 0.0002, 0.0004, 0.0006, ..., 0.0006, -0.0006, -0.0007]) - >>> sr + >>> import diffsptk + >>> x, sample_rate = diffsptk.read("assets/data.wav") + >>> x.shape + torch.Size([19200]) + >>> sample_rate 16000 """ - x, sr = sf.read(filename, **kwargs) + x, sample_rate = sf.read(filename, **kwargs) if dtype is None: dtype = torch.get_default_dtype() x = torch.tensor(x.T if channel_first else x, device=device, dtype=dtype) - return x, sr + return x, sample_rate def write( filename: str, x: torch.Tensor | np.ndarray, - sr: int, + sample_rate: int, channel_first: bool = True, **kwargs, ) -> None: @@ -173,7 +174,7 @@ def write( x : Tensor or ndarray [shape=(C, T) or (T, C) or (T,)]] The waveform. - sr : int + sample_rate : int The sample rate in Hz. channel_first : bool @@ -186,9 +187,12 @@ def write( Examples -------- - >>> x, sr = diffsptk.read("assets/data.wav") - >>> diffsptk.write("out.wav", x, sr) + >>> import diffsptk + >>> import os + >>> x, sample_rate = diffsptk.read("assets/data.wav") + >>> diffsptk.write("out.wav", x, sample_rate) + >>> os.remove("out.wav") """ x = x.detach().cpu().numpy() if torch.is_tensor(x) else x - sf.write(filename, x.T if channel_first else x, sr, **kwargs) + sf.write(filename, x.T if channel_first else x, sample_rate, **kwargs) diff --git a/diffsptk/version.py b/diffsptk/version.py index 41a3956..903a158 100644 --- a/diffsptk/version.py +++ b/diffsptk/version.py @@ -1 +1 @@ -__version__ = "3.3.2.dev0" +__version__ = "3.4.0" diff --git a/pyproject.toml b/pyproject.toml index 0cf5b40..4e0261f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,8 +29,8 @@ dependencies = [ "soundfile >= 0.10.2", "torch >= 2.3.1", "torchaudio >= 2.3.1", - "torchlpc >= 0.6.0", - "torchcomp >= 0.2.0", + "torchlpc >= 0.1.0, < 0.7.0", + "torchcomp >= 0.1.0, < 0.3.0", "penn >= 0.0.1, < 1.0.0", "pylstraight >= 0.1.0", "vector-quantize-pytorch >= 1.14.9", diff --git a/tests/test_drc.py b/tests/test_drc.py index c2a9d8c..7244e6e 100644 --- a/tests/test_drc.py +++ b/tests/test_drc.py @@ -68,5 +68,5 @@ def test_compatibility( def test_learnable(T=20): - drc = diffsptk.DRC(-20, 2, 50, 50, 16000, learnable=True) + drc = diffsptk.DRC(sample_rate=16000, learnable=True) U.check_learnable(drc, (T,)) diff --git a/tests/test_dtw.py b/tests/test_dtw.py index 6aa266f..f86808d 100644 --- a/tests/test_dtw.py +++ b/tests/test_dtw.py @@ -45,7 +45,7 @@ def _dtw(x, y): assert np.allclose(output_distance, target_distance), ( f"Output: {output_distance}\nTarget: {target_distance}" ) - return torch.cat(diffsptk.functional.dtw_merge(x, y, indices[0]), dim=-1) + return diffsptk.functional.dtw_merge(x, y, indices[0]) U.check_compatibility( device, @@ -75,3 +75,13 @@ def test_various_shape(T=10): [(1, T, 1), (1, T, 1)], ], ) + + indices = torch.stack([torch.arange(T), torch.arange(T)], dim=-1) + dtw_merge = lambda x, y: diffsptk.functional.dtw_merge(x, y, indices) + U.check_various_shape( + dtw_merge, + [ + [(T,), (T,)], + [(T, 1), (T, 1)], + ], + ) diff --git a/tests/test_public_utils.py b/tests/test_public_utils.py index e4b3823..1f5ccf6 100644 --- a/tests/test_public_utils.py +++ b/tests/test_public_utils.py @@ -25,7 +25,7 @@ @pytest.mark.parametrize("mode", ["hts", "auto"]) def test_get_alpha(mode): - alpha = diffsptk.get_alpha(sr=48000, mode=mode) + alpha = diffsptk.get_alpha(48000, mode=mode) assert alpha == 0.55 diff --git a/tests/test_world_synth.py b/tests/test_world_synth.py index 6cd4896..ed0722f 100644 --- a/tests/test_world_synth.py +++ b/tests/test_world_synth.py @@ -44,6 +44,7 @@ def test_compatibility(device, dtype, P=80, sr=16000, L=1024, B=2): [f"rm {tmp1} {tmp2} {tmp3} {tmp4}"], dx=[None, n, n], eq=lambda a, b: np.corrcoef(a, b)[0, 1] > 0.95, + opt={"out_length": 19200}, ) U.check_differentiability(