1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-12-18 23:55:50 -05:00
sdrangel/plugins/channelrx/demodlora/lorademod.cpp

332 lines
7.3 KiB
C++
Raw Normal View History

2015-01-10 19:12:58 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2012 maintech GmbH, Otto-Hahn-Str. 15, 97204 Hoechberg, Germany //
// written by Christian Daniel //
// (c) 2015 John Greb
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "../../channelrx/demodlora/lorademod.h"
#include <dsp/downchannelizer.h>
2015-01-10 19:12:58 -05:00
#include <QTime>
2015-08-17 02:29:34 -04:00
#include <QDebug>
2015-01-10 19:12:58 -05:00
#include <stdio.h>
#include "../../channelrx/demodlora/lorabits.h"
2015-01-15 05:57:05 -05:00
2015-01-10 19:12:58 -05:00
MESSAGE_CLASS_DEFINITION(LoRaDemod::MsgConfigureLoRaDemod, Message)
LoRaDemod::LoRaDemod(BasebandSampleSink* sampleSink) :
m_sampleSink(sampleSink),
m_settingsMutex(QMutex::Recursive)
2015-01-10 19:12:58 -05:00
{
setObjectName("LoRaDemod");
2015-01-10 19:12:58 -05:00
m_Bandwidth = 7813;
m_sampleRate = 96000;
m_frequency = 0;
m_nco.setFreq(m_frequency, m_sampleRate);
2015-01-11 07:27:11 -05:00
m_interpolator.create(16, m_sampleRate, m_Bandwidth/1.9);
2015-01-10 19:12:58 -05:00
m_sampleDistanceRemain = (Real)m_sampleRate / m_Bandwidth;
2015-01-11 07:27:11 -05:00
m_chirp = 0;
2015-01-10 19:12:58 -05:00
m_angle = 0;
2015-01-12 14:59:45 -05:00
m_bin = 0;
2015-01-13 14:14:36 -05:00
m_result = 0;
m_count = 0;
2015-01-17 10:59:44 -05:00
m_header = 0;
2015-01-22 17:30:55 -05:00
m_time = 0;
2015-08-17 02:29:34 -04:00
m_tune = 0;
2015-01-11 14:30:48 -05:00
loraFilter = new sfft(LORA_SFFT_LEN);
2015-01-13 19:48:48 -05:00
negaFilter = new sfft(LORA_SFFT_LEN);
2015-01-15 05:57:05 -05:00
2015-01-20 19:53:00 -05:00
mov = new float[4*LORA_SFFT_LEN];
2015-01-25 12:27:20 -05:00
history = new short[1024];
2015-01-27 19:02:49 -05:00
finetune = new short[16];
2015-01-10 19:12:58 -05:00
}
LoRaDemod::~LoRaDemod()
{
2015-01-11 14:30:48 -05:00
if (loraFilter)
delete loraFilter;
2015-01-13 19:48:48 -05:00
if (negaFilter)
delete negaFilter;
2015-01-20 19:53:00 -05:00
if (mov)
delete [] mov;
2015-01-22 17:30:55 -05:00
if (history)
delete [] history;
2015-01-27 19:02:49 -05:00
if (finetune)
delete [] finetune;
2015-01-10 19:12:58 -05:00
}
void LoRaDemod::configure(MessageQueue* messageQueue, Real Bandwidth)
{
Message* cmd = MsgConfigureLoRaDemod::create(Bandwidth);
2015-08-17 02:29:34 -04:00
messageQueue->push(cmd);
2015-01-10 19:12:58 -05:00
}
2015-01-25 12:27:20 -05:00
void LoRaDemod::dumpRaw()
{
2015-01-27 19:02:49 -05:00
short bin, j, max;
2015-01-25 12:27:20 -05:00
char text[256];
2015-02-09 18:54:51 -05:00
max = m_time / 4 - 3;
2015-08-17 02:29:34 -04:00
2015-02-13 13:16:22 -05:00
if (max > 140)
2015-08-17 02:29:34 -04:00
{
2015-02-13 13:16:22 -05:00
max = 140; // about 2 symbols to each char
2015-08-17 02:29:34 -04:00
}
2015-02-09 18:54:51 -05:00
2015-08-17 02:29:34 -04:00
for ( j=0; j < max; j++)
{
2015-02-13 13:16:22 -05:00
bin = (history[(j + 1) * 4] + m_tune ) & (LORA_SFFT_LEN - 1);
2015-01-31 18:41:06 -05:00
text[j] = toGray(bin >> 1);
2015-01-25 12:27:20 -05:00
}
2015-08-17 02:29:34 -04:00
2015-02-12 04:37:08 -05:00
prng6(text, max);
2015-02-13 13:16:22 -05:00
// First block is always 8 symbols
interleave6(text, 6);
interleave6(&text[8], max);
hamming6(text, 6);
hamming6(&text[8], max);
2015-02-09 18:54:51 -05:00
2015-08-17 02:29:34 -04:00
for ( j=0; j < max / 2; j++)
{
2015-02-13 13:16:22 -05:00
text[j] = (text[j * 2 + 1] << 4) | (0xf & text[j * 2 + 0]);
2015-08-17 02:29:34 -04:00
2015-02-09 18:54:51 -05:00
if ((text[j] < 32 )||( text[j] > 126))
2015-08-17 02:29:34 -04:00
{
2015-02-09 18:54:51 -05:00
text[j] = 0x5f;
2015-08-17 02:29:34 -04:00
}
2015-02-07 05:03:51 -05:00
}
2015-08-17 02:29:34 -04:00
2015-02-13 13:16:22 -05:00
text[3] = text[2];
text[2] = text[1];
text[1] = text[0];
2015-01-27 19:02:49 -05:00
text[j] = 0;
2015-08-17 02:29:34 -04:00
2015-02-13 13:16:22 -05:00
printf("%s\n", &text[1]);
2015-01-25 12:27:20 -05:00
}
2015-01-22 17:30:55 -05:00
short LoRaDemod::synch(short bin)
{
2015-01-27 19:02:49 -05:00
short i, j;
2015-08-17 02:29:34 -04:00
if (bin < 0)
{
2015-01-25 12:27:20 -05:00
if (m_time > 70)
2015-08-17 02:29:34 -04:00
{
2015-01-25 12:27:20 -05:00
dumpRaw();
2015-08-17 02:29:34 -04:00
}
2015-01-22 17:30:55 -05:00
m_time = 0;
2015-01-25 12:27:20 -05:00
return -1;
2015-01-22 17:30:55 -05:00
}
2015-08-17 02:29:34 -04:00
2015-01-27 19:02:49 -05:00
history[m_time] = bin;
2015-08-17 02:29:34 -04:00
2015-01-22 17:30:55 -05:00
if (m_time > 12)
2015-08-17 02:29:34 -04:00
{
2015-01-27 19:02:49 -05:00
if (bin == history[m_time - 6])
2015-08-17 02:29:34 -04:00
{
if (bin == history[m_time - 12])
{
2015-01-25 12:27:20 -05:00
m_tune = LORA_SFFT_LEN - bin;
2015-01-27 19:02:49 -05:00
j = 0;
2015-08-17 02:29:34 -04:00
2015-01-27 19:02:49 -05:00
for (i=0; i<12; i++)
2015-08-17 02:29:34 -04:00
{
2015-01-27 19:02:49 -05:00
j += finetune[15 & (m_time - i)];
2015-08-17 02:29:34 -04:00
}
2015-01-27 19:02:49 -05:00
if (j < 0)
2015-08-17 02:29:34 -04:00
{
2015-01-27 19:02:49 -05:00
m_tune += 1;
2015-08-17 02:29:34 -04:00
}
2015-01-27 19:02:49 -05:00
m_tune &= (LORA_SFFT_LEN - 1);
m_time = 0;
2015-01-25 12:27:20 -05:00
return -1;
2015-01-22 17:30:55 -05:00
}
2015-08-17 02:29:34 -04:00
}
}
2015-01-22 17:30:55 -05:00
m_time++;
2015-01-25 12:27:20 -05:00
m_time &= 1023;
2015-08-17 02:29:34 -04:00
2015-01-25 12:27:20 -05:00
if (m_time & 3)
2015-08-17 02:29:34 -04:00
{
2015-01-25 12:27:20 -05:00
return -1;
2015-08-17 02:29:34 -04:00
}
2015-01-25 12:27:20 -05:00
return (bin + m_tune) & (LORA_SFFT_LEN - 1);
2015-01-22 17:30:55 -05:00
}
2015-01-13 19:48:48 -05:00
2015-01-20 19:53:00 -05:00
int LoRaDemod::detect(Complex c, Complex a)
2015-01-12 14:59:45 -05:00
{
2015-01-27 19:02:49 -05:00
int p, q;
2015-01-22 17:30:55 -05:00
short i, result, negresult, movpoint;
2015-01-20 19:53:00 -05:00
float peak, negpeak, tfloat;
2015-01-13 14:14:36 -05:00
float mag[LORA_SFFT_LEN];
2015-01-13 19:48:48 -05:00
float rev[LORA_SFFT_LEN];
2015-01-13 14:14:36 -05:00
2015-01-13 19:48:48 -05:00
loraFilter->run(c * a);
negaFilter->run(c * conj(a));
2015-01-20 19:53:00 -05:00
// process spectrum twice in FFTLEN
if (++m_count & ((1 << DATA_BITS) - 1))
2015-08-17 02:29:34 -04:00
{
2015-01-13 14:14:36 -05:00
return m_result;
2015-08-17 02:29:34 -04:00
}
2015-01-20 19:53:00 -05:00
movpoint = 3 & (m_count >> DATA_BITS);
2015-01-13 14:14:36 -05:00
loraFilter->fetch(mag);
2015-01-13 19:48:48 -05:00
negaFilter->fetch(rev);
2015-01-17 10:59:44 -05:00
peak = negpeak = 0.0f;
result = negresult = 0;
2015-08-17 02:29:34 -04:00
for (i = 0; i < LORA_SFFT_LEN; i++)
{
if (rev[i] > negpeak)
{
2015-01-17 10:59:44 -05:00
negpeak = rev[i];
negresult = i;
2015-01-13 19:48:48 -05:00
}
2015-08-17 02:29:34 -04:00
2015-01-20 19:53:00 -05:00
tfloat = mov[i] + mov[LORA_SFFT_LEN + i] +mov[2 * LORA_SFFT_LEN + i]
+ mov[3 * LORA_SFFT_LEN + i] + mag[i];
2015-08-17 02:29:34 -04:00
if (tfloat > peak)
{
2015-01-20 19:53:00 -05:00
peak = tfloat;
2015-01-17 10:59:44 -05:00
result = i;
2015-01-12 14:59:45 -05:00
}
2015-08-17 02:29:34 -04:00
2015-01-20 19:53:00 -05:00
mov[movpoint * LORA_SFFT_LEN + i] = mag[i];
2015-01-12 14:59:45 -05:00
}
2015-01-27 19:02:49 -05:00
p = (result - 1 + LORA_SFFT_LEN) & (LORA_SFFT_LEN -1);
q = (result + 1) & (LORA_SFFT_LEN -1);
finetune[15 & m_time] = (mag[p] > mag[q]) ? -1 : 1;
2015-02-12 04:37:08 -05:00
if (peak < negpeak * LORA_SQUELCH)
2015-08-17 02:29:34 -04:00
{
2015-01-27 19:02:49 -05:00
result = -1;
2015-08-17 02:29:34 -04:00
}
2015-01-27 19:02:49 -05:00
result = synch(result);
2015-08-17 02:29:34 -04:00
2015-01-25 12:27:20 -05:00
if (result >= 0)
2015-08-17 02:29:34 -04:00
{
2015-01-25 12:27:20 -05:00
m_result = result;
2015-08-17 02:29:34 -04:00
}
2015-01-13 14:14:36 -05:00
return m_result;
2015-01-12 14:59:45 -05:00
}
void LoRaDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool pO)
2015-01-10 19:12:58 -05:00
{
2015-01-12 14:59:45 -05:00
int newangle;
2015-01-10 19:12:58 -05:00
Complex ci;
2015-01-12 14:59:45 -05:00
2015-01-10 19:12:58 -05:00
m_sampleBuffer.clear();
2015-08-17 02:29:34 -04:00
m_settingsMutex.lock();
2015-08-17 02:29:34 -04:00
for(SampleVector::const_iterator it = begin; it < end; ++it)
{
Complex c(it->real() / 32768.0f, it->imag() / 32768.0f);
2015-01-10 19:12:58 -05:00
c *= m_nco.nextIQ();
if(m_interpolator.decimate(&m_sampleDistanceRemain, c, &ci))
2015-08-17 02:29:34 -04:00
{
2015-01-11 07:27:11 -05:00
m_chirp = (m_chirp + 1) & (SPREADFACTOR - 1);
m_angle = (m_angle + m_chirp) & (SPREADFACTOR - 1);
Complex cangle(cos(M_PI*2*m_angle/SPREADFACTOR),-sin(M_PI*2*m_angle/SPREADFACTOR));
2015-01-13 19:48:48 -05:00
newangle = detect(ci, cangle);
2015-01-12 14:59:45 -05:00
2015-01-15 05:57:05 -05:00
m_bin = (m_bin + newangle) & (LORA_SFFT_LEN - 1);
Complex nangle(cos(M_PI*2*m_bin/LORA_SFFT_LEN),sin(M_PI*2*m_bin/LORA_SFFT_LEN));
2015-01-13 19:48:48 -05:00
m_sampleBuffer.push_back(Sample(nangle.real() * 100, nangle.imag() * 100));
2015-01-10 19:12:58 -05:00
m_sampleDistanceRemain += (Real)m_sampleRate / m_Bandwidth;
}
}
2015-08-17 02:29:34 -04:00
if(m_sampleSink != 0)
{
2015-01-15 05:57:05 -05:00
m_sampleSink->feed(m_sampleBuffer.begin(), m_sampleBuffer.end(), false);
2015-08-17 02:29:34 -04:00
}
m_settingsMutex.unlock();
2015-01-10 19:12:58 -05:00
}
void LoRaDemod::start()
{
}
void LoRaDemod::stop()
{
}
2015-08-17 02:29:34 -04:00
bool LoRaDemod::handleMessage(const Message& cmd)
2015-01-10 19:12:58 -05:00
{
2015-08-17 02:29:34 -04:00
qDebug() << "LoRaDemod::handleMessage";
if (DownChannelizer::MsgChannelizerNotification::match(cmd))
2015-08-17 02:29:34 -04:00
{
DownChannelizer::MsgChannelizerNotification& notif = (DownChannelizer::MsgChannelizerNotification&) cmd;
2015-08-17 02:29:34 -04:00
m_settingsMutex.lock();
2015-08-17 02:29:34 -04:00
m_sampleRate = notif.getSampleRate();
m_nco.setFreq(-notif.getFrequencyOffset(), m_sampleRate);
2015-01-11 07:27:11 -05:00
m_interpolator.create(16, m_sampleRate, m_Bandwidth/1.9);
2015-01-10 19:12:58 -05:00
m_sampleDistanceRemain = m_sampleRate / m_Bandwidth;
2015-08-17 02:29:34 -04:00
m_settingsMutex.unlock();
qDebug() << "LoRaDemod::handleMessage: MsgChannelizerNotification: m_sampleRate: " << m_sampleRate
<< " frequencyOffset: " << notif.getFrequencyOffset();
2015-08-17 02:29:34 -04:00
2015-01-10 19:12:58 -05:00
return true;
2015-08-17 02:29:34 -04:00
}
else if (MsgConfigureLoRaDemod::match(cmd))
{
MsgConfigureLoRaDemod& cfg = (MsgConfigureLoRaDemod&) cmd;
m_settingsMutex.lock();
2015-08-17 02:29:34 -04:00
m_Bandwidth = cfg.getBandwidth();
2015-01-11 07:27:11 -05:00
m_interpolator.create(16, m_sampleRate, m_Bandwidth/1.9);
2015-08-17 02:29:34 -04:00
m_settingsMutex.unlock();
2015-08-17 02:29:34 -04:00
qDebug() << " MsgConfigureLoRaDemod: m_Bandwidth: " << m_Bandwidth;
2015-01-10 19:12:58 -05:00
return true;
2015-08-17 02:29:34 -04:00
}
else
{
if(m_sampleSink != 0)
{
2015-01-10 19:12:58 -05:00
return m_sampleSink->handleMessage(cmd);
2015-08-17 02:29:34 -04:00
}
2015-01-10 19:12:58 -05:00
else
2015-08-17 02:29:34 -04:00
{
2015-01-10 19:12:58 -05:00
return false;
2015-08-17 02:29:34 -04:00
}
2015-01-10 19:12:58 -05:00
}
}