-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstftandinverse.py
58 lines (48 loc) · 1.42 KB
/
stftandinverse.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
from __future__ import division
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
import scipy.io.wavfile
import cv2
import math
fs, x = scipy.io.wavfile.read('input.wav')
# to cut the long file into a appropriate size
start = 19 #unit in secs
end = 23 #unit in secs
startnum = start*fs
endnum = end*fs
x=(zip(*x[startnum:endnum]))
x=x[0]
# output the cutted session
scipy.io.wavfile.write('outori.wav',fs,np.array(x))
N = len(x)
time = np.arange(N) / fs
# spectrogram plotting
winlen=2048
hop=128
f, t, Sxx2 = signal.spectrogram(x, fs, nperseg=winlen, noverlap=winlen-hop,return_onesided=False, mode='complex') # the options are essential for the reconstruction
plt.figure()
plt.pcolormesh(t, f[0:winlen/2], np.absolute(Sxx2[0:winlen/2]))
plt.ylabel('Frequency [Hz]')
plt.ylim(0, 4000)
plt.xlabel('Time [sec]')
plt.savefig('complex')
def istft(X, fs, T, winlen, hop):
x = np.zeros(T*fs)
print("empty sound length="+str(len(x)))
print("X shape="+str(X.shape))
for n,i in enumerate(range(0, len(x)-winlen, hop)):
print("n="+str(n))
print("i="+str(i))
print("i+winlen="+str(i+winlen))
x[i:i+winlen] += scipy.real(scipy.ifft(X.T[n]))
x = x/max(x)
return x
s2=istft(Sxx2, fs, N/fs, winlen, hop)
# plot the waveform
# tlist=scipy.linspace(0, N/fs, N, endpoint=False)
# plt.figure()
# plt.plot(tlist, s2)
# plt.show()
# write the reconstruction
scipy.io.wavfile.write('out.wav',fs,s2)