diffsptk 1.1.0__tar.gz → 1.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. {diffsptk-1.1.0 → diffsptk-1.2.1}/PKG-INFO +15 -5
  2. {diffsptk-1.1.0 → diffsptk-1.2.1}/README.md +13 -3
  3. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/__init__.py +4 -0
  4. diffsptk-1.2.1/diffsptk/core/alaw.py +75 -0
  5. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/c2acr.py +1 -2
  6. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/fbank.py +12 -5
  7. diffsptk-1.2.1/diffsptk/core/ialaw.py +77 -0
  8. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/iulaw.py +3 -3
  9. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/levdur.py +1 -1
  10. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/linear_intpl.py +4 -4
  11. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mfcc.py +7 -7
  12. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mgc2mgc.py +3 -0
  13. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/par2lpc.py +1 -0
  14. diffsptk-1.2.1/diffsptk/core/plp.py +166 -0
  15. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/stft.py +9 -7
  16. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ulaw.py +3 -3
  17. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/zcross.py +3 -1
  18. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/misc/utils.py +18 -8
  19. diffsptk-1.2.1/diffsptk/version.py +1 -0
  20. {diffsptk-1.1.0 → diffsptk-1.2.1}/pyproject.toml +1 -1
  21. diffsptk-1.1.0/diffsptk/version.py +0 -1
  22. {diffsptk-1.1.0 → diffsptk-1.2.1}/.gitignore +0 -0
  23. {diffsptk-1.1.0 → diffsptk-1.2.1}/LICENSE +0 -0
  24. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/__init__.py +0 -0
  25. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/acorr.py +0 -0
  26. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ap.py +0 -0
  27. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/b2mc.py +0 -0
  28. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/c2mpir.py +0 -0
  29. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/c2ndps.py +0 -0
  30. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/cdist.py +0 -0
  31. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/cqt.py +0 -0
  32. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/dct.py +0 -0
  33. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/decimate.py +0 -0
  34. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/delay.py +0 -0
  35. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/delta.py +0 -0
  36. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/dequantize.py +0 -0
  37. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/df2.py +0 -0
  38. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/dfs.py +0 -0
  39. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/entropy.py +0 -0
  40. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/excite.py +0 -0
  41. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/fftcep.py +0 -0
  42. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/frame.py +0 -0
  43. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/freqt.py +0 -0
  44. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/freqt2.py +0 -0
  45. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/gmm.py +0 -0
  46. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/gnorm.py +0 -0
  47. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/grpdelay.py +0 -0
  48. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/idct.py +0 -0
  49. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ifreqt2.py +0 -0
  50. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ignorm.py +0 -0
  51. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/imglsadf.py +0 -0
  52. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/imsvq.py +0 -0
  53. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/interpolate.py +0 -0
  54. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ipqmf.py +0 -0
  55. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/istft.py +0 -0
  56. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ivq.py +0 -0
  57. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lar2par.py +0 -0
  58. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lbg.py +0 -0
  59. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lpc.py +0 -0
  60. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lpc2lsp.py +0 -0
  61. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lpc2par.py +0 -0
  62. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lpccheck.py +0 -0
  63. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lsp2lpc.py +0 -0
  64. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lsp2sp.py +0 -0
  65. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/lspcheck.py +0 -0
  66. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/magic_intpl.py +0 -0
  67. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mc2b.py +0 -0
  68. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mcep.py +0 -0
  69. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mcpf.py +0 -0
  70. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mgc2sp.py +0 -0
  71. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mgcep.py +0 -0
  72. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mglsadf.py +0 -0
  73. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mlpg.py +0 -0
  74. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mlsacheck.py +0 -0
  75. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/mpir2c.py +0 -0
  76. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/msvq.py +0 -0
  77. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/ndps2c.py +0 -0
  78. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/norm0.py +0 -0
  79. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/par2lar.py +0 -0
  80. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/pca.py +0 -0
  81. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/phase.py +0 -0
  82. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/pitch.py +0 -0
  83. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/pol_root.py +0 -0
  84. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/poledf.py +0 -0
  85. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/pqmf.py +0 -0
  86. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/quantize.py +0 -0
  87. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/rlevdur.py +0 -0
  88. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/rmse.py +0 -0
  89. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/root_pol.py +0 -0
  90. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/smcep.py +0 -0
  91. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/snr.py +0 -0
  92. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/spec.py +0 -0
  93. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/unframe.py +0 -0
  94. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/vq.py +0 -0
  95. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/window.py +0 -0
  96. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/yingram.py +0 -0
  97. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/core/zerodf.py +0 -0
  98. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/misc/__init__.py +0 -0
  99. {diffsptk-1.1.0 → diffsptk-1.2.1}/diffsptk/misc/signals.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: diffsptk
