2024-06-16 05:31:13 -04:00
|
|
|
/* anr.c
|
|
|
|
|
|
|
|
This file is part of a program that implements a Software-Defined Radio.
|
|
|
|
|
|
|
|
Copyright (C) 2012, 2013 Warren Pratt, NR0V
|
|
|
|
Copyright (C) 2024 Edouard Griffiths, F4EXB Adapted to SDRangel
|
|
|
|
|
|
|
|
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 2
|
|
|
|
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, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
The author can be reached by email at
|
|
|
|
|
|
|
|
warren@wpratt.com
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "comm.hpp"
|
|
|
|
#include "anr.hpp"
|
|
|
|
#include "amd.hpp"
|
2024-07-02 18:52:16 -04:00
|
|
|
#include "snba.hpp"
|
2024-06-16 05:31:13 -04:00
|
|
|
#include "emnr.hpp"
|
|
|
|
#include "anf.hpp"
|
|
|
|
#include "bandpass.hpp"
|
|
|
|
#include "RXA.hpp"
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
|
|
|
ANR* ANR::create_anr (
|
|
|
|
int run,
|
|
|
|
int position,
|
|
|
|
int buff_size,
|
2024-06-24 21:50:48 -04:00
|
|
|
float *in_buff,
|
|
|
|
float *out_buff,
|
2024-06-16 05:31:13 -04:00
|
|
|
int dline_size,
|
|
|
|
int n_taps,
|
|
|
|
int delay,
|
2024-07-17 20:08:05 -04:00
|
|
|
double two_mu,
|
|
|
|
double gamma,
|
|
|
|
double lidx,
|
|
|
|
double lidx_min,
|
|
|
|
double lidx_max,
|
|
|
|
double ngamma,
|
|
|
|
double den_mult,
|
|
|
|
double lincr,
|
|
|
|
double ldecr
|
2024-06-16 05:31:13 -04:00
|
|
|
)
|
|
|
|
{
|
|
|
|
ANR *a = new ANR;
|
|
|
|
a->run = run;
|
|
|
|
a->position = position;
|
|
|
|
a->buff_size = buff_size;
|
|
|
|
a->in_buff = in_buff;
|
|
|
|
a->out_buff = out_buff;
|
|
|
|
a->dline_size = dline_size;
|
|
|
|
a->mask = dline_size - 1;
|
|
|
|
a->n_taps = n_taps;
|
|
|
|
a->delay = delay;
|
|
|
|
a->two_mu = two_mu;
|
|
|
|
a->gamma = gamma;
|
|
|
|
a->in_idx = 0;
|
|
|
|
a->lidx = lidx;
|
|
|
|
a->lidx_min = lidx_min;
|
|
|
|
a->lidx_max = lidx_max;
|
|
|
|
a->ngamma = ngamma;
|
|
|
|
a->den_mult = den_mult;
|
|
|
|
a->lincr = lincr;
|
|
|
|
a->ldecr = ldecr;
|
|
|
|
|
2024-07-17 20:08:05 -04:00
|
|
|
memset (a->d, 0, sizeof(double) * ANR_DLINE_SIZE);
|
|
|
|
memset (a->w, 0, sizeof(double) * ANR_DLINE_SIZE);
|
2024-06-16 05:31:13 -04:00
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::destroy_anr (ANR *a)
|
|
|
|
{
|
|
|
|
delete a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::xanr (ANR *a, int position)
|
|
|
|
{
|
|
|
|
int i, j, idx;
|
2024-07-17 20:08:05 -04:00
|
|
|
double c0, c1;
|
|
|
|
double y, error, sigma, inv_sigp;
|
|
|
|
double nel, nev;
|
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
if (a->run && (a->position == position))
|
|
|
|
{
|
|
|
|
for (i = 0; i < a->buff_size; i++)
|
|
|
|
{
|
|
|
|
a->d[a->in_idx] = a->in_buff[2 * i + 0];
|
|
|
|
|
|
|
|
y = 0;
|
|
|
|
sigma = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < a->n_taps; j++)
|
|
|
|
{
|
|
|
|
idx = (a->in_idx + j + a->delay) & a->mask;
|
|
|
|
y += a->w[j] * a->d[idx];
|
|
|
|
sigma += a->d[idx] * a->d[idx];
|
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
inv_sigp = 1.0 / (sigma + 1e-10);
|
|
|
|
error = a->d[a->in_idx] - y;
|
|
|
|
|
|
|
|
a->out_buff[2 * i + 0] = y;
|
|
|
|
a->out_buff[2 * i + 1] = 0.0;
|
|
|
|
|
2024-07-17 20:08:05 -04:00
|
|
|
if ((nel = error * (1.0 - a->two_mu * sigma * inv_sigp)) < 0.0)
|
|
|
|
nel = -nel;
|
|
|
|
if ((nev = a->d[a->in_idx] - (1.0 - a->two_mu * a->ngamma) * y - a->two_mu * error * sigma * inv_sigp) < 0.0)
|
|
|
|
nev = -nev;
|
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
if (nev < nel)
|
|
|
|
{
|
2024-07-17 20:08:05 -04:00
|
|
|
if ((a->lidx += a->lincr) > a->lidx_max)
|
|
|
|
a->lidx = a->lidx_max;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-07-17 20:08:05 -04:00
|
|
|
if ((a->lidx -= a->ldecr) < a->lidx_min)
|
|
|
|
a->lidx = a->lidx_min;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-17 20:08:05 -04:00
|
|
|
a->ngamma = a->gamma * (a->lidx * a->lidx) * (a->lidx * a->lidx) * a->den_mult;
|
2024-06-16 05:31:13 -04:00
|
|
|
c0 = 1.0 - a->two_mu * a->ngamma;
|
|
|
|
c1 = a->two_mu * error * inv_sigp;
|
|
|
|
|
|
|
|
for (j = 0; j < a->n_taps; j++)
|
|
|
|
{
|
|
|
|
idx = (a->in_idx + j + a->delay) & a->mask;
|
|
|
|
a->w[j] = c0 * a->w[j] + c1 * a->d[idx];
|
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
a->in_idx = (a->in_idx + a->mask) & a->mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (a->in_buff != a->out_buff)
|
2024-07-17 20:08:05 -04:00
|
|
|
{
|
2024-07-16 17:15:13 -04:00
|
|
|
std::copy(a->in_buff, a->in_buff + a->buff_size * 2, a->out_buff);
|
2024-07-17 20:08:05 -04:00
|
|
|
}
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::flush_anr (ANR *a)
|
|
|
|
{
|
2024-07-17 20:08:05 -04:00
|
|
|
memset (a->d, 0, sizeof(double) * ANR_DLINE_SIZE);
|
|
|
|
memset (a->w, 0, sizeof(double) * ANR_DLINE_SIZE);
|
2024-06-16 05:31:13 -04:00
|
|
|
a->in_idx = 0;
|
|
|
|
}
|
|
|
|
|
2024-06-24 21:50:48 -04:00
|
|
|
void ANR::setBuffers_anr (ANR *a, float* in, float* out)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
a->in_buff = in;
|
|
|
|
a->out_buff = out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::setSamplerate_anr (ANR *a, int)
|
|
|
|
{
|
|
|
|
flush_anr(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::setSize_anr (ANR *a, int size)
|
|
|
|
{
|
|
|
|
a->buff_size = size;
|
|
|
|
flush_anr(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* RXA Properties *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
|
|
|
void ANR::SetANRRun (RXA& rxa, int run)
|
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
ANR *a = rxa.anr;
|
2024-07-13 17:59:46 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
if (a->run != run)
|
|
|
|
{
|
2024-07-17 20:08:05 -04:00
|
|
|
RXA::bp1Check (
|
|
|
|
rxa,
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.amd->run,
|
|
|
|
rxa.snba->run,
|
|
|
|
rxa.emnr->run,
|
|
|
|
rxa.anf->run,
|
2024-07-17 20:08:05 -04:00
|
|
|
run
|
|
|
|
);
|
2024-06-16 05:31:13 -04:00
|
|
|
a->run = run;
|
|
|
|
RXA::bp1Set (rxa);
|
|
|
|
flush_anr (a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 20:08:05 -04:00
|
|
|
void ANR::SetANRVals (RXA& rxa, int taps, int delay, double gain, double leakage)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.anr->n_taps = taps;
|
|
|
|
rxa.anr->delay = delay;
|
|
|
|
rxa.anr->two_mu = gain;
|
|
|
|
rxa.anr->gamma = leakage;
|
|
|
|
flush_anr (rxa.anr);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::SetANRTaps (RXA& rxa, int taps)
|
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.anr->n_taps = taps;
|
|
|
|
flush_anr (rxa.anr);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::SetANRDelay (RXA& rxa, int delay)
|
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.anr->delay = delay;
|
|
|
|
flush_anr (rxa.anr);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-17 20:08:05 -04:00
|
|
|
void ANR::SetANRGain (RXA& rxa, double gain)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.anr->two_mu = gain;
|
|
|
|
flush_anr (rxa.anr);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-17 20:08:05 -04:00
|
|
|
void ANR::SetANRLeakage (RXA& rxa, double leakage)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.anr->gamma = leakage;
|
|
|
|
flush_anr (rxa.anr);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ANR::SetANRPosition (RXA& rxa, int position)
|
|
|
|
{
|
2024-07-22 18:39:21 -04:00
|
|
|
rxa.anr->position = position;
|
|
|
|
rxa.bp1->position = position;
|
|
|
|
flush_anr (rxa.anr);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WDSP
|