2024-06-29 18:03:13 +00: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"
|
|
|
|
|
2024-07-21 09:24:54 +00: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 18:03:13 +00:00
|
|
|
#define MAX_SAMPLERATE (1536000)
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::initBlanker()
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
|
|
|
int i;
|
2024-07-23 06:06:13 +00: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);
|
|
|
|
|
2024-07-27 11:45:09 +00:00
|
|
|
std::fill(dline.begin(), dline.end(), 0);
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
ANB::ANB (
|
|
|
|
int _run,
|
|
|
|
int _buffsize,
|
|
|
|
float* _in,
|
|
|
|
float* _out,
|
|
|
|
double _samplerate,
|
|
|
|
double _tau,
|
|
|
|
double _hangtime,
|
|
|
|
double _advtime,
|
|
|
|
double _backtau,
|
|
|
|
double _threshold
|
2024-07-24 02:52:00 +00:00
|
|
|
) :
|
|
|
|
run(_run),
|
|
|
|
buffsize(_buffsize),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
samplerate(_samplerate),
|
|
|
|
tau(_tau),
|
|
|
|
hangtime(_hangtime),
|
|
|
|
advtime(_advtime),
|
|
|
|
backtau(_backtau),
|
2024-07-27 11:45:09 +00:00
|
|
|
threshold(_threshold),
|
|
|
|
dtime(0),
|
|
|
|
htime(0),
|
|
|
|
itime(0),
|
|
|
|
atime(0)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-27 11:45:09 +00:00
|
|
|
tau = tau < 0.0 ? 0.0 : (tau > MAX_TAU ? MAX_TAU : tau);
|
|
|
|
hangtime = hangtime < 0.0 ? 0.0 : (hangtime > MAX_ADVTIME ? MAX_ADVTIME : hangtime);
|
|
|
|
advtime = advtime < 0.0 ? 0.0 : (advtime > MAX_ADVTIME ? MAX_ADVTIME : advtime);
|
|
|
|
samplerate = samplerate < 0.0 ? 0.0 : (samplerate > MAX_SAMPLERATE ? MAX_SAMPLERATE : samplerate);
|
|
|
|
wave.resize((int)(MAX_SAMPLERATE * MAX_TAU) + 1);
|
2024-07-23 06:06:13 +00:00
|
|
|
dline_size = (int)((MAX_TAU + MAX_ADVTIME) * MAX_SAMPLERATE) + 1;
|
2024-07-27 11:45:09 +00:00
|
|
|
dline.resize(dline_size * 2);
|
2024-07-23 06:06:13 +00:00
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-27 04:01:39 +00:00
|
|
|
void ANB::flush()
|
|
|
|
{
|
|
|
|
initBlanker();
|
|
|
|
}
|
|
|
|
|
2024-07-23 17:39:09 +00:00
|
|
|
void ANB::execute()
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
|
|
|
double scale;
|
|
|
|
double mag;
|
|
|
|
int i;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (run)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
for (i = 0; i < buffsize; i++)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
double xr = in[2 * i + 0];
|
|
|
|
double xi = in[2 * i + 1];
|
2024-07-17 19:19:57 +00:00
|
|
|
mag = sqrt(xr*xr + xi*xi);
|
2024-07-23 06:06:13 +00: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 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (mag > (avg * threshold))
|
|
|
|
count = trans_count + adv_count;
|
2024-06-29 18:03:13 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
switch (state)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
|
|
|
case 0:
|
2024-07-23 06:06:13 +00:00
|
|
|
out[2 * i + 0] = dline[2 * out_idx + 0];
|
|
|
|
out[2 * i + 1] = dline[2 * out_idx + 1];
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (count > 0)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
state = 1;
|
|
|
|
dtime = 0;
|
|
|
|
power = 1.0;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
break;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
case 1:
|
2024-07-23 06:06:13 +00: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 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (++dtime > trans_count)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
state = 2;
|
|
|
|
atime = 0;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
break;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
case 2:
|
2024-07-23 06:06:13 +00:00
|
|
|
out[2 * i + 0] = 0.0;
|
|
|
|
out[2 * i + 1] = 0.0;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (++atime > adv_count)
|
|
|
|
state = 3;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
break;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
case 3:
|
2024-07-23 06:06:13 +00:00
|
|
|
if (count > 0)
|
|
|
|
htime = -count;
|
2024-06-29 18:03:13 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
out[2 * i + 0] = 0.0;
|
|
|
|
out[2 * i + 1] = 0.0;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (++htime > hang_count)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
state = 4;
|
|
|
|
itime = 0;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
break;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-06-29 18:03:13 +00:00
|
|
|
case 4:
|
2024-07-23 06:06:13 +00: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 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (count > 0)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
state = 1;
|
|
|
|
dtime = 0;
|
|
|
|
power = scale;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
2024-07-23 06:06:13 +00:00
|
|
|
else if (++itime > trans_count)
|
2024-07-17 19:19:57 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
state = 0;
|
2024-07-17 19:19:57 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 21:12:10 +00:00
|
|
|
break;
|
|
|
|
default:
|
2024-06-29 18:03:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (count > 0)
|
|
|
|
count--;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (++in_idx == dline_size)
|
|
|
|
in_idx = 0;
|
2024-07-17 19:19:57 +00:00
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
if (++out_idx == dline_size)
|
|
|
|
out_idx = 0;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
}
|
2024-07-23 06:06:13 +00:00
|
|
|
else if (in != out)
|
2024-07-17 19:19:57 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
std::copy(in, in + buffsize * 2, out);
|
2024-07-17 19:19:57 +00:00
|
|
|
}
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 21:12:10 +00:00
|
|
|
void ANB::setBuffers(float* _in, float* _out)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 21:12:10 +00:00
|
|
|
in = _in;
|
|
|
|
out = _out;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setSize(int size)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
buffsize = size;
|
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
2024-07-23 06:06:13 +00:00
|
|
|
* Common interface *
|
2024-06-29 18:03:13 +00:00
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setRun (int _run)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
run = _run;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setBuffsize (int size)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
buffsize = size;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setSamplerate (int rate)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
samplerate = (double) rate;
|
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setTau (double _tau)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
tau = _tau;
|
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setHangtime (double time)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
hangtime = time;
|
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setAdvtime (double time)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
advtime = time;
|
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 21:12:10 +00:00
|
|
|
void ANB::setBacktau (double _tau)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 21:12:10 +00:00
|
|
|
backtau = _tau;
|
2024-07-23 06:06:13 +00:00
|
|
|
initBlanker();
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:06:13 +00:00
|
|
|
void ANB::setThreshold (double thresh)
|
2024-06-29 18:03:13 +00:00
|
|
|
{
|
2024-07-23 06:06:13 +00:00
|
|
|
threshold = thresh;
|
2024-06-29 18:03:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|