-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBLETX.m
93 lines (72 loc) · 2.77 KB
/
BLETX.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
function [txWaveform, txSyms, numPkt] = BLETX(txBits, OSR)
persistent BlueBeeMap
persistent chipLenBLE
persistent SHR
persistent PHR
persistent numBitPerPHYPayload
persistent numBytePerPHYPayload
if isempty(BlueBeeMap)
BlueBeeMap = BlueBeeMapGenerator;
end
if isempty(chipLenBLE)
chipLenBLE = 16;
end
if isempty(numBitPerPHYPayload)
% This setting aligns with that in ZigBee, in which each ZigBee
% packet's PHY payload contains 96 bits (i.e., 24 symbols).
numBitPerPHYPayload = 96;
numBytePerPHYPayload = numBitPerPHYPayload / 8;
end
if isempty(SHR)
% Synchronization header (SHR)
% Preamble is 4 octets, all set to 0.
preamble = zeros(4 * 8, 1);
% Start-of-frame delimiter (SFD)
SFD = [1 1 1 0 0 1 0 1]'; % value from standard (see Fig. 68, IEEE 802.15.4, 2011 Revision)
SHR = [preamble; SFD];
end
if isempty(PHR)
% PHY Header (PHR)
reservedValue = 0;
PHR = [int2bit(numBytePerPHYPayload, 7, false); reservedValue];
end
% Start to generate waveforms
numPkt = ceil(length(txBits) / numBitPerPHYPayload);
if mod(length(txBits), numBitPerPHYPayload) > 0
numPadBits = numBitPerPHYPayload * numPkt - length(txBits);
txBits = [txBits, zeros(1, numPadBits)];
end
txBits = reshape(txBits, [numBitPerPHYPayload, numPkt]);
numSymPerPkt = (length(SHR) + length(PHR) + numBitPerPHYPayload) / 4;
txSyms = zeros(numSymPerPkt, numPkt);
numSampPerPkt = numSymPerPkt * OSR * 16;
txWaveform = zeros(numSampPerPkt, numPkt);
for ithPkt = 1:1:numPkt
% PHY protocol data unit:
PPDU = [SHR; PHR; txBits(:, ithPkt)];
% pre-allocate matrix for performance
chips = zeros(chipLenBLE, length(PPDU) / 4);
for idx = 1:length(PPDU) / 4
% Bit to symbol mapping
currBits = PPDU(1 + (idx - 1) * 4:idx * 4);
symbol = bit2int(currBits, 4, false);
txSyms(idx, ithPkt) = symbol;
% Symbol to chip mapping
chips(:, idx) = BlueBeeMap(1 + symbol, :)'; % +1 for 1-based indexing
end
txWaveform(:, ithPkt) = bleWaveformGenerator(chips(:), OSR);
end
txWaveform = [txWaveform; zeros(OSR/2, size(txWaveform, 2))];
% %++++++++++++++++++++++++++++
% numMsg = length(messages);
% chips = zeros(1, 16 * numMsg);
% for ith = 1: 1: numMsg
% chips(1, 16*(ith-1)+1: 16*(ith)) = BlueBeeMap(messages(1,ith),:);
% end
%
% % BLEBits = chips(1:2:end);
% sps = 100;
% txWaveform = bleWaveformGenerator(chips', sps);
% [num_smpl, ~] = size(txWaveform);
% txWaveform = [zeros(1, sps), reshape(txWaveform, [1, num_smpl]), zeros(1, sps)];
end