-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.py
75 lines (59 loc) · 2 KB
/
io.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
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 12 20:46:52 2020
@author: vipul
"""
import os
import numpy as np
import h5py
import librosa
from scipy.signal import decimate
from matplotlib import pyplot as plt
# ----------------------------------------------------------------------------
def load_h5(h5_path):
# load training data
with h5py.File(h5_path, 'r') as hf:
print ('List of arrays in input file:')
print(hf.keys())
X = np.array(hf.get('data'))
Y = np.array(hf.get('label'))
print ('Shape of X:', X.shape)
print ('Shape of Y:', Y.shape)
return X, Y
def upsample_wav(wav, args, model):
# load signal
x_hr, fs = librosa.load(wav, sr=args.sr)
# downscale signal
# x_lr = np.array(x_hr[0::args.r])
x_lr = decimate(x_hr, args.r)
# x_lr = decimate(x_hr, args.r, ftype='fir', zero_phase=True)
# x_lr = downsample_bt(x_hr, args.r)
# upscale the low-res version
P = model.predict(x_lr.reshape((1,len(x_lr),1)))
x_pr = P.flatten()
# crop so that it works with scaling ratio
x_hr = x_hr[:len(x_pr)]
x_lr = x_lr[:len(x_pr)]
# save the file
outname = wav + '.' + args.out_label
librosa.output.write_wav(outname + '.hr.wav', x_hr, fs)
librosa.output.write_wav(outname + '.lr.wav', x_lr, fs / args.r)
librosa.output.write_wav(outname + '.pr.wav', x_pr, fs)
# save the spectrum
S = get_spectrum(x_pr, n_fft=2048)
save_spectrum(S, outfile=outname + '.pr.png')
S = get_spectrum(x_hr, n_fft=2048)
save_spectrum(S, outfile=outname + '.hr.png')
S = get_spectrum(x_lr, n_fft=2048/args.r)
save_spectrum(S, outfile=outname + '.lr.png')
# ----------------------------------------------------------------------------
def get_spectrum(x, n_fft=2048):
S = librosa.stft(x, n_fft)
p = np.angle(S)
S = np.log1p(np.abs(S))
return S
def save_spectrum(S, lim=800, outfile='spectrogram.png'):
plt.imshow(S.T, aspect=10)
# plt.xlim([0,lim])
plt.tight_layout()
plt.savefig(outfile)