-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
139 lines (127 loc) · 3.83 KB
/
utils.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
from threading import Thread
class MyThread(Thread):
'''Multiple threads wrapper'''
def __init__(self, func, args):
Thread.__init__(self)
self.func = func
self.args = args
self.result = None
def run(self):
self.result = self.func(*self.args)
def get_result(self):
return self.result
def wave_analysis(histogram:list, threshold:float, low_granularity:int, high_granularity:int) -> list:
'''wave analysis
Params:
histogram: a wave
threshold: the threshold
low_granularity: the low granularity of a wave peak, if lower than threshold in peak smaller than low_granularity, ignored
high_granularity: the high granularity of a wave peak, only wave length higher than high_granularity will be accepted
----------
Return:
wave_peaks: the list of peaks, each one with up_point and down_point
----------
Note:
the constraint is somehow strict
'''
up_point = -1
in_peak = False
if histogram[0] > threshold:
up_point = 0
in_peak = True
wave_peaks = []
for i, x in enumerate(histogram):
if not in_peak:
if x >= threshold:
# start a new peak
up_point = i
in_peak = True
else:
# stay in the background
continue
else:
if x < threshold:
if i - up_point <= low_granularity:
# ignore the noise
continue
else:
if i - up_point <= high_granularity:
# refuse this peak
in_peak = False
continue
else:
# accept this peak
down_point = i
wave_peaks.append((up_point, i))
in_peak = False
else:
# stay in the peak
continue
# deal with the last peak
if in_peak and up_point != -1 and len(histogram)-1-up_point > high_granularity:
down_point = len(histogram)-1
wave_peaks.append((up_point, down_point))
in_peak = False
return wave_peaks
def digit2char(charactor:str) -> str:
'''convert a digit charactor to possible English charactor
Params:
charactor: a digit or English charactor
----------
Return:
charactor: English charactor
'''
if charactor == '0':
charactor = 'O'
elif charactor == '1':
charactor = 'I'
elif charactor == '2':
charactor = 'Z'
elif charactor == '3':
charactor = 'E'
elif charactor == '4':
charactor = 'A'
elif charactor == '5':
charactor = 'S'
elif charactor == '6':
charactor = 'G'
elif charactor == '7':
charactor = 'T'
elif charactor == '8':
charactor = 'B'
elif charactor == '9':
charactor = 'P'
else:
charactor = charactor
return charactor
def char2digit(charactor:str) -> str:
'''convert to a English charactor to possible digit charactor
Params:
charactor: a digit or English charactor
----------
Return:
charactor: digit or English charactor
'''
if charactor == 'O':
charactor = '0'
elif charactor == 'I':
charactor = '1'
elif charactor == 'Z':
charactor = '2'
elif charactor == 'E':
charactor = '3'
elif charactor == 'A':
charactor = '4'
elif charactor == 'S':
charactor = '5'
elif charactor == 'G':
charactor = '6'
elif charactor == 'T':
charactor = '7'
elif charactor == 'B':
charactor = '8'
elif charactor == 'P':
charactor = '9'
else:
charactor = charactor
return charactor