-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvoice_analy.py
49 lines (37 loc) · 1.29 KB
/
voice_analy.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
import numpy as np
import scipy as sp
from scipy.io import wavfile
class Voice_Analy:
def __init__(self):
pass
def get_data_from_wave(self, path):
sample_rate, data = wavfile.read(path)
return sample_rate, data
def save_wave(self, path, sample_rate, data):
wavfile.write(path, sample_rate, data.astype(np.int16))
def stft(self, x, window, step):
length = len(x)
N = len(window)
M = int(sp.ceil(float(length - N + step) / step))
new_x = np.zeros(N + (M - 1) * step).astype(np.float32)
new_x[:length] = x
X = np.zeros([M, N], dtype=np.complex64)
for m in range(M):
start = step * m
X[m, :] = np.fft.fft(new_x[start:start + N] * window)
return X
def istft(self, X, window, step):
M, N = X.shape
length = (M - 1) * step + N
x = np.zeros(length, dtype=np.float32)
wsum = np.zeros(length, dtype=np.float32)
for m in range(M):
start = step * m
x[start:start + N] = x[start:start + N] + np.fft.ifft(X[m, :]).real * window
wsum[start:start + N] += window ** 2
pos = (wsum != 0)
x[pos] /= wsum[pos]
return x
if __name__ == "__main__":
va = Voice_Analy()
va.__init__()