-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigbee_rxer.m
163 lines (130 loc) · 7.46 KB
/
zigbee_rxer.m
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
function [rxBits, rxSyms] = zigbee_rxer(rxSignal, OSR, isCoherent, numPkt)
persistent chipLen
persistent chipMapStd
persistent chipMapPhase
persistent numBitPerPHYPayload
% numPkt = size(rxSignal, 2);
% numBitPerPkt = 96; % According to WEBee, each ZigBee packet's PHY payload contains 96 bits (i.e., 24 symbols).
% rxBits = zeros(numBitPerPkt, numPkt);
if isempty(chipLen)
chipLen = 32;
end
if isempty(numBitPerPHYPayload)
% According to WEBee, each ZigBee packet's PHY payload contains 96 bits (i.e., 24 symbols).
numBitPerPHYPayload = 96;
% numBytePerPHYPayload = numBitPerPHYPayload / 8;
end
rxBits = zeros(numBitPerPHYPayload, numPkt);
if isCoherent == false
if mod(size(rxSignal(:, 1), 1), OSR) > 0
numPadSampPerSig = OSR - mod(size(rxSignal(:, 1), 1), OSR);
rxSignal = [rxSignal; zeros(numPadSampPerSig, numPkt)];
end
rxSignal = rxSignal(1:2:end-2, :);
if isempty(chipMapPhase)
chipMapPhase = [0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0;
0, 1, 0, 0, 1, 1, 1, 0, 0,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0, 1;
0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0;
0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0;
0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1;
0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1];
end
phaseShifts = angle(rxSignal(2:1:size(rxSignal, 1), :) .* conj(rxSignal(1:1:size(rxSignal, 1)-1, :)));
phaseShifts = phaseShifts > 0;
if mod(size(phaseShifts, 1), chipLen) > 0
numPadChips = chipLen - mod(size(phaseShifts, 1), chipLen);
padChips = zeros(numPadChips, numPkt);
phaseShifts = [phaseShifts; padChips];
end
rxSyms = zeros(size(phaseShifts, 1) / chipLen / 4, numPkt);
for ithPkt = 1:1:numPkt
bits = zeros(4, size(phaseShifts, 1) / chipLen);
for idx = 1:1:(size(phaseShifts, 1) / chipLen)
% Chip to symbol mapping
thisChip = phaseShifts(1 + (idx - 1) * chipLen:idx * chipLen, ithPkt);
% find the chip sequence that looks the most like the received (minimum number of bit errors)
[~, symbol] = min(sum(xor(thisChip', chipMapPhase), 2));
symbol = symbol - 1; % -1 for 1-based indexing
rxSyms(idx, ithPkt) = symbol;
% Symbol to bit mapping
bits(:, idx) = int2bit(symbol, 4, false);
end
%% Remove headers
bits = bits(:);
preambleLen = 4 * 8; % 4 octets
SFDLen = 8; % 1 octet
PHRLen = 8; % 1 octet
offset = preambleLen + SFDLen + PHRLen;
rxBits(:, ithPkt) = bits(1 + offset:end);
end
else
if isempty(chipMapStd)
% See Table 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 1 1 0 0 0 0 0 0 1 1 1 0 1 1 1 1 0 1 1 1 0 0 0];
end
re = real(rxSignal(1:end - OSR / 2, :)); % remove "dummy" real part at end
im = imag(rxSignal(1 + OSR / 2:end, :)); % remove "dummy" imag part at start
if mod(size(re(:, 1), 1), OSR) > 0
numPadSampPerSig = OSR - mod(size(re(:, 1), 1), OSR);
re = [re; zeros(numPadSampPerSig, size(re, 2))];
im = [im; zeros(numPadSampPerSig, size(im, 2))];
end
rxSyms = zeros(size(re, 1) / OSR / chipLen, numPkt);
for ithPkt = 1:1:numPkt
%O-QPSK demodulation
filteredReal = intdump(re(:, ithPkt), OSR);
filteredImag = intdump(im(:, ithPkt), OSR);
temp = [transpose(filteredReal); transpose(filteredImag)];
demodulated = temp(:) > 0; % Slicing, convert [-1 1] to [0 1]
if mod(length(demodulated), chipLen) > 0
numPadChips = chipLen - mod(length(demodulated), chipLen);
padChips = zeros(numPadChips, 1);
demodulated = [demodulated; padChips];
end
bits = zeros(4, length(demodulated) / chipLen);
for idx = 1:1:(length(demodulated) / chipLen)
% Chip to symbol mapping
thisChip = demodulated(1 + (idx - 1) * chipLen:idx * chipLen);
% find the chip sequence that looks the most like the received (minimum number of bit errors)
[~, symbol] = min(sum(xor(thisChip', chipMapStd), 2));
symbol = symbol - 1; % -1 for 1-based indexing
rxSyms(idx, ithPkt) = symbol;
% Symbol to bit mapping
bits(:, idx) = int2bit(symbol, 4, false);
end
%% Remove headers
bits = bits(:);
preambleLen = 4 * 8; % 4 octets
SFDLen = 8; % 1 octet
PHRLen = 8; % 1 octet
offset = preambleLen + SFDLen + PHRLen;
rxBits(:, ithPkt) = bits(1 + offset:end);
end
end
end