From cb4352a996d82d2b786ca3405029b31bba49a758 Mon Sep 17 00:00:00 2001 From: John Greb Date: Sat, 17 Jan 2015 15:59:44 +0000 Subject: [PATCH] Bit tools. --- plugins/channel/lora/lorabits.h | 36 ++++++++++++++++++++++++++++++ plugins/channel/lora/lorademod.cpp | 20 ++++++++++------- plugins/channel/lora/lorademod.h | 9 ++++---- 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 plugins/channel/lora/lorabits.h diff --git a/plugins/channel/lora/lorabits.h b/plugins/channel/lora/lorabits.h new file mode 100644 index 000000000..0f238d897 --- /dev/null +++ b/plugins/channel/lora/lorabits.h @@ -0,0 +1,36 @@ +/* + Interleaving is "easiest" if the same number of bits is used per symbol as for FEC + Chosen mode "spreading 8, low rate" has 6 bits per symbol, so use 4:6 FEC +*/ + +// Needs adjusting for different sizes +void interleave(short* inout) +{ + int i, index = 6; + short in[index * 2]; + for (i = 0; i < index; i++) + in[i] = inout[i]; + for (i = 0; i < index; i++) { + inout[i] = (1 & in[0 + i]) | (2 & in[1 + i]) | (4 & in[2 + i]) + | (8 & in[3 + i]) | (16 & in[4 + i]) | (32 & in[5 + i]); + in[i + index] = in[i]; + } +} + +// Same sequence for any size +void make_gray(void) +{ + short gray[1<<8]; +// short ungray[1<<8]; + short k = 0; + for (short i = 0; i < 1<<8; i++) { + gray[i] = k; +// ungray[k] = i; + short r = (i+1) & ~i; + k ^= r; + } +// for (short i = 0; i < 1<<5; i++) +// printf("%x:%x:%x.\n", i, gray[i], ungray[gray[i]]); +} + + diff --git a/plugins/channel/lora/lorademod.cpp b/plugins/channel/lora/lorademod.cpp index f10dca46e..ea70f0720 100644 --- a/plugins/channel/lora/lorademod.cpp +++ b/plugins/channel/lora/lorademod.cpp @@ -40,6 +40,7 @@ LoRaDemod::LoRaDemod(SampleSink* sampleSink) : m_bin = 0; m_result = 0; m_count = 0; + m_header = 0; loraFilter = new sfft(LORA_SFFT_LEN); negaFilter = new sfft(LORA_SFFT_LEN); @@ -64,8 +65,8 @@ void LoRaDemod::configure(MessageQueue* messageQueue, Real Bandwidth) int LoRaDemod::detect(Complex c, Complex a) { - int i; - float peak; + int i, result, negresult; + float peak, negpeak; float mag[LORA_SFFT_LEN]; float rev[LORA_SFFT_LEN]; @@ -77,18 +78,21 @@ int LoRaDemod::detect(Complex c, Complex a) // process spectrum every 32 samples loraFilter->fetch(mag); negaFilter->fetch(rev); - peak = 0.0f; - m_result = 0; + peak = negpeak = 0.0f; + result = negresult = 0; for (i = 0; i < LORA_SFFT_LEN; i++) { - if (rev[i]/3 > peak) { - peak = rev[i]/3; - m_result = i; + if (rev[i] > negpeak) { + negpeak = rev[i]; + negresult = i; } if (mag[i] > peak) { peak = mag[i]; - m_result = i; + result = i; } } + if (peak > negpeak) { + m_result = result; + } return m_result; } diff --git a/plugins/channel/lora/lorademod.h b/plugins/channel/lora/lorademod.h index 33da4ada3..db2c029c3 100644 --- a/plugins/channel/lora/lorademod.h +++ b/plugins/channel/lora/lorademod.h @@ -25,10 +25,10 @@ #include "util/message.h" #include "dsp/fftfilt.h" -#define SPREADFACTOR (1<<8) - -/* Chosen for number of bins, not symbol length */ -#define LORA_SFFT_LEN (128) +#define DATA_BITS (6) +#define SAMPLEBITS (DATA_BITS + 2) +#define SPREADFACTOR (1 << SAMPLEBITS) +#define LORA_SFFT_LEN (SPREADFACTOR / 2) class LoRaDemod : public SampleSink { public: @@ -73,6 +73,7 @@ private: int m_bin; int m_result; int m_count; + int m_header; sfft* loraFilter; sfft* negaFilter;