-
Notifications
You must be signed in to change notification settings - Fork 15
/
stft.py
78 lines (64 loc) · 1.92 KB
/
stft.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# shmzhang@aslp, 2021-04
import numpy as np
def dataLength(tau, stftshift, fftsize):
len = (tau-1)*stftshift+fftsize
return len
def numFrames(len, stftshift):
tau = len//stftshift
return tau
def stft(x, stftshift, fftsize):
"""Short time DFT
Args:
x (np.ndarray): input signal
stftshift (int): time shift
fftsize (int): fft window size
Returns:
stftx: complex spectra of input signal
"""
w = 0.5*(1-np.cos(2*np.pi/fftsize*np.arange(fftsize)))
awin = np.sqrt(w*2.0*stftshift/fftsize)
T = numFrames(len(x), stftshift)
F = fftsize//2+1
stftx = np.zeros([F, T], dtype=np.complex)
idx1 = 0
for tau in range(T):
tx = np.zeros(fftsize)
idx2 = min(idx1+fftsize, len(x))
tx[:idx2-idx1] = x[idx1:idx2]
tx = tx*awin
fx = np.fft.rfft(tx, fftsize)
stftx[:, tau] = fx
idx1 = idx1+stftshift
return stftx
def istft(stftx, stftshift):
"""inverse-Short time DFT.
Args:
stftx (np.ndarray): complex spectra.
stftshift (int): time shift.
Returns:
x: reconstruction of input spectra.
"""
F, T = stftx.shape
fftsize = (F-1)*2
w = 0.5*(1-np.cos(2*np.pi/fftsize*np.arange(fftsize)))
swin = np.sqrt(w*2.0*stftshift/fftsize)
lenx = dataLength(T, stftshift, fftsize)
x = np.zeros(lenx)
idx1 = 0
fx = np.zeros(fftsize, dtype=np.complex)
for tau in range(T):
fx[:F] = stftx[:, tau]
fx[F-1:] = np.conj(stftx[1:, tau][::-1])
tx = np.fft.ifft(fx, fftsize)
tx = tx*swin
tx = np.real(tx)
x[idx1:idx1+fftsize] = x[idx1: idx1+fftsize]+tx
idx1 = idx1+stftshift
return x
if __name__ == "__main__":
import soundfile as sf
sig, sr = sf.read("sample.wav")
stftx = stft(sig[:, 0], 502, 1024)
output = istft(stftx, 502)
output = output[:len(sig)]
sf.write("reconstruct.wav", output, sr)