-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShannon_th.py
310 lines (305 loc) · 12.6 KB
/
Shannon_th.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
308
309
310
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 9 12:16:37 2020
@author: Administrator
"""
import math
import numpy as np
import os
import random
from scipy.io import wavfile
import matplotlib.pyplot as plt
from scipy import signal
def gen_wavlist(wavpath):
for (dirpath, dirnames, filenames) in os.walk(wavpath):
lis = list(range(len(filenames)))
random.shuffle(lis)
for i in range(len(lis)):
j = lis[i]
filename = filenames[j]
if filename.endswith('.wav'):
filepath = os.sep.join([dirpath, filename])
fs, audio = wavfile.read(filepath)
audio = audio/max(abs(audio))
return audio,fs
def Shannon_Entropy(X,h,mu,sigma):
N = len(X)
T = 20
A = 1/(N*h*math.sqrt(2*math.pi))
wd = 20*sigma/T
#print(111);
#print(np.shape(np.arange(mu-10*sigma)));
#print(np.shape(np.arange(mu+10*sigma)));
#print(np.shape(np.arange(wd)));
x = np.arange(mu-10*sigma,mu+10*sigma,wd)
#print(np.shape(x));
infoentropy = 0
for k in range(T):
pk = 0
for n in range(N):
pk = pk + math.exp(-0.5*((x[k]+wd/2-X[n])/h)**2)
pk = A * pk
infoentropy = infoentropy - pk*math.log(pk)*wd
return infoentropy
def low_filter(x):
(b,a) = signal.butter(N=6,Wn=0.1,btype='lowpass',output='ba')
yn_filtered = signal.filtfilt(b,a,x)
return yn_filtered
def segment(audio,fs):
# print("求取香浓熵前的一些处理--------------------")
xlen = len(audio)
#每段单独处理
seg_len = np.int32(0.02*fs)
N = seg_len+1
seg_overlap = np.int32(seg_len/2)
seg_num = np.int32((xlen-seg_overlap-1)/(seg_len-seg_overlap))
Shannon = []
th = []
# print("开始求取香浓熵----------")
for k in range(seg_num):
X = audio[k*(seg_len-seg_overlap):k*(seg_len-seg_overlap)+N]
X = np.reshape(X,[-1,1])
mu = np.sum(X)/N#均值
sigma = math.sqrt(np.dot((X-np.dot(mu,np.ones((N,1),int))).T,(X-np.dot(mu,np.ones((N,1),int))))/(N-1))#方差
#print(sigma)
#threshold = mu - sigma*0.1#门限
h = 1.06*sigma*(N**(-0.2))#高斯核带宽
# print("开始关键步骤----------" + str(k))
infoentropy = Shannon_Entropy(X,h,mu,sigma)
# print("关键步骤结束----------")
#if infoentropy>threshold:
if infoentropy>-1.5:#原本是-0.5,目前测试下来-1.0最好
th.append(1)
else:
th.append(0)
Shannon.append(infoentropy)###香农曲线
#滤波
Shannon = low_filter(Shannon)
# print("香浓熵求取结束--------------------")
#求极大值
# print("开始提取预选框分割--------------------")
anchor = []
points = []
for i in range(1,seg_num-1):
if (((Shannon[i]>Shannon[i-1])and(Shannon[i]>Shannon[i+1]))and(th[i]==1)):#设定的阈值 and(th[i]==1)
point = i*(seg_len-seg_overlap)
points.append(point/8000)
#左右分界是2:1
#考虑了边界的情况
"""
#0.25/2000
if (point+1300)>xlen and (point-700)<0:
anchor.append([10,xlen-10])
elif (point+1300)<=xlen and (point-700)<0:
anchor.append([10,point+1300])
elif (point+1300)>xlen and (point-700)>=0:
anchor.append([point-700,xlen-10])
else:
anchor.append([point-700,point+1300])
if (point+700)>xlen and (point-1300)<0:
anchor.append([10,xlen-10])
elif (point+700)<=xlen and (point-1300)<0:
anchor.append([10,point+700])
elif (point+700)>xlen and (point-1300)>=0:
anchor.append([point-1300,xlen-10])
else:
anchor.append([point-1300,point+700])
if (point+1000)>xlen and (point-1000)<0:
anchor.append([10,xlen-10])
elif (point+1000)<=xlen and (point-1000)<0:
anchor.append([10,point+1000])
elif (point+1000)>xlen and (point-1000)>=0:
anchor.append([point-1000,xlen-10])
else:
anchor.append([point-1000,point+1000])
#0.3/2400
if (point+1600)>xlen and (point-800)<0:
anchor.append([10,xlen-10])
elif (point+1600)<=xlen and (point-800)<0:
anchor.append([10,point+1600])
elif (point+1600)>xlen and (point-800)>=0:
anchor.append([point-800,xlen-10])
else:
anchor.append([point-800,point+1600])
if (point+800)>xlen and (point-1600)<0:
anchor.append([10,xlen-10])
elif (point+800)<=xlen and (point-1600)<0:
anchor.append([10,point+800])
elif (point+800)>xlen and (point-1600)>=0:
anchor.append([point-1600,xlen-10])
else:
anchor.append([point-1600,point+800])
if (point+1000)>xlen and (point-1000)<0:
anchor.append([10,xlen-10])
elif (point+1200)<=xlen and (point-1200)<0:
anchor.append([10,point+1200])
elif (point+1200)>xlen and (point-1200)>=0:
anchor.append([point-1200,xlen-10])
else:
anchor.append([point-1200,point+1200])
"""
#0.4/3200
if (point+2100)>xlen and (point-1100)<0:
anchor.append([10,xlen-10])
elif (point+2100)<=xlen and (point-1100)<0:
anchor.append([10,point+2100])
elif (point+2100)>xlen and (point-1100)>=0:
anchor.append([point-1100,xlen-10])
else:
anchor.append([point-1100,point+2100])
if (point+1100)>xlen and (point-2100)<0:
anchor.append([10,xlen-10])
elif (point+1100)<=xlen and (point-2100)<0:
anchor.append([10,point+1100])
elif (point+1100)>xlen and (point-2100)>=0:
anchor.append([point-2100,xlen-10])
else:
anchor.append([point-2100,point+1100])
if (point+1600)>xlen and (point-1600)<0:
anchor.append([10,xlen-10])
elif (point+1600)<=xlen and (point-1600)<0:
anchor.append([10,point+1600])
elif (point+1600)>xlen and (point-1600)>=0:
anchor.append([point-1600,xlen-10])
else:
anchor.append([point-1600,point+1600])
#0.5/4000
if (point+2600)>xlen and (point-1400)<0:
anchor.append([10,xlen-10])
elif (point+2600)<=xlen and (point-1400)<0:
anchor.append([10,point+2600])
elif (point+2600)>xlen and (point-1400)>=0:
anchor.append([point-1400,xlen-10])
else:
anchor.append([point-1400,point+2600])
if (point+1400)>xlen and (point-2600)<0:
anchor.append([10,xlen-10])
elif (point+1400)<=xlen and (point-2600)<0:
anchor.append([10,point+1400])
elif (point+1400)>xlen and (point-2600)>=0:
anchor.append([point-2600,xlen-10])
else:
anchor.append([point-2600,point+1400])
if (point+2000)>xlen and (point-2000)<0:
anchor.append([10,xlen-10])
elif (point+2000)<=xlen and (point-2000)<0:
anchor.append([10,point+2000])
elif (point+2000)>xlen and (point-2000)>=0:
anchor.append([point-2000,xlen-10])
else:
anchor.append([point-2000,point+2000])
#0.6/4800
if (point+3200)>xlen and (point-1600)<0:
anchor.append([10,xlen-10])
elif (point+3200)<=xlen and (point-1600)<0:
anchor.append([10,point+3200])
elif (point+3200)>xlen and (point-1600)>=0:
anchor.append([point-1600,xlen-10])
else:
anchor.append([point-1600,point+3200])
if (point+1600)>xlen and (point-3200)<0:
anchor.append([10,xlen-10])
elif (point+1600)<=xlen and (point-3200)<0:
anchor.append([10,point+1600])
elif (point+1600)>xlen and (point-3200)>=0:
anchor.append([point-3200,xlen-10])
else:
anchor.append([point-3200,point+1600])
if (point+2000)>xlen and (point-2000)<0:
anchor.append([10,xlen-10])
elif (point+2400)<=xlen and (point-2400)<0:
anchor.append([10,point+2400])
elif (point+2400)>xlen and (point-2400)>=0:
anchor.append([point-2400,xlen-10])
else:
anchor.append([point-2400,point+2400])
"""
#0.7/5600
if (point+3700)>xlen and (point-1900)<0:
anchor.append([10,xlen-10])
elif (point+3700)<=xlen and (point-1900)<0:
anchor.append([10,point+3700])
elif (point+3700)>xlen and (point-1900)>=0:
anchor.append([point-1900,xlen-10])
else:
anchor.append([point-1900,point+3700])
if (point+1900)>xlen and (point-3700)<0:
anchor.append([10,xlen-10])
elif (point+1900)<=xlen and (point-3700)<0:
anchor.append([10,point+1900])
elif (point+1900)>xlen and (point-3700)>=0:
anchor.append([point-3700,xlen-10])
else:
anchor.append([point-3700,point+1900])
if (point+2000)>xlen and (point-2000)<0:
anchor.append([10,xlen-10])
elif (point+2800)<=xlen and (point-2800)<0:
anchor.append([10,point+2800])
elif (point+2800)>xlen and (point-2800)>=0:
anchor.append([point-2800,xlen-10])
else:
anchor.append([point-2800,point+2800])
"""
#未考虑边界的情况
"""
#0.25/2000
if (point+1300)<xlen and (point-700)>0:
anchor.append([point-700,point+1300])
if (point-1300)>0 and (point+700)<xlen:
anchor.append([point-1300,point+700])
if ((point+1000)<xlen and (point-1000)>0):
anchor.append([point-1000,point+1000])
#0.3/2400
if (point+1600)<xlen and (point-800)>0:
anchor.append([point-800,point+1600])
if (point-1600)>0 and (point+800)<xlen:
anchor.append([point-1600,point+800])
if ((point+1200)<xlen and (point-1200)>0):
anchor.append([point-1200,point+1200])
#0.4/3200
if (point+2100)<xlen and (point-1100)>0:
anchor.append([point-1100,point+2100])
if (point-2100)>0 and (point+1100)<xlen:
anchor.append([point-2100,point+1100])
if ((point+1600)<xlen and (point-1600)>0):
anchor.append([point-1600,point+1600])
"""
"""
#0.5/4000
if (point+2600)<xlen and (point-1400)>0:
anchor.append([point-1400,point+2600])
if (point-2600)>0 and (point+1400)<xlen:
anchor.append([point-2600,point+1400])
if ((point+2000)<xlen and (point-2000)>0):
anchor.append([point-2000,point+2000])
#0.6/4800
if (point+3200)<xlen and (point-1600)>0:
anchor.append([point-1600,point+3200])
if (point-3200)>0 and (point+1600)<xlen:
anchor.append([point-3200,point+1600])
if ((point+2400)<xlen and (point-2400)>0):
anchor.append([point-2400,point+2400])
#0.7/5600
if (point+3700)<xlen and (point-1900)>0:
anchor.append([point-1900,point+3700])
if (point-3700)>0 and (point+1900)<xlen:
anchor.append([point-3700,point+1900])
if ((point+2800)<xlen and (point-2800)>0):
anchor.append([point-2800,point+2800])
"""
"""
#0.75/6000
if ((point+4800)<xlen and (point-4800)>0 ):
anchor.append([point-1200,point+4800])
anchor.append([point-3000,point+3000])
anchor.append([point-4800,point+1200])
"""
# print("预选框提取结束--------------------")
# amptitu = np.ones((len(points)))
# 画图
#
# plt.plot(list(np.arange(0,len(audio)/8000,1/8000)),audio,'b-')
# plt.show()
# plt.plot(range(len(Shannon))*((seg_len-seg_overlap)/8000),Shannon,'k-',points,amptitu,'b*')
# plt.show()
return anchor