-
Notifications
You must be signed in to change notification settings - Fork 1
/
RQA_func_spg.py
300 lines (198 loc) · 7.84 KB
/
RQA_func_spg.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
__author__ = 'mac'
from pylab import *
import scipy.ndimage.interpolation as scindim
import numpy as np
import matplotlib.gridspec as gridspec
import librosa
from matplotlib.colors import LogNorm
import numpy
from numpy import *
from skimage import img_as_uint
import skimage
import skimage.exposure
import numpy.random
import matplotlib as plt
## Max pooling function
def search_max(search_fsout):
max_arg_ts = int(argmax(search_fsout))
return max_arg_ts
## Max pooling function
def max_pooling2d(image, pool_size):
dim_img = shape(image)
pooled_img = zeros(( int(dim_img[0]/pool_size), int(dim_img[0]/pool_size) ), dtype=numpy.float32)
for p in range(0, dim_img[0]/pool_size):
for q in range(0, dim_img[1]/pool_size):
set = image[ p*pool_size: (p+1)*pool_size ,q*pool_size : (q+1)*pool_size ]
cal = amax( reshape(set, (pool_size**2, 1)) )
pooled_img[p, q] = cal
## FL2 END
## FL1 END
return pooled_img
def max_pooling1d(image, pool_size):
dim_img = shape(image)
pooled_img = zeros((int(dim_img[0]/pool_size),), dtype=numpy.float32)
for p in range(0, dim_img[0]/pool_size):
set = image[ p*pool_size: (p+1)*pool_size ]
cal_arg = argmax( abs( reshape(set, (pool_size, 1)) ))
#
# print cal_arg
# print set
# print int(squeeze(cal_arg))
cal = set[ int(squeeze(cal_arg)) ]
pooled_img[p] = cal
## FL2 END
## FL1 END
return pooled_img
def max_pooling_avg(image, pool_size):
dim_img = shape(image)
pooled_img = zeros((int(dim_img[0]/pool_size),), dtype=numpy.float32)
for p in range(0, dim_img[0]/pool_size):
set = image[ p*pool_size: (p+1)*pool_size ]
# print cal_arg
# print set
# print int(squeeze(cal_arg))
cal = average(set[:])
pooled_img[p] = cal
## FL2 END
## FL1 END
return pooled_img
def gaussian(x, mu, sig):
return np.exp(-np.power(x - mu, 2.) / (2 * np.power(sig, 2.)))
def form_gs_mat(sz,mu,sig):
gs_mat = zeros((sz, sz), dtype=float32)
for y in range(1,sz+1):
for x in range(1,sz+1):
if y >= (-1*x):
gs_mat[x-1, y-1]=gaussian( (-x+y), mu, sig)
elif y < (-1*x):
gs_mat[x-1, y-1]=gaussian( -(-x+y), mu, sig)
return gs_mat
def MR_RQA(file_name, audio_sig, SR, offset, jump_num, TR, FR, jump, FN, RPpow, img_process, th, th_ts, gain, clim, I_mode, img_sz, img_plot, img_path, RPs_in_level, RPs_interval):
num_level = RPs_in_level[-1]
## List for output variable
total_bin = np.zeros((jump_num, FN, img_sz, img_sz), dtype="float32")
img_orsz = img_sz
# Set NFFT, HOP_LENGTH for spectrogram
NFFT = 128
HOP_LENGTH = NFFT/4
# Plot per a line
PL = int(ceil(FN/2))
# Open a main window
if img_plot in (1, 2, 3):
fig_sz_H = 15
fig_sz_W = 5
fig = figure(figsize=(fig_sz_H,fig_sz_W ))
fig = gridspec.GridSpec(1, num_level)
# fig = gridspec.GridSpec(num_level + 2, PL, height_ratios=[0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.8, 0.8])
# ax2 = subplot(fig[14, :])
# audio_sig_show = audio_sig[0:SR]
# D = np.abs(librosa.stft(audio_sig_show, n_fft = NFFT, hop_length = HOP_LENGTH))
# img = librosa.display.specshow(D, sr=SR, y_axis='linear', x_axis='time',norm=LogNorm(vmin=0.01, vmax=10))
# img.set_cmap('jet')
# # show(block=False)
# specgram(audio_sig, NFFT = NFFT, scale_by_freq=True,sides='default', noverlap = 0)
# xticks([])
print 'FN', FN
pic_bin = np.zeros((FN, img_sz, img_sz), dtype="float32")
print "\n"
for index in range(0, jump_num):
# if img_plot in (1, 2, 3):
# audio_sig_show = audio_sig[0:SR]
# ax1 = subplot(fig[13, :])
# len_y= audio_sig_show.size
# t = linspace(0, len_y/float(SR), num = len_y)
# t = linspace(0, len_y, num = len_y)
# pic = plot(t, audio_sig_show, 'b-')
# # xticks([])
# draw()
# Parameter for a plot
y_val = 0.98
############## CAVEAT! #################
# For plot :
# This for loop should not be aggregated with the following for loop,
# because this loop generates border in the timeseries plot.
for q in np.arange(0, RPs_in_level[0]):
# For plot : y_val
y_val = y_val - q*0.005
x_start = RPs_interval[q][0] # /float(SR)
x_end = RPs_interval[q][0] + TR[-7] # /float(SR)
# For plot : Set box size for each level
x_list = [x_start, x_end, x_end, x_start, x_start]
y_list = [-y_val, -y_val, y_val, y_val, -y_val]
# For plot : Decrease box size in the time series plot
# RGB=(1 - 0.05*q ,1 - 0.05*q,0.1)
# RGB=(0,0,0)
#
# if img_plot in (1, 2, 3):
#
# pic = plot(x_list, y_list, 'r-', linewidth=1)
# draw()
########################################
## Put audio signal into "conved_sigs" list with pre-determined offset value.
conved_sigs = audio_sig[(index*jump + offset):((index+1)*jump + offset + TR[0]*2)]
rp_count = 0
# gs_mat = form_gs_mat(img_orsz, 0, 8)
r = 0
for q in range(0, num_level):
NFFT = 64
HOP_LENGTH = NFFT/4
seg_sig = audio_sig[(RPs_interval[q][0]+jump*index):(RPs_interval[q][0]+ jump*index + HOP_LENGTH*img_sz + NFFT-HOP_LENGTH)]
# Plot per a line
PL = int(ceil(FN/2))
if img_plot in (1, 2, 3):
ax1 = subplot(fig[0, q])
audio_sig_show = audio_sig
D = np.abs(librosa.stft(seg_sig, n_fft = NFFT, hop_length = HOP_LENGTH))
img = librosa.display.specshow(D, sr=SR, y_axis='linear', x_axis='time',norm=LogNorm(vmin=0.01, vmax=10))
img.set_cmap('jet')
axis('off')
pic_bin[q, :, :] = D[0:img_sz,0:img_sz]
rp_count += 1
### END for loop
if img_plot == 1:
draw()
show(block=False)
elif img_plot == 2:
draw()
savefig(img_path + file_name + '_' + str(index+1)+ '.png', dpi=100)
print "Image has been saved : " + img_path + file_name + '_' + str(index+1)+ '.png'
elif img_plot == 3:
draw()
show(block=False)
print str(index+1), "-th jump image...",
if (index+1) != 1 and remainder(index+1,5) == 0 : print '\n',
total_bin[index-1, :, :, :] = pic_bin
print "\n"
if img_plot in (0,2):
close("all")
return total_bin
def RQA_eval(x_in, TR, RPpow, img_process):
RQA_mat_L = zeros((TR, TR))
# Set L,R matrix to compute Recurrence matrix
RQA_mat_L[0:TR, :] = x_in
RQA_mat_R = RQA_mat_L.transpose()
# RQA_value = abs(RQA_mat_L - RQA_mat_R)**2
# RQA_value -= mean(RQA_value)
# RQA_value /= std(RQA_value)
if img_process == -1:
# Subtract two matirices
RQA_value = RQA_mat_L - RQA_mat_R
if img_process == 0:
# RQA_value = RQA_value/amax(abs(RQA_value))
# Subtract two matirices
RQA_value = abs(RQA_mat_L - RQA_mat_R)
RQA_value = RQA_value**RPpow
if img_process == 1:
# RQA_value = RQA_value/amax(abs(RQA_value))
RQA_value = img_as_uint(RQA_value)
RQA_value = skimage.exposure.equalize_hist(RQA_value)
if img_process == 2:
# RQA_value = RQA_value/amax(abs(RQA_value))
RQA_value = img_as_uint(RQA_value)
RQA_value = skimage.exposure.equalize_adapthist(RQA_value, clip_limit=0.01)
RQA_value -= mean(RQA_value)
return RQA_value
def preprocessing():
return 0
def logistic(z,z0,a):
return 1.0 / (1.0 + np.exp(-a*(z-z0)))