-
Notifications
You must be signed in to change notification settings - Fork 19
/
rtl-zwave.py
executable file
·328 lines (254 loc) · 6.93 KB
/
rtl-zwave.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/python
from math import *
from scipy import signal
import matplotlib.pyplot as plt
from scipy import arange
import numpy as np
import sys
from struct import *
from scipy.signal import firwin
def zwave_print(frame):
print "Frame: " + frame.encode("hex")
def bits2bytes(bits):
r = ""
by = 0
c = 0
for b in bits:
by = (by << 1) | b
c = c + 1
if(c & 7 == 0):
r = r + chr(by)
by = 0
return r
def butter_bandpass(lowcut, highcut, fs, order=5):
nyq = 0.5 * fs
low = lowcut / nyq
high = highcut / nyq
b, a = signal.butter(order, [low, high], btype='band')
return b, a
def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
b, a = butter_bandpass(lowcut, highcut, fs, order=order)
y = signal.lfilter(b, a, data)
return y
# fEU1 869.85 bw 300khz
# fEU2 868.40 bw 400khz
samp = 2024000
#f = open('test_sample4', 'r')
f = open(sys.argv[1], 'r')
#f = open('zwave_100k.bin', 'r')
n = 0
sigre = []
sigim = []
def rotate_90(j,re,im):
if(j==1):
tmp = re
re = im
im = tmp
elif(j==2):
re = 255 - re
im = 255 - im
elif(j==3):
tmp = 255 - re
re = im
im =tmp
return (re,im)
try:
while(True):
(re, im) = unpack("2B", f.read(2))
# "rotate 90" whatever that means?
j = n & 3
re,im = rotate_90(j,re,im)
re = re - 127
im = im - 127
sigre.append(re)
sigim.append(im)
n = n + 1
#if(n >= 50000):
# break;
except Exception:
print "read error"
sig = np.array(sigre) + 1j*np.array(sigim)
sig = butter_bandpass_filter(sig,350e3,500e3,samp)
#Wn = 300.0e3 / float(samp)
#b, a = signal.butter(6, Wn, 'low')
#sig = signal.lfilter(b, a, sig)
plt.plot(sig.real,label="real")
plt.plot(sig.imag,label="imag")
plt.xlabel('Sample')
plt.title("RAW sampled data")
plt.legend()
plt.show()
sp = np.fft.fft(sig)
freq = np.fft.fftfreq(len(sig)) * samp
plt.plot(freq, sp.real, 'g-', freq, sp.imag, 'b-')
plt.xlabel('Frequency')
plt.title("Spectrum of RAW sampled data")
plt.show()
sold = 0
'''
Algorithm
The imput signal is on the form s(t) = a*exp(-i*w*t+p)
where a is the amplitude
w if the angular frequncy, (in reality w is a function of t but we will ignore that)
p if the phase difference
We wish to find w...
First we take the time derivative(s') of s
s' = -i(w)*a*exp(-i*w*t+p)
then we multiply s' by by conj(s) where conj is complex conjugation
s'*conj(s) = -i(w)*a*exp(-i*w*t+p)*a*exp(i*w*t + p)
= -i(w)*a*a
finally we devide the result by the norm of s squared
s'*conj(s) / |s|^2 = -i(w+p)
Releated to the FSK demodulation, we know that w will fall out to two distinct values.
w1 and w2, and that w2-w1 = dw.
w will have the form w = wc +/- dw, where wc is the center frequnecy.
wc + p will show up as a DC component in the s'*conj(s) / |s|^2 function.
'''
# FSK decoder
def aes_fsk(sig):
s1 = 0
s2 = 0
y2=[]
kk=0
for s in sig:
p = np.abs(s1)
if(p > 0):
ds = (s - s2) / 2
q = (np.conj(s1) * ds)
k = -q.imag/(p * p)
if(k > pi or k < -pi):
k=kk
else:
kk = k
else:
k=0
s2 = s1
s1 = s
y2.append(k)
return y2
# FSK decoder
def atan_fsk(sig):
s1 = 0
y2=[]
for s in sig:
k = np.angle(s*np.conj(s1))
s1 = s
y2.append(k)
return y2
#y2 = aes_fsk(sig)
y2 = atan_fsk(sig)
#y2 = pll_fsk(sig)
Wn = 101e3 / float(samp)
#Wn = 2*9.6e3 / float(samp)
b, a = signal.butter(12, Wn, 'low')
y1 = signal.lfilter(b, a, y2)
Wn = 10.1e3 / float(samp)
#Wn = 2.0e3 / float(samp)
b, a = signal.butter(3, Wn, 'low')
print b
print a
lock_det = signal.lfilter(b, a, y2)
S_IDLE = 0
S_PREAMP = 1
S_BITLOCK = 3
B_PREAMP = 1
B_SOF0 = 2
B_SOF1 = 3
B_DATA = 4
n = 0
pre_len = 0 # Length of preamble bit
pre_cnt = 0;
bit_len = 0
bit_cnt = 0.0;
wc = 0 # center frequency
bits = []
state = S_IDLE
dif = []
last_logic = False
lead_in = 10
n=0
for s in y1:
logic = (s - wc) > 0
#If we are in bitlock mode, make sure that the signal does not derivate by more than
# 1/2 seperation, TODO calculate 1/2 seperation
if(state == S_BITLOCK):
if(fabs(wc - lock_det[n]) < 0.1):
signal=True
else:
signal=False
elif(fabs(lock_det[n]) > 0.01):
signal=True
else:
signal = False
if(signal):
if(state == S_IDLE):
state = S_PREAMP
pre_cnt = 0
pre_len = 0
print "Frame start",n
elif(state == S_PREAMP):
wc = lock_det[n]
pre_len = pre_len + 1
if(logic ^ last_logic): #edge trigger (rising and falling)
pre_cnt = pre_cnt + 1
if(pre_cnt == lead_in): # skip the first lead_in
pre_len = 0;
elif(pre_cnt > 30):
print "Center freq ",wc/(2.0*pi)*samp
state = S_BITLOCK
state_b = B_PREAMP
bit_len = float(pre_len) / (pre_cnt - lead_in)
#print bit_len
bit_cnt = bit_len / 2.0
last_bit = not logic
elif(state == S_BITLOCK): #Preamble has been detected now we are processing bits not samples
if(logic ^ last_logic):
bit_cnt = bit_len / 2.0 #Re-sync on edges
else:
bit_cnt = bit_cnt + 1.0
if(bit_cnt >= bit_len): # new bit
if(state_b == B_PREAMP):
if( logic and last_bit):
state_b = B_SOF1
b_cnt = 1 #This was the first SOF bit
elif(state_b == B_SOF0):
if( not logic ):
b_cnt = b_cnt +1
if(b_cnt == 4):
b_cnt = 0
state_b = B_DATA
else:
print "SOF 0 error",b_cnt,n
state = S_IDLE
elif(state_b == B_SOF1):
if( logic ):
b_cnt = b_cnt +1
if(b_cnt == 4):
b_cnt = 0
state_b = B_SOF0
else:
print "SOF 1 error",b_cnt,n
state = S_IDLE
elif(state_b == B_DATA):
print "Data",n
# print logic
bits.append(logic)
last_bit = logic
bit_cnt = bit_cnt - bit_len
else: # No LOCK
if(state == S_BITLOCK):
frame = bits2bytes(bits)
zwave_print(frame)
bits = []
# break
state = S_IDLE
last_logic = logic
dif.append(s - wc)
n = n + 1
# print len(dif)
plt.plot(y2)
plt.plot(y1)
plt.plot(dif)
plt.plot(lock_det)
plt.show()
sys.exit(0)