-
Notifications
You must be signed in to change notification settings - Fork 48
/
LoRaDemod.cpp
396 lines (355 loc) · 13.1 KB
/
LoRaDemod.cpp
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2016-2016 Lime Microsystems
// SPDX-License-Identifier: BSL-1.0
#include <Pothos/Framework.hpp>
#include <iostream>
#include <complex>
#include <cstring>
#include <cmath>
#include "LoRaDetector.hpp"
/***********************************************************************
* |PothosDoc LoRa Demod
*
* Demodulate LoRa packets from a complex sample stream into symbols.
*
* <h2>Input format</h2>
*
* The input port 0 accepts a complex sample stream of modulated chirps
* received at the specified bandwidth and carrier frequency.
*
* <h2>Output format</h2>
*
* The output port 0 produces a packet containing demodulated symbols.
* The format of the packet payload is a buffer of unsigned shorts.
* A 16-bit short can fit all size symbols from 7 to 12 bits.
*
* <h2>Debug port raw</h2>
*
* The raw debug port outputs the LoRa signal annotated with labels
* for important synchronization points in the input sample stream.
*
* <h2>Debug port dec</h2>
*
* The dec debug port outputs the LoRa signal downconverted
* by a locally generated chirp with the same annotation labels as the raw output.
*
* |category /LoRa
* |keywords lora
*
* |param sf[Spread factor] The spreading factor controls the symbol spread.
* Each symbol will occupy 2^SF number of samples given the waveform BW.
* |default 10
*
* |param sync[Sync word] The sync word is a 2-nibble, 2-symbol sync value.
* The sync word is encoded after the up-chirps and before the down-chirps.
* The demodulator ignores packets that do not match the sync word.
* |default 0x12
*
* |param thresh[Threshold] The minimum required level in dB for the detector.
* The threshold level is used to enter and exit the demodulation state machine.
* |units dB
* |default -30.0
*
* |param mtu[Symbol MTU] Produce MTU at most symbols after sync is found.
* The demodulator does not inspect the payload and will produce at most
* the specified MTU number of symbols or less if the detector squelches.
* |units symbols
* |default 256
*
* |factory /lora/lora_demod(sf)
* |setter setSync(sync)
* |setter setThreshold(thresh)
* |setter setMTU(mtu)
**********************************************************************/
class LoRaDemod : public Pothos::Block
{
public:
LoRaDemod(const size_t sf):
N(1 << sf),
_fineSteps(128),
_detector(N),
_sync(0x12),
_thresh(-30.0),
_mtu(256)
{
this->registerCall(this, POTHOS_FCN_TUPLE(LoRaDemod, setSync));
this->registerCall(this, POTHOS_FCN_TUPLE(LoRaDemod, setThreshold));
this->registerCall(this, POTHOS_FCN_TUPLE(LoRaDemod, setMTU));
this->setupInput(0, typeid(std::complex<float>));
this->setupOutput(0);
this->setupOutput("raw", typeid(std::complex<float>));
this->setupOutput("dec", typeid(std::complex<float>));
this->setupOutput("fft", typeid(std::complex<float>));
this->registerSignal("error");
this->registerSignal("power");
this->registerSignal("snr");
//use at most two input symbols available
this->input(0)->setReserve(N*2);
//store port pointers to avoid lookup by name
_rawPort = this->output("raw");
_decPort = this->output("dec");
_fftPort = this->output("fft");
//generate chirp table
float phase = -M_PI;
double phaseAccum = 0.0;
for (size_t i = 0; i < N; i++)
{
phaseAccum += phase;
auto entry = std::polar(1.0, phaseAccum);
_upChirpTable.push_back(std::complex<float>(std::conj(entry)));
_downChirpTable.push_back(std::complex<float>(entry));
phase += (2*M_PI)/N;
}
phaseAccum = 0.0;
phase = 2.0 * M_PI / (N * _fineSteps);
for (size_t i = 0; i < N * _fineSteps; i++){
phaseAccum += phase;
auto entry = std::polar(1.0, phaseAccum);
_fineTuneTable.push_back(std::complex<float>(entry));
}
_fineTuneIndex = 0;
}
static Block *make(const size_t sf)
{
return new LoRaDemod(sf);
}
void setSync(const unsigned char sync)
{
_sync = sync;
}
void setThreshold(const double thresh_dB)
{
_thresh = thresh_dB;
}
void setMTU(const size_t mtu)
{
_mtu = mtu;
}
void activate(void)
{
_state = STATE_FRAMESYNC;
_chirpTable = _upChirpTable.data();
}
void work(void)
{
auto inPort = this->input(0);
if (inPort->elements() < N*2) return;
size_t total = 0;
auto inBuff = inPort->buffer().as<const std::complex<float> *>();
auto rawBuff = _rawPort->buffer().as<std::complex<float> *>();
auto decBuff = _decPort->buffer().as<std::complex<float> *>();
auto fftBuff = _fftPort->buffer().as<std::complex<float> *>();
//process the available symbol
for (size_t i = 0; i < N; i++){
auto samp = inBuff[i];
auto decd = samp*_chirpTable[i] * _fineTuneTable[_fineTuneIndex];
_fineTuneIndex -= _finefreqError * _fineSteps;
if (_fineTuneIndex < 0) _fineTuneIndex += N * _fineSteps;
else if (_fineTuneIndex >= int(N * _fineSteps)) _fineTuneIndex -= N * _fineSteps;
rawBuff[i] = samp;
decBuff[i] = decd;
_detector.feed(i, decd);
}
float power = 0;
float powerAvg = 0;
float snr = 0;
float fIndex = 0;
auto value = _detector.detect(power,powerAvg,fIndex,fftBuff);
snr = power - powerAvg;
const bool squelched = (snr < _thresh);
switch (_state)
{
////////////////////////////////////////////////////////////////
case STATE_FRAMESYNC:
////////////////////////////////////////////////////////////////
{
//format as observed from inspecting RN2483
bool syncd = not squelched and (_prevValue+4)/8 == 0;
bool match0 = (value+4)/8 == unsigned(_sync>>4);
bool match1 = false;
//if the symbol matches sync word0 then check sync word1 as well
//otherwise assume its the frame sync and adjust for frequency error
if (syncd and match0)
{
int ft = _fineTuneIndex;
for (size_t i = 0; i < N; i++)
{
auto samp = inBuff[i + N];
auto decd = samp*_chirpTable[i] * _fineTuneTable[ft];
ft -= _finefreqError * _fineSteps;
if (ft < 0) ft += N * _fineSteps;
else if (ft >= int(N * _fineSteps)) ft -= N * _fineSteps;
rawBuff[i+N] = samp;
decBuff[i+N] = decd;
_detector.feed(i, decd);
}
auto value1 = _detector.detect(power,powerAvg,fIndex);
//format as observed from inspecting RN2483
match1 = (value1+4)/8 == unsigned(_sync & 0xf);
}
if (syncd and match0 and match1)
{
total = 2*N;
_state = STATE_DOWNCHIRP0;
_chirpTable = _downChirpTable.data();
_id = "SYNC";
}
//otherwise its a frequency error
else if (not squelched)
{
total = N - value;
_finefreqError += fIndex;
std::stringstream stream;
stream.precision(4);
stream << std::fixed << "P " << fIndex;
_id = stream.str();
// _id = "P " + std::to_string(fIndex);
}
//just noise
else
{
total = N;
_finefreqError = 0;
_fineTuneIndex = 0;
_id = "";
}
} break;
////////////////////////////////////////////////////////////////
case STATE_DOWNCHIRP0:
////////////////////////////////////////////////////////////////
{
_state = STATE_DOWNCHIRP1;
total = N;
_id = "DC";
int error = value;
if (value > N/2) error -= N;
//std::cout << "error0 " << error << std::endl;
_freqError = error;
} break;
////////////////////////////////////////////////////////////////
case STATE_DOWNCHIRP1:
////////////////////////////////////////////////////////////////
{
_state = STATE_QUARTERCHIRP;
total = N;
_chirpTable = _upChirpTable.data();
_id = "";
_outSymbols = Pothos::BufferChunk(typeid(int16_t), _mtu);
int error = value;
if (value > N/2) error -= N;
//std::cout << "error1 " << error << std::endl;
_freqError = (_freqError + error)/2;
this->emitSignal("error", _freqError);
this->emitSignal("power", power);
this->emitSignal("snr", snr);
} break;
////////////////////////////////////////////////////////////////
case STATE_QUARTERCHIRP:
////////////////////////////////////////////////////////////////
{
_state = STATE_DATASYMBOLS;
total = N/4 + (_freqError / 2);
_finefreqError += (_freqError / 2);
_symCount = 0;
_id = "QC";
} break;
////////////////////////////////////////////////////////////////
case STATE_DATASYMBOLS:
////////////////////////////////////////////////////////////////
{
total = N;
_outSymbols.as<int16_t *>()[_symCount++] = int16_t(value);
if (_symCount >= _mtu or squelched)
{
//for (size_t j = 0; j < _symCount; j++)
// std::cout << "demod[" << j << "]=" << _outSymbols.as<const uint16_t *>()[j] << std::endl;
Pothos::Packet pkt;
pkt.payload = _outSymbols;
pkt.payload.length = _symCount*sizeof(int16_t);
this->output(0)->postMessage(pkt);
_finefreqError = 0;
_state = STATE_FRAMESYNC;
}
std::stringstream stream;
stream.precision(4);
stream << std::fixed << "S" << _symCount << " " << fIndex;
_id = stream.str();
//_id = "S" + std::to_string(_symCount) + " " + std::to_string(fIndex);
// _finefreqError += fIndex;
} break;
}
if (not _id.empty())
{
_rawPort->postLabel(Pothos::Label(_id, Pothos::Object(), 0));
_decPort->postLabel(Pothos::Label(_id, Pothos::Object(), 0));
_fftPort->postLabel(Pothos::Label(_id, Pothos::Object(), 0));
}
inPort->consume(total);
_rawPort->produce(total);
_decPort->produce(total);
_fftPort->produce(N);
_prevValue = value;
}
//! Custom output buffer manager with slabs large enough for debug output
Pothos::BufferManager::Sptr getOutputBufferManager(const std::string &name, const std::string &domain)
{
if (name == "raw" or name == "dec")
{
this->output(name)->setReserve(N * 2);
Pothos::BufferManagerArgs args;
args.bufferSize = N*2*sizeof(std::complex<float>);
return Pothos::BufferManager::make("generic", args);
}else if (name == "fft"){
this->output(name)->setReserve(N);
Pothos::BufferManagerArgs args;
args.bufferSize = N*sizeof(std::complex<float>);
return Pothos::BufferManager::make("generic", args);
}
return Pothos::Block::getOutputBufferManager(name, domain);
}
//! Custom input buffer manager with slabs large enough for fft input
Pothos::BufferManager::Sptr getInputBufferManager(const std::string &name, const std::string &domain)
{
if (name == "0")
{
Pothos::BufferManagerArgs args;
args.bufferSize = std::max(args.bufferSize,
N*2*sizeof(std::complex<float>));
return Pothos::BufferManager::make("generic", args);
}
return Pothos::Block::getInputBufferManager(name, domain);
}
private:
//configuration
const size_t N;
const size_t _fineSteps;
LoRaDetector<float> _detector;
std::complex<float> *_chirpTable;
std::vector<std::complex<float>> _upChirpTable;
std::vector<std::complex<float>> _downChirpTable;
std::vector<std::complex<float>> _fineTuneTable;
unsigned char _sync;
float _thresh;
size_t _mtu;
Pothos::OutputPort *_rawPort;
Pothos::OutputPort *_decPort;
Pothos::OutputPort *_fftPort;
//state
enum LoraDemodState
{
STATE_FRAMESYNC,
STATE_DOWNCHIRP0,
STATE_DOWNCHIRP1,
STATE_QUARTERCHIRP,
STATE_DATASYMBOLS,
};
LoraDemodState _state;
size_t _symCount;
Pothos::BufferChunk _outSymbols;
std::string _id;
short _prevValue;
int _freqError;
int _fineTuneIndex;
float _finefreqError;
};
static Pothos::BlockRegistry registerLoRaDemod(
"/lora/lora_demod", &LoRaDemod::make);