2019-03-17 16:31:42 -04:00
|
|
|
// This file is part of LeanSDR Copyright (C) 2016-2019 <pabr@pabr.org>.
|
|
|
|
// See the toplevel README for more information.
|
|
|
|
//
|
|
|
|
// 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, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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 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/>.
|
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
#ifndef LEANSDR_SDR_H
|
|
|
|
#define LEANSDR_SDR_H
|
|
|
|
|
|
|
|
#include "leansdr/dsp.h"
|
2019-03-17 16:31:42 -04:00
|
|
|
#include "leansdr/math.h"
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
namespace leansdr
|
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Abbreviations for floating-point types
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
typedef float f32;
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
typedef complex<u8> cu8;
|
|
|
|
typedef complex<s8> cs8;
|
|
|
|
typedef complex<u16> cu16;
|
|
|
|
typedef complex<s16> cs16;
|
|
|
|
typedef complex<f32> cf32;
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// SDR blocks
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// AUTO-NOTCH FILTER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
// Periodically detects the [nslots] strongest peaks with a FFT,
|
2018-02-24 18:07:08 -05:00
|
|
|
// removes them with a first-order filter.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct auto_notch : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
int decimation;
|
|
|
|
float k;
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
auto_notch(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<complex<T>> &_out,
|
|
|
|
int _nslots,
|
|
|
|
T _agc_rms_setpoint) : runnable(sch, "auto_notch"),
|
|
|
|
decimation(1024 * 4096),
|
|
|
|
k(0.002), // k(0.01)
|
|
|
|
fft(4096),
|
|
|
|
in(_in),
|
|
|
|
out(_out, fft.n),
|
|
|
|
nslots(_nslots),
|
|
|
|
phase(0),
|
|
|
|
gain(1),
|
|
|
|
agc_rms_setpoint(_agc_rms_setpoint)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
__slots = new slot[nslots];
|
|
|
|
|
|
|
|
for (int s = 0; s < nslots; ++s)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
__slots[s].i = -1;
|
2019-03-17 16:31:42 -04:00
|
|
|
__slots[s].expj = new complex<float>[fft.n];
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~auto_notch()
|
|
|
|
{
|
|
|
|
delete[] __slots;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
while (in.readable() >= fft.n && out.writable() >= fft.n)
|
|
|
|
{
|
|
|
|
phase += fft.n;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (phase >= decimation)
|
|
|
|
{
|
|
|
|
phase -= decimation;
|
|
|
|
detect();
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
process();
|
|
|
|
in.read(fft.n);
|
|
|
|
out.written(fft.n);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void detect()
|
|
|
|
{
|
|
|
|
complex<T> *pin = in.rd();
|
2019-03-17 16:31:42 -04:00
|
|
|
complex<float> *data = new complex<float>[fft.n];
|
2018-02-24 18:07:08 -05:00
|
|
|
float m0 = 0, m2 = 0;
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
for (int i = 0; i < fft.n; ++i)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
data[i].re = pin[i].re;
|
|
|
|
data[i].im = pin[i].im;
|
2019-03-17 16:31:42 -04:00
|
|
|
m2 += (float)pin[i].re * pin[i].re + (float)pin[i].im * pin[i].im;
|
2018-02-24 18:07:08 -05:00
|
|
|
if (gen_abs(pin[i].re) > m0)
|
|
|
|
m0 = gen_abs(pin[i].re);
|
|
|
|
if (gen_abs(pin[i].im) > m0)
|
|
|
|
m0 = gen_abs(pin[i].im);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (agc_rms_setpoint && m2)
|
|
|
|
{
|
|
|
|
float rms = gen_sqrt(m2 / fft.n);
|
|
|
|
if (sch->debug)
|
|
|
|
fprintf(stderr, "(pow %f max %f)", rms, m0);
|
|
|
|
float new_gain = agc_rms_setpoint / rms;
|
|
|
|
gain = gain * 0.9 + new_gain * 0.1;
|
|
|
|
}
|
|
|
|
|
|
|
|
fft.inplace(data, true);
|
2019-03-17 16:31:42 -04:00
|
|
|
float *amp = new float[fft.n];
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
for (int i = 0; i < fft.n; ++i)
|
2018-02-24 18:07:08 -05:00
|
|
|
amp[i] = hypotf(data[i].re, data[i].im);
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
for (slot *s = __slots; s < __slots + nslots; ++s)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
int iamax = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < fft.n; ++i)
|
|
|
|
if (amp[i] > amp[iamax])
|
2018-02-24 18:07:08 -05:00
|
|
|
iamax = i;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (iamax != s->i)
|
|
|
|
{
|
|
|
|
if (sch->debug)
|
2019-03-17 16:31:42 -04:00
|
|
|
fprintf(stderr, "%s: slot %d new peak %d -> %d\n", name, (int)(s - __slots), s->i, iamax);
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
s->i = iamax;
|
|
|
|
s->estim.re = 0;
|
|
|
|
s->estim.im = 0;
|
|
|
|
s->estt = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < fft.n; ++i)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
float a = 2 * M_PI * s->i * i / fft.n;
|
|
|
|
s->expj[i].re = cosf(a);
|
|
|
|
s->expj[i].im = sinf(a);
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
amp[iamax] = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
if (iamax - 1 >= 0)
|
2018-02-24 18:07:08 -05:00
|
|
|
amp[iamax - 1] = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
if (iamax + 1 < fft.n)
|
2018-02-24 18:07:08 -05:00
|
|
|
amp[iamax + 1] = 0;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
delete[] amp;
|
|
|
|
delete[] data;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void process()
|
|
|
|
{
|
|
|
|
complex<T> *pin = in.rd(), *pend = pin + fft.n, *pout = out.wr();
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
for (slot *s = __slots; s < __slots + nslots; ++s)
|
2018-02-24 18:07:08 -05:00
|
|
|
s->ej = s->expj;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; pin < pend; ++pin, ++pout)
|
|
|
|
{
|
|
|
|
complex<float> out = *pin;
|
2019-03-17 16:31:42 -04:00
|
|
|
// TODO Optimize for nslots==1 ?
|
|
|
|
|
|
|
|
for (slot *s = __slots; s < __slots + nslots; ++s->ej, ++s)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
complex<float> bb(pin->re * s->ej->re + pin->im * s->ej->im,
|
2019-03-17 16:31:42 -04:00
|
|
|
-pin->re * s->ej->im + pin->im * s->ej->re);
|
2018-02-24 18:07:08 -05:00
|
|
|
s->estim.re = bb.re * k + s->estim.re * (1 - k);
|
|
|
|
s->estim.im = bb.im * k + s->estim.im * (1 - k);
|
|
|
|
complex<float> sub(
|
2019-03-17 16:31:42 -04:00
|
|
|
s->estim.re * s->ej->re - s->estim.im * s->ej->im,
|
|
|
|
s->estim.re * s->ej->im + s->estim.im * s->ej->re);
|
2018-02-24 18:07:08 -05:00
|
|
|
out.re -= sub.re;
|
|
|
|
out.im -= sub.im;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
pout->re = gain * out.re;
|
|
|
|
pout->im = gain * out.im;
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
cfft_engine<float> fft;
|
2019-03-17 16:31:42 -04:00
|
|
|
pipereader<complex<T>> in;
|
|
|
|
pipewriter<complex<T>> out;
|
|
|
|
int nslots;
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
struct slot
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
complex<float> estim;
|
|
|
|
complex<float> *expj, *ej;
|
|
|
|
int estt;
|
2019-03-17 16:31:42 -04:00
|
|
|
};
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
slot *__slots;
|
2018-02-22 16:52:49 -05:00
|
|
|
int phase;
|
|
|
|
float gain;
|
|
|
|
T agc_rms_setpoint;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// SIGNAL STRENGTH ESTIMATOR
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Outputs RMS values.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct ss_estimator : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
unsigned long window_size; // Samples per estimation
|
2018-02-22 16:52:49 -05:00
|
|
|
unsigned long decimation; // Output rate
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
ss_estimator(scheduler *sch, pipebuf<complex<T>> &_in, pipebuf<T> &_out) : runnable(sch, "SS estimator"),
|
|
|
|
window_size(1024),
|
|
|
|
decimation(1024),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
phase(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
while (in.readable() >= window_size && out.writable() >= 1)
|
|
|
|
{
|
|
|
|
phase += window_size;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (phase >= decimation)
|
|
|
|
{
|
|
|
|
phase -= decimation;
|
|
|
|
complex<T> *p = in.rd(), *pend = p + window_size;
|
|
|
|
float s = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; p < pend; ++p)
|
2019-03-17 16:31:42 -04:00
|
|
|
s += (float)p->re * p->re + (float)p->im * p->im;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
out.write(sqrtf(s / window_size));
|
|
|
|
}
|
|
|
|
in.read(window_size);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
|
|
|
pipereader<complex<T>> in;
|
2018-02-22 16:52:49 -05:00
|
|
|
pipewriter<T> out;
|
|
|
|
unsigned long phase;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct ss_amp_estimator : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
unsigned long window_size; // Samples per estimation
|
2018-02-22 16:52:49 -05:00
|
|
|
unsigned long decimation; // Output rate
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
ss_amp_estimator(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<T> &_out_ss,
|
|
|
|
pipebuf<T> &_out_ampmin,
|
|
|
|
pipebuf<T> &_out_ampmax) : runnable(sch, "SS estimator"),
|
|
|
|
window_size(1024),
|
|
|
|
decimation(1024),
|
|
|
|
in(_in),
|
|
|
|
out_ss(_out_ss),
|
|
|
|
out_ampmin(_out_ampmin),
|
|
|
|
out_ampmax(_out_ampmax),
|
|
|
|
phase(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
while (in.readable() >= window_size && out_ss.writable() >= 1 && out_ampmin.writable() >= 1 && out_ampmax.writable() >= 1)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
phase += window_size;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (phase >= decimation)
|
|
|
|
{
|
|
|
|
phase -= decimation;
|
|
|
|
complex<T> *p = in.rd(), *pend = p + window_size;
|
|
|
|
float s2 = 0;
|
|
|
|
float amin = 1e38, amax = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; p < pend; ++p)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
float mag2 = (float)p->re * p->re + (float)p->im * p->im;
|
2018-02-24 18:07:08 -05:00
|
|
|
s2 += mag2;
|
|
|
|
float mag = sqrtf(mag2);
|
|
|
|
if (mag < amin)
|
|
|
|
amin = mag;
|
|
|
|
if (mag > amax)
|
|
|
|
amax = mag;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
out_ss.write(sqrtf(s2 / window_size));
|
|
|
|
out_ampmin.write(amin);
|
|
|
|
out_ampmax.write(amax);
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
in.read(window_size);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
|
|
|
pipereader<complex<T>> in;
|
2018-02-22 16:52:49 -05:00
|
|
|
pipewriter<T> out_ss, out_ampmin, out_ampmax;
|
|
|
|
unsigned long phase;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// AGC
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct simple_agc : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
float out_rms; // Desired RMS output power
|
|
|
|
float bw; // Bandwidth
|
|
|
|
float estimated; // Input power
|
|
|
|
static const int chunk_size = 128;
|
|
|
|
|
|
|
|
simple_agc(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<complex<T>> &_out) : runnable(sch, "AGC"),
|
|
|
|
out_rms(1),
|
|
|
|
bw(0.001),
|
|
|
|
estimated(0),
|
|
|
|
in(_in),
|
|
|
|
out(_out, chunk_size)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
|
|
|
pipereader<complex<T>> in;
|
|
|
|
pipewriter<complex<T>> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
while (in.readable() >= chunk_size && out.writable() >= chunk_size)
|
|
|
|
{
|
|
|
|
complex<T> *pin = in.rd(), *pend = pin + chunk_size;
|
|
|
|
float amp2 = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; pin < pend; ++pin)
|
|
|
|
amp2 += pin->re * pin->re + pin->im * pin->im;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
amp2 /= chunk_size;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (!estimated)
|
|
|
|
estimated = amp2;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
estimated = estimated * (1 - bw) + amp2 * bw;
|
|
|
|
float gain = estimated ? out_rms / sqrtf(estimated) : 0;
|
|
|
|
pin = in.rd();
|
|
|
|
complex<T> *pout = out.wr();
|
2019-03-17 16:31:42 -04:00
|
|
|
float bwcomp = 1 - bw;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; pin < pend; ++pin, ++pout)
|
|
|
|
{
|
|
|
|
pout->re = pin->re * gain;
|
|
|
|
pout->im = pin->im * gain;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
in.read(chunk_size);
|
|
|
|
out.written(chunk_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// simple_agc
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
typedef uint16_t u_angle; // [0,2PI[ in 65536 steps
|
2018-02-24 18:07:08 -05:00
|
|
|
typedef int16_t s_angle; // [-PI,PI[ in 65536 steps
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// GENERIC CONSTELLATION DECODING BY LOOK-UP TABLE.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Metrics and phase errors are pre-computed on a RxR grid.
|
|
|
|
// R must be a power of 2.
|
|
|
|
// Up to 256 symbols.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
struct softsymbol
|
2019-03-17 16:31:42 -04:00
|
|
|
{ // TBD obsolete
|
|
|
|
int16_t cost; // For Viterbi with TBM=int16_t
|
2018-02-22 16:52:49 -05:00
|
|
|
uint8_t symbol;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Target RMS amplitude for AGC
|
|
|
|
//const float cstln_amp = 73; // Best for 32APSK 9/10
|
|
|
|
//const float cstln_amp = 90; // Best for QPSK
|
|
|
|
//const float cstln_amp = 64; // Best for BPSK
|
|
|
|
//const float cstln_amp = 75; // Best for BPSK at 45°
|
2019-03-17 16:31:42 -04:00
|
|
|
const float cstln_amp = 75; // Trade-off
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
// A struct that temporarily holds all the info we precompute for the LUT.
|
|
|
|
struct full_ss
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
uint8_t nearest; // Index of nearest in constellation
|
|
|
|
uint16_t dists2[256]; // Squared distances
|
|
|
|
float p[8]; // 0..1 probability of bits being 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Options for soft-symbols.
|
|
|
|
// These functions are overloaded to keep cstln_lut<SOFTSYMB> generic:
|
|
|
|
// to_softsymb(const full_ss *fss, SOFTSYMB *ss)
|
|
|
|
// softsymb_harden(SOFTSYMB *ss) {
|
|
|
|
// softsymb_to_dump(const SOFTSYMB &ss, int bit) To grey 0..255
|
|
|
|
// For LUT initialization only. Performance is not critical.
|
|
|
|
|
|
|
|
// Hard decision soft-symbols.
|
|
|
|
// Value is the symbol index, 0..255.
|
|
|
|
typedef uint8_t hard_ss;
|
|
|
|
void to_softsymb(const full_ss *fss, hard_ss *ss);
|
|
|
|
void softsymb_harden(hard_ss *ss);
|
|
|
|
uint8_t softsymb_to_dump(const hard_ss &ss, int bit);
|
|
|
|
|
|
|
|
// Euclidian QPSK soft-symbols.
|
|
|
|
// Additive metric suitable for Viterbi.
|
|
|
|
// Backward-compatible with simplified Viterbi (TBD remove)
|
|
|
|
struct eucl_ss
|
|
|
|
{
|
|
|
|
static const int MAX_SYMBOLS = 4;
|
|
|
|
uint16_t dists2[MAX_SYMBOLS];
|
|
|
|
uint16_t discr2; // 2nd_nearest - nearest
|
|
|
|
uint8_t nearest;
|
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
void to_softsymb(const full_ss *fss, eucl_ss *ss);
|
|
|
|
void softsymb_harden(eucl_ss *ss);
|
|
|
|
uint8_t softsymb_to_dump(const eucl_ss &ss, int bit);
|
|
|
|
|
|
|
|
// Log-Likelihood Ratios soft-symbols
|
|
|
|
typedef int8_t llr_t; // log(p(0)/p(1)), clip -127=1 +127=0
|
|
|
|
|
|
|
|
inline bool llr_harden(llr_t v)
|
|
|
|
{
|
|
|
|
return v & 128;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct llr_ss
|
|
|
|
{
|
|
|
|
llr_t bits[8]; // Up to 8 bit considered independent
|
|
|
|
};
|
|
|
|
|
|
|
|
void to_softsymb(const full_ss *fss, llr_ss *ss);
|
|
|
|
void softsymb_harden(llr_ss *ss);
|
|
|
|
uint8_t softsymb_to_dump(const llr_ss &ss, int bit);
|
|
|
|
|
|
|
|
struct cstln_base
|
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
enum predef
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
BPSK, // DVB-S2 (and DVB-S variant)
|
|
|
|
QPSK, // DVB-S
|
2018-02-24 18:07:08 -05:00
|
|
|
PSK8,
|
|
|
|
APSK16,
|
2019-03-17 16:31:42 -04:00
|
|
|
APSK32, // DVB-S2
|
|
|
|
APSK64E, // DVB-S2X
|
2018-02-24 18:07:08 -05:00
|
|
|
QAM16,
|
|
|
|
QAM64,
|
2019-03-17 16:31:42 -04:00
|
|
|
QAM256, // For experimentation only
|
|
|
|
COUNT
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
static const char *names[];
|
|
|
|
float amp_max; // Max amplitude. 1 for PSK, 0 if not applicable.
|
|
|
|
complex<int8_t> *symbols;
|
|
|
|
int nsymbols;
|
|
|
|
int nrotations;
|
|
|
|
};
|
|
|
|
// cstln_base
|
|
|
|
|
|
|
|
template <typename SOFTSYMB, int R>
|
|
|
|
struct cstln_lut : cstln_base
|
|
|
|
{
|
|
|
|
cstln_lut(cstln_base::predef type,
|
|
|
|
float mer = 10,
|
|
|
|
float gamma1 = 0,
|
|
|
|
float gamma2 = 0,
|
|
|
|
float gamma3 = 0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case BPSK:
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = 1;
|
2018-02-24 18:07:08 -05:00
|
|
|
nrotations = 2;
|
|
|
|
nsymbols = 2;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
|
|
|
#if 0 // BPSK at 0°
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[0] = polar(1, 2, 0);
|
|
|
|
symbols[1] = polar(1, 2, 1);
|
2019-03-17 16:31:42 -04:00
|
|
|
#else // BPSK at 45°
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[0] = polar(1, 8, 1);
|
|
|
|
symbols[1] = polar(1, 8, 5);
|
2018-02-22 16:52:49 -05:00
|
|
|
#endif
|
2019-03-17 16:31:42 -04:00
|
|
|
make_lut_from_symbols(mer);
|
2018-02-24 18:07:08 -05:00
|
|
|
break;
|
|
|
|
case QPSK:
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = 1;
|
2018-02-24 18:07:08 -05:00
|
|
|
// EN 300 421, section 4.5 Baseband shaping and modulation
|
|
|
|
// EN 302 307, section 5.4.1
|
|
|
|
nrotations = 4;
|
|
|
|
nsymbols = 4;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[0] = polar(1, 4, 0.5);
|
|
|
|
symbols[1] = polar(1, 4, 3.5);
|
|
|
|
symbols[2] = polar(1, 4, 1.5);
|
|
|
|
symbols[3] = polar(1, 4, 2.5);
|
2019-03-17 16:31:42 -04:00
|
|
|
make_lut_from_symbols(mer);
|
2018-02-24 18:07:08 -05:00
|
|
|
break;
|
|
|
|
case PSK8:
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = 1;
|
2018-02-24 18:07:08 -05:00
|
|
|
// EN 302 307, section 5.4.2
|
|
|
|
nrotations = 8;
|
|
|
|
nsymbols = 8;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[0] = polar(1, 8, 1);
|
|
|
|
symbols[1] = polar(1, 8, 0);
|
|
|
|
symbols[2] = polar(1, 8, 4);
|
|
|
|
symbols[3] = polar(1, 8, 5);
|
|
|
|
symbols[4] = polar(1, 8, 2);
|
|
|
|
symbols[5] = polar(1, 8, 7);
|
|
|
|
symbols[6] = polar(1, 8, 3);
|
|
|
|
symbols[7] = polar(1, 8, 6);
|
2019-03-17 16:31:42 -04:00
|
|
|
make_lut_from_symbols(mer);
|
2018-02-24 18:07:08 -05:00
|
|
|
break;
|
|
|
|
case APSK16:
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
// Default gamma for non-DVB-S2 applications.
|
|
|
|
if (!gamma1)
|
|
|
|
gamma1 = 2.57;
|
2018-02-24 18:07:08 -05:00
|
|
|
// EN 302 307, section 5.4.3
|
|
|
|
float r1 = sqrtf(4 / (1 + 3 * gamma1 * gamma1));
|
|
|
|
float r2 = gamma1 * r1;
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = r2;
|
2018-02-24 18:07:08 -05:00
|
|
|
nrotations = 4;
|
|
|
|
nsymbols = 16;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[0] = polar(r2, 12, 1.5);
|
|
|
|
symbols[1] = polar(r2, 12, 10.5);
|
|
|
|
symbols[2] = polar(r2, 12, 4.5);
|
|
|
|
symbols[3] = polar(r2, 12, 7.5);
|
|
|
|
symbols[4] = polar(r2, 12, 0.5);
|
|
|
|
symbols[5] = polar(r2, 12, 11.5);
|
|
|
|
symbols[6] = polar(r2, 12, 5.5);
|
|
|
|
symbols[7] = polar(r2, 12, 6.5);
|
|
|
|
symbols[8] = polar(r2, 12, 2.5);
|
|
|
|
symbols[9] = polar(r2, 12, 9.5);
|
|
|
|
symbols[10] = polar(r2, 12, 3.5);
|
|
|
|
symbols[11] = polar(r2, 12, 8.5);
|
|
|
|
symbols[12] = polar(r1, 4, 0.5);
|
|
|
|
symbols[13] = polar(r1, 4, 3.5);
|
|
|
|
symbols[14] = polar(r1, 4, 1.5);
|
|
|
|
symbols[15] = polar(r1, 4, 2.5);
|
2019-03-17 16:31:42 -04:00
|
|
|
make_lut_from_symbols(mer);
|
2018-02-24 18:07:08 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case APSK32:
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
// Default gammas for non-DVB-S2 applications.
|
|
|
|
if (!gamma1)
|
|
|
|
gamma1 = 2.53;
|
|
|
|
if (!gamma2)
|
|
|
|
gamma2 = 4.30;
|
2018-02-24 18:07:08 -05:00
|
|
|
// EN 302 307, section 5.4.3
|
|
|
|
float r1 = sqrtf(
|
2019-03-17 16:31:42 -04:00
|
|
|
8 / (1 + 3 * gamma1 * gamma1 + 4 * gamma2 * gamma2));
|
2018-02-24 18:07:08 -05:00
|
|
|
float r2 = gamma1 * r1;
|
|
|
|
float r3 = gamma2 * r1;
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = r3;
|
2018-02-24 18:07:08 -05:00
|
|
|
nrotations = 4;
|
|
|
|
nsymbols = 32;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[0] = polar(r2, 12, 1.5);
|
|
|
|
symbols[1] = polar(r2, 12, 2.5);
|
|
|
|
symbols[2] = polar(r2, 12, 10.5);
|
|
|
|
symbols[3] = polar(r2, 12, 9.5);
|
|
|
|
symbols[4] = polar(r2, 12, 4.5);
|
|
|
|
symbols[5] = polar(r2, 12, 3.5);
|
|
|
|
symbols[6] = polar(r2, 12, 7.5);
|
|
|
|
symbols[7] = polar(r2, 12, 8.5);
|
|
|
|
symbols[8] = polar(r3, 16, 1);
|
|
|
|
symbols[9] = polar(r3, 16, 3);
|
|
|
|
symbols[10] = polar(r3, 16, 14);
|
|
|
|
symbols[11] = polar(r3, 16, 12);
|
|
|
|
symbols[12] = polar(r3, 16, 6);
|
|
|
|
symbols[13] = polar(r3, 16, 4);
|
|
|
|
symbols[14] = polar(r3, 16, 9);
|
|
|
|
symbols[15] = polar(r3, 16, 11);
|
|
|
|
symbols[16] = polar(r2, 12, 0.5);
|
|
|
|
symbols[17] = polar(r1, 4, 0.5);
|
|
|
|
symbols[18] = polar(r2, 12, 11.5);
|
|
|
|
symbols[19] = polar(r1, 4, 3.5);
|
|
|
|
symbols[20] = polar(r2, 12, 5.5);
|
|
|
|
symbols[21] = polar(r1, 4, 1.5);
|
|
|
|
symbols[22] = polar(r2, 12, 6.5);
|
|
|
|
symbols[23] = polar(r1, 4, 2.5);
|
|
|
|
symbols[24] = polar(r3, 16, 0);
|
|
|
|
symbols[25] = polar(r3, 16, 2);
|
|
|
|
symbols[26] = polar(r3, 16, 15);
|
|
|
|
symbols[27] = polar(r3, 16, 13);
|
|
|
|
symbols[28] = polar(r3, 16, 7);
|
|
|
|
symbols[29] = polar(r3, 16, 5);
|
|
|
|
symbols[30] = polar(r3, 16, 8);
|
|
|
|
symbols[31] = polar(r3, 16, 10);
|
2019-03-17 16:31:42 -04:00
|
|
|
make_lut_from_symbols(mer);
|
2018-02-24 18:07:08 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case APSK64E:
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
// Default gammas for non-DVB-S2 applications.
|
|
|
|
if (!gamma1)
|
|
|
|
gamma1 = 2.4;
|
|
|
|
if (!gamma2)
|
|
|
|
gamma2 = 4.3;
|
|
|
|
if (!gamma3)
|
|
|
|
gamma3 = 7.0;
|
2018-02-24 18:07:08 -05:00
|
|
|
// EN 302 307-2, section 5.4.5, Table 13e
|
|
|
|
float r1 = sqrtf(
|
2019-03-17 16:31:42 -04:00
|
|
|
64 / (4 + 12 * gamma1 * gamma1 + 20 * gamma2 * gamma2 + 28 * gamma3 * gamma3));
|
2018-02-24 18:07:08 -05:00
|
|
|
float r2 = gamma1 * r1;
|
|
|
|
float r3 = gamma2 * r1;
|
|
|
|
float r4 = gamma3 * r1;
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = r4;
|
2018-02-24 18:07:08 -05:00
|
|
|
nrotations = 4;
|
|
|
|
nsymbols = 64;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
2018-02-24 18:07:08 -05:00
|
|
|
polar2(0, r4, 1.0 / 4, 7.0 / 4, 3.0 / 4, 5.0 / 4);
|
|
|
|
polar2(4, r4, 13.0 / 28, 43.0 / 28, 15.0 / 28, 41.0 / 28);
|
|
|
|
polar2(8, r4, 1.0 / 28, 55.0 / 28, 27.0 / 28, 29.0 / 28);
|
|
|
|
polar2(12, r1, 1.0 / 4, 7.0 / 4, 3.0 / 4, 5.0 / 4);
|
|
|
|
polar2(16, r4, 9.0 / 28, 47.0 / 28, 19.0 / 28, 37.0 / 28);
|
|
|
|
polar2(20, r4, 11.0 / 28, 45.0 / 28, 17.0 / 28, 39.0 / 28);
|
|
|
|
polar2(24, r3, 1.0 / 20, 39.0 / 20, 19.0 / 20, 21.0 / 20);
|
|
|
|
polar2(28, r2, 1.0 / 12, 23.0 / 12, 11.0 / 12, 13.0 / 12);
|
|
|
|
polar2(32, r4, 5.0 / 28, 51.0 / 28, 23.0 / 28, 33.0 / 28);
|
|
|
|
polar2(36, r3, 9.0 / 20, 31.0 / 20, 11.0 / 20, 29.0 / 20);
|
|
|
|
polar2(40, r4, 3.0 / 28, 53.0 / 28, 25.0 / 28, 31.0 / 28);
|
|
|
|
polar2(44, r2, 5.0 / 12, 19.0 / 12, 7.0 / 12, 17.0 / 12);
|
|
|
|
polar2(48, r3, 1.0 / 4, 7.0 / 4, 3.0 / 4, 5.0 / 4);
|
|
|
|
polar2(52, r3, 7.0 / 20, 33.0 / 20, 13.0 / 20, 27.0 / 20);
|
|
|
|
polar2(56, r3, 3.0 / 20, 37.0 / 20, 17.0 / 20, 23.0 / 20);
|
|
|
|
polar2(60, r2, 1.0 / 4, 7.0 / 4, 3.0 / 4, 5.0 / 4);
|
2019-03-17 16:31:42 -04:00
|
|
|
make_lut_from_symbols(mer);
|
2018-02-24 18:07:08 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QAM16:
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = 0;
|
2018-02-24 18:07:08 -05:00
|
|
|
make_qam(16);
|
|
|
|
break;
|
|
|
|
case QAM64:
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = 1;
|
2018-02-24 18:07:08 -05:00
|
|
|
make_qam(64);
|
|
|
|
break;
|
|
|
|
case QAM256:
|
2019-03-17 16:31:42 -04:00
|
|
|
amp_max = 1;
|
2018-02-24 18:07:08 -05:00
|
|
|
make_qam(256);
|
|
|
|
break;
|
|
|
|
default:
|
2019-03-17 16:31:42 -04:00
|
|
|
fail("Constellation not implemented");
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
struct result
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
SOFTSYMB ss;
|
2018-02-24 18:07:08 -05:00
|
|
|
s_angle phase_error;
|
2019-03-17 16:31:42 -04:00
|
|
|
uint8_t symbol; // Nearest symbol, useful for C&T recovery
|
2018-02-22 16:52:49 -05:00
|
|
|
};
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
inline result *lookup(float I, float Q)
|
|
|
|
{
|
|
|
|
// Handling of overflows beyond the lookup table:
|
|
|
|
// - For BPSK/QPSK/8PSK we only care about the phase,
|
|
|
|
// so the following is harmless and improves locking at low SNR.
|
|
|
|
// - For amplitude modulations this is not appropriate.
|
|
|
|
// However, if there is enough noise to cause overflow,
|
|
|
|
// demodulation would probably fail anyway.
|
|
|
|
//
|
|
|
|
// Comment-out for better throughput at high SNR.
|
2018-02-22 16:52:49 -05:00
|
|
|
#if 1
|
2018-02-24 18:07:08 -05:00
|
|
|
while (I < -128 || I > 127 || Q < -128 || Q > 127)
|
|
|
|
{
|
|
|
|
I *= 0.5;
|
|
|
|
Q *= 0.5;
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
#endif
|
2019-03-17 16:31:42 -04:00
|
|
|
return &lut[(u8)(s8)I][(u8)(s8)Q];
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
inline result *lookup(int I, int Q)
|
|
|
|
{
|
|
|
|
// Ignore wrapping modulo 256
|
2019-03-17 16:31:42 -04:00
|
|
|
return &lut[(u8)I][(u8)Q];
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<signed char> polar(float r, int n, float i)
|
|
|
|
{
|
|
|
|
float a = i * 2 * M_PI / n;
|
|
|
|
return complex<signed char>(r * cosf(a) * cstln_amp,
|
2019-03-17 16:31:42 -04:00
|
|
|
r * sinf(a) * cstln_amp);
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
// Helper function for some constellation tables
|
2018-02-24 18:07:08 -05:00
|
|
|
void polar2(int i, float r, float a0, float a1, float a2, float a3)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
float a[] = {a0, a1, a2, a3};
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int j = 0; j < 4; ++j)
|
|
|
|
{
|
|
|
|
float phi = a[j] * M_PI;
|
|
|
|
symbols[i + j] = complex<signed char>(r * cosf(phi) * cstln_amp,
|
2019-03-17 16:31:42 -04:00
|
|
|
r * sinf(phi) * cstln_amp);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void make_qam(int n)
|
|
|
|
{
|
|
|
|
nrotations = 4;
|
|
|
|
nsymbols = n;
|
2019-03-17 16:31:42 -04:00
|
|
|
symbols = new complex<signed char>[nsymbols];
|
2018-02-24 18:07:08 -05:00
|
|
|
int m = sqrtl(n);
|
|
|
|
float scale;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
{ // Average power in first quadrant with unit grid
|
|
|
|
int q = m / 2;
|
2019-03-17 16:31:42 -04:00
|
|
|
float avgpower = 2 * (q * 0.25 + (q - 1) * q / 2 + (q - 1) * q * (2 * q - 1) / 6) / q;
|
2018-02-24 18:07:08 -05:00
|
|
|
scale = 1.0 / sqrtf(avgpower);
|
|
|
|
}
|
|
|
|
// Arbitrary mapping
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
int s = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int x = 0; x < m; ++x)
|
2019-03-17 16:31:42 -04:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int y = 0; y < m; ++y)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
float I = x - (float)(m - 1) / 2;
|
|
|
|
float Q = y - (float)(m - 1) / 2;
|
2018-02-24 18:07:08 -05:00
|
|
|
symbols[s].re = I * scale * cstln_amp;
|
|
|
|
symbols[s].im = Q * scale * cstln_amp;
|
|
|
|
++s;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
make_lut_from_symbols(20); // TBD
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
result lut[R][R];
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
void make_lut_from_symbols(float mer)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
// Note: Excessively low values of MER will break 16APSK and 32APSK.
|
|
|
|
float sigma = cstln_amp * exp10f(-mer / 20);
|
|
|
|
|
|
|
|
// Precomputed values.
|
|
|
|
// Shared scope so that we don't have to reset dists2[nsymbols..] to -1.
|
|
|
|
struct full_ss fss;
|
|
|
|
|
|
|
|
for (int s = 0; s < 256; ++s)
|
|
|
|
fss.dists2[s] = -1;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int I = -R / 2; I < R / 2; ++I)
|
2019-03-17 16:31:42 -04:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int Q = -R / 2; Q < R / 2; ++Q)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
// Nearest symbol
|
|
|
|
fss.nearest = 0;
|
|
|
|
fss.dists2[0] = 65535;
|
|
|
|
// Conditional probabilities:
|
|
|
|
// Sum likelyhoods from all candidate symbols.
|
|
|
|
//
|
|
|
|
// P(TX[b]==B | RX==IQ) =
|
|
|
|
// sum(S=0..nsymbols-1, P(TX[b]==B | RX==IQ && TXs==S))
|
|
|
|
//
|
|
|
|
// P(TX[b] == B | RX==IQ && TX==S) =
|
|
|
|
// P(TX[b]==B && RX==IQ && TX==S) / P(RX==IQ && TX==S)
|
|
|
|
float probs[8][2];
|
|
|
|
memset(probs, 0, sizeof(probs));
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int s = 0; s < nsymbols; ++s)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
float d2 = ((I - symbols[s].re) * (I - symbols[s].re) + (Q - symbols[s].im) * (Q - symbols[s].im));
|
|
|
|
|
|
|
|
if (d2 < fss.dists2[fss.nearest])
|
|
|
|
fss.nearest = s;
|
|
|
|
|
|
|
|
fss.dists2[s] = d2;
|
|
|
|
float p = expf(-d2 / (2 * sigma * sigma)) / (sqrtf(2 * M_PI) * sigma);
|
|
|
|
|
|
|
|
for (int bit = 0; bit < 8; ++bit)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
probs[bit][(s >> bit) & 1] += p;
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
for (int b = 0; b < 8; ++b)
|
|
|
|
{
|
|
|
|
float p = probs[b][1] / (probs[b][0] + probs[b][1]);
|
|
|
|
// Avoid trouble when sigma is unrealistically low.
|
|
|
|
if (!isnormal(p))
|
|
|
|
p = 0;
|
|
|
|
fss.p[b] = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
result *pr = &lut[I & (R - 1)][Q & (R - 1)];
|
|
|
|
to_softsymb(&fss, &pr->ss);
|
|
|
|
// Always record nearest symbol and phase error for C&T.
|
|
|
|
pr->symbol = fss.nearest;
|
|
|
|
float ph_symbol = atan2f(symbols[pr->symbol].im,
|
|
|
|
symbols[pr->symbol].re);
|
|
|
|
float ph_err = atan2f(Q, I) - ph_symbol;
|
|
|
|
pr->phase_error = (int32_t)(ph_err * 65536 / (2 * M_PI)); // Mod 65536
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
void dump(FILE *f)
|
|
|
|
{
|
|
|
|
int bps = log2(nsymbols);
|
|
|
|
fprintf(f, "P5\n%d %d\n255\n", R, R * (bps + 1));
|
|
|
|
|
|
|
|
for (int bit = 0; bit < bps + 1; ++bit)
|
|
|
|
{
|
|
|
|
for (int Q = R / 2 - 1; Q >= -R / 2; --Q)
|
|
|
|
{
|
|
|
|
for (int I = -R / 2; I < R / 2; ++I)
|
|
|
|
{
|
|
|
|
result *pr = &lut[I & (R - 1)][Q & (R - 1)];
|
|
|
|
uint8_t v;
|
|
|
|
if (bit < bps)
|
|
|
|
v = softsymb_to_dump(pr->ss, bit);
|
|
|
|
else
|
|
|
|
v = 128 + pr->phase_error / 64;
|
|
|
|
// Highlight the constellation symbols.
|
|
|
|
for (int s = 0; s < nsymbols; ++s)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
if (symbols[s].re == I && symbols[s].im == Q)
|
|
|
|
v ^= 128;
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
fputc(v, f);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert soft metric to Hamming distance
|
2018-02-24 18:07:08 -05:00
|
|
|
void harden()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < R; ++i)
|
2019-03-17 16:31:42 -04:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int q = 0; q < R; ++q)
|
2019-03-17 16:31:42 -04:00
|
|
|
softsymb_harden(&lut[i][q].ss);
|
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// cstln_lut
|
|
|
|
|
|
|
|
// SAMPLER INTERFACE FOR CSTLN_RECEIVER
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
2018-02-24 18:07:08 -05:00
|
|
|
struct sampler_interface
|
|
|
|
{
|
|
|
|
virtual ~sampler_interface()
|
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
virtual complex<T> interp(const complex<T> *pin, float mu, float phase) = 0;
|
|
|
|
virtual void update_freq(float freqw, int period = 1)
|
|
|
|
{
|
|
|
|
(void) freqw;
|
|
|
|
(void) period;
|
|
|
|
} // 65536 = 1 Hz
|
|
|
|
virtual int readahead() = 0;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// NEAREST-SAMPLE SAMPLER FOR CSTLN_RECEIVER
|
|
|
|
// Suitable for bandpass-filtered, oversampled signals only
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct nearest_sampler : sampler_interface<T>
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
int readahead()
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:45:03 -05:00
|
|
|
complex<T> interp(const complex<T> *pin, float mu, float phase)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-11-12 18:45:03 -05:00
|
|
|
(void) mu;
|
2018-02-24 18:07:08 -05:00
|
|
|
return pin[0] * trig.expi(-phase);
|
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
trig16 trig;
|
|
|
|
};
|
|
|
|
// nearest_sampler
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// LINEAR SAMPLER FOR CSTLN_RECEIVER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct linear_sampler : sampler_interface<T>
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
int readahead()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
complex<T> interp(const complex<T> *pin, float mu, float phase)
|
|
|
|
{
|
|
|
|
// Derotate pin[0] and pin[1]
|
|
|
|
complex<T> s0 = pin[0] * trig.expi(-phase);
|
|
|
|
complex<T> s1 = pin[1] * trig.expi(-(phase + freqw));
|
|
|
|
// Interpolate linearly
|
|
|
|
return s0 * (1 - mu) + s1 * mu;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
void update_freq(float _freqw, int period = 1)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
(void) period;
|
2018-02-24 18:07:08 -05:00
|
|
|
freqw = _freqw;
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
trig16 trig;
|
|
|
|
float freqw;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// linear_sampler
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// FIR SAMPLER FOR CSTLN_RECEIVER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T, typename Tc>
|
|
|
|
struct fir_sampler : sampler_interface<T>
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
fir_sampler(int _ncoeffs, Tc *_coeffs, int _subsampling = 1) : ncoeffs(_ncoeffs),
|
|
|
|
coeffs(_coeffs),
|
|
|
|
subsampling(_subsampling),
|
|
|
|
shifted_coeffs(new complex<T>[ncoeffs]),
|
|
|
|
update_freq_phase(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
do_update_freq(0); // In case application never calls update_freq()
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
int readahead()
|
2018-02-22 16:52:49 -05:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
return ncoeffs - 1;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<T> interp(const complex<T> *pin, float mu, float phase)
|
|
|
|
{
|
|
|
|
// Apply FIR filter with subsampling
|
|
|
|
complex<T> acc(0, 0);
|
2019-03-17 16:31:42 -04:00
|
|
|
complex<T> *pc = shifted_coeffs + (int)((1 - mu) * subsampling);
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<T> *pcend = shifted_coeffs + ncoeffs;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (subsampling == 1)
|
|
|
|
{
|
|
|
|
// Special case for heavily oversampled signals,
|
|
|
|
// where filtering is expensive.
|
|
|
|
// gcc-4.9.2 can vectorize this form with NEON on ARM.
|
|
|
|
while (pc < pcend)
|
|
|
|
acc += (*pc++) * (*pin++);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Not vectorized because the coefficients are not
|
|
|
|
// guaranteed to be contiguous in memory.
|
|
|
|
for (; pc < pcend; pc += subsampling, ++pin)
|
|
|
|
acc += (*pc) * (*pin);
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Derotate
|
|
|
|
return trig.expi(-phase) * acc;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
void update_freq(float freqw, int period)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
// Throttling: Update one coeff per 16 processed samples,
|
|
|
|
// to keep the overhead of freq tracking below about 10%.
|
2019-03-17 16:31:42 -04:00
|
|
|
update_freq_phase -= period;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (update_freq_phase <= 0)
|
|
|
|
{
|
|
|
|
update_freq_phase = ncoeffs * 16;
|
|
|
|
do_update_freq(freqw);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
void do_update_freq(float freqw)
|
|
|
|
{
|
|
|
|
float f = freqw / subsampling;
|
|
|
|
for (int i = 0; i < ncoeffs; ++i)
|
|
|
|
shifted_coeffs[i] = trig.expi(-f * (i - ncoeffs / 2)) * coeffs[i];
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
trig16 trig;
|
|
|
|
int ncoeffs;
|
|
|
|
Tc *coeffs;
|
|
|
|
int subsampling;
|
|
|
|
cf32 *shifted_coeffs;
|
|
|
|
int update_freq_phase;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// fir_sampler
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// CONSTELLATION RECEIVER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Linear interpolation: good enough for 1.2 samples/symbol,
|
|
|
|
// but higher oversampling is recommended.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T, typename SOFTSYMB>
|
|
|
|
struct cstln_receiver : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
sampler_interface<T> *sampler;
|
2019-03-17 16:31:42 -04:00
|
|
|
cstln_lut<SOFTSYMB, 256> *cstln;
|
|
|
|
unsigned long meas_decimation; // Measurement rate
|
|
|
|
float omega, min_omega, max_omega; // Samples per symbol
|
|
|
|
float freqw, min_freqw, max_freqw; // Freq offs (65536 = 1 Hz)
|
2018-02-22 16:52:49 -05:00
|
|
|
float pll_adjustment;
|
2019-03-17 16:31:42 -04:00
|
|
|
bool allow_drift; // Follow carrier beyond safe limits
|
2018-02-22 16:52:49 -05:00
|
|
|
static const unsigned int chunk_size = 128;
|
|
|
|
float kest;
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
cstln_receiver(scheduler *sch,
|
|
|
|
sampler_interface<T> *_sampler,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<SOFTSYMB> &_out,
|
|
|
|
pipebuf<float> *_freq_out = NULL,
|
|
|
|
pipebuf<float> *_ss_out = NULL,
|
|
|
|
pipebuf<float> *_mer_out = NULL,
|
|
|
|
pipebuf<cf32> *_cstln_out = NULL) : runnable(sch, "Constellation receiver"),
|
|
|
|
sampler(_sampler),
|
|
|
|
cstln(NULL),
|
|
|
|
meas_decimation(1048576),
|
|
|
|
pll_adjustment(1.0),
|
|
|
|
allow_drift(false),
|
|
|
|
kest(0.01),
|
|
|
|
in(_in),
|
|
|
|
out(_out, chunk_size),
|
|
|
|
est_insp(cstln_amp * cstln_amp),
|
|
|
|
agc_gain(1),
|
|
|
|
mu(0),
|
|
|
|
phase(0),
|
|
|
|
est_sp(0),
|
|
|
|
est_ep(0),
|
|
|
|
meas_count(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
set_omega(1);
|
|
|
|
set_freq(0);
|
|
|
|
freq_out = _freq_out ? new pipewriter<float>(*_freq_out) : NULL;
|
|
|
|
ss_out = _ss_out ? new pipewriter<float>(*_ss_out) : NULL;
|
|
|
|
mer_out = _mer_out ? new pipewriter<float>(*_mer_out) : NULL;
|
|
|
|
cstln_out = _cstln_out ? new pipewriter<cf32>(*_cstln_out) : NULL;
|
|
|
|
memset(hist, 0, sizeof(hist));
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void set_omega(float _omega, float tol = 10e-6)
|
|
|
|
{
|
|
|
|
omega = _omega;
|
|
|
|
min_omega = omega * (1 - tol);
|
|
|
|
max_omega = omega * (1 + tol);
|
|
|
|
update_freq_limits();
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void set_freq(float freq)
|
|
|
|
{
|
|
|
|
freqw = freq * 65536;
|
|
|
|
update_freq_limits();
|
|
|
|
refresh_freq_tap();
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void set_allow_drift(bool d)
|
|
|
|
{
|
|
|
|
allow_drift = d;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void update_freq_limits()
|
|
|
|
{
|
|
|
|
// Prevent PLL from crossing +-SR/n/2 and locking at +-SR/n.
|
|
|
|
int n = 4;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (cstln)
|
|
|
|
{
|
|
|
|
switch (cstln->nsymbols)
|
|
|
|
{
|
|
|
|
case 2:
|
|
|
|
n = 2;
|
2019-03-17 16:31:42 -04:00
|
|
|
break; // BPSK
|
2018-02-24 18:07:08 -05:00
|
|
|
case 4:
|
|
|
|
n = 4;
|
2019-03-17 16:31:42 -04:00
|
|
|
break; // QPSK
|
2018-02-24 18:07:08 -05:00
|
|
|
case 8:
|
|
|
|
n = 8;
|
2019-03-17 16:31:42 -04:00
|
|
|
break; // 8PSK
|
2018-02-24 18:07:08 -05:00
|
|
|
case 16:
|
|
|
|
n = 12;
|
2019-03-17 16:31:42 -04:00
|
|
|
break; // 16APSK
|
2018-02-24 18:07:08 -05:00
|
|
|
case 32:
|
|
|
|
n = 16;
|
2019-03-17 16:31:42 -04:00
|
|
|
break; // 32APSK
|
2018-02-24 18:07:08 -05:00
|
|
|
default:
|
|
|
|
n = 4;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
min_freqw = freqw - 65536 / max_omega / n / 2;
|
|
|
|
max_freqw = freqw + 65536 / max_omega / n / 2;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
if (!cstln)
|
2019-03-17 16:31:42 -04:00
|
|
|
fail("constellation not set");
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
// Magic constants that work with the qa recordings.
|
|
|
|
float freq_alpha = 0.04;
|
|
|
|
float freq_beta = 0.0012 / omega * pll_adjustment;
|
|
|
|
float gain_mu = 0.02 / (cstln_amp * cstln_amp) * 2;
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
int max_meas = chunk_size / meas_decimation + 1;
|
2018-02-24 18:07:08 -05:00
|
|
|
// Large margin on output_size because mu adjustments
|
|
|
|
// can lead to more than chunk_size/min_omega symbols.
|
2019-03-17 16:31:42 -04:00
|
|
|
while (in.readable() >= chunk_size + sampler->readahead() && out.writable() >= chunk_size && (!freq_out || freq_out->writable() >= max_meas) && (!ss_out || ss_out->writable() >= max_meas) && (!mer_out || mer_out->writable() >= max_meas) && (!cstln_out || cstln_out->writable() >= max_meas))
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
sampler->update_freq(freqw, chunk_size);
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
complex<T> *pin = in.rd(), *pin0 = pin, *pend = pin + chunk_size;
|
2019-03-17 16:31:42 -04:00
|
|
|
SOFTSYMB *pout = out.wr(), *pout0 = pout;
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
// These are scoped outside the loop for SS and MER estimation.
|
2019-03-17 16:31:42 -04:00
|
|
|
complex<float> sg{0.0f, 0.0f}; // Symbol before AGC;
|
|
|
|
complex<float> s; // For MER estimation and constellation viewer
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<signed char> *cstln_point = NULL;
|
|
|
|
|
|
|
|
while (pin < pend)
|
|
|
|
{
|
|
|
|
// Here mu is the time of the next symbol counted from 0 at pin.
|
|
|
|
if (mu < 1)
|
|
|
|
{
|
|
|
|
// Here 0<=mu<1 is the fractional time of the next symbol
|
|
|
|
// between pin and pin+1.
|
2019-03-17 16:31:42 -04:00
|
|
|
sg = sampler->interp(pin, mu, phase + mu * freqw);
|
2018-02-24 18:07:08 -05:00
|
|
|
s = sg * agc_gain;
|
|
|
|
|
|
|
|
// Constellation look-up
|
2019-03-17 16:31:42 -04:00
|
|
|
typename cstln_lut<SOFTSYMB, 256>::result *cr =
|
|
|
|
cstln->lookup(s.re, s.im);
|
2018-02-24 18:07:08 -05:00
|
|
|
*pout = cr->ss;
|
|
|
|
++pout;
|
|
|
|
|
|
|
|
// PLL
|
|
|
|
phase += cr->phase_error * freq_alpha;
|
|
|
|
freqw += cr->phase_error * freq_beta;
|
|
|
|
|
|
|
|
// Modified Mueller and Müller
|
|
|
|
// mu[k]=real((c[k]-c[k-2])*conj(p[k-1])-(p[k]-p[k-2])*conj(c[k-1]))
|
|
|
|
// =dot(c[k]-c[k-2],p[k-1]) - dot(p[k]-p[k-2],c[k-1])
|
|
|
|
// p = received signals
|
|
|
|
// c = decisions (constellation points)
|
|
|
|
hist[2] = hist[1];
|
|
|
|
hist[1] = hist[0];
|
|
|
|
hist[0].p.re = s.re;
|
|
|
|
hist[0].p.im = s.im;
|
2019-03-17 16:31:42 -04:00
|
|
|
cstln_point = &cstln->symbols[cr->symbol];
|
2018-02-24 18:07:08 -05:00
|
|
|
hist[0].c.re = cstln_point->re;
|
|
|
|
hist[0].c.im = cstln_point->im;
|
2019-03-17 16:31:42 -04:00
|
|
|
float muerr = ((hist[0].p.re - hist[2].p.re) * hist[1].c.re + (hist[0].p.im - hist[2].p.im) * hist[1].c.im) - ((hist[0].c.re - hist[2].c.re) * hist[1].p.re + (hist[0].c.im - hist[2].c.im) * hist[1].p.im);
|
2018-02-24 18:07:08 -05:00
|
|
|
float mucorr = muerr * gain_mu;
|
|
|
|
const float max_mucorr = 0.1;
|
|
|
|
// TBD Optimize out statically
|
|
|
|
if (mucorr < -max_mucorr)
|
|
|
|
mucorr = -max_mucorr;
|
|
|
|
if (mucorr > max_mucorr)
|
|
|
|
mucorr = max_mucorr;
|
|
|
|
mu += mucorr;
|
2019-03-17 16:31:42 -04:00
|
|
|
mu += omega; // Next symbol time;
|
|
|
|
} // mu<1
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
// Next sample
|
|
|
|
++pin;
|
|
|
|
--mu;
|
|
|
|
phase += freqw;
|
2019-03-17 16:31:42 -04:00
|
|
|
} // chunk_size
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
in.read(pin - pin0);
|
|
|
|
out.written(pout - pout0);
|
|
|
|
|
|
|
|
// Normalize phase so that it never exceeds 32 bits.
|
|
|
|
// Max freqw is 2^31/65536/chunk_size = 256 Hz
|
|
|
|
// (this may happen with leandvb --drift --decim).
|
2019-03-17 16:31:42 -04:00
|
|
|
phase = fmodf(phase, 65536); // Rounding direction irrelevant
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
if (cstln_point)
|
|
|
|
{
|
|
|
|
// Output the last interpolated PSK symbol, max once per chunk_size
|
|
|
|
if (cstln_out)
|
|
|
|
cstln_out->write(s);
|
|
|
|
|
|
|
|
// AGC
|
|
|
|
// For APSK we must do AGC on the symbols, not the whole signal.
|
|
|
|
// TODO Use a better estimator at low SNR.
|
|
|
|
float insp = sg.re * sg.re + sg.im * sg.im;
|
|
|
|
est_insp = insp * kest + est_insp * (1 - kest);
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (est_insp)
|
|
|
|
agc_gain = cstln_amp / gen_sqrt(est_insp);
|
|
|
|
|
|
|
|
// SS and MER
|
|
|
|
complex<float> ev(s.re - cstln_point->re,
|
2019-03-17 16:31:42 -04:00
|
|
|
s.im - cstln_point->im);
|
2018-02-24 18:07:08 -05:00
|
|
|
float sig_power, ev_power;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (cstln->nsymbols == 2)
|
|
|
|
{
|
|
|
|
// Special case for BPSK: Ignore quadrature component of noise.
|
|
|
|
// TBD Projection on I axis assumes BPSK at 45°
|
2019-03-17 16:31:42 -04:00
|
|
|
float sig_real = (cstln_point->re + cstln_point->im) * 0.707;
|
2018-02-24 18:07:08 -05:00
|
|
|
float ev_real = (ev.re + ev.im) * 0.707;
|
|
|
|
sig_power = sig_real * sig_real;
|
|
|
|
ev_power = ev_real * ev_real;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
sig_power = (int)cstln_point->re * cstln_point->re + (int)cstln_point->im * cstln_point->im;
|
2018-02-24 18:07:08 -05:00
|
|
|
ev_power = ev.re * ev.re + ev.im * ev.im;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
est_sp = sig_power * kest + est_sp * (1 - kest);
|
|
|
|
est_ep = ev_power * kest + est_ep * (1 - kest);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is best done periodically ouside the inner loop,
|
|
|
|
// but will cause non-deterministic output.
|
|
|
|
|
|
|
|
if (!allow_drift)
|
|
|
|
{
|
|
|
|
if (freqw < min_freqw || freqw > max_freqw)
|
|
|
|
freqw = (max_freqw + min_freqw) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output measurements
|
|
|
|
|
|
|
|
refresh_freq_tap();
|
|
|
|
|
|
|
|
meas_count += pin - pin0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
while (meas_count >= meas_decimation)
|
|
|
|
{
|
|
|
|
meas_count -= meas_decimation;
|
|
|
|
if (freq_out)
|
|
|
|
freq_out->write(freq_tap);
|
|
|
|
if (ss_out)
|
|
|
|
ss_out->write(sqrtf(est_insp));
|
|
|
|
if (mer_out)
|
|
|
|
mer_out->write(
|
2019-03-17 16:31:42 -04:00
|
|
|
est_ep ? 10 * logf(est_sp / est_ep) / logf(10) : 0);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
} // Work to do
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
float freq_tap;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void refresh_freq_tap()
|
|
|
|
{
|
|
|
|
freq_tap = freqw / 65536;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
struct
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
complex<float> p; // Received symbol
|
|
|
|
complex<float> c; // Matched constellation point
|
2018-02-22 16:52:49 -05:00
|
|
|
} hist[3];
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
pipereader<complex<T>> in;
|
|
|
|
pipewriter<SOFTSYMB> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
float est_insp, agc_gain;
|
2019-03-17 16:31:42 -04:00
|
|
|
float mu; // PSK time expressed in clock ticks
|
|
|
|
float phase; // 65536=2pi
|
2018-02-22 16:52:49 -05:00
|
|
|
// Signal estimation
|
2019-03-17 16:31:42 -04:00
|
|
|
float est_sp; // Estimated RMS signal power
|
|
|
|
float est_ep; // Estimated RMS error vector power
|
2018-02-22 16:52:49 -05:00
|
|
|
unsigned long meas_count;
|
|
|
|
pipewriter<float> *freq_out, *ss_out, *mer_out;
|
|
|
|
pipewriter<cf32> *cstln_out;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// FAST QPSK RECEIVER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Optimized for u8 input, no AGC, uses phase information only.
|
|
|
|
// Outputs hard symbols.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct fast_qpsk_receiver : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
typedef u8 hardsymbol;
|
2019-03-17 16:31:42 -04:00
|
|
|
unsigned long meas_decimation; // Measurement rate
|
|
|
|
float omega, min_omega, max_omega; // Samples per symbol
|
|
|
|
signed long freqw, min_freqw, max_freqw; // Freq offs (angle per sample)
|
2018-02-22 16:52:49 -05:00
|
|
|
float pll_adjustment;
|
2019-03-17 16:31:42 -04:00
|
|
|
bool allow_drift; // Follow carrier beyond safe limits
|
2018-02-22 16:52:49 -05:00
|
|
|
static const unsigned int chunk_size = 128;
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
fast_qpsk_receiver(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<hardsymbol> &_out,
|
|
|
|
pipebuf<float> *_freq_out = NULL,
|
|
|
|
pipebuf<complex<T>> *_cstln_out = NULL) : runnable(sch, "Fast QPSK receiver"),
|
|
|
|
meas_decimation(1048576),
|
|
|
|
pll_adjustment(1.0),
|
|
|
|
allow_drift(false),
|
|
|
|
in(_in),
|
|
|
|
out(_out, chunk_size),
|
|
|
|
mu(0),
|
|
|
|
phase(0),
|
|
|
|
meas_count(0)
|
2018-02-22 16:52:49 -05:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
set_omega(1);
|
|
|
|
set_freq(0);
|
|
|
|
freq_out = _freq_out ? new pipewriter<float>(*_freq_out) : NULL;
|
2019-03-17 16:31:42 -04:00
|
|
|
cstln_out = _cstln_out ? new pipewriter<complex<T>>(*_cstln_out) : NULL;
|
2018-02-24 18:07:08 -05:00
|
|
|
memset(hist, 0, sizeof(hist));
|
|
|
|
init_lookup_tables();
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void set_omega(float _omega, float tol = 10e-6)
|
|
|
|
{
|
|
|
|
omega = _omega;
|
|
|
|
min_omega = omega * (1 - tol);
|
|
|
|
max_omega = omega * (1 + tol);
|
|
|
|
update_freq_limits();
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void set_freq(float freq)
|
|
|
|
{
|
|
|
|
freqw = freq * 65536;
|
|
|
|
update_freq_limits();
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void update_freq_limits()
|
|
|
|
{
|
|
|
|
// Prevent PLL from locking at +-symbolrate/4.
|
|
|
|
// TODO The +-SR/8 limit is suitable for QPSK only.
|
|
|
|
min_freqw = freqw - 65536 / max_omega / 8;
|
|
|
|
max_freqw = freqw + 65536 / max_omega / 8;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static const int RLUT_BITS = 8;
|
|
|
|
static const int RLUT_ANGLES = 1 << RLUT_BITS;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
// Magic constants that work with the qa recordings.
|
|
|
|
signed long freq_alpha = 0.04 * 65536;
|
|
|
|
signed long freq_beta = 0.0012 * 256 * 65536 / omega * pll_adjustment;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (!freq_beta)
|
2019-03-17 16:31:42 -04:00
|
|
|
fail("Excessive oversampling");
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
float gain_mu = 0.02 / (cstln_amp * cstln_amp) * 2;
|
|
|
|
|
|
|
|
int max_meas = chunk_size / meas_decimation + 1;
|
|
|
|
// Largin margin on output_size because mu adjustments
|
|
|
|
// can lead to more than chunk_size/min_omega symbols.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
while (in.readable() >= chunk_size + 1 && // +1 for interpolation
|
|
|
|
out.writable() >= chunk_size && (!freq_out || freq_out->writable() >= max_meas) && (!cstln_out || cstln_out->writable() >= max_meas))
|
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<T> *pin = in.rd(), *pin0 = pin, *pend = pin + chunk_size;
|
|
|
|
hardsymbol *pout = out.wr(), *pout0 = pout;
|
|
|
|
|
|
|
|
cu8 s;
|
2019-03-17 16:31:42 -04:00
|
|
|
u_angle symbol_arg = 0; // Exported for constellation viewer
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
while (pin < pend)
|
|
|
|
{
|
|
|
|
// Here mu is the time of the next symbol counted from 0 at pin.
|
|
|
|
if (mu < 1)
|
|
|
|
{
|
|
|
|
// Here 0<=mu<1 is the fractional time of the next symbol
|
|
|
|
// between pin and pin+1.
|
|
|
|
|
|
|
|
// Derotate and interpolate
|
2019-03-17 16:31:42 -04:00
|
|
|
#if 0 /* Phase only (does not work)
|
|
|
|
Careful with the float/signed/unsigned casts */
|
2018-02-24 18:07:08 -05:00
|
|
|
u_angle a0 = fast_arg(pin[0]) - phase;
|
|
|
|
u_angle a1 = fast_arg(pin[1]) - (phase+freqw);
|
|
|
|
s_angle da = a1 - a0;
|
|
|
|
symbol_arg = a0 + (s_angle)(da*mu);
|
|
|
|
s = arg_to_symbol(symbol_arg);
|
2019-03-17 16:31:42 -04:00
|
|
|
#elif 1 // Linear by lookup-table. 1.2M on bench3bishs
|
2018-02-24 18:07:08 -05:00
|
|
|
polar *p0 = &lut_polar[pin[0].re][pin[0].im];
|
2019-03-17 16:31:42 -04:00
|
|
|
u_angle a0 = (u_angle)(p0->a - phase) >> (16 - RLUT_BITS);
|
2018-02-24 18:07:08 -05:00
|
|
|
cu8 *p0r = &lut_rect[a0][p0->r >> 1];
|
|
|
|
polar *p1 = &lut_polar[pin[1].re][pin[1].im];
|
2019-03-17 16:31:42 -04:00
|
|
|
u_angle a1 = (u_angle)(p1->a - (phase + freqw)) >> (16 - RLUT_BITS);
|
2018-02-24 18:07:08 -05:00
|
|
|
cu8 *p1r = &lut_rect[a1][p1->r >> 1];
|
2019-03-17 16:31:42 -04:00
|
|
|
s.re = (int)(p0r->re + (p1r->re - p0r->re) * mu);
|
|
|
|
s.im = (int)(p0r->im + (p1r->im - p0r->im) * mu);
|
2018-02-24 18:07:08 -05:00
|
|
|
symbol_arg = fast_arg(s);
|
2019-03-17 16:31:42 -04:00
|
|
|
#else // Linear floating-point, for reference
|
|
|
|
float a0 = -(int)phase * M_PI / 32768;
|
|
|
|
float cosa0 = cosf(a0), sina0 = sinf(a0);
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<float>
|
2019-03-17 16:31:42 -04:00
|
|
|
p0r(((float)pin[0].re - 128) * cosa0 - ((float)pin[0].im - 128) * sina0,
|
|
|
|
((float)pin[0].re - 128) * sina0 + ((float)pin[0].im - 128) * cosa0);
|
|
|
|
float a1 = -(int)(phase + freqw) * M_PI / 32768;
|
|
|
|
float cosa1 = cosf(a1), sina1 = sinf(a1);
|
2018-02-24 18:07:08 -05:00
|
|
|
complex<float>
|
2019-03-17 16:31:42 -04:00
|
|
|
p1r(((float)pin[1].re - 128) * cosa1 - ((float)pin[1].im - 128) * sina1,
|
|
|
|
((float)pin[1].re - 128) * sina1 + ((float)pin[1].im - 128) * cosa1);
|
|
|
|
s.re = (int)(128 + p0r.re + (p1r.re - p0r.re) * mu);
|
|
|
|
s.im = (int)(128 + p0r.im + (p1r.im - p0r.im) * mu);
|
2018-02-24 18:07:08 -05:00
|
|
|
symbol_arg = fast_arg(s);
|
2018-02-22 16:52:49 -05:00
|
|
|
#endif
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
int quadrant = symbol_arg >> 14;
|
2019-03-17 16:31:42 -04:00
|
|
|
static unsigned char quadrant_to_symbol[4] = {0, 2, 3, 1};
|
2018-02-24 18:07:08 -05:00
|
|
|
*pout = quadrant_to_symbol[quadrant];
|
|
|
|
++pout;
|
|
|
|
|
|
|
|
// PLL
|
2019-03-17 16:31:42 -04:00
|
|
|
s_angle phase_error = (s_angle)(symbol_arg & 16383) - 8192;
|
2018-02-24 18:07:08 -05:00
|
|
|
phase += (phase_error * freq_alpha + 32768) >> 16;
|
|
|
|
freqw += (phase_error * freq_beta + 32768 * 256) >> 24;
|
|
|
|
|
|
|
|
// Modified Mueller and Müller
|
|
|
|
// mu[k]=real((c[k]-c[k-2])*conj(p[k-1])-(p[k]-p[k-2])*conj(c[k-1]))
|
|
|
|
// =dot(c[k]-c[k-2],p[k-1]) - dot(p[k]-p[k-2],c[k-1])
|
|
|
|
// p = received signals
|
|
|
|
// c = decisions (constellation points)
|
|
|
|
hist[2] = hist[1];
|
|
|
|
hist[1] = hist[0];
|
2018-02-22 16:52:49 -05:00
|
|
|
#define HIST_FLOAT 0
|
|
|
|
#if HIST_FLOAT
|
2018-02-24 18:07:08 -05:00
|
|
|
hist[0].p.re = (float)s.re - 128;
|
|
|
|
hist[0].p.im = (float)s.im - 128;
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
cu8 cp = arg_to_symbol((symbol_arg & 49152) + 8192);
|
2018-02-24 18:07:08 -05:00
|
|
|
hist[0].c.re = (float)cp.re - 128;
|
|
|
|
hist[0].c.im = (float)cp.im - 128;
|
|
|
|
|
|
|
|
float muerr =
|
2019-03-17 16:31:42 -04:00
|
|
|
((hist[0].p.re - hist[2].p.re) * hist[1].c.re +
|
|
|
|
(hist[0].p.im - hist[2].p.im) * hist[1].c.im) -
|
|
|
|
((hist[0].c.re - hist[2].c.re) * hist[1].p.re +
|
|
|
|
(hist[0].c.im - hist[2].c.im) * hist[1].p.im);
|
2018-02-22 16:52:49 -05:00
|
|
|
#else
|
2018-02-24 18:07:08 -05:00
|
|
|
hist[0].p = s;
|
|
|
|
hist[0].c = arg_to_symbol((symbol_arg & 49152) + 8192);
|
|
|
|
|
|
|
|
int muerr =
|
2019-03-17 16:31:42 -04:00
|
|
|
((signed char)(hist[0].p.re - hist[2].p.re) * ((int)hist[1].c.re - 128) + (signed char)(hist[0].p.im - hist[2].p.im) * ((int)hist[1].c.im - 128)) - ((signed char)(hist[0].c.re - hist[2].c.re) * ((int)hist[1].p.re - 128) + (signed char)(hist[0].c.im - hist[2].c.im) * ((int)hist[1].p.im - 128));
|
2018-02-22 16:52:49 -05:00
|
|
|
#endif
|
2018-02-24 18:07:08 -05:00
|
|
|
float mucorr = muerr * gain_mu;
|
|
|
|
const float max_mucorr = 0.1;
|
|
|
|
// TBD Optimize out statically
|
|
|
|
if (mucorr < -max_mucorr)
|
|
|
|
mucorr = -max_mucorr;
|
|
|
|
if (mucorr > max_mucorr)
|
|
|
|
mucorr = max_mucorr;
|
|
|
|
mu += mucorr;
|
2019-03-17 16:31:42 -04:00
|
|
|
mu += omega; // Next symbol time;
|
|
|
|
} // mu<1
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
// Next sample
|
|
|
|
++pin;
|
|
|
|
--mu;
|
|
|
|
phase += freqw;
|
2019-03-17 16:31:42 -04:00
|
|
|
} // chunk_size
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
in.read(pin - pin0);
|
|
|
|
out.written(pout - pout0);
|
|
|
|
|
|
|
|
if (symbol_arg && cstln_out)
|
|
|
|
// Output the last interpolated PSK symbol, max once per chunk_size
|
|
|
|
cstln_out->write(s);
|
|
|
|
|
|
|
|
// This is best done periodically ouside the inner loop,
|
|
|
|
// but will cause non-deterministic output.
|
|
|
|
|
|
|
|
if (!allow_drift)
|
|
|
|
{
|
|
|
|
if (freqw < min_freqw || freqw > max_freqw)
|
|
|
|
freqw = (max_freqw + min_freqw) / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output measurements
|
|
|
|
|
|
|
|
meas_count += pin - pin0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
while (meas_count >= meas_decimation)
|
|
|
|
{
|
|
|
|
meas_count -= meas_decimation;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (freq_out)
|
2019-03-17 16:31:42 -04:00
|
|
|
freq_out->write((float)freqw / 65536);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
} // Work to do
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
struct polar
|
|
|
|
{
|
|
|
|
u_angle a;
|
|
|
|
unsigned char r;
|
|
|
|
} lut_polar[256][256];
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
u_angle fast_arg(const cu8 &c)
|
|
|
|
{
|
|
|
|
// TBD read cu8 as u16 index, same endianness as in init()
|
|
|
|
return lut_polar[c.re][c.im].a;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
cu8 lut_rect[RLUT_ANGLES][256];
|
|
|
|
cu8 lut_sincos[65536];
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
cu8 arg_to_symbol(u_angle a)
|
|
|
|
{
|
|
|
|
return lut_sincos[a];
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void init_lookup_tables()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 256; ++i)
|
2019-03-17 16:31:42 -04:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int q = 0; q < 256; ++q)
|
|
|
|
{
|
|
|
|
// Don't cast float to unsigned directly
|
2019-03-17 16:31:42 -04:00
|
|
|
lut_polar[i][q].a = (s_angle)(atan2f(q - 128, i - 128) * 65536 / (2 * M_PI));
|
|
|
|
lut_polar[i][q].r = (int)hypotf(i - 128, q - 128);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (unsigned long a = 0; a < 65536; ++a)
|
|
|
|
{
|
|
|
|
float f = 2 * M_PI * a / 65536;
|
|
|
|
lut_sincos[a].re = 128 + cstln_amp * cosf(f);
|
|
|
|
lut_sincos[a].im = 128 + cstln_amp * sinf(f);
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int a = 0; a < RLUT_ANGLES; ++a)
|
2019-03-17 16:31:42 -04:00
|
|
|
{
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int r = 0; r < 256; ++r)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
lut_rect[a][r].re = (int)(128 + r * cos(2 * M_PI * a / RLUT_ANGLES));
|
|
|
|
lut_rect[a][r].im = (int)(128 + r * sin(2 * M_PI * a / RLUT_ANGLES));
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
struct
|
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
#if HIST_FLOAT
|
2019-03-17 16:31:42 -04:00
|
|
|
complex<float> p; // Received symbol
|
|
|
|
complex<float> c; // Matched constellation point
|
2018-02-22 16:52:49 -05:00
|
|
|
#else
|
2019-03-17 16:31:42 -04:00
|
|
|
cu8 p; // Received symbol
|
|
|
|
cu8 c; // Matched constellation point
|
2018-02-22 16:52:49 -05:00
|
|
|
#endif
|
|
|
|
} hist[3];
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-22 16:52:49 -05:00
|
|
|
pipereader<cu8> in;
|
|
|
|
pipewriter<hardsymbol> out;
|
2019-03-17 16:31:42 -04:00
|
|
|
float mu; // PSK time expressed in clock ticks. TBD fixed point.
|
2018-02-22 16:52:49 -05:00
|
|
|
u_angle phase;
|
|
|
|
unsigned long meas_count;
|
|
|
|
pipewriter<float> *freq_out, *mer_out;
|
|
|
|
pipewriter<cu8> *cstln_out;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// fast_qpsk_receiver
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// CONSTELLATION TRANSMITTER
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Maps symbols to I/Q points.
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename Tout, int Zout>
|
|
|
|
struct cstln_transmitter : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
cstln_lut<hard_ss, 256> *cstln;
|
|
|
|
|
|
|
|
cstln_transmitter(scheduler *sch,
|
|
|
|
pipebuf<u8> &_in,
|
|
|
|
pipebuf<complex<Tout>> &_out) : runnable(sch, "cstln_transmitter"),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
cstln(0)
|
2018-02-22 16:52:49 -05:00
|
|
|
{
|
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
if (!cstln)
|
2019-03-17 16:31:42 -04:00
|
|
|
fail("constellation not set");
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
int count = min(in.readable(), out.writable());
|
|
|
|
u8 *pin = in.rd(), *pend = pin + count;
|
|
|
|
complex<Tout> *pout = out.wr();
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; pin < pend; ++pin, ++pout)
|
|
|
|
{
|
|
|
|
complex<signed char> *cp = &cstln->symbols[*pin];
|
|
|
|
pout->re = Zout + cp->re;
|
|
|
|
pout->im = Zout + cp->im;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
in.read(count);
|
|
|
|
out.written(count);
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-22 16:52:49 -05:00
|
|
|
pipereader<u8> in;
|
2019-03-17 16:31:42 -04:00
|
|
|
pipewriter<complex<Tout>> out;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// cstln_transmitter
|
|
|
|
|
|
|
|
// FREQUENCY SHIFTER
|
|
|
|
|
|
|
|
// Resolution is sample_freq/65536.
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct rotator : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
rotator(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<complex<T>> &_out,
|
|
|
|
float freq) : runnable(sch, "rotator"),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
index(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
|
|
|
int ifreq = freq * 65536;
|
|
|
|
if (sch->debug)
|
|
|
|
fprintf(stderr, "Rotate: req=%f real=%f\n", freq, ifreq / 65536.0);
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int i = 0; i < 65536; ++i)
|
|
|
|
{
|
|
|
|
lut_cos[i] = cosf(2 * M_PI * i * ifreq / 65536);
|
|
|
|
lut_sin[i] = sinf(2 * M_PI * i * ifreq / 65536);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
|
|
|
void run()
|
|
|
|
{
|
|
|
|
unsigned long count = min(in.readable(), out.writable());
|
|
|
|
complex<T> *pin = in.rd(), *pend = pin + count;
|
|
|
|
complex<T> *pout = out.wr();
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (; pin < pend; ++pin, ++pout, ++index)
|
|
|
|
{
|
|
|
|
float c = lut_cos[index];
|
|
|
|
float s = lut_sin[index];
|
|
|
|
pout->re = pin->re * c - pin->im * s;
|
|
|
|
pout->im = pin->re * s + pin->im * c;
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
in.read(count);
|
|
|
|
out.written(count);
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
|
|
|
pipereader<complex<T>> in;
|
|
|
|
pipewriter<complex<T>> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
float lut_cos[65536];
|
|
|
|
float lut_sin[65536];
|
2019-03-17 16:31:42 -04:00
|
|
|
unsigned short index; // Current phase
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// rotator
|
|
|
|
|
|
|
|
// SPECTRUM-BASED CNR ESTIMATOR
|
|
|
|
|
|
|
|
// Assumes that the spectrum is as follows:
|
|
|
|
//
|
|
|
|
// ---|--noise---|-roll-off-|---carrier+noise----|-roll-off-|---noise--|---
|
|
|
|
// | (bw/2) | (bw) | (bw/2) | (bw) | (bw/2) |
|
|
|
|
//
|
|
|
|
// Maximum roll-off 0.5
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T>
|
|
|
|
struct cnr_fft : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
cnr_fft(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<float> &_out,
|
|
|
|
float _bandwidth, int nfft = 4096) : runnable(sch, "cnr_fft"),
|
|
|
|
bandwidth(_bandwidth),
|
|
|
|
freq_tap(NULL),
|
|
|
|
tap_multiplier(1),
|
|
|
|
decimation(1048576),
|
|
|
|
kavg(0.1),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
fft(nfft),
|
|
|
|
avgpower(NULL),
|
|
|
|
phase(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
if (bandwidth > 0.25)
|
|
|
|
fail("CNR estimator requires Fsampling > 4x Fsignal");
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
float bandwidth;
|
2018-02-24 18:07:08 -05:00
|
|
|
float *freq_tap, tap_multiplier;
|
2018-02-22 16:52:49 -05:00
|
|
|
int decimation;
|
|
|
|
float kavg;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void run()
|
|
|
|
{
|
|
|
|
while (in.readable() >= fft.n && out.writable() >= 1)
|
|
|
|
{
|
|
|
|
phase += fft.n;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (phase >= decimation)
|
|
|
|
{
|
|
|
|
phase -= decimation;
|
|
|
|
do_cnr();
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
in.read(fft.n);
|
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
void do_cnr()
|
|
|
|
{
|
|
|
|
float center_freq = freq_tap ? *freq_tap * tap_multiplier : 0;
|
|
|
|
int icf = floor(center_freq * fft.n + 0.5);
|
2019-03-17 16:31:42 -04:00
|
|
|
complex<T> *data = new complex<T>[fft.n];
|
2018-02-24 18:07:08 -05:00
|
|
|
memcpy(data, in.rd(), fft.n * sizeof(data[0]));
|
|
|
|
fft.inplace(data, true);
|
2019-03-17 16:31:42 -04:00
|
|
|
T *power = new T[fft.n];
|
|
|
|
|
|
|
|
for (int i = 0; i < fft.n; ++i)
|
2018-02-24 18:07:08 -05:00
|
|
|
power[i] = data[i].re * data[i].re + data[i].im * data[i].im;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (!avgpower)
|
|
|
|
{
|
|
|
|
// Initialize with first spectrum
|
|
|
|
avgpower = new T[fft.n];
|
|
|
|
memcpy(avgpower, power, fft.n * sizeof(avgpower[0]));
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Accumulate and low-pass filter
|
2019-03-17 16:31:42 -04:00
|
|
|
for (int i = 0; i < fft.n; ++i)
|
2018-02-24 18:07:08 -05:00
|
|
|
avgpower[i] = avgpower[i] * (1 - kavg) + power[i] * kavg;
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
int bwslots = (bandwidth / 4) * fft.n;
|
|
|
|
|
|
|
|
if (!bwslots)
|
|
|
|
{
|
|
|
|
delete[] power;
|
|
|
|
delete[] data;
|
2018-02-24 18:07:08 -05:00
|
|
|
return;
|
2019-03-17 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Measure carrier+noise in center band
|
2019-03-17 16:31:42 -04:00
|
|
|
float c2plusn2 = avgslots(icf - bwslots, icf + bwslots);
|
2018-02-24 18:07:08 -05:00
|
|
|
// Measure noise left and right of roll-off zones
|
2019-03-17 16:31:42 -04:00
|
|
|
float n2 = (avgslots(icf - bwslots * 4, icf - bwslots * 3) + avgslots(icf + bwslots * 3, icf + bwslots * 4)) / 2;
|
2018-02-24 18:07:08 -05:00
|
|
|
float c2 = c2plusn2 - n2;
|
|
|
|
float cnr = (c2 > 0 && n2 > 0) ? 10 * logf(c2 / n2) / logf(10) : -50;
|
|
|
|
out.write(cnr);
|
2019-03-17 16:31:42 -04:00
|
|
|
delete[] power;
|
|
|
|
delete[] data;
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
float avgslots(int i0, int i1)
|
|
|
|
{ // i0 <= i1
|
2018-02-24 18:07:08 -05:00
|
|
|
T s = 0;
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int i = i0; i <= i1; ++i)
|
|
|
|
s += avgpower[i & (fft.n - 1)];
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
return s / (i1 - i0 + 1);
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
2018-02-24 18:07:08 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
pipereader<complex<T>> in;
|
2018-02-24 18:07:08 -05:00
|
|
|
pipewriter<float> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
cfft_engine<T> fft;
|
|
|
|
T *avgpower;
|
|
|
|
int phase;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// cnr_fft
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
template <typename T, int NFFT>
|
|
|
|
struct spectrum : runnable
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
spectrum(scheduler *sch,
|
|
|
|
pipebuf<complex<T>> &_in,
|
|
|
|
pipebuf<float[NFFT]> &_out) : runnable(sch, "spectrum"),
|
|
|
|
decimation(1048576),
|
|
|
|
kavg(0.1),
|
|
|
|
decim(1), in(in),
|
|
|
|
out(_out),
|
|
|
|
fft(NFFT),
|
|
|
|
avgpower(NULL),
|
|
|
|
phase(0)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int decimation;
|
|
|
|
float kavg;
|
2019-03-17 16:31:42 -04:00
|
|
|
int decim;
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
void run()
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
while (in.readable() >= fft.n * decim && out.writable() >= 1)
|
2018-02-24 18:07:08 -05:00
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
phase += fft.n * decim;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (phase >= decimation)
|
|
|
|
{
|
|
|
|
phase -= decimation;
|
|
|
|
do_spectrum();
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
in.read(fft.n * decim);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
private:
|
2018-02-24 18:07:08 -05:00
|
|
|
void do_spectrum()
|
|
|
|
{
|
|
|
|
complex<T> data[fft.n];
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
if (decim == 1)
|
|
|
|
{
|
|
|
|
memcpy(data, in.rd(), fft.n * sizeof(data[0]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
complex<T> *pin = in.rd();
|
|
|
|
|
|
|
|
for (int i = 0; i < fft.n; ++i, pin += decim)
|
|
|
|
data[i] = *pin;
|
|
|
|
}
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
fft.inplace(data, true);
|
2019-03-17 16:31:42 -04:00
|
|
|
float power[NFFT];
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
for (int i = 0; i < fft.n; ++i)
|
2019-03-17 16:31:42 -04:00
|
|
|
power[i] = (float)data[i].re * data[i].re + (float)data[i].im * data[i].im;
|
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
if (!avgpower)
|
|
|
|
{
|
|
|
|
// Initialize with first spectrum
|
|
|
|
avgpower = new float[fft.n];
|
|
|
|
memcpy(avgpower, power, fft.n * sizeof(avgpower[0]));
|
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
2018-02-24 18:07:08 -05:00
|
|
|
// Accumulate and low-pass filter
|
|
|
|
for (int i = 0; i < fft.n; ++i)
|
|
|
|
avgpower[i] = avgpower[i] * (1 - kavg) + power[i] * kavg;
|
|
|
|
|
|
|
|
// Reuse power[]
|
|
|
|
for (int i = 0; i < fft.n / 2; ++i)
|
|
|
|
{
|
2019-03-17 16:31:42 -04:00
|
|
|
power[i] = 10 * log10f(avgpower[NFFT / 2 + i]);
|
|
|
|
power[NFFT / 2 + i] = 10 * log10f(avgpower[i]);
|
2018-02-24 18:07:08 -05:00
|
|
|
}
|
2019-03-17 16:31:42 -04:00
|
|
|
|
|
|
|
memcpy(out.wr(), power, sizeof(power[0]) * NFFT);
|
2018-02-24 18:07:08 -05:00
|
|
|
out.written(1);
|
2018-02-22 16:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
pipereader<complex<T>> in;
|
|
|
|
pipewriter<float[NFFT]> out;
|
2018-02-22 16:52:49 -05:00
|
|
|
cfft_engine<T> fft;
|
|
|
|
T *avgpower;
|
|
|
|
int phase;
|
2018-02-24 18:07:08 -05:00
|
|
|
};
|
|
|
|
// spectrum
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
} // namespace leansdr
|
2018-02-22 16:52:49 -05:00
|
|
|
|
2019-03-17 16:31:42 -04:00
|
|
|
#endif // LEANSDR_SDR_H
|