-
Notifications
You must be signed in to change notification settings - Fork 49
/
decodeOOK.py
108 lines (88 loc) · 2.4 KB
/
decodeOOK.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
import wave
import sys
from struct import *
from collections import Counter
track=wave.open(sys.argv[1])
frames=track.getnframes()
n= 0
max= 0
samples= []
keylen = 12
avg = 0;
print "number of frames: {}".format(frames)
while n < frames:
n += 1
current = 0
frame = track.readframes(1)
if len(frame) != 2 and len(frame) != 4:
continue
left_channel = frame[:2]
current= unpack("<h",left_channel)[0]
if current > max:
max= current;
samples.append(current)
avg = sum(samples) / len(samples)
avg = avg + (max/100) + 100
print "Max: ",str(max),"Average: ",str(avg)
peaks= []
foundKeys = []
sPeak = 0 #startPeak
ePeak = 0 #endPeak
minPeakDistance = 1 #minimum peak distance
for currFrame in range(0,len(samples)):
if(samples[currFrame] > avg):
if(sPeak == 0):
sPeak = currFrame #~starts here
else:
if(sPeak != 0 and (sPeak+minPeakDistance) < currFrame ):
ePeak = currFrame
distance = ePeak - sPeak
peaks.append({"d":distance,"s":ePeak,"e":ePeak})
sPeak = 0
currentSeg = []
n = 0
#get average peak distance (to see dividers between signals)
#avgPeakLen = sum(peaks["d"]) / len(peaks)
#avgPeakLen = sum([p['d'] for p in peaks]) / len(peaks)
avgPeakLen = sum([peaks[i+1]['s'] - p['e'] for i, p in enumerate(peaks) if i < len(peaks) - 1])/(i + 1)
avgPeakLen = avgPeakLen * 3
print "avgpl:",avgPeakLen
print "len Peaks",len(peaks)
tmpBin = ""
while n < len(peaks):
#if(peaks[n]["d"] > avgPeakLen and len(currentSeg) > 0):
if(n > 1):
diff = peaks[n]["s"] - peaks[n-1]["e"]
#if (diff > 30):
#print diff
if(n > 1 and ((peaks[n]["s"] - peaks[n-1]["e"]) > avgPeakLen and len(currentSeg) > 1)):
mean = (sum(currentSeg) / len(currentSeg)) - 1
for c in currentSeg:
if ( c > mean ):
tmpBin += "0"
else:
tmpBin += "1"
foundKeys.append(tmpBin)
#print tmpBin
tmpBin = "";
currentSeg = []
else:
currentSeg.append(peaks[n]["d"])
n= n + 1
#leftovers
if (len(currentSeg) > 0):
mean = (sum(currentSeg) / len(currentSeg)) - 1
for c in currentSeg:
if ( c > mean ):
tmpBin += "0"
else:
tmpBin += "1"
foundKeys.append(tmpBin)
tmpBin = "";
keyList = Counter(foundKeys)
print "\n\n(Top)Found Keys:"
x = 0
for k, v in keyList.most_common(10):
if (len(k) > 2 and int(k,2) != 0):
x+=1
print x,":",k,"(",v,") - len:",len(k), "Hex:",hex(int(k,2))