-
Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathunet_source_separation_utils.py
56 lines (37 loc) · 1.41 KB
/
unet_source_separation_utils.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
import sys
import numpy as np
from scipy import signal
# ======================
# Pre/Post process
# ======================
def preemphasis(data, coeff=0.97):
return signal.lfilter([1,-coeff], [1], data).astype(np.float32)
def inv_preemphasis(data, coeff=0.97):
return signal.lfilter([1], [1,-coeff], data).astype(np.float32)
def lowpass(data, stop_freq, sample_freq, N=4):
wn = 2.0 * stop_freq / sample_freq
b, a = signal.butter(N, wn, btype="low")
data = signal.filtfilt(b,a, data)
return data
def tfconvert(x, window_len, hop_len, mult, window='hann') :
noverlap = window_len - hop_len
_, _, y = signal.stft(x, window=window, nperseg=window_len, noverlap=noverlap)
y_re = np.real(y) * (window_len//2 + 1)
y_im = np.imag(y) * (window_len//2 + 1)
y_mag = np.log(np.sqrt(y_re ** 2 + y_im ** 2)+1.0).astype(np.float32)
y_phase = np.arctan2(y_im, y_re).astype(np.float32)
y_mag = zero_pad(y_mag, mult)
y_phase = zero_pad(y_phase, mult)
return y_mag, y_phase
def zero_pad(x, mult) :
mod = x.shape[2] % mult
if mod > 0 :
pad = mult - mod
x = np.concatenate(( x, np.zeros((x.shape[0], x.shape[1], pad), dtype=np.float32) ), axis=2)
return x
def calc_time(sample_len ,sr) :
quot = sample_len // sr
rem = (sample_len % sr) / sr
min = quot // 60
sec = quot % 60 + rem
print('Time length : {}min {:.02f}sec'.format(min,sec))