-
Notifications
You must be signed in to change notification settings - Fork 1
/
MNSS_function.py
298 lines (209 loc) · 9.29 KB
/
MNSS_function.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
__author__ = 'mac'
import librosa
import librosa.decompose
from matplotlib.colors import LogNorm
import scipy.io.wavfile
from pylab import *
from numpy import *
import numpy as np
import scipy.io.wavfile
import scipy.linalg
from scikits.samplerate import resample
import pickle
import math
from random import *
# from xp00_s import NN_judge
from NN_wrapper import NN_judge2
from random import random
'''
TF domain Music Noise Segmentation
'''
# Convert Sampling Rate
def wav_convert(data, SR, tar_freq):
# If the input signal is stereo, make it mono.
if ndim(data) == 2:
# Mix stereo signal into a mono signal
buff01 = 0.49 * (data[:, 0] + data[:, 1])
wave_ts = array(buff01)
else:
wave_ts = array(data[:])
wave_ts = array(wave_ts)
# Set a sampling rate
up_SR = 44100
# Compute a ratio to feed into resample function
ratio = float(float(tar_freq)/float(up_SR))
# Resample the file.
wave_ts = resample(wave_ts, ratio , 'linear')
# Transpose the data list.
wave_ts = transpose(wave_ts)
# Return wave_ts signal
return wave_ts
def MNSS_function(file_names):
##
SN = 1
file_total = file_names[0]
file_total_n = file_names[1]
file_total_m = file_names[2]
# # Set files for input data.
# file_total = wav_folder + sls + inst_name + sls + fn_for
# file_total_n = wav_folder + sls + inst_name_n + sls + fn_for_n
# file_total_m = wav_folder + sls + inst_name_m + sls + fn_for_m
print "wav file path: ", file_total
print "wav file path_n: ", file_total_n
print "wav file path_m: ", file_total_m
# Use scipy.io library to read wave signal.
SR, data = scipy.io.wavfile.read(file_total)
SR, data_n = scipy.io.wavfile.read(file_total_n)
SR, data_m = scipy.io.wavfile.read(file_total_m)
tar_freq = 44100
data_ts = wav_convert(data, SR, tar_freq )
data_ts_n = wav_convert(data_n, SR, tar_freq )
data_ts_m = wav_convert(data_m, SR, tar_freq )
## The log2 of RQA plot image size
RP = 6 ## 32 by 32
img_W = 4
## Feature image size for ANALYZE
# Unit window size
img_sz_org = [(2**RP), img_W*(2**RP)]
# Total window size
img_sz = [4*(2**RP), img_W*(2**RP)]
## FFT specification
NFFT = 2*img_sz[0]
HOP_LENGTH = NFFT/4
## Set size of dimension
depth = 8
stride = img_sz_org[0]/4
x_stride = img_sz_org[0]/4
D_org = np.abs(librosa.stft(data_ts, n_fft = NFFT, hop_length = HOP_LENGTH))
# Show original spectrogram
figure()
img = librosa.display.specshow(D_org, sr=SR, cmap = 'jet', y_axis='linear', x_axis='time', norm=LogNorm(vmin=0.01, vmax=10))
subplot(depth ,1 ,1)
seg_sig = data_ts
D = np.abs(librosa.stft(seg_sig, n_fft = NFFT, hop_length = HOP_LENGTH))
D_n = np.abs(librosa.stft(data_ts_n, n_fft = NFFT, hop_length = HOP_LENGTH))
D_m = np.abs(librosa.stft(data_ts_m, n_fft = NFFT, hop_length = HOP_LENGTH))
time_length = int( (shape(D)[1] - img_sz_org[1]) /float(x_stride))
X_test = np.zeros((time_length*(depth+1), 1, img_sz_org[0], img_sz_org[1]), dtype="float32")
y_test = np.zeros((time_length*(depth+1), 1, 1), dtype="float32")
for r in range(0, time_length):
x_offset = r * x_stride
# print 'x_offset', x_offset
for k in range(0, depth+1):
offset = stride * k
x_start = x_offset + 0
x_end = x_offset + img_sz_org[1]
y_start = (offset + 0)
y_end = (offset + img_sz_org[0])# /float(SR)
Ds = D[ y_start : y_end, x_start : x_end ]
X_test[r*(depth+1)+k,0,:,:] = abs(Ds)
subplot(depth+1, 1, (depth+1)-k)
img = librosa.display.specshow(Ds, sr=SR, y_axis='linear', cmap = 'jet', x_axis='time', norm=LogNorm(vmin=0.01, vmax=10))
## Dicern whether the frames are noise or music
##################################
pr_val = NN_judge2(X_test, y_test)
##################################
pr_val_raw = (pr_val + 1e-10)
pr_val = log10(pr_val + 1e-10)
pr_val_box_music = pr_val[:,0].reshape((depth+1), time_length)
pr_val_box_noise = pr_val[:,1].reshape((depth+1), time_length)
pr_val_box_music_raw = pr_val_raw[:,0].reshape((depth+1), time_length)
pr_val_box_noise_raw = pr_val_raw[:,1].reshape((depth+1), time_length)
D_mask_m = 0*D[0:(img_sz_org[0]*3), 0:stride*time_length ]
D_mask_n = 0*D[0:(img_sz_org[0]*3), 0:stride*time_length ]
D_mask_mb = 0*D[0:(img_sz_org[0]*3), 0:stride*time_length ]
D_mask_nb = 0*D[0:(img_sz_org[0]*3), 0:stride*time_length ]
for r in range(0, time_length):
x_offset = r * x_stride
for k in range(0, depth+1):
offset = stride * k
x_start = x_offset + 0
x_end = x_offset + img_sz_org[1]
y_start = (offset + 0)
y_end = (offset + img_sz_org[0])# /float(SR)
D_mask_m[ y_start : y_end, x_start : x_end ] = \
D_mask_m[ y_start : y_end, x_start : x_end ] + pr_val_box_music[k, r]
D_mask_n[ y_start : y_end, x_start : x_end ] = \
D_mask_n[ y_start : y_end, x_start : x_end ] + pr_val_box_noise[k, r]
D_mask_mb[ y_start : y_end, x_start : x_end ] = \
D_mask_m[ y_start : y_end, x_start : x_end ] + pr_val_box_music_raw[k, r]
D_mask_nb[ y_start : y_end, x_start : x_end ] = \
D_mask_n[ y_start : y_end, x_start : x_end ] + pr_val_box_noise_raw[k, r]
for r in range(0, time_length):
x_offset = r * x_stride
for k in range(0, depth+4):
offset = stride * k
x_start = x_offset + 0
x_end = x_offset + img_sz_org[0]/4
y_start = (offset + 0)
y_end = (offset + img_sz_org[0]/4)# /float(SR)
k_com_L = 4/float(amin([k+1, 4]))
k_com_H = 4/float(amin([(depth+4-k), 4]))
D_mask_m[ y_start : y_end, x_start : x_end ] = \
D_mask_m[ y_start : y_end, x_start : x_end ]*float(float(k_com_L)*float(k_com_H))
D_mask_n[ y_start : y_end, x_start : x_end ] = \
D_mask_n[ y_start : y_end, x_start : x_end ]*float(float(k_com_L)*float(k_com_H))
D_show = D[0:(img_sz_org[0]*3), 0:stride*time_length ]
D_show_n = D_n[0:(img_sz_org[0]*3), 0:stride*time_length ]
D_show_m = D_m[0:(img_sz_org[0]*3), 0:stride*time_length ]
D_show = flipud(D_show)
D_show_n = flipud(D_show_n)
D_show_m = flipud(D_show_m)
D_mask_dn = flipud(D_mask_n)
D_mask_dm = flipud(D_mask_m)
D_music_m = (D_mask_dm - D_mask_dn)
D_soft_m = D_music_m
D_music_m[D_music_m>0]=1
D_music_m[D_music_m<=0]=0
D_noise_n = (D_mask_dn - D_mask_dm)
D_soft_n = D_noise_n
D_noise_n[D_noise_n>0]=1
D_noise_n[D_noise_n<=0]=0
D_noise_n = (D_mask_dn - D_mask_dm)
total_plot = 7
with open( 'pickle_folder/' + 'MNN_visual_data_'+ str(SN) + '.pickle', 'w') as f:
pickle.dump([D_show, D_show_n, D_show_m, D_mask_dn, D_mask_dm, D_music_m, D_noise_n], f)
subplot(total_plot, 1, 1)
img = librosa.display.specshow(flipud(D_show_n), cmap = 'jet', y_axis='linear', norm=LogNorm(vmin=0.01, vmax=10))
subplot(total_plot, 1, 2)
img = librosa.display.specshow(flipud(D_show_m), cmap = 'jet', y_axis='linear', norm=LogNorm(vmin=0.01, vmax=10))
subplot(total_plot, 1, 3)
img = librosa.display.specshow(flipud(D_show), cmap = 'jet', y_axis='linear', norm=LogNorm(vmin=0.01, vmax=10))
subplot(total_plot, 1, 4)
imshow(D_mask_dn, interpolation = 'none', cmap = 'jet' )
subplot(total_plot, 1, 5)
imshow(D_mask_dm, interpolation = 'none', cmap = 'jet' )
subplot(total_plot, 1, 6)
imshow(log10(np.array(D_show)*np.array(D_noise_n)), interpolation = 'none', cmap = 'jet' )
subplot(total_plot, 1, 7)
imshow(log10(np.array(D_show)*np.array(D_music_m)), interpolation = 'none', cmap = 'jet' )
show()
# with open(pickle_path + open_data + '.pickle') as f:
# sum_mat_X_data, sum_mat_y_data, sum_mat_k_data, sum_mat_D_data, jump_num, FN, nb_classes = pickle.load(f)
seg_sig = data_ts
D_out = librosa.stft(seg_sig, n_fft = NFFT, hop_length = HOP_LENGTH)
shape_Dout = shape(D_out)
D_wr = 0 * D_music_m
shape_D = shape(D_music_m)
D_wr = zeros((shape_D[0],shape_D[1]), dtype=complex)
D_wr[0:shape_D[0], 0:shape_D[1] ] = D_out[ 0:shape_D[0], 0:shape_D[1] ]
D_n_out = np.array(D_wr)*np.array(D_noise_n)
D_m_out = np.array(D_wr)*np.array(D_music_m)
D_yn = zeros((shape_Dout[0],shape_D[1]), dtype=complex)
D_ym = zeros((shape_Dout[0],shape_D[1]), dtype=complex)
D_yn[0:shape_D[0], 0:shape_D[1] ] = D_n_out
D_ym[0:shape_D[0], 0:shape_D[1] ] = D_m_out
y_noise = librosa.istft(D_n_out)
y_music = librosa.istft(D_m_out)
file_total_wr = 'result_data'
sls = '/'
print 'SR', SR
print 'tar_freq', tar_freq
SR_w = 33000
scipy.io.wavfile.write(file_total_wr + sls + 'y_music.wav', SR_w, y_music)
scipy.io.wavfile.write(file_total_wr + sls + 'y_noise.wav', SR_w, y_noise)
show()
print 'max and min of pvb music', amax(pr_val_box_music), amin(pr_val_box_music)
print 'max and min of pvb noise', amax(pr_val_box_noise), amin(pr_val_box_noise)
print 'max and min of pr_val', amax(pr_val), amin(pr_val)
print 'shape of pr_val_box_music ', shape(pr_val_box_music)