-
Notifications
You must be signed in to change notification settings - Fork 1
/
simpdec.py
196 lines (173 loc) · 6.45 KB
/
simpdec.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
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 26 18:05:16 2014
@author: Norman
"""
from collections import deque
import numpy as np
class genericDecoder:
def __init__(self, name='1-31'):
self.tolerance = 5
self.reset()
self.searchTyp = 'factor' # default if not defined other
def initEV1527():
self.typ = 'EV1527'
self.pulse = deque(2*[0])
self.minLength = 10
self.name = name;
if (self.name == '1-31'):
initEV1527()
#self.startFact = 31
self.start_seq= [1,-31]
self.high = [1,-3]
self.low = [3,-1]
self.searchTyp = 'sequence' # default if not defined other
elif (self.name =='1-18'):
initEV1527()
self.start_seq= [1,-18]
#self.startFact = 18
self.high = [1,-8]
self.low = [1,-4]
self.searchTyp = 'sequence' # default if not defined other
elif (self.name =='PT2262'):
self.typ = 'PT2262'
self.pulse = deque(4*[0])
#self.start_seq= [1,31]
self.startFact = 31
self.minLength = 10
#self.searchTyp = 'sequence' # default if not defined other
#high/low are hardcoded at the moment
else:
print'Decoder unbekannt.'
# schould raise exception here
return
self.doDecode = {'EV1527':self.decEV1527, 'PT2262':self.decPT2262, 'Test':self.doTest}
self.doSearch = {'factor':self.searchFact, 'sequence':self.searchSeq}
self.doState = {'ready':self.doSearch[self.searchTyp], 'decoding':self.doDecode[self.typ]}
def decode(self, pulse):
self.pulse.popleft()
self.pulse.append(pulse)
self.doState[self.state]()
return True
def reset(self):
#print 'reset'
self.message=[]
self.takt=0
self.bitcnt=1
self.synccnt=0
self.state='ready'
def searchFact(self): # Suche Startsequenz nach factor 1/n *takt
#print self.pulse
try:
if (abs(self.pulse[1]+self.startFact*self.pulse[0]) < self.tolerance*self.pulse[0]): # Startsequenz 1/31
#print 'Frac %d, abs(%d/%d)' %(abs(self.pulse[1]/float(self.pulse[0])), self.pulse[1], self.pulse[0])
self.synccnt+=1
if (self.synccnt>1):# 2. Sync
self.synccnt=0 # nächste Meldung vorbereiten
self.takt = abs(self.pulse[0]) # der erste ist der Takt
self.state = 'decoding'
self.bitcnt=3
#print self.state
#print self.pulse
except:
#division by zero in 1st run?
print'searchStart fact exceptVal = 0*pulse'
return
def checkSeq(self, seqIn, tolIn=0.1):
#print self.pulse
#if (len(self.message)>=24):
# print "check"
pulse = np.asarray(self.pulse)
seq = np.asarray(seqIn)
if (self.takt!=0):
Val=(pulse/float(self.takt))
else:
return False
#print Val
tol = tolIn*abs(seq)
if (np.all(abs(Val-seq)<tol)):
return True
else:
return False
def searchSeq(self): # Suche Startsequenz nach Muster [1 n]*takt
#print 'searchStart'
#print self.pulse[1], self.pulse[0]
try:
self.takt = self.pulse[-2]
if (self.checkSeq(self.start_seq)):
self.state = 'decoding'
#print self.state
except:
#division by zero in 1st run?
print'searchStart except'
return
def decEV1527(self): #EVOOK decoder
if (self.bitcnt<2):# we'll check 2 bits together
self.bitcnt +=1
else:
self.bitcnt=1
# store normalized values
try:
#Val=[int(round(x/float(self.takt))) for x in list(self.pulse)]
#print Val
if (self.checkSeq(self.low,0.3)):
self.message.append(0) # 0 detected
elif (self.checkSeq(self.high,0.3)):
self.message.append(1) # 1 detected
else: # no bit detected, could be the end of the message
if (len(self.message)>=self.minLength):
print self.typ, self.name, len(self.message)
print self.message
self.reset()
except:
self.reset()
def decPT2262(self): #PT2262 decoder
#print 'decEV1527'
#print self.bitcnt
if (self.bitcnt<4):# we'll check 4 bits together
self.bitcnt +=1
else:
self.bitcnt=1;
# store normalized values
try:
#Val=[int(round(x/float(self.takt))) for x in list(self.pulse)]
#print Val
tol=0.5
if (self.checkSeq([1,-3,1,-3],tol)):
self.message.append(0) # 0 detected
elif (self.checkSeq([3,-1,3,-1],tol)):
self.message.append(1) # 1 detected
elif (self.checkSeq([1,-3,3,-1],tol)):
self.message.append(2) # F detected
else: # no bit detected, could be the end of the message
if (len(self.message)>=self.minLength):
print self.name, len(self.message)
if (np.any(self.message>2)):
print 'message contains floating states => decoder not valid'
else:
print self.message
self.reset()
#print self.pulse
#print self.message
except:
self.reset()
def doTest(self):
print 'TestDecoder'
if __name__ == '__main__':
ev = genericDecoder('1-31')
tcm = genericDecoder('1-18')
pt = genericDecoder('PT2262')
seq = np.load('Logilink.npy')
#seq = np.load('IT-Schalter onoff.npy')
#seq = np.load('IT.npy')
#seq = np.load('tcm.npy')
#seq = np.load('OSV2.npy')
#seq = np.load('ManchestarAS.npy')
cnt=0
for pulse in seq:
#print cnt
#cnt +=1
ev.decode(pulse)
tcm.decode(pulse)
pt.decode(pulse)
#reload(sys.modules["decoder"])