2024-06-29 14:03:13 -04:00
|
|
|
/* anb.h
|
|
|
|
|
|
|
|
This file is part of a program that implements a Software-Defined Radio.
|
|
|
|
|
|
|
|
Copyright (C) 2013, 2014 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 "anb.hpp"
|
|
|
|
#include "RXA.hpp"
|
|
|
|
|
2024-07-21 05:24:54 -04:00
|
|
|
#define MAX_TAU (0.01) // maximum transition time, signal<->zero (slew time)
|
|
|
|
#define MAX_ADVTIME (0.01) // maximum deadtime (zero output) in advance of detected noise
|
2024-06-29 14:03:13 -04:00
|
|
|
#define MAX_SAMPLERATE (1536000)
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::initBlanker()
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
|
|
|
int i;
|
2024-07-23 02:06:13 -04:00
|
|
|
trans_count = (int)(tau * samplerate);
|
|
|
|
|
|
|
|
if (trans_count < 2)
|
|
|
|
trans_count = 2;
|
|
|
|
|
|
|
|
hang_count = (int)(hangtime * samplerate);
|
|
|
|
adv_count = (int)(advtime * samplerate);
|
|
|
|
count = 0;
|
|
|
|
in_idx = trans_count + adv_count;
|
|
|
|
out_idx = 0;
|
|
|
|
coef = PI / trans_count;
|
|
|
|
state = 0;
|
|
|
|
avg = 1.0;
|
|
|
|
power = 1.0;
|
|
|
|
backmult = exp(-1.0 / (samplerate * backtau));
|
|
|
|
ombackmult = 1.0 - backmult;
|
|
|
|
|
|
|
|
for (i = 0; i <= trans_count; i++)
|
|
|
|
wave[i] = 0.5 * cos(i * coef);
|
|
|
|
|
|
|
|
std::fill(dline, dline + dline_size * 2, 0);
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
ANB::ANB (
|
|
|
|
int _run,
|
|
|
|
int _buffsize,
|
|
|
|
float* _in,
|
|
|
|
float* _out,
|
|
|
|
double _samplerate,
|
|
|
|
double _tau,
|
|
|
|
double _hangtime,
|
|
|
|
double _advtime,
|
|
|
|
double _backtau,
|
|
|
|
double _threshold
|
2024-06-29 14:03:13 -04:00
|
|
|
)
|
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
run = _run;
|
|
|
|
buffsize = _buffsize;
|
|
|
|
in = _in;
|
|
|
|
out = _out;
|
|
|
|
samplerate = _samplerate;
|
|
|
|
tau = _tau;
|
|
|
|
hangtime = _hangtime;
|
|
|
|
advtime = _advtime;
|
|
|
|
backtau = _backtau;
|
|
|
|
threshold = _threshold;
|
|
|
|
wave = new double[((int)(MAX_SAMPLERATE * MAX_TAU) + 1)];
|
|
|
|
dline_size = (int)((MAX_TAU + MAX_ADVTIME) * MAX_SAMPLERATE) + 1;
|
|
|
|
dline = new float[dline_size * 2];
|
|
|
|
initBlanker();
|
|
|
|
legacy = new float[2048 * 2]; /////////////// legacy interface - remove
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
ANB::~ANB()
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
delete[] legacy; /////////////// legacy interface - remove
|
|
|
|
delete[] dline;
|
|
|
|
delete[] wave;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 13:39:09 -04:00
|
|
|
void ANB::execute()
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
|
|
|
double scale;
|
|
|
|
double mag;
|
|
|
|
int i;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (run)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
for (i = 0; i < buffsize; i++)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
double xr = in[2 * i + 0];
|
|
|
|
double xi = in[2 * i + 1];
|
2024-07-17 15:19:57 -04:00
|
|
|
mag = sqrt(xr*xr + xi*xi);
|
2024-07-23 02:06:13 -04:00
|
|
|
avg = backmult * avg + ombackmult * mag;
|
|
|
|
dline[2 * in_idx + 0] = in[2 * i + 0];
|
|
|
|
dline[2 * in_idx + 1] = in[2 * i + 1];
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (mag > (avg * threshold))
|
|
|
|
count = trans_count + adv_count;
|
2024-06-29 14:03:13 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
switch (state)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
|
|
|
case 0:
|
2024-07-23 02:06:13 -04:00
|
|
|
out[2 * i + 0] = dline[2 * out_idx + 0];
|
|
|
|
out[2 * i + 1] = dline[2 * out_idx + 1];
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (count > 0)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
state = 1;
|
|
|
|
dtime = 0;
|
|
|
|
power = 1.0;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
break;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
case 1:
|
2024-07-23 02:06:13 -04:00
|
|
|
scale = power * (0.5 + wave[dtime]);
|
|
|
|
out[2 * i + 0] = dline[2 * out_idx + 0] * scale;
|
|
|
|
out[2 * i + 1] = dline[2 * out_idx + 1] * scale;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (++dtime > trans_count)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
state = 2;
|
|
|
|
atime = 0;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
break;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
case 2:
|
2024-07-23 02:06:13 -04:00
|
|
|
out[2 * i + 0] = 0.0;
|
|
|
|
out[2 * i + 1] = 0.0;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (++atime > adv_count)
|
|
|
|
state = 3;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
break;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
case 3:
|
2024-07-23 02:06:13 -04:00
|
|
|
if (count > 0)
|
|
|
|
htime = -count;
|
2024-06-29 14:03:13 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
out[2 * i + 0] = 0.0;
|
|
|
|
out[2 * i + 1] = 0.0;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (++htime > hang_count)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
state = 4;
|
|
|
|
itime = 0;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
break;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-06-29 14:03:13 -04:00
|
|
|
case 4:
|
2024-07-23 02:06:13 -04:00
|
|
|
scale = 0.5 - wave[itime];
|
|
|
|
out[2 * i + 0] = dline[2 * out_idx + 0] * scale;
|
|
|
|
out[2 * i + 1] = dline[2 * out_idx + 1] * scale;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (count > 0)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
state = 1;
|
|
|
|
dtime = 0;
|
|
|
|
power = scale;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
2024-07-23 02:06:13 -04:00
|
|
|
else if (++itime > trans_count)
|
2024-07-17 15:19:57 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
state = 0;
|
2024-07-17 15:19:57 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:12:10 -04:00
|
|
|
break;
|
|
|
|
default:
|
2024-06-29 14:03:13 -04:00
|
|
|
break;
|
|
|
|
}
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (count > 0)
|
|
|
|
count--;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (++in_idx == dline_size)
|
|
|
|
in_idx = 0;
|
2024-07-17 15:19:57 -04:00
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
if (++out_idx == dline_size)
|
|
|
|
out_idx = 0;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
}
|
2024-07-23 02:06:13 -04:00
|
|
|
else if (in != out)
|
2024-07-17 15:19:57 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
std::copy(in, in + buffsize * 2, out);
|
2024-07-17 15:19:57 -04:00
|
|
|
}
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:12:10 -04:00
|
|
|
void ANB::setBuffers(float* _in, float* _out)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 17:12:10 -04:00
|
|
|
in = _in;
|
|
|
|
out = _out;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setSize(int size)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
buffsize = size;
|
|
|
|
initBlanker();
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
2024-07-23 02:06:13 -04:00
|
|
|
* Common interface *
|
2024-06-29 14:03:13 -04:00
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setRun (int _run)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
run = _run;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setBuffsize (int size)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
buffsize = size;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setSamplerate (int rate)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
samplerate = (double) rate;
|
|
|
|
initBlanker();
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setTau (double _tau)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
tau = _tau;
|
|
|
|
initBlanker();
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setHangtime (double time)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
hangtime = time;
|
|
|
|
initBlanker();
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setAdvtime (double time)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
advtime = time;
|
|
|
|
initBlanker();
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 17:12:10 -04:00
|
|
|
void ANB::setBacktau (double _tau)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 17:12:10 -04:00
|
|
|
backtau = _tau;
|
2024-07-23 02:06:13 -04:00
|
|
|
initBlanker();
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 02:06:13 -04:00
|
|
|
void ANB::setThreshold (double thresh)
|
2024-06-29 14:03:13 -04:00
|
|
|
{
|
2024-07-23 02:06:13 -04:00
|
|
|
threshold = thresh;
|
2024-06-29 14:03:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|