3
- Version: 1.1.0
3
+ Version: 1.2.1
4
4
  Summary: Speech signal processing modules for machine learning
5
5
  Project-URL: Homepage, https://sp-tk.sourceforge.net/
6
6
  Project-URL: Documentation, https://sp-nitech.github.io/diffsptk/latest/
@@ -22,7 +22,7 @@ Requires-Dist: torch>=1.11.0
22
22
  Requires-Dist: torchaudio>=0.11.0
23
23
  Requires-Dist: torchcrepe>=0.0.21
24
24
  Requires-Dist: torchlpc>=0.2.0
25
- Requires-Dist: vector-quantize-pytorch>=0.8.0
25
+ Requires-Dist: vector-quantize-pytorch<=1.12.12,>=0.8.0
26
26
  Provides-Extra: dev
27
27
  Requires-Dist: black; extra == 'dev'
28
28
  Requires-Dist: build; extra == 'dev'
@@ -41,10 +41,10 @@ diffsptk
41
41
  *diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework.
42
42
 
43
43
  [![Latest Manual](https://img.shields.io/badge/docs-latest-blue.svg)](https://sp-nitech.github.io/diffsptk/latest/)
44
- [![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/1.1.0/)
44
+ [![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/1.2.1/)
45
45
  [![Downloads](https://static.pepy.tech/badge/diffsptk)](https://pepy.tech/project/diffsptk)
46
46
  [![Python Version](https://img.shields.io/pypi/pyversions/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
47
- [![PyTorch Version](https://img.shields.io/badge/pytorch-1.11.0%20%7C%202.1.2-orange.svg)](https://pypi.python.org/pypi/diffsptk)
47
+ [![PyTorch Version](https://img.shields.io/badge/pytorch-1.11.0%20%7C%202.2.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
48
48
  [![PyPI Version](https://img.shields.io/pypi/v/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
49
49
  [![Codecov](https://codecov.io/gh/sp-nitech/diffsptk/branch/master/graph/badge.svg)](https://app.codecov.io/gh/sp-nitech/diffsptk)
50
50
  [![License](https://img.shields.io/github/license/sp-nitech/diffsptk.svg)](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
@@ -129,7 +129,7 @@ diffsptk.write("voiced.wav", x_voiced, sr)
129
129
  diffsptk.write("unvoiced.wav", x_unvoiced, sr)
130
130
  ```
131
131
 
132
- ### Mel-spectrogram and MFCC extraction
132
+ ### Mel-spectrogram, MFCC, and PLP extraction
133
133
  ```python
134
134
  import diffsptk
135
135
 
@@ -165,6 +165,16 @@ mfcc = diffsptk.MFCC(
165
165
  )
166
166
  Y = mfcc(X)
167
167
  print(Y.shape)
168
+
169
+ # Extract PLP.
170
+ plp = diffsptk.PLP(
171
+ plp_order=M,
172
+ n_channel=n_channel,
173
+ fft_length=n_fft,
174
+ sample_rate=sr,
175
+ )
176
+ Y = plp(X)
177
+ print(Y.shape)
168
178
  ```
169
179
 
170
180
  ### Subband decomposition
@@ -3,10 +3,10 @@ diffsptk
3
3
  *diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework.
4
4
 
5
5
  [![Latest Manual](https://img.shields.io/badge/docs-latest-blue.svg)](https://sp-nitech.github.io/diffsptk/latest/)
6
- [![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/1.1.0/)
6
+ [![Stable Manual](https://img.shields.io/badge/docs-stable-blue.svg)](https://sp-nitech.github.io/diffsptk/1.2.1/)
7
7
  [![Downloads](https://static.pepy.tech/badge/diffsptk)](https://pepy.tech/project/diffsptk)
8
8
  [![Python Version](https://img.shields.io/pypi/pyversions/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
9
- [![PyTorch Version](https://img.shields.io/badge/pytorch-1.11.0%20%7C%202.1.2-orange.svg)](https://pypi.python.org/pypi/diffsptk)
9
+ [![PyTorch Version](https://img.shields.io/badge/pytorch-1.11.0%20%7C%202.2.0-orange.svg)](https://pypi.python.org/pypi/diffsptk)
10
10
  [![PyPI Version](https://img.shields.io/pypi/v/diffsptk.svg)](https://pypi.python.org/pypi/diffsptk)
11
11
  [![Codecov](https://codecov.io/gh/sp-nitech/diffsptk/branch/master/graph/badge.svg)](https://app.codecov.io/gh/sp-nitech/diffsptk)
12
12
  [![License](https://img.shields.io/github/license/sp-nitech/diffsptk.svg)](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
@@ -91,7 +91,7 @@ diffsptk.write("voiced.wav", x_voiced, sr)
91
91
  diffsptk.write("unvoiced.wav", x_unvoiced, sr)
92
92
  ```
93
93
 
94
- ### Mel-spectrogram and MFCC extraction
94
+ ### Mel-spectrogram, MFCC, and PLP extraction
95
95
  ```python
96
96
  import diffsptk
97
97
 
@@ -127,6 +127,16 @@ mfcc = diffsptk.MFCC(
127
127
  )
128
128
  Y = mfcc(X)
129
129
  print(Y.shape)
130
+
131
+ # Extract PLP.
132
+ plp = diffsptk.PLP(
133
+ plp_order=M,
134
+ n_channel=n_channel,
135
+ fft_length=n_fft,
136
+ sample_rate=sr,
137
+ )
138
+ Y = plp(X)
139
+ print(Y.shape)
130
140
  ```
131
141
 
132
142
  ### Subband decomposition
@@ -1,4 +1,5 @@
1
1
  from .acorr import AutocorrelationAnalysis
2
+ from .alaw import ALawCompression
2
3
  from .ap import Aperiodicity
3
4
  from .b2mc import MLSADigitalFilterCoefficientsToMelCepstrum
4
5
  from .c2acr import CepstrumToAutocorrelation
@@ -27,6 +28,7 @@ from .gmm import GaussianMixtureModeling
27
28
  from .gmm import GaussianMixtureModeling as GMM
28
29
  from .gnorm import GeneralizedCepstrumGainNormalization
29
30
  from .grpdelay import GroupDelay
31
+ from .ialaw import ALawExpansion
30
32
  from .idct import InverseDiscreteCosineTransform
31
33
  from .idct import InverseDiscreteCosineTransform as IDCT
32
34
  from .ifreqt2 import SecondOrderAllPassInverseFrequencyTransform
@@ -78,6 +80,8 @@ from .pca import PrincipalComponentAnalysis
78
80
  from .pca import PrincipalComponentAnalysis as PCA
79
81
  from .phase import Phase
80
82
  from .pitch import Pitch
83
+ from .plp import PerceptualLinearPredictiveCoefficientsAnalysis
84
+ from .plp import PerceptualLinearPredictiveCoefficientsAnalysis as PLP
81
85
  from .pol_root import RootsToPolynomial
82
86
  from .poledf import AllPoleDigitalFilter
83
87
  from .pqmf import PseudoQuadratureMirrorFilterBanks
@@ -0,0 +1,75 @@
1
+ # ------------------------------------------------------------------------ #
2
+ # Copyright 2022 SPTK Working Group #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); #
5
+ # you may not use this file except in compliance with the License. #
6
+ # You may obtain a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ # ------------------------------------------------------------------------ #
16
+
17
+ import math
18
+
19
+ import torch
20
+ import torch.nn as nn
21
+
22
+
23
+ class ALawCompression(nn.Module):
24
+ """See `this page <https://sp-nitech.github.io/sptk/latest/main/alaw.html>`_
25
+ for details.
26
+
27
+ Parameters
28
+ ----------
29
+ abs_max : float > 0 [scalar]
30
+ Absolute maximum value of input.
31
+
32
+ a : float >= 1 [scalar]
33
+ Compression factor, :math:`A`.
34
+
35
+ """
36
+
37
+ def __init__(self, abs_max=1, a=87.6):
38
+ super(ALawCompression, self).__init__()
39
+
40
+ self.abs_max = abs_max
41
+ self.a = a
42
+
43
+ assert 0 < self.abs_max
44
+ assert 1 <= self.a
45
+
46
+ self.const = self.abs_max / (1 + math.log(self.a))
47
+
48
+ def forward(self, x):
49
+ """Compress waveform by A-law algorithm.
50
+
51
+ Parameters
52
+ ----------
53
+ x : Tensor [shape=(...,)]
54
+ Waveform.
55
+
56
+ Returns
57
+ -------
58
+ y : Tensor [shape=(...,)]
59
+ Compressed waveform.
60
+
61
+ Examples
62
+ --------
63
+ >>> x = diffsptk.ramp(4)
64
+ >>> alaw = diffsptk.ALawCompression(4)
65
+ >>> y = alaw(x)
66
+ >>> y
67
+ tensor([0.0000, 2.9868, 3.4934, 3.7897, 4.0000])
68
+
69
+ """
70
+ x_abs = x.abs() / self.abs_max
71
+ x1 = self.a * x_abs
72
+ x2 = 1 + torch.log(x1)
73
+ condition = x_abs < (1 / self.a)
74
+ y = self.const * torch.sign(x) * torch.where(condition, x1, x2)
75
+ return y
@@ -68,6 +68,5 @@ class CepstrumToAutocorrelation(nn.Module):
68
68
  """
69
69
  x = torch.fft.rfft(c, n=self.fft_length).real
70
70
  x = torch.exp(2 * x)
71
- r = torch.fft.hfft(x)[..., : self.acr_order + 1]
72
- r = r / self.fft_length
71
+ r = torch.fft.hfft(x, norm="forward")[..., : self.acr_order + 1]
73
72
  return r
@@ -59,11 +59,13 @@ class MelFilterBankAnalysis(nn.Module):
59
59
  f_min=0,
60
60
  f_max=None,
61
61
  floor=1e-5,
62
+ use_power=False,
62
63
  out_format="y",
63
64
  ):
64
65
  super(MelFilterBankAnalysis, self).__init__()
65
66
 
66
67
  self.floor = floor
68
+ self.use_power = use_power
67
69
  self.out_format = out_format
68
70
 
69
71
  if f_max is None:
@@ -87,6 +89,9 @@ class MelFilterBankAnalysis(nn.Module):
87
89
  def hz_to_mel(x):
88
90
  return 1127 * np.log(x / 700 + 1)
89
91
 
92
+ def mel_to_hz(x):
93
+ return 700 * (np.exp(x / 1127) - 1)
94
+
90
95
  lower_bin_index = max(1, int(f_min / sample_rate * fft_length + 1.5))
91
96
  upper_bin_index = min(
92
97
  fft_length // 2, int(f_max / sample_rate * fft_length + 0.5)
@@ -96,17 +101,18 @@ class MelFilterBankAnalysis(nn.Module):
96
101
  mel_max = hz_to_mel(f_max)
97
102
 
98
103
  seed = np.arange(1, n_channel + 2)
99
- freq = (mel_max - mel_min) / (n_channel + 1) * seed + mel_min
104
+ center_frequencies = (mel_max - mel_min) / (n_channel + 1) * seed + mel_min
105
+ self.center_frequencies = mel_to_hz(center_frequencies)
100
106
 
101
107
  seed = np.arange(lower_bin_index, upper_bin_index)
102
108
  mel = hz_to_mel(sample_rate * seed / fft_length)
103
- lower_channel_map = [np.argmax(0 < (m <= freq)) for m in mel]
109
+ lower_channel_map = [np.argmax(0 < (m <= center_frequencies)) for m in mel]
104
110
 
105
- diff = freq - np.insert(freq[:-1], 0, mel_min)
111
+ diff = center_frequencies - np.insert(center_frequencies[:-1], 0, mel_min)
106
112
  weights = np.zeros((fft_length // 2 + 1, n_channel))
107
113
  for i, k in enumerate(seed):
108
114
  m = lower_channel_map[i]
109
- w = (freq[max(0, m)] - mel[i]) / diff[max(0, m)]
115
+ w = (center_frequencies[max(0, m)] - mel[i]) / diff[max(0, m)]
110
116
  if 0 < m:
111
117
  weights[k, m - 1] += w
112
118
  if m < n_channel:
@@ -141,7 +147,8 @@ class MelFilterBankAnalysis(nn.Module):
141
147
  [3.3640, 3.4518, 2.7717, 0.5088]])
142
148
 
143
149
  """
144
- y = torch.matmul(torch.sqrt(x), self.H)
150
+ y = x if self.use_power else torch.sqrt(x)
151
+ y = torch.matmul(y, self.H)
145
152
  y = torch.log(torch.clip(y, min=self.floor))
146
153
  E = (2 * x[..., 1:-1]).sum(-1) + x[..., 0] + x[..., -1]
147
154
  E = torch.log(E / (2 * (x.size(-1) - 1))).unsqueeze(-1)
@@ -0,0 +1,77 @@
1
+ # ------------------------------------------------------------------------ #
2
+ # Copyright 2022 SPTK Working Group #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); #
5
+ # you may not use this file except in compliance with the License. #
6
+ # You may obtain a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ # ------------------------------------------------------------------------ #
16
+
17
+ import math
18
+
19
+ import torch
20
+ import torch.nn as nn
21
+
22
+
23
+ class ALawExpansion(nn.Module):
24
+ """See `this page <https://sp-nitech.github.io/sptk/latest/main/ialaw.html>`_
25
+ for details.
26
+
27
+ Parameters
28
+ ----------
29
+ abs_max : float > 0 [scalar]
30
+ Absolute maximum value of input.
31
+
32
+ a : float >= 1 [scalar]
33
+ Compression factor, :math:`A`.
34
+
35
+ """
36
+
37
+ def __init__(self, abs_max=1, a=87.6):
38
+ super(ALawExpansion, self).__init__()
39
+
40
+ self.abs_max = abs_max
41
+ self.a = a
42
+
43
+ assert 0 < self.abs_max
44
+ assert 1 <= self.a
45
+
46
+ self.const = self.abs_max / self.a
47
+ self.z = 1 + math.log(self.a)
48
+
49
+ def forward(self, y):
50
+ """Expand waveform by A-law algorithm.
51
+
52
+ Parameters
53
+ ----------
54
+ y : Tensor [shape=(...,)]
55
+ Compressed waveform.
56
+
57
+ Returns
58
+ -------
59
+ x : Tensor [shape=(...,)]
60
+ Waveform.
61
+
62
+ Examples
63
+ --------
64
+ >>> x = diffsptk.ramp(4)
65
+ >>> alaw = diffsptk.ALawCompression(4)
66
+ >>> ialaw = diffsptk.ALawExpansion(4)
67
+ >>> x2 = ialaw(alaw(x))
68
+ >>> x2
69
+ tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000])
70
+
71
+ """
72
+ y_abs = y.abs() / self.abs_max
73
+ y1 = self.z * y_abs
74
+ y2 = torch.exp(y1 - 1)
75
+ condition = y_abs < (1 / self.z)
76
+ x = self.const * torch.sign(y) * torch.where(condition, y1, y2)
77
+ return x
@@ -39,7 +39,7 @@ class MuLawExpansion(nn.Module):
39
39
  self.mu = mu
40
40
 
41
41
  assert 0 < self.abs_max
42
- assert 0 < self.mu
42
+ assert 1 <= self.mu
43
43
 
44
44
  self.const = self.abs_max / self.mu
45
45
 
@@ -66,6 +66,6 @@ class MuLawExpansion(nn.Module):
66
66
  tensor([0.0000, 1.0000, 2.0000, 3.0000, 4.0000])
67
67
 
68
68
  """
69
- y2 = y.abs() / self.abs_max
70
- x = self.const * torch.sign(y) * (torch.pow(1 + self.mu, y2) - 1)
69
+ y_abs = y.abs() / self.abs_max
70
+ x = self.const * torch.sign(y) * (torch.pow(1 + self.mu, y_abs) - 1)
71
71
  return x
@@ -23,7 +23,7 @@ from ..misc.utils import symmetric_toeplitz
23
23
 
24
24
  class LevinsonDurbin(nn.Module):
25
25
  """See `this page <https://sp-nitech.github.io/sptk/latest/main/levdur.html>`_
26
- for details.
26
+ for details. The implementation is based on a simple matrix inversion.
27
27
 
28
28
  Parameters
29
29
  ----------
@@ -17,6 +17,8 @@
17
17
  import torch.nn as nn
18
18
  import torch.nn.functional as F
19
19
 
20
+ from ..misc.utils import replicate1
21
+
20
22
 
21
23
  class LinearInterpolation(nn.Module):
22
24
  """Perform linear interpolation.
@@ -38,8 +40,6 @@ class LinearInterpolation(nn.Module):
38
40
 
39
41
  assert 1 <= self.upsampling_factor
40
42
 
41
- self.pad = nn.ReplicationPad1d((0, 1))
42
-
43
43
  def forward(self, x):
44
44
  """Interpolate filter coefficients.
45
45
 
@@ -76,8 +76,8 @@ class LinearInterpolation(nn.Module):
76
76
  assert x.dim() == 3, "Input must be 3D tensor"
77
77
  B, T, D = x.shape
78
78
 
79
- x = x.mT
80
- x = self.pad(x)
79
+ x = x.mT.contiguous() # (B, D, T)
80
+ x = replicate1(x, left=False)
81
81
  x = F.interpolate(
82
82
  x,
83
83
  size=T * self.upsampling_factor + 1,
@@ -73,6 +73,7 @@ class MelFrequencyCepstralCoefficientsAnalysis(nn.Module):
73
73
  self.mfcc_order = mfcc_order
74
74
 
75
75
  assert 1 <= self.mfcc_order < n_channel
76
+ assert 1 <= lifter
76
77
 
77
78
  if out_format == 0 or out_format == "y":
78
79
  self.format_func = lambda y, c, E: y
@@ -90,11 +91,10 @@ class MelFrequencyCepstralCoefficientsAnalysis(nn.Module):
90
91
  )
91
92
  self.dct = DiscreteCosineTransform(n_channel)
92
93
 
93
- m = np.arange(1, self.mfcc_order + 1)
94
- liftering_vector = 1 + (lifter / 2) * np.sin((np.pi / lifter) * m)
95
- self.register_buffer("liftering_vector", numpy_to_torch(liftering_vector))
96
-
97
- self.const = np.sqrt(2)
94
+ m = np.arange(self.mfcc_order + 1)
95
+ v = 1 + (lifter / 2) * np.sin((np.pi / lifter) * m)
96
+ v[0] = np.sqrt(2)
97
+ self.register_buffer("liftering_vector", numpy_to_torch(v))
98
98
 
99
99
  def forward(self, x):
100
100
  """Compute MFCC.
@@ -128,6 +128,6 @@ class MelFrequencyCepstralCoefficientsAnalysis(nn.Module):
128
128
  """
129
129
  y, E = self.fbank(x)
130
130
  y = self.dct(y)
131
- c = y[..., :1] * self.const
132
- y = y[..., 1 : self.mfcc_order + 1] * self.liftering_vector
131
+ y = y[..., : self.mfcc_order + 1] * self.liftering_vector
132
+ c, y = torch.split(y, [1, self.mfcc_order], dim=-1)
133
133
  return self.format_func(y, c, E)
@@ -20,6 +20,7 @@ import torch.nn as nn
20
20
  import torch.nn.functional as F
21
21
 
22
22
  from ..misc.utils import cexp
23
+ from ..misc.utils import check_size
23
24
  from ..misc.utils import clog
24
25
  from ..misc.utils import numpy_to_torch
25
26
  from .freqt import FrequencyTransform
@@ -80,6 +81,8 @@ class GeneralizedCepstrumToGeneralizedCepstrum(nn.Module):
80
81
  Output cepstrum.
81
82
 
82
83
  """
84
+ check_size(c1.size(-1), self.in_order + 1, "dimension of cepstrum")
85
+
83
86
  c01 = F.pad(c1[..., 1:], (1, 0))
84
87
  C1 = torch.fft.fft(c01, n=self.n_fft)
85
88
 
@@ -34,6 +34,7 @@ class ParcorCoefficientsToLinearPredictiveCoefficients(nn.Module):
34
34
  super(ParcorCoefficientsToLinearPredictiveCoefficients, self).__init__()
35
35
 
36
36
  self.lpc_order = lpc_order
37
+
37
38
  assert 0 <= self.lpc_order
38
39
 
39
40
  def forward(self, k):
@@ -0,0 +1,166 @@
1
+ # ------------------------------------------------------------------------ #
2
+ # Copyright 2022 SPTK Working Group #
3
+ # #
4
+ # Licensed under the Apache License, Version 2.0 (the "License"); #
5
+ # you may not use this file except in compliance with the License. #
6
+ # You may obtain a copy of the License at #
7
+ # #
8
+ # http://www.apache.org/licenses/LICENSE-2.0 #
9
+ # #
10
+ # Unless required by applicable law or agreed to in writing, software #
11
+ # distributed under the License is distributed on an "AS IS" BASIS, #
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
13
+ # See the License for the specific language governing permissions and #
14
+ # limitations under the License. #
15
+ # ------------------------------------------------------------------------ #
16
+
17
+ import numpy as np
18
+ import torch
19
+ import torch.nn as nn
20
+
21
+ from ..misc.utils import numpy_to_torch
22
+ from ..misc.utils import replicate1
23
+ from .fbank import MelFilterBankAnalysis
24
+ from .levdur import LevinsonDurbin
25
+ from .mgc2mgc import MelGeneralizedCepstrumToMelGeneralizedCepstrum
26
+
27
+
28
+ class PerceptualLinearPredictiveCoefficientsAnalysis(nn.Module):
29
+ """See `this page <https://sp-nitech.github.io/sptk/latest/main/plp.html>`_
30
+ for details.
31
+
32
+ Parameters
33
+ ----------
34
+ mfcc_order : int >= 1 [scalar]
35
+ Order of MFCC, :math:`M`.
36
+
37
+ n_channel : int >= 1 [scalar]
38
+ Number of mel-filter banks, :math:`C`.
39
+
40
+ fft_length : int >= 2 [scalar]
41
+ Number of FFT bins, :math:`L`.
42
+
43
+ sample_rate : int >= 1 [scalar]
44
+ Sample rate in Hz.
45
+
46
+ lifter : int >= 1 [scalar]
47
+ Liftering coefficient.
48
+
49
+ compression_factor : float > 0 [scalar]
50
+ Amplitude compression factor.
51
+
52
+ f_min : float >= 0 [scalar]
53
+ Minimum frequency in Hz.
54
+
55
+ f_max : float <= sample_rate // 2 [scalar]
56
+ Maximum frequency in Hz.
57
+
58
+ floor : float > 0 [scalar]
59
+ Minimum mel-filter bank output in linear scale.
60
+
61
+ out_format : ['y', 'yE', 'yc', 'ycE']
62
+ `y` is MFCC, `c` is C0, and `E` is energy.
63
+
64
+ n_fft : int >> :math:`M` [scalar]
65
+ Number of FFT bins. Accurate conversion requires the large value.
66
+
67
+ """
68
+
69
+ def __init__(
70
+ self,
71
+ plp_order,
72
+ n_channel,
73
+ fft_length,
74
+ sample_rate,
75
+ lifter=1,
76
+ compression_factor=0.33,
77
+ out_format="y",
78
+ n_fft=512,
79
+ **fbank_kwargs,
80
+ ):
81
+ super(PerceptualLinearPredictiveCoefficientsAnalysis, self).__init__()
82
+
83
+ self.plp_order = plp_order
84
+ self.compression_factor = compression_factor
85
+
86
+ assert 1 <= self.plp_order < n_channel
87
+ assert 1 <= lifter
88
+ assert 0 < self.compression_factor
89
+
90
+ if out_format == 0 or out_format == "y":
91
+ self.format_func = lambda y, c, E: y
92
+ elif out_format == 1 or out_format == "yE":
93
+ self.format_func = lambda y, c, E: torch.cat((y, E), dim=-1)
94
+ elif out_format == 2 or out_format == "yc":
95
+ self.format_func = lambda y, c, E: torch.cat((y, c), dim=-1)
96
+ elif out_format == 3 or out_format == "ycE":
97
+ self.format_func = lambda y, c, E: torch.cat((y, c, E), dim=-1)
98
+ else:
99
+ raise ValueError(f"out_format {out_format} is not supported")
100
+
101
+ self.fbank = MelFilterBankAnalysis(
102
+ n_channel,
103
+ fft_length,
104
+ sample_rate,
105
+ use_power=True,
106
+ out_format="y,E",
107
+ **fbank_kwargs,
108
+ )
109
+ self.levdur = LevinsonDurbin(self.plp_order)
110
+ self.lpc2c = MelGeneralizedCepstrumToMelGeneralizedCepstrum(
111
+ self.plp_order,
112
+ self.plp_order,
113
+ in_gamma=-1,
114
+ in_norm=True,
115
+ in_mul=True,
116
+ n_fft=n_fft,
117
+ )
118
+
119
+ f = self.fbank.center_frequencies[:-1] ** 2
120
+ e = (f / (f + 1.6e5)) ** 2 * (f + 1.44e6) / (f + 9.61e6)
121
+ self.register_buffer("equal_loudness_curve", numpy_to_torch(e))
122
+
123
+ m = np.arange(self.plp_order + 1)
124
+ v = 1 + (lifter / 2) * np.sin((np.pi / lifter) * m)
125
+ v[0] = 2
126
+ self.register_buffer("liftering_vector", numpy_to_torch(v))
127
+
128
+ def forward(self, x):
129
+ """Compute PLP.
130
+
131
+ Parameters
132
+ ----------
133
+ x : Tensor [shape=(..., L/2+1)]
134
+ Power spectrum.
135
+
136
+ Returns
137
+ -------
138
+ y : Tensor [shape=(..., M)]
139
+ PLP without C0.
140
+
141
+ E : Tensor [shape=(..., 1)]
142
+ Energy.
143
+
144
+ c : Tensor [shape=(..., 1)]
145
+ C0.
146
+
147
+ Examples
148
+ --------
149
+ >>> x = diffsptk.ramp(19)
150
+ >>> stft = diffsptk.STFT(frame_length=10, frame_period=10, fft_length=32)
151
+ >>> plp = diffsptk.PLP(4, 8, 32, 8000)
152
+ >>> y = plp(stft(x))
153
+ >>> y
154
+ tensor([[-0.2896, -0.2356, -0.0586, -0.0387],
155
+ [ 0.4468, -0.5820, 0.0104, -0.0505]])
156
+
157
+ """
158
+ y, E = self.fbank(x)
159
+ y = (torch.exp(y) * self.equal_loudness_curve) ** self.compression_factor
160
+ y = replicate1(y)
161
+ y = torch.fft.hfft(y, norm="forward")[..., : self.plp_order + 1].real
162
+ y = self.levdur(y)
163
+ y = self.lpc2c(y)
164
+ y *= self.liftering_vector
165
+ c, y = torch.split(y, [1, self.plp_order], dim=-1)
166
+ return self.format_func(y, c, E)
@@ -81,13 +81,15 @@ class ShortTermFourierTransform(nn.Module):
81
81
  self.stft = nn.Sequential(
82
82
  Frame(frame_length, frame_period, center=center, zmean=zmean),
83
83
  Window(frame_length, fft_length, norm=norm, window=window),
84
- Lambda(torch.fft.rfft)
85
- if out_format == "complex"
86
- else Spectrum(
87
- fft_length,
88
- out_format=out_format,
89
- eps=eps,
90
- relative_floor=relative_floor,
84
+ (
85
+ Lambda(torch.fft.rfft)
86
+ if out_format == "complex"
87
+ else Spectrum(
88
+ fft_length,
89
+ out_format=out_format,
90
+ eps=eps,
91
+ relative_floor=relative_floor,
92
+ )
91
93
  ),
92
94
  )
93
95
 
@@ -41,7 +41,7 @@ class MuLawCompression(nn.Module):
41
41
  self.mu = mu
42
42
 
43
43
  assert 0 < self.abs_max
44
- assert 0 < self.mu
44
+ assert 1 <= self.mu
45
45
 
46
46
  self.const = self.abs_max / math.log1p(self.mu)
47
47
 
@@ -67,6 +67,6 @@ class MuLawCompression(nn.Module):
67
67
  tensor([0.0000, 3.0084, 3.5028, 3.7934, 4.0000])
68
68
 
69
69
  """
70
- x2 = x.abs() / self.abs_max
71
- y = self.const * torch.sign(x) * torch.log1p(self.mu * x2)
70
+ x_abs = x.abs() / self.abs_max
71
+ y = self.const * torch.sign(x) * torch.log1p(self.mu * x_abs)
72
72
  return y
@@ -17,6 +17,8 @@
17
17
  import torch
18
18
  import torch.nn as nn
19
19
 
20
+ from ..misc.utils import replicate1
21
+
20
22
 
21
23
  class ZeroCrossingAnalysis(nn.Module):
22
24
  """See `this page <https://sp-nitech.github.io/sptk/latest/main/zcross.html>`_
@@ -65,7 +67,7 @@ class ZeroCrossingAnalysis(nn.Module):
65
67
 
66
68
  """
67
69
  x = torch.sign(x)
68
- x = torch.cat((x[..., :1], x), dim=-1)
70
+ x = replicate1(x, right=False)
69
71
  x = x.unfold(-1, self.frame_length + 1, self.frame_length)
70
72
  z = 0.5 * (x[..., 1:] - x[..., :-1]).abs().sum(-1)
71
73
  if self.norm:
@@ -74,13 +74,23 @@ def numpy_to_torch(x):
74
74
 
75
75
 
76
76
  def to_3d(x):
77
- d = x.dim()
78
- if d == 1:
79
- y = x.view(1, 1, -1)
80
- elif d == 2:
81
- y = x.unsqueeze(1)
82
- else:
83
- y = x.view(-1, 1, x.size(-1))
77
+ y = x.view(-1, 1, x.size(-1))
78
+ return y
79
+
80
+
81
+ def reflect(x):
82
+ d = x.size(-1)
83
+ y = x.view(-1, d)
84
+ y = F.pad(y, (d - 1, 0), mode="reflect")
85
+ y = y.view(*x.size()[:-1], -1)
86
+ return y
87
+
88
+
89
+ def replicate1(x, left=True, right=True):
90
+ d = x.size(-1)
91
+ y = x.view(-1, d)
92
+ y = F.pad(y, (1 if left else 0, 1 if right else 0), mode="replicate")
93
+ y = y.view(*x.size()[:-1], -1)
84
94
  return y
85
95
 
86
96
 
@@ -177,7 +187,7 @@ def get_gamma(gamma, c):
177
187
 
178
188
  def symmetric_toeplitz(x):
179
189
  d = x.size(-1)
180
- xx = torch.cat((x[..., 1:].flip(-1), x), dim=-1)
190
+ xx = reflect(x)
181
191
  X = xx.unfold(-1, d, 1).flip(-2)
182
192
  return X
183
193
 
@@ -0,0 +1 @@
1
+ __version__ = "1.2.1"
@@ -33,7 +33,7 @@
33
33
  "torchaudio >= 0.11.0",
34
34
  "torchcrepe >= 0.0.21",
35
35
  "torchlpc >= 0.2.0",
36
- "vector-quantize-pytorch >= 0.8.0",
36
+ "vector-quantize-pytorch >= 0.8.0, <= 1.12.12",
37
37
  "numpy",
38
38
  ]
39
39
  dynamic = ["version"]
@@ -1 +0,0 @@
1
- __version__ = "1.1.0"
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes