1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2026-04-28 02:14:03 -04:00

LoRa demod rework (1)

This commit is contained in:
f4exb 2020-01-30 18:26:43 +01:00
parent 5a62eab198
commit b30b0aa6bd
7 changed files with 149 additions and 102 deletions

View File

@ -138,7 +138,7 @@ void LoRaDemod::applySettings(const LoRaDemodSettings& settings, bool force)
qDebug() << "LoRaDemod::applySettings:"
<< " m_centerFrequency: " << settings.m_centerFrequency
<< " m_bandwidthIndex: " << settings.m_bandwidthIndex
<< " m_spread: " << settings.m_spread
<< " m_spreadFactor: " << settings.m_spreadFactor
<< " m_rgbColor: " << settings.m_rgbColor
<< " m_title: " << settings.m_title
<< " force: " << force;

View File

@ -98,7 +98,13 @@ void LoRaDemodGUI::on_BW_valueChanged(int value)
void LoRaDemodGUI::on_Spread_valueChanged(int value)
{
(void) value;
m_settings.m_spreadFactor = value;
ui->SpreadText->setText(tr("%1").arg(value));
int spectrumRate = 1 << m_settings.m_spreadFactor;
ui->glSpectrum->setSampleRate(spectrumRate);
ui->glSpectrum->setCenterFrequency(spectrumRate/2);
applySettings();
}
void LoRaDemodGUI::onWidgetRolled(QWidget* widget, bool rollDown)
@ -123,8 +129,9 @@ LoRaDemodGUI::LoRaDemodGUI(PluginAPI* pluginAPI, DeviceUISet *deviceUISet, Baseb
m_LoRaDemod = (LoRaDemod*) rxChannel; //new LoRaDemod(m_deviceUISet->m_deviceSourceAPI);
m_LoRaDemod->setSpectrumSink(m_spectrumVis);
ui->glSpectrum->setCenterFrequency(16000);
ui->glSpectrum->setSampleRate(32000);
int spectrumRate = 1 << m_settings.m_spreadFactor;
ui->glSpectrum->setSampleRate(spectrumRate);
ui->glSpectrum->setCenterFrequency(spectrumRate/2);
ui->glSpectrum->setDisplayWaterfall(true);
ui->glSpectrum->setDisplayMaxHold(true);
@ -183,5 +190,6 @@ void LoRaDemodGUI::displaySettings()
blockApplySettings(true);
ui->BWText->setText(QString("%1 Hz").arg(thisBW));
ui->BW->setValue(m_settings.m_bandwidthIndex);
ui->SpreadText->setText(tr("%1").arg(m_settings.m_spreadFactor));
blockApplySettings(false);
}

View File

@ -6,10 +6,16 @@
<rect>
<x>0</x>
<y>0</y>
<width>302</width>
<height>410</height>
<width>350</width>
<height>550</height>
</rect>
</property>
<property name="minimumSize">
<size>
<width>350</width>
<height>550</height>
</size>
</property>
<property name="font">
<font>
<family>Liberation Sans</family>
@ -22,9 +28,9 @@
<widget class="QWidget" name="settingsContainer" native="true">
<property name="geometry">
<rect>
<x>35</x>
<x>10</x>
<y>35</y>
<width>242</width>
<width>331</width>
<height>96</height>
</rect>
</property>
@ -50,24 +56,27 @@
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Bandwidth</string>
<string>BW</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="mabel">
<property name="text">
<string>Spreading</string>
<string>SF</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSlider" name="BW">
<property name="toolTip">
<string>Bandwidth</string>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>4</number>
<number>6</number>
</property>
<property name="pageStep">
<number>1</number>
@ -82,23 +91,45 @@
</item>
<item row="1" column="1">
<widget class="QSlider" name="Spread">
<property name="toolTip">
<string>Spreading factor</string>
</property>
<property name="minimum">
<number>0</number>
<number>5</number>
</property>
<property name="maximum">
<number>2</number>
<number>12</number>
</property>
<property name="pageStep">
<number>1</number>
</property>
<property name="value">
<number>0</number>
<number>7</number>
</property>
<property name="sliderPosition">
<number>7</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="SpreadText">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>5</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="BWText">
<property name="minimumSize">
@ -115,31 +146,15 @@
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="SpreadText">
<property name="minimumSize">
<size>
<width>50</width>
<height>0</height>
</size>
</property>
<property name="text">
<string>6:4 2^8</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="spectrumContainer" native="true">
<property name="geometry">
<rect>
<x>40</x>
<x>10</x>
<y>140</y>
<width>218</width>
<height>184</height>
<width>331</width>
<height>391</height>
</rect>
</property>
<property name="windowTitle">
@ -165,16 +180,10 @@
<widget class="GLSpectrum" name="glSpectrum" native="true">
<property name="minimumSize">
<size>
<width>200</width>
<height>150</height>
<width>0</width>
<height>350</height>
</size>
</property>
<property name="font">
<font>
<family>Sans Serif</family>
<pointsize>9</pointsize>
</font>
</property>
</widget>
</item>
<item>

View File

@ -7,8 +7,8 @@
#include "settings/serializable.h"
#include "lorademodsettings.h"
const int LoRaDemodSettings::bandwidths[] = {7813,15625,20833,31250,62500};
const int LoRaDemodSettings::nb_bandwidths = 5;
const int LoRaDemodSettings::bandwidths[] = {7813, 15625, 31250, 62500, 125000, 250000, 50000};
const int LoRaDemodSettings::nb_bandwidths = 7;
LoRaDemodSettings::LoRaDemodSettings() :
m_centerFrequency(0),
@ -21,7 +21,7 @@ LoRaDemodSettings::LoRaDemodSettings() :
void LoRaDemodSettings::resetToDefaults()
{
m_bandwidthIndex = 0;
m_spread = 0;
m_spreadFactor = 0;
m_rgbColor = QColor(255, 0, 255).rgb();
m_title = "LoRa Demodulator";
}
@ -31,7 +31,7 @@ QByteArray LoRaDemodSettings::serialize() const
SimpleSerializer s(1);
s.writeS32(1, m_centerFrequency);
s.writeS32(2, m_bandwidthIndex);
s.writeS32(3, m_spread);
s.writeS32(3, m_spreadFactor);
if (m_spectrumGUI) {
s.writeBlob(4, m_spectrumGUI->serialize());
@ -62,7 +62,7 @@ bool LoRaDemodSettings::deserialize(const QByteArray& data)
d.readS32(1, &m_centerFrequency, 0);
d.readS32(2, &m_bandwidthIndex, 0);
d.readS32(3, &m_spread, 0);
d.readS32(3, &m_spreadFactor, 0);
if (m_spectrumGUI) {
d.readBlob(4, &bytetmp);

View File

@ -13,7 +13,7 @@ struct LoRaDemodSettings
{
int m_centerFrequency;
int m_bandwidthIndex;
int m_spread;
int m_spreadFactor;
uint32_t m_rgbColor;
QString m_title;

View File

@ -26,12 +26,10 @@
const int LoRaDemodSink::DATA_BITS = 6;
const int LoRaDemodSink::SAMPLEBITS = LoRaDemodSink::DATA_BITS + 2;
const int LoRaDemodSink::SPREADFACTOR = (1 << LoRaDemodSink::SAMPLEBITS);
const int LoRaDemodSink::LORA_SFFT_LEN = (LoRaDemodSink::SPREADFACTOR / 2);
const int LoRaDemodSink::LORA_SQUELCH = 3;
LoRaDemodSink::LoRaDemodSink() :
m_spectrumSink(nullptr)
m_spectrumSink(nullptr)
{
m_Bandwidth = LoRaDemodSettings::bandwidths[0];
m_channelSampleRate = 96000;
@ -49,20 +47,25 @@ LoRaDemodSink::LoRaDemodSink() :
m_time = 0;
m_tune = 0;
loraFilter = new sfft(LORA_SFFT_LEN);
negaFilter = new sfft(LORA_SFFT_LEN);
mov = new float[4*LORA_SFFT_LEN];
history = new short[1024];
finetune = new short[16];
m_nbSymbols = 1 << m_settings.m_spreadFactor;
m_sfftLength = m_nbSymbols / 2;
m_loraFilter = new sfft(m_sfftLength);
m_negaFilter = new sfft(m_sfftLength);
m_mov = new float[4*m_sfftLength];
m_mag = new float[m_sfftLength];
m_rev = new float[m_sfftLength];
m_history = new short[1024];
m_finetune = new short[16];
}
LoRaDemodSink::~LoRaDemodSink()
{
delete loraFilter;
delete negaFilter;
delete [] mov;
delete [] history;
delete [] finetune;
delete m_loraFilter;
delete m_negaFilter;
delete [] m_mov;
delete [] m_history;
delete [] m_finetune;
}
void LoRaDemodSink::dumpRaw()
@ -78,7 +81,7 @@ void LoRaDemodSink::dumpRaw()
for ( j=0; j < max; j++)
{
bin = (history[(j + 1) * 4] + m_tune ) & (LORA_SFFT_LEN - 1);
bin = (m_history[(j + 1) * 4] + m_tune ) % m_sfftLength;
text[j] = toGray(bin >> 1);
}
@ -120,26 +123,26 @@ short LoRaDemodSink::synch(short bin)
return -1;
}
history[m_time] = bin;
m_history[m_time] = bin;
if (m_time > 12)
{
if (bin == history[m_time - 6])
if (bin == m_history[m_time - 6])
{
if (bin == history[m_time - 12])
if (bin == m_history[m_time - 12])
{
m_tune = LORA_SFFT_LEN - bin;
m_tune = m_sfftLength - bin;
j = 0;
for (i=0; i<12; i++) {
j += finetune[15 & (m_time - i)];
j += m_finetune[15 & (m_time - i)];
}
if (j < 0) {
m_tune += 1;
}
m_tune &= (LORA_SFFT_LEN - 1);
m_tune %= m_sfftLength;
m_time = 0;
return -1;
}
@ -153,7 +156,7 @@ short LoRaDemodSink::synch(short bin)
return -1;
}
return (bin + m_tune) & (LORA_SFFT_LEN - 1);
return (bin + m_tune) % m_sfftLength;
}
int LoRaDemodSink::detect(Complex c, Complex a)
@ -161,11 +164,9 @@ int LoRaDemodSink::detect(Complex c, Complex a)
int p, q;
short i, result, negresult, movpoint;
float peak, negpeak, tfloat;
float mag[LORA_SFFT_LEN];
float rev[LORA_SFFT_LEN];
loraFilter->run(c * a);
negaFilter->run(c * conj(a));
m_loraFilter->run(c * a);
m_negaFilter->run(c * conj(a));
// process spectrum twice in FFTLEN
if (++m_count & ((1 << DATA_BITS) - 1)) {
@ -174,21 +175,21 @@ int LoRaDemodSink::detect(Complex c, Complex a)
movpoint = 3 & (m_count >> DATA_BITS);
loraFilter->fetch(mag);
negaFilter->fetch(rev);
m_loraFilter->fetch(m_mag);
m_negaFilter->fetch(m_rev);
peak = negpeak = 0.0f;
result = negresult = 0;
for (i = 0; i < LORA_SFFT_LEN; i++)
for (i = 0; i < m_sfftLength; i++)
{
if (rev[i] > negpeak)
if (m_rev[i] > negpeak)
{
negpeak = rev[i];
negpeak = m_rev[i];
negresult = i;
}
tfloat = mov[i] + mov[LORA_SFFT_LEN + i] +mov[2 * LORA_SFFT_LEN + i]
+ mov[3 * LORA_SFFT_LEN + i] + mag[i];
tfloat = m_mov[i] + m_mov[m_sfftLength + i] +m_mov[2 * m_sfftLength + i]
+ m_mov[3 * m_sfftLength + i] + m_mag[i];
if (tfloat > peak)
{
@ -196,15 +197,14 @@ int LoRaDemodSink::detect(Complex c, Complex a)
result = i;
}
mov[movpoint * LORA_SFFT_LEN + i] = mag[i];
m_mov[movpoint * m_sfftLength + i] = m_mag[i];
}
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;
p = (result - 1 + m_sfftLength) % m_sfftLength;
q = (result + 1) % m_sfftLength;
m_finetune[15 & m_time] = (m_mag[p] > m_mag[q]) ? -1 : 1;
if (peak < negpeak * LORA_SQUELCH)
{
if (peak < negpeak * LORA_SQUELCH) {
result = -1;
}
@ -231,15 +231,25 @@ void LoRaDemodSink::feed(const SampleVector::const_iterator& begin, const Sample
if (m_interpolator.decimate(&m_sampleDistanceRemain, c, &ci))
{
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));
newangle = detect(ci, cangle);
m_angle = (m_angle + m_chirp) % m_nbSymbols;
Complex upRamp(cos(M_PI*2*m_angle/m_nbSymbols), sin(M_PI*2*m_angle/m_nbSymbols));
Complex dechirpUp = ci * conj(upRamp); // de-chirp the up ramp to get peamble and data
Complex dechirpDown = ci * upRamp; // de-chirp the down ramp to get sync
m_sampleBuffer.push_back(Sample(dechirpUp.real() * SDR_RX_SCALEF, dechirpUp.imag() * SDR_RX_SCALEF));
// Bullshit...
// Complex cangle(cos(M_PI*2*m_angle/m_nbSymbols),-sin(M_PI*2*m_angle/m_nbSymbols));
// newangle = detect(ci, cangle);
// m_bin = (m_bin + newangle) % m_sfftLength;
// Complex nangle(cos(M_PI*2*m_bin/m_sfftLength),sin(M_PI*2*m_bin/m_sfftLength));
// m_sampleBuffer.push_back(Sample(nangle.real() * 16384, nangle.imag() * 16384));
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));
m_sampleBuffer.push_back(Sample(nangle.real() * 100, nangle.imag() * 100));
m_sampleDistanceRemain += (Real) m_channelSampleRate / m_Bandwidth;
m_chirp++;
if (m_chirp >= m_nbSymbols) {
m_chirp = 0;
}
}
}
@ -252,9 +262,10 @@ void LoRaDemodSink::applyChannelSettings(int channelSampleRate, int bandwidth, i
{
qDebug() << "LoRaDemodSink::applyChannelSettings:"
<< " channelSampleRate: " << channelSampleRate
<< " channelFrequencyOffset: " << channelFrequencyOffset;
<< " channelFrequencyOffset: " << channelFrequencyOffset
<< " bandwidth: " << bandwidth;
if((channelFrequencyOffset != m_channelFrequencyOffset) ||
if ((channelFrequencyOffset != m_channelFrequencyOffset) ||
(channelSampleRate != m_channelSampleRate) || force)
{
m_nco.setFreq(-channelFrequencyOffset, channelSampleRate);
@ -277,10 +288,26 @@ void LoRaDemodSink::applySettings(const LoRaDemodSettings& settings, bool force)
qDebug() << "LoRaDemodSink::applySettings:"
<< " m_centerFrequency: " << settings.m_centerFrequency
<< " m_bandwidthIndex: " << settings.m_bandwidthIndex
<< " m_spread: " << settings.m_spread
<< " m_spreadFactor: " << settings.m_spreadFactor
<< " m_rgbColor: " << settings.m_rgbColor
<< " m_title: " << settings.m_title
<< " force: " << force;
if ((settings.m_spreadFactor != m_settings.m_spreadFactor) || force)
{
m_nbSymbols = 1 << settings.m_spreadFactor;
m_sfftLength = m_nbSymbols / 2;
delete m_loraFilter;
delete m_negaFilter;
delete m_mov;
delete m_mag;
delete m_rev;
m_loraFilter = new sfft(m_sfftLength);
m_negaFilter = new sfft(m_sfftLength);
m_mov = new float[4*m_sfftLength];
m_mag = new float[m_sfftLength];
m_rev = new float[m_sfftLength];
}
m_settings = settings;
}

View File

@ -55,11 +55,13 @@ private:
int m_time;
short m_tune;
sfft* loraFilter;
sfft* negaFilter;
float* mov;
short* history;
short* finetune;
sfft* m_loraFilter;
sfft* m_negaFilter;
float* m_mov;
float* m_mag;
float* m_rev;
short* m_history;
short* m_finetune;
NCO m_nco;
Interpolator m_interpolator;
@ -68,10 +70,11 @@ private:
BasebandSampleSink* m_spectrumSink;
SampleVector m_sampleBuffer;
unsigned int m_nbSymbols;
unsigned int m_sfftLength;
static const int DATA_BITS;
static const int SAMPLEBITS;
static const int SPREADFACTOR;
static const int LORA_SFFT_LEN;
static const int LORA_SQUELCH;
int detect(Complex sample, Complex angle);