-
Notifications
You must be signed in to change notification settings - Fork 0
/
a4_test.py
307 lines (226 loc) · 8.01 KB
/
a4_test.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import librosa as lb
import librosa.display
import numpy as np
import matplotlib.pyplot as plt
from scipy.io.wavfile import write
N_fft = 1024
# Hop length is 512.
def updataVarialbe(H, P, W, alpha):
"""
Calculate delta.
:param H:
:param W:
:param alpha:
:return:
"""
H_next = H.copy()
H_prev = H.copy()
P_up = P.copy()
P_down = P.copy()
# Shift H back one column.
# Change the last column to zero-column.
# Calculate H_h,(i + 1) matrix in (23).
# Shift H go up one column.
# Change the first column to zero-column.
# Calculate H_h,(i - 1) matrix in (23).
for i in range(len(H)):
H_next[i] = np.roll(H_next[i], -1)
H_next[i][-1] = 0
H_prev[i] = np.roll(H_prev[i], 1)
H_prev[i][0] = 0
# Shift P go up one row.
# Change the first column to zero-column.
# Calculate P_h+1,i matrix in (23).
P_up = np.roll(P_up, (-1) * len(P[0]))
P_up[-1] = np.zeros(len(P[0]))
# Shift P back one row.
# Change the last column to zero-column.
# Calculate P_h-1,i matrix in (23).
P_down = np.roll(P_down, len(P[0]))
P_down[0] = np.zeros(len(P[0]))
"""
print('H_prev', H_prev)
print()
print('H', H)
print()
print('H_next', H_next)
"""
# Calculate delta matrix in (23).
delta = alpha * ((H_prev - 2 * H + H_next) / 4) - \
(1 - alpha) * ((P_down - 2 * P + P_up) / 4)
# print(delta)
return delta
def plotSpectrogram(fileName, H_spec, P_spec, H, P, fs, K_max, alpha, gamma):
"""
Plot four plot of spectrogram in one figure.
:param H_spec: spectrogram H before binaried.
:param P_spec: spectrogram P before binaried.
:param H: spectrogram H after binaried.
:param P: spectrogram P after binaried.
:param fs: sampling frequency.
"""
plt.figure(figsize=(10, 5))
fig = plt.gcf()
fig.canvas.set_window_title(f'Separation result of \'{fileName}\' with alpha='
f'{alpha} - gamma={gamma} - K_max={K_max}')
plt.subplot(2, 2, 1)
lb.display.specshow(lb.amplitude_to_db(np.abs(H_spec), ref=np.max), sr=fs, \
x_axis='time', y_axis='linear')
plt.title('H spectrogram before binarized')
plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.subplot(2, 2, 2)
lb.display.specshow(lb.amplitude_to_db(np.abs(P_spec), ref=np.max), sr=fs, \
x_axis='time', y_axis='linear')
plt.title('P spectrogram before binarize')
plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.subplot(2, 2, 3)
lb.display.specshow(lb.amplitude_to_db(np.abs(H), ref=np.max), sr=fs, \
x_axis='time', y_axis='linear')
plt.title('H spectrogram after binarized')
plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.subplot(2, 2, 4)
lb.display.specshow(lb.amplitude_to_db(np.abs(P), ref=np.max), sr=fs, \
x_axis='time', y_axis='linear')
plt.title('P spectrogram after binarized')
plt.colorbar(format='%+2.0f dB')
plt.tight_layout()
plt.show()
def setInitialValue(data, gamma):
"""
Calculate short Fourier transform of the
input signal, the range-compressed version of the
spectrogram. Set initial value of harmonic
component and percusive component of the
spectrogram.
:param data is input data.
gamma is used in calculate
range-compress version of spectrogram.
:return: STFT of input signal.
W range-compressed version of
the spectrogram.
H, P the harmonic and percusive
component matrix.
"""
# Calculate STFT of input signal.
# Step 1.
F_stft = lb.stft(data, n_fft=N_fft, hop_length=int(N_fft/2))
# Calculate range-compressed version
# the power spectrogram.
# Step 2 - equation (24).
W = np.abs(F_stft) ** (2 * gamma)
# Set initial value.
# Step 3 - equation (25).
H = 0.5 * W
P = 0.5 * W
return F_stft, W, H, P
def process(H, P, W, alpha):
# Calculate update variable delta in (23).
# Step 4.
delta = updataVarialbe(H, P, W, alpha)
# Updata H_h,i and P_h,i in (26), (27).
# Step 5.
for i in range(len(H)):
for j in range(len(H[i])):
H[i][j] = min(max(H[i][j] + delta[i][j], 0), W[i][j])
P = W - H
return H, P
def binarizeSeperationResult(H, P, W):
"""
Step 7 - Equation (28).
:param H:
:param P:
:param W:
:return: (H_h,i, P_h,i) = (0, W_h,i) if H_h,i < P_h,i
(H_h,i, P_h,i) = (W_h,i, 0) if H_h,i >= P_h,i
"""
for i in range(len(H)):
for j in range(len(H[i])):
H[i][j] = W[i][j] - (W[i][j] * int(H[i][j] < P[i][j]))
P[i][j] = W[i][j] - H[i][j]
return H, P
def convertIntoWaveform(H, P, F, LENGTH, gamma):
"""
Convert H, P into waveforms.
Step 8 - Equation (29), (30).
:param H:
:param P:
:param F:
:return:
"""
arg_F_with_j = np.angle(F) * 1j
H_complex = (H ** (1/(2*gamma))) * np.exp(arg_F_with_j)
P_complex = (P ** (1/(2*gamma))) * np.exp(arg_F_with_j)
h = lb.istft(H_complex, hop_length=int(N_fft/2), length=LENGTH)
p = lb.istft(P_complex, hop_length=int(N_fft/2), length=LENGTH)
return h, p
def writeToFile(fileName, h, p, fs, alpha, gamma):
"""
Write two audio file of the harmonic
and percussive component of the origninal
file with name has information of paramter
alpha and gamma.
:param h: Waveforms of harmonic component.
:param p: Waveforms of percussive component.
:param fs: Sampling frequency.
:param alpha: Control parameter.
:param gamma: Compress parameter.
"""
write(fileName[:-4] + '_Harmonic_a_' + str(alpha * 10) + \
'_g_' + str(gamma * 10) + '.wav', fs, h)
write(fileName[:-4] + '_Percussive_a_' + str(alpha * 10) + \
'_g_' + str(gamma * 10) + '.wav', fs, p)
def calculateSNR(data, p):
"""
Calculate the signal-to-noise ratio.
:param data: original data.
:param p: percusive component wave form.
:return: signal-to-noise ratio in dB.
"""
numerator = data ** 2
denominator = (data - p) ** 2
snr = 10 * np.log10(sum(numerator) / sum(denominator))
return snr
def main():
# Get fileName and input parameters.
fileName = input('Enter the song file name with extension (.wav): ')
K_max = int(input('Enter the number of interation: '))
alpha = float(input('Enter alpha: '))
gamma = float(input('Enter gamma: '))
# Load file.
data, fs = lb.load(fileName)
# F_stft is the short Fourier transfomr of the input signal.
# W is range-compresed version of the power spectrogram.
# H is the matrix of harmonic compnonet H_h,i, h is row
# i is the column.
# P is the matrix of percussive component P_h,i h is row
# i is the column.
F_stft, W, H, P = setInitialValue(data, gamma)
# Step 6.
k = 0
while k < K_max:
# Printout loop counters.
print(k)
# Step 4 and step 5.
H, P = process(H, P, W, alpha)
# Update loop counter.
k += 1
print('Done.')
# Store the spectrogram before binarized.
H_spec = H.copy()
P_spec = P.copy()
# Step 7.
H, P = binarizeSeperationResult(H, P, W)
# Step 8.
h, p = convertIntoWaveform(H, P, F_stft, len(data), gamma)
# Calculate signal-to-noise ratio
snr = calculateSNR(data, p)
print('\nSignal-to-noise rate:', snr, '(dB)')
# Write two the separation of the harmonic
# and the percussive into two files.
writeToFile(fileName, h, p, fs, alpha, gamma)
# Plot the results.
plotSpectrogram(fileName, H_spec, P_spec, H, P, fs, K_max, alpha, gamma)
main()