diffsptk 3.0.1__tar.gz → 3.1.0__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.
- {diffsptk-3.0.1 → diffsptk-3.1.0}/PKG-INFO +14 -25
- {diffsptk-3.0.1 → diffsptk-3.1.0}/README.md +12 -23
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/functional.py +85 -2
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/__init__.py +3 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ap.py +3 -9
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/dfs.py +1 -1
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/drc.py +1 -1
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/fbank.py +13 -2
- diffsptk-3.1.0/diffsptk/modules/fftr.py +144 -0
- diffsptk-3.1.0/diffsptk/modules/ifftr.py +132 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/imdct.py +44 -10
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/imdst.py +13 -3
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/istft.py +39 -17
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lbg.py +4 -3
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mdct.py +49 -9
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mdst.py +13 -3
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mfcc.py +9 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mglsadf.py +1 -1
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/pitch_spec.py +6 -18
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/plp.py +6 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/spec.py +31 -9
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/stft.py +40 -5
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/unframe.py +25 -7
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/window.py +28 -12
- diffsptk-3.1.0/diffsptk/modules/world_synth.py +297 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/librosa/constantq.py +28 -19
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/librosa/filters.py +48 -41
- diffsptk-3.1.0/diffsptk/third_party/librosa/typing.py +29 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/librosa/util.py +23 -10
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/world/common.py +70 -15
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/utils/private.py +9 -3
- diffsptk-3.1.0/diffsptk/version.py +1 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/pyproject.toml +4 -5
- diffsptk-3.0.1/diffsptk/version.py +0 -1
- {diffsptk-3.0.1 → diffsptk-3.1.0}/.gitignore +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/CITATION.cff +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/LICENSE +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/__init__.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/acorr.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/acr2csm.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/alaw.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/b2mc.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/base.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/c2acr.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/c2mpir.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/c2ndps.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/cdist.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/chroma.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/cqt.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/csm2acr.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/dct.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/decimate.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/delay.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/delta.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/dequantize.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/df2.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/dht.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/dst.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/entropy.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/excite.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/fftcep.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/flux.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/frame.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/freqt.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/freqt2.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/gammatone.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/gmm.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/gnorm.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/grpdelay.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/hilbert.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/hilbert2.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/histogram.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ialaw.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ica.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/icqt.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/idct.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/idht.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/idst.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ifreqt2.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/igammatone.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ignorm.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/imglsadf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/imsvq.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/interpolate.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ipnorm.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ipqmf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/is2par.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/iulaw.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ivq.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lar2par.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/levdur.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/linear_intpl.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lpc.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lpc2lsp.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lpc2par.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lpccheck.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lsp2lpc.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lsp2sp.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/lspcheck.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/magic_intpl.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mc2b.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mcep.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mcpf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mgc2mgc.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mgc2sp.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mgcep.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mlpg.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mlsacheck.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/mpir2c.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/msvq.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ndps2c.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/nmf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/norm0.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/par2is.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/par2lar.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/par2lpc.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/pca.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/phase.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/pitch.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/pnorm.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/pol_root.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/poledf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/pqmf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/quantize.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/rlevdur.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/rmse.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/root_pol.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/smcep.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/snr.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/ulaw.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/vq.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/wht.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/yingram.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/zcross.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/modules/zerodf.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/signals.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/librosa/LICENSE +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/librosa/__init__.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/world/LICENSE +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/third_party/world/__init__.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/typing.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/utils/__init__.py +0 -0
- {diffsptk-3.0.1 → diffsptk-3.1.0}/diffsptk/utils/public.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: diffsptk
|
|
3
|
-
Version: 3.0
|
|
3
|
+
Version: 3.1.0
|
|
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/
|
|
@@ -19,7 +19,7 @@ Classifier: Programming Language :: Python :: 3.12
|
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
20
20
|
Requires-Python: >=3.10
|
|
21
21
|
Requires-Dist: numpy>=1.23.0
|
|
22
|
-
Requires-Dist: penn
|
|
22
|
+
Requires-Dist: penn<1.0.0,>=0.0.1
|
|
23
23
|
Requires-Dist: pylstraight>=0.1.0
|
|
24
24
|
Requires-Dist: scipy>=1.12.0
|
|
25
25
|
Requires-Dist: soundfile>=0.10.2
|
|
@@ -47,11 +47,11 @@ Description-Content-Type: text/markdown
|
|
|
47
47
|
|
|
48
48
|
*diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework.
|
|
49
49
|
|
|
50
|
-
[](https://sp-nitech.github.io/diffsptk/3.0
|
|
50
|
+
[](https://sp-nitech.github.io/diffsptk/3.1.0/)
|
|
51
51
|
[](https://pepy.tech/project/diffsptk)
|
|
52
52
|
[](https://clickpy.clickhouse.com/dashboard/diffsptk)
|
|
53
53
|
[](https://pypi.python.org/pypi/diffsptk)
|
|
54
|
-
[](https://pypi.python.org/pypi/diffsptk)
|
|
55
55
|
[](https://pypi.python.org/pypi/diffsptk)
|
|
56
56
|
[](https://app.codecov.io/gh/sp-nitech/diffsptk)
|
|
57
57
|
[](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
|
|
@@ -65,7 +65,7 @@ Description-Content-Type: text/markdown
|
|
|
65
65
|
|
|
66
66
|
## Documentation
|
|
67
67
|
|
|
68
|
-
- See [this page](https://sp-nitech.github.io/diffsptk/3.0
|
|
68
|
+
- See [this page](https://sp-nitech.github.io/diffsptk/3.1.0/) for the reference manual.
|
|
69
69
|
- Our [paper](https://www.isca-speech.org/archive/ssw_2023/yoshimura23_ssw.html) is available on the ISCA Archive.
|
|
70
70
|
|
|
71
71
|
## Installation
|
|
@@ -167,7 +167,7 @@ diffsptk.write("voiced.wav", x_voiced, sr)
|
|
|
167
167
|
diffsptk.write("unvoiced.wav", x_unvoiced, sr)
|
|
168
168
|
```
|
|
169
169
|
|
|
170
|
-
### WORLD analysis and
|
|
170
|
+
### WORLD analysis and synthesis
|
|
171
171
|
|
|
172
172
|
```python
|
|
173
173
|
import diffsptk
|
|
@@ -208,26 +208,15 @@ pitch_spec = diffsptk.PitchAdaptiveSpectralAnalysis(
|
|
|
208
208
|
algorithm="cheap-trick",
|
|
209
209
|
out_format="power",
|
|
210
210
|
)
|
|
211
|
-
|
|
211
|
+
S = pitch_spec(x, f0)
|
|
212
212
|
|
|
213
|
-
#
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
excite = diffsptk.ExcitationGeneration(frame_period=fp, unvoiced_region="zeros")
|
|
221
|
-
p = (sr / f0).nan_to_num(posinf=0)
|
|
222
|
-
pulse = excite(p)
|
|
223
|
-
noise = diffsptk.nrand(len(pulse) - 1)
|
|
224
|
-
|
|
225
|
-
# Make mixed excitation signal and reconstruct x.
|
|
226
|
-
mlsa = diffsptk.MLSA(filter_order=M, frame_period=fp, alpha=alpha, taylor_order=20)
|
|
227
|
-
e_p = pulse - mlsa(pulse, mc_a)
|
|
228
|
-
e_a = mlsa(noise, mc_a)
|
|
229
|
-
e = e_p + e_a
|
|
230
|
-
x_hat = mlsa(e, mc_h)
|
|
213
|
+
# Reconstruct x.
|
|
214
|
+
world_synth = diffsptk.WorldSynthesis(
|
|
215
|
+
frame_period=fp,
|
|
216
|
+
sample_rate=sr,
|
|
217
|
+
fft_length=n_fft,
|
|
218
|
+
)
|
|
219
|
+
x_hat = world_synth(f0, A, S)
|
|
231
220
|
|
|
232
221
|
# Write reconstructed waveform.
|
|
233
222
|
diffsptk.write("reconst.wav", x_hat, sr)
|
|
@@ -2,11 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
*diffsptk* is a differentiable version of [SPTK](https://github.com/sp-nitech/SPTK) based on the PyTorch framework.
|
|
4
4
|
|
|
5
|
-
[](https://sp-nitech.github.io/diffsptk/3.0
|
|
5
|
+
[](https://sp-nitech.github.io/diffsptk/3.1.0/)
|
|
6
6
|
[](https://pepy.tech/project/diffsptk)
|
|
7
7
|
[](https://clickpy.clickhouse.com/dashboard/diffsptk)
|
|
8
8
|
[](https://pypi.python.org/pypi/diffsptk)
|
|
9
|
-
[](https://pypi.python.org/pypi/diffsptk)
|
|
10
10
|
[](https://pypi.python.org/pypi/diffsptk)
|
|
11
11
|
[](https://app.codecov.io/gh/sp-nitech/diffsptk)
|
|
12
12
|
[](https://github.com/sp-nitech/diffsptk/blob/master/LICENSE)
|
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
|
|
21
21
|
## Documentation
|
|
22
22
|
|
|
23
|
-
- See [this page](https://sp-nitech.github.io/diffsptk/3.0
|
|
23
|
+
- See [this page](https://sp-nitech.github.io/diffsptk/3.1.0/) for the reference manual.
|
|
24
24
|
- Our [paper](https://www.isca-speech.org/archive/ssw_2023/yoshimura23_ssw.html) is available on the ISCA Archive.
|
|
25
25
|
|
|
26
26
|
## Installation
|
|
@@ -122,7 +122,7 @@ diffsptk.write("voiced.wav", x_voiced, sr)
|
|
|
122
122
|
diffsptk.write("unvoiced.wav", x_unvoiced, sr)
|
|
123
123
|
```
|
|
124
124
|
|
|
125
|
-
### WORLD analysis and
|
|
125
|
+
### WORLD analysis and synthesis
|
|
126
126
|
|
|
127
127
|
```python
|
|
128
128
|
import diffsptk
|
|
@@ -163,26 +163,15 @@ pitch_spec = diffsptk.PitchAdaptiveSpectralAnalysis(
|
|
|
163
163
|
algorithm="cheap-trick",
|
|
164
164
|
out_format="power",
|
|
165
165
|
)
|
|
166
|
-
|
|
166
|
+
S = pitch_spec(x, f0)
|
|
167
167
|
|
|
168
|
-
#
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
excite = diffsptk.ExcitationGeneration(frame_period=fp, unvoiced_region="zeros")
|
|
176
|
-
p = (sr / f0).nan_to_num(posinf=0)
|
|
177
|
-
pulse = excite(p)
|
|
178
|
-
noise = diffsptk.nrand(len(pulse) - 1)
|
|
179
|
-
|
|
180
|
-
# Make mixed excitation signal and reconstruct x.
|
|
181
|
-
mlsa = diffsptk.MLSA(filter_order=M, frame_period=fp, alpha=alpha, taylor_order=20)
|
|
182
|
-
e_p = pulse - mlsa(pulse, mc_a)
|
|
183
|
-
e_a = mlsa(noise, mc_a)
|
|
184
|
-
e = e_p + e_a
|
|
185
|
-
x_hat = mlsa(e, mc_h)
|
|
168
|
+
# Reconstruct x.
|
|
169
|
+
world_synth = diffsptk.WorldSynthesis(
|
|
170
|
+
frame_period=fp,
|
|
171
|
+
sample_rate=sr,
|
|
172
|
+
fft_length=n_fft,
|
|
173
|
+
)
|
|
174
|
+
x_hat = world_synth(f0, A, S)
|
|
186
175
|
|
|
187
176
|
# Write reconstructed waveform.
|
|
188
177
|
diffsptk.write("reconst.wav", x_hat, sr)
|
|
@@ -593,6 +593,7 @@ def fbank(
|
|
|
593
593
|
f_min: float = 0,
|
|
594
594
|
f_max: float | None = None,
|
|
595
595
|
floor: float = 1e-5,
|
|
596
|
+
gamma: float = 0,
|
|
596
597
|
use_power: bool = False,
|
|
597
598
|
out_format: str = "y",
|
|
598
599
|
) -> tuple[Tensor, Tensor] | Tensor:
|
|
@@ -618,6 +619,9 @@ def fbank(
|
|
|
618
619
|
floor : float > 0
|
|
619
620
|
The minimum mel filter bank output in linear scale.
|
|
620
621
|
|
|
622
|
+
gamma : float in [-1, 1]
|
|
623
|
+
The parameter of the generalized logarithmic function.
|
|
624
|
+
|
|
621
625
|
use_power : bool
|
|
622
626
|
If True, use the power spectrum instead of the amplitude spectrum.
|
|
623
627
|
|
|
@@ -641,6 +645,7 @@ def fbank(
|
|
|
641
645
|
f_min=f_min,
|
|
642
646
|
f_max=f_max,
|
|
643
647
|
floor=floor,
|
|
648
|
+
gamma=gamma,
|
|
644
649
|
use_power=use_power,
|
|
645
650
|
out_format=out_format,
|
|
646
651
|
)
|
|
@@ -672,6 +677,33 @@ def fftcep(x: Tensor, cep_order: int, accel: float = 0, n_iter: int = 0) -> Tens
|
|
|
672
677
|
return nn.CepstralAnalysis._func(x, cep_order=cep_order, accel=accel, n_iter=n_iter)
|
|
673
678
|
|
|
674
679
|
|
|
680
|
+
def fftr(
|
|
681
|
+
x: Tensor, fft_length: int | None = None, out_format: str = "complex"
|
|
682
|
+
) -> Tensor:
|
|
683
|
+
"""Compute FFT of a real signal.
|
|
684
|
+
|
|
685
|
+
Parameters
|
|
686
|
+
----------
|
|
687
|
+
x : Tensor [shape=(..., N)]
|
|
688
|
+
The real input signal.
|
|
689
|
+
|
|
690
|
+
fft_length : int >= 2 or None
|
|
691
|
+
The FFT length, :math:`L`.
|
|
692
|
+
|
|
693
|
+
out_format : ['complex', 'real', 'imaginary', 'amplitude', 'power']
|
|
694
|
+
The output format.
|
|
695
|
+
|
|
696
|
+
Returns
|
|
697
|
+
-------
|
|
698
|
+
out : Tensor [shape=(..., L/2+1)]
|
|
699
|
+
The output spectrum.
|
|
700
|
+
|
|
701
|
+
"""
|
|
702
|
+
return nn.RealValuedFastFourierTransform._func(
|
|
703
|
+
x, fft_length=fft_length, out_format=out_format
|
|
704
|
+
)
|
|
705
|
+
|
|
706
|
+
|
|
675
707
|
def flux(
|
|
676
708
|
x: Tensor,
|
|
677
709
|
y: Tensor | None = None,
|
|
@@ -1051,6 +1083,26 @@ def idst(y: Tensor, dst_type: int = 2) -> Tensor:
|
|
|
1051
1083
|
return nn.InverseDiscreteSineTransform._func(y, dst_type=dst_type)
|
|
1052
1084
|
|
|
1053
1085
|
|
|
1086
|
+
def ifftr(y: Tensor, out_length: int | None = None) -> Tensor:
|
|
1087
|
+
"""Compute inverse FFT of a complex spectrum.
|
|
1088
|
+
|
|
1089
|
+
Parameters
|
|
1090
|
+
----------
|
|
1091
|
+
y : Tensor [shape=(..., L/2+1)]
|
|
1092
|
+
The complex input spectrum.
|
|
1093
|
+
|
|
1094
|
+
out_length : int or None
|
|
1095
|
+
The output length, :math:`N`.
|
|
1096
|
+
|
|
1097
|
+
Returns
|
|
1098
|
+
-------
|
|
1099
|
+
out : Tensor [shape=(..., N)]
|
|
1100
|
+
The real output signal.
|
|
1101
|
+
|
|
1102
|
+
"""
|
|
1103
|
+
return nn.RealValuedInverseFastFourierTransform._func(y, out_length=out_length)
|
|
1104
|
+
|
|
1105
|
+
|
|
1054
1106
|
def ifreqt2(
|
|
1055
1107
|
c: Tensor, out_order: int, alpha: float = 0, theta: float = 0, n_fft: int = 512
|
|
1056
1108
|
) -> Tensor:
|
|
@@ -1059,7 +1111,7 @@ def ifreqt2(
|
|
|
1059
1111
|
Parameters
|
|
1060
1112
|
----------
|
|
1061
1113
|
c : Tensor [shape=(..., M1+1)]
|
|
1062
|
-
|
|
1114
|
+
The cepstral coefficients.
|
|
1063
1115
|
|
|
1064
1116
|
out_order : int >= 0
|
|
1065
1117
|
The order of the output sequence, :math:`M_2`.
|
|
@@ -1243,6 +1295,7 @@ def istft(
|
|
|
1243
1295
|
center: bool = True,
|
|
1244
1296
|
window: str = "blackman",
|
|
1245
1297
|
norm: str = "power",
|
|
1298
|
+
symmetric: bool = True,
|
|
1246
1299
|
) -> Tensor:
|
|
1247
1300
|
"""Compute inverse short-time Fourier transform.
|
|
1248
1301
|
|
|
@@ -1273,6 +1326,9 @@ def istft(
|
|
|
1273
1326
|
norm : ['none', 'power', 'magnitude']
|
|
1274
1327
|
The normalization type of the window.
|
|
1275
1328
|
|
|
1329
|
+
symmetric : bool
|
|
1330
|
+
If True, the window is symmetric, otherwise periodic.
|
|
1331
|
+
|
|
1276
1332
|
Returns
|
|
1277
1333
|
-------
|
|
1278
1334
|
out : Tensor [shape=(..., T)]
|
|
@@ -1288,6 +1344,7 @@ def istft(
|
|
|
1288
1344
|
center=center,
|
|
1289
1345
|
window=window,
|
|
1290
1346
|
norm=norm,
|
|
1347
|
+
symmetric=symmetric,
|
|
1291
1348
|
)
|
|
1292
1349
|
|
|
1293
1350
|
|
|
@@ -1766,6 +1823,7 @@ def mfcc(
|
|
|
1766
1823
|
f_min: float = 0,
|
|
1767
1824
|
f_max: float | None = None,
|
|
1768
1825
|
floor: float = 1e-5,
|
|
1826
|
+
gamma: float = 0,
|
|
1769
1827
|
out_format: str = "y",
|
|
1770
1828
|
) -> Tensor:
|
|
1771
1829
|
"""Compute the MFCC from the power spectrum.
|
|
@@ -1796,6 +1854,9 @@ def mfcc(
|
|
|
1796
1854
|
floor : float > 0
|
|
1797
1855
|
The minimum mel filter bank output in linear scale.
|
|
1798
1856
|
|
|
1857
|
+
gamma : float in [-1, 1]
|
|
1858
|
+
The parameter of the generalized logarithmic function.
|
|
1859
|
+
|
|
1799
1860
|
out_format : ['y', 'yE', 'yc', 'ycE']
|
|
1800
1861
|
`y` is MFCC, `c` is C0, and `E` is energy.
|
|
1801
1862
|
|
|
@@ -1820,6 +1881,7 @@ def mfcc(
|
|
|
1820
1881
|
f_min=f_min,
|
|
1821
1882
|
f_max=f_max,
|
|
1822
1883
|
floor=floor,
|
|
1884
|
+
gamma=gamma,
|
|
1823
1885
|
out_format=out_format,
|
|
1824
1886
|
)
|
|
1825
1887
|
|
|
@@ -2202,6 +2264,7 @@ def plp(
|
|
|
2202
2264
|
f_min: float = 0,
|
|
2203
2265
|
f_max: float | None = None,
|
|
2204
2266
|
floor: float = 1e-5,
|
|
2267
|
+
gamma: float = 0,
|
|
2205
2268
|
n_fft: int = 512,
|
|
2206
2269
|
out_format: str = "y",
|
|
2207
2270
|
) -> Tensor:
|
|
@@ -2236,6 +2299,9 @@ def plp(
|
|
|
2236
2299
|
floor : float > 0
|
|
2237
2300
|
The minimum mel filter bank output in linear scale.
|
|
2238
2301
|
|
|
2302
|
+
gamma : float in [-1, 1]
|
|
2303
|
+
The parameter of the generalized logarithmic function.
|
|
2304
|
+
|
|
2239
2305
|
n_fft : int >> M
|
|
2240
2306
|
The number of FFT bins for the conversion from LPC to cepstrum.
|
|
2241
2307
|
|
|
@@ -2264,6 +2330,7 @@ def plp(
|
|
|
2264
2330
|
f_min=f_min,
|
|
2265
2331
|
f_max=f_max,
|
|
2266
2332
|
floor=floor,
|
|
2333
|
+
gamma=gamma,
|
|
2267
2334
|
n_fft=n_fft,
|
|
2268
2335
|
out_format=out_format,
|
|
2269
2336
|
)
|
|
@@ -2593,6 +2660,7 @@ def stft(
|
|
|
2593
2660
|
mode: str = "constant",
|
|
2594
2661
|
window: str = "blackman",
|
|
2595
2662
|
norm: str = "power",
|
|
2663
|
+
symmetric: bool = True,
|
|
2596
2664
|
eps: float = 1e-9,
|
|
2597
2665
|
relative_floor: float | None = None,
|
|
2598
2666
|
out_format: str = "power",
|
|
@@ -2629,6 +2697,9 @@ def stft(
|
|
|
2629
2697
|
norm : ['none', 'power', 'magnitude']
|
|
2630
2698
|
The normalization type of the window.
|
|
2631
2699
|
|
|
2700
|
+
symmetric : bool
|
|
2701
|
+
If True, the window is symmetric, otherwise periodic.
|
|
2702
|
+
|
|
2632
2703
|
eps : float >= 0
|
|
2633
2704
|
A small value added to the power spectrum.
|
|
2634
2705
|
|
|
@@ -2654,6 +2725,7 @@ def stft(
|
|
|
2654
2725
|
mode=mode,
|
|
2655
2726
|
window=window,
|
|
2656
2727
|
norm=norm,
|
|
2728
|
+
symmetric=symmetric,
|
|
2657
2729
|
eps=eps,
|
|
2658
2730
|
relative_floor=relative_floor,
|
|
2659
2731
|
out_format=out_format,
|
|
@@ -2691,6 +2763,7 @@ def unframe(
|
|
|
2691
2763
|
center: bool = True,
|
|
2692
2764
|
window: str = "rectangular",
|
|
2693
2765
|
norm: str = "none",
|
|
2766
|
+
symmetric: bool = True,
|
|
2694
2767
|
) -> Tensor:
|
|
2695
2768
|
"""Revert framed waveform.
|
|
2696
2769
|
|
|
@@ -2715,6 +2788,9 @@ def unframe(
|
|
|
2715
2788
|
norm : ['none', 'power', 'magnitude']
|
|
2716
2789
|
The normalization type of the window.
|
|
2717
2790
|
|
|
2791
|
+
symmetric : bool
|
|
2792
|
+
If True, the window is symmetric, otherwise periodic.
|
|
2793
|
+
|
|
2718
2794
|
Returns
|
|
2719
2795
|
-------
|
|
2720
2796
|
out : Tensor [shape=(..., T)]
|
|
@@ -2728,6 +2804,7 @@ def unframe(
|
|
|
2728
2804
|
center=center,
|
|
2729
2805
|
window=window,
|
|
2730
2806
|
norm=norm,
|
|
2807
|
+
symmetric=symmetric,
|
|
2731
2808
|
)
|
|
2732
2809
|
|
|
2733
2810
|
|
|
@@ -2757,6 +2834,7 @@ def window(
|
|
|
2757
2834
|
*,
|
|
2758
2835
|
window: str = "blackman",
|
|
2759
2836
|
norm: str = "power",
|
|
2837
|
+
symmetric: bool = True,
|
|
2760
2838
|
) -> Tensor:
|
|
2761
2839
|
"""Apply a window function to the given waveform.
|
|
2762
2840
|
|
|
@@ -2776,13 +2854,18 @@ def window(
|
|
|
2776
2854
|
norm : ['none', 'power', 'magnitude']
|
|
2777
2855
|
The normalization type of the window.
|
|
2778
2856
|
|
|
2857
|
+
symmetric : bool
|
|
2858
|
+
If True, the window is symmetric, otherwise periodic.
|
|
2859
|
+
|
|
2779
2860
|
Returns
|
|
2780
2861
|
-------
|
|
2781
2862
|
out : Tensor [shape=(..., L2)]
|
|
2782
2863
|
The windowed waveform.
|
|
2783
2864
|
|
|
2784
2865
|
"""
|
|
2785
|
-
return nn.Window._func(
|
|
2866
|
+
return nn.Window._func(
|
|
2867
|
+
x, out_length=out_length, window=window, norm=norm, symmetric=symmetric
|
|
2868
|
+
)
|
|
2786
2869
|
|
|
2787
2870
|
|
|
2788
2871
|
def yingram(
|
|
@@ -47,6 +47,7 @@ from .excite import ExcitationGeneration
|
|
|
47
47
|
from .fbank import MelFilterBankAnalysis
|
|
48
48
|
from .fbank import MelFilterBankAnalysis as FBANK
|
|
49
49
|
from .fftcep import CepstralAnalysis
|
|
50
|
+
from .fftr import RealValuedFastFourierTransform
|
|
50
51
|
from .flux import Flux
|
|
51
52
|
from .frame import Frame
|
|
52
53
|
from .freqt import FrequencyTransform
|
|
@@ -70,6 +71,7 @@ from .idht import InverseDiscreteHartleyTransform
|
|
|
70
71
|
from .idht import InverseDiscreteHartleyTransform as IDHT
|
|
71
72
|
from .idst import InverseDiscreteSineTransform
|
|
72
73
|
from .idst import InverseDiscreteSineTransform as IDST
|
|
74
|
+
from .ifftr import RealValuedInverseFastFourierTransform
|
|
73
75
|
from .ifreqt2 import SecondOrderAllPassInverseFrequencyTransform
|
|
74
76
|
from .igammatone import GammatoneFilterBankSynthesis
|
|
75
77
|
from .ignorm import GeneralizedCepstrumInverseGainNormalization
|
|
@@ -160,6 +162,7 @@ from .wht import WalshHadamardTransform as WHT
|
|
|
160
162
|
from .wht import WalshHadamardTransform as InverseWalshHadamardTransform
|
|
161
163
|
from .wht import WalshHadamardTransform as IWHT
|
|
162
164
|
from .window import Window
|
|
165
|
+
from .world_synth import WorldSynthesis
|
|
163
166
|
from .yingram import Yingram
|
|
164
167
|
from .zcross import ZeroCrossingAnalysis
|
|
165
168
|
from .zerodf import AllZeroDigitalFilter
|
|
@@ -137,16 +137,10 @@ class Aperiodicity(BaseNonFunctionalModule):
|
|
|
137
137
|
>>> f0 = pitch(x)
|
|
138
138
|
>>> f0.shape
|
|
139
139
|
torch.Size([7])
|
|
140
|
-
>>> aperiodicity = diffsptk.Aperiodicity(160, 16000,
|
|
140
|
+
>>> aperiodicity = diffsptk.Aperiodicity(160, 16000, 1024)
|
|
141
141
|
>>> ap = aperiodicity(x, f0)
|
|
142
|
-
>>> ap
|
|
143
|
-
|
|
144
|
-
[0.0010, 0.8419, 0.3644, 0.5912, 0.9590],
|
|
145
|
-
[0.0010, 0.5316, 0.3091, 0.5430, 0.9540],
|
|
146
|
-
[0.0010, 0.3986, 0.1930, 0.4222, 0.9234],
|
|
147
|
-
[0.0010, 0.3627, 0.1827, 0.4106, 0.9228],
|
|
148
|
-
[0.0010, 0.3699, 0.1827, 0.4106, 0.9228],
|
|
149
|
-
[0.0010, 0.7659, 0.7081, 0.8378, 0.9912]])
|
|
142
|
+
>>> ap.shape
|
|
143
|
+
torch.Size([7, 513])
|
|
150
144
|
|
|
151
145
|
"""
|
|
152
146
|
d = x.dim()
|
|
@@ -53,7 +53,7 @@ class InfiniteImpulseResponseDigitalFilter(BaseFunctionalModule):
|
|
|
53
53
|
) -> None:
|
|
54
54
|
super().__init__()
|
|
55
55
|
|
|
56
|
-
_, _, tensors = self._precompute(*get_values(locals(),
|
|
56
|
+
_, _, tensors = self._precompute(*get_values(locals(), drop=1))
|
|
57
57
|
if learnable and b is not None:
|
|
58
58
|
self.b = nn.Parameter(tensors[0])
|
|
59
59
|
else:
|
|
@@ -74,7 +74,7 @@ class DynamicRangeCompression(BaseFunctionalModule):
|
|
|
74
74
|
) -> None:
|
|
75
75
|
super().__init__()
|
|
76
76
|
|
|
77
|
-
self.values, _, tensors = self._precompute(*get_values(locals(),
|
|
77
|
+
self.values, _, tensors = self._precompute(*get_values(locals(), drop=1))
|
|
78
78
|
if learnable:
|
|
79
79
|
self.params = nn.Parameter(tensors[0])
|
|
80
80
|
else:
|
|
@@ -46,6 +46,9 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
46
46
|
floor : float > 0
|
|
47
47
|
The minimum mel filter bank output in linear scale.
|
|
48
48
|
|
|
49
|
+
gamma : float in [-1, 1]
|
|
50
|
+
The parameter of the generalized logarithmic function.
|
|
51
|
+
|
|
49
52
|
use_power : bool
|
|
50
53
|
If True, use the power spectrum instead of the amplitude spectrum.
|
|
51
54
|
|
|
@@ -68,6 +71,7 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
68
71
|
f_min: float = 0,
|
|
69
72
|
f_max: float | None = None,
|
|
70
73
|
floor: float = 1e-5,
|
|
74
|
+
gamma: float = 0,
|
|
71
75
|
use_power: bool = False,
|
|
72
76
|
out_format: str | int = "y",
|
|
73
77
|
) -> None:
|
|
@@ -133,6 +137,7 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
133
137
|
f_min: float,
|
|
134
138
|
f_max: float | None,
|
|
135
139
|
floor: float,
|
|
140
|
+
gamma: float,
|
|
136
141
|
) -> None:
|
|
137
142
|
if fft_length <= 1:
|
|
138
143
|
raise ValueError("fft_length must be greater than 1.")
|
|
@@ -146,6 +151,8 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
146
151
|
raise ValueError("Invalid f_min and f_max.")
|
|
147
152
|
if floor <= 0:
|
|
148
153
|
raise ValueError("floor must be positive.")
|
|
154
|
+
if 1 < abs(gamma):
|
|
155
|
+
raise ValueError("gamma must be in [-1, 1].")
|
|
149
156
|
|
|
150
157
|
@staticmethod
|
|
151
158
|
def _precompute(
|
|
@@ -155,6 +162,7 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
155
162
|
f_min: float,
|
|
156
163
|
f_max: float | None,
|
|
157
164
|
floor: float,
|
|
165
|
+
gamma: float,
|
|
158
166
|
use_power: bool,
|
|
159
167
|
out_format: str | int,
|
|
160
168
|
device: torch.device | None = None,
|
|
@@ -167,6 +175,7 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
167
175
|
f_min,
|
|
168
176
|
f_max,
|
|
169
177
|
floor,
|
|
178
|
+
gamma,
|
|
170
179
|
)
|
|
171
180
|
|
|
172
181
|
if out_format in (0, "y"):
|
|
@@ -211,19 +220,21 @@ class MelFilterBankAnalysis(BaseFunctionalModule):
|
|
|
211
220
|
if m < n_channel:
|
|
212
221
|
weights[k, m] += 1 - w
|
|
213
222
|
|
|
214
|
-
return (floor, use_power, formatter), None, (to(weights, dtype=dtype),)
|
|
223
|
+
return (floor, gamma, use_power, formatter), None, (to(weights, dtype=dtype),)
|
|
215
224
|
|
|
216
225
|
@staticmethod
|
|
217
226
|
def _forward(
|
|
218
227
|
x: torch.Tensor,
|
|
219
228
|
floor: float,
|
|
229
|
+
gamma: float,
|
|
220
230
|
use_power: bool,
|
|
221
231
|
formatter: Callable,
|
|
222
232
|
H: torch.Tensor,
|
|
223
233
|
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:
|
|
224
234
|
y = x if use_power else torch.sqrt(x)
|
|
225
235
|
y = torch.matmul(y, H)
|
|
226
|
-
y = torch.
|
|
236
|
+
y = torch.clip(y, min=floor)
|
|
237
|
+
y = torch.log(y) if gamma == 0 else (torch.pow(x, gamma) - 1) / gamma
|
|
227
238
|
E = (2 * x[..., 1:-1]).sum(-1) + x[..., 0] + x[..., -1]
|
|
228
239
|
E = torch.log(E / (2 * (x.size(-1) - 1))).unsqueeze(-1)
|
|
229
240
|
return formatter(y, E)
|
|
@@ -0,0 +1,144 @@
|
|
|
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 torch
|
|
18
|
+
import torch.nn.functional as F
|
|
19
|
+
from torch import nn
|
|
20
|
+
|
|
21
|
+
from ..typing import Callable, Precomputed
|
|
22
|
+
from ..utils.private import get_values, to
|
|
23
|
+
from .base import BaseFunctionalModule
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class RealValuedFastFourierTransform(BaseFunctionalModule):
|
|
27
|
+
"""See `this page <https://sp-nitech.github.io/sptk/latest/main/fftr.html>`_
|
|
28
|
+
for details.
|
|
29
|
+
|
|
30
|
+
Parameters
|
|
31
|
+
----------
|
|
32
|
+
fft_length : int >= 2
|
|
33
|
+
The FFT length, :math:`L`.
|
|
34
|
+
|
|
35
|
+
out_format : ['complex', 'real', 'imaginary', 'amplitude', 'power']
|
|
36
|
+
The output format.
|
|
37
|
+
|
|
38
|
+
learnable : bool
|
|
39
|
+
Whether to make the DFT basis learnable. If True, the module performs DFT rather
|
|
40
|
+
than FFT.
|
|
41
|
+
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
fft_length: int,
|
|
47
|
+
out_format: str | int = "complex",
|
|
48
|
+
learnable: bool = False,
|
|
49
|
+
) -> None:
|
|
50
|
+
super().__init__()
|
|
51
|
+
|
|
52
|
+
self.values, _, tensors = self._precompute(*get_values(locals()))
|
|
53
|
+
if learnable is True:
|
|
54
|
+
self.W = nn.Parameter(tensors[0])
|
|
55
|
+
elif learnable == "debug":
|
|
56
|
+
self.register_buffer("W", tensors[0])
|
|
57
|
+
|
|
58
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
59
|
+
"""Compute FFT of a real signal.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
x : Tensor [shape=(..., N)]
|
|
64
|
+
The real input signal.
|
|
65
|
+
|
|
66
|
+
Returns
|
|
67
|
+
-------
|
|
68
|
+
out : Tensor [shape=(..., L/2+1)]
|
|
69
|
+
The output spectrum.
|
|
70
|
+
|
|
71
|
+
Examples
|
|
72
|
+
--------
|
|
73
|
+
>>> x = diffsptk.ramp(1, 3)
|
|
74
|
+
>>> x
|
|
75
|
+
tensor([1., 2., 3.])
|
|
76
|
+
>>> fftr = diffsptk.RealValuedFastFourierTransform(8, out_format="real")
|
|
77
|
+
>>> y = fftr(x)
|
|
78
|
+
>>> y
|
|
79
|
+
tensor([ 6.0000, 2.4142, -2.0000, -0.4142, 2.0000])
|
|
80
|
+
|
|
81
|
+
"""
|
|
82
|
+
return self._forward(x, *self.values, **self._buffers, **self._parameters)
|
|
83
|
+
|
|
84
|
+
@staticmethod
|
|
85
|
+
def _func(x: torch.Tensor, *args, **kwargs) -> torch.Tensor:
|
|
86
|
+
values, _, _ = RealValuedFastFourierTransform._precompute(*args, **kwargs)
|
|
87
|
+
return RealValuedFastFourierTransform._forward(x, *values)
|
|
88
|
+
|
|
89
|
+
@staticmethod
|
|
90
|
+
def _takes_input_size() -> bool:
|
|
91
|
+
return False
|
|
92
|
+
|
|
93
|
+
@staticmethod
|
|
94
|
+
def _check(fft_length: int | None) -> None:
|
|
95
|
+
if fft_length is not None and (fft_length <= 0 or fft_length % 2 == 1):
|
|
96
|
+
raise ValueError("fft_length must be positive even.")
|
|
97
|
+
|
|
98
|
+
@staticmethod
|
|
99
|
+
def _precompute(
|
|
100
|
+
fft_length: int | None,
|
|
101
|
+
out_format: str | int,
|
|
102
|
+
learnable: bool = False,
|
|
103
|
+
device: torch.device | None = None,
|
|
104
|
+
dtype: torch.dtype | None = None,
|
|
105
|
+
) -> Precomputed:
|
|
106
|
+
RealValuedFastFourierTransform._check(fft_length)
|
|
107
|
+
|
|
108
|
+
if out_format in (0, "complex"):
|
|
109
|
+
formatter = lambda x: x
|
|
110
|
+
elif out_format in (1, "real"):
|
|
111
|
+
formatter = lambda x: x.real
|
|
112
|
+
elif out_format in (2, "imaginary"):
|
|
113
|
+
formatter = lambda x: x.imag
|
|
114
|
+
elif out_format in (3, "amplitude"):
|
|
115
|
+
formatter = lambda x: x.abs()
|
|
116
|
+
elif out_format in (4, "power"):
|
|
117
|
+
formatter = lambda x: x.abs().square()
|
|
118
|
+
else:
|
|
119
|
+
raise ValueError(f"out_format {out_format} is not supported.")
|
|
120
|
+
|
|
121
|
+
if learnable:
|
|
122
|
+
W = torch.fft.fft(torch.eye(fft_length, device=device, dtype=torch.double))
|
|
123
|
+
W = W[..., : fft_length // 2 + 1]
|
|
124
|
+
W = torch.cat([W.real, W.imag], dim=-1)
|
|
125
|
+
tensors = (to(W, dtype=dtype),)
|
|
126
|
+
else:
|
|
127
|
+
tensors = None
|
|
128
|
+
return (fft_length, formatter), None, tensors
|
|
129
|
+
|
|
130
|
+
@staticmethod
|
|
131
|
+
def _forward(
|
|
132
|
+
x: torch.Tensor,
|
|
133
|
+
fft_length: int | None,
|
|
134
|
+
formatter: Callable,
|
|
135
|
+
W: torch.Tensor | None = None,
|
|
136
|
+
) -> torch.Tensor:
|
|
137
|
+
if W is None:
|
|
138
|
+
y = torch.fft.rfft(x, n=fft_length)
|
|
139
|
+
else:
|
|
140
|
+
if fft_length is not None and fft_length != x.size(-1):
|
|
141
|
+
x = F.pad(x, (0, fft_length - x.size(-1)))
|
|
142
|
+
y = torch.matmul(x, W)
|
|
143
|
+
y = torch.complex(*torch.tensor_split(y, 2, dim=-1))
|
|
144
|
+
return formatter(y)
|