2024-06-16 05:31:13 -04:00
|
|
|
/* amsq.c
|
|
|
|
|
|
|
|
This file is part of a program that implements a Software-Defined Radio.
|
|
|
|
|
|
|
|
Copyright (C) 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 "amsq.hpp"
|
|
|
|
#include "RXA.hpp"
|
|
|
|
#include "TXA.hpp"
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::compute_slews()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
int i;
|
2024-07-05 21:25:41 -04:00
|
|
|
double delta, theta;
|
2024-07-26 11:52:34 -04:00
|
|
|
delta = PI / (double)ntup;
|
2024-06-16 05:31:13 -04:00
|
|
|
theta = 0.0;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
for (i = 0; i <= ntup; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
cup[i] = muted_gain + (1.0 - muted_gain) * 0.5 * (1.0 - cos (theta));
|
2024-06-16 05:31:13 -04:00
|
|
|
theta += delta;
|
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
delta = PI / (double)ntdown;
|
2024-06-16 05:31:13 -04:00
|
|
|
theta = 0.0;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
for (i = 0; i <= ntdown; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
cdown[i] = muted_gain + (1.0 - muted_gain) * 0.5 * (1.0 + cos (theta));
|
2024-06-16 05:31:13 -04:00
|
|
|
theta += delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::calc()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
// signal averaging
|
2024-07-28 05:36:45 -04:00
|
|
|
trigsig.resize(size * 2);
|
2024-07-26 11:52:34 -04:00
|
|
|
avm = exp(-1.0 / (rate * avtau));
|
|
|
|
onem_avm = 1.0 - avm;
|
|
|
|
avsig = 0.0;
|
2024-06-16 05:31:13 -04:00
|
|
|
// level change
|
2024-07-26 11:52:34 -04:00
|
|
|
ntup = (int)(tup * rate);
|
|
|
|
ntdown = (int)(tdown * rate);
|
2024-07-28 05:36:45 -04:00
|
|
|
cup.resize((ntup + 1) * 2); // (float *)malloc0((ntup + 1) * sizeof(float));
|
|
|
|
cdown.resize((ntdown + 1) * 2); // (float *)malloc0((ntdown + 1) * sizeof(float));
|
2024-07-26 11:52:34 -04:00
|
|
|
compute_slews();
|
2024-06-16 05:31:13 -04:00
|
|
|
// control
|
2024-07-26 11:52:34 -04:00
|
|
|
state = 0;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
AMSQ::AMSQ (
|
|
|
|
int _run,
|
|
|
|
int _size,
|
|
|
|
float* _in,
|
|
|
|
float* _out,
|
|
|
|
float* _trigger,
|
|
|
|
int _rate,
|
|
|
|
double _avtau,
|
|
|
|
double _tup,
|
|
|
|
double _tdown,
|
|
|
|
double _tail_thresh,
|
|
|
|
double _unmute_thresh,
|
|
|
|
double _min_tail,
|
|
|
|
double _max_tail,
|
|
|
|
double _muted_gain
|
|
|
|
) :
|
|
|
|
run(_run),
|
|
|
|
size(_size),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
trigger(_trigger),
|
|
|
|
rate((double) _rate),
|
|
|
|
avtau(_avtau),
|
|
|
|
tup(_tup),
|
|
|
|
tdown(_tdown),
|
|
|
|
tail_thresh(_tail_thresh),
|
|
|
|
unmute_thresh(_unmute_thresh),
|
|
|
|
min_tail(_min_tail),
|
|
|
|
max_tail(_max_tail),
|
|
|
|
muted_gain(_muted_gain)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::flush()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-28 05:36:45 -04:00
|
|
|
std::fill(trigsig.begin(), trigsig.end(), 0);
|
2024-07-26 11:52:34 -04:00
|
|
|
avsig = 0.0;
|
|
|
|
state = 0;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum _amsqstate
|
|
|
|
{
|
|
|
|
MUTED,
|
|
|
|
INCREASE,
|
|
|
|
UNMUTED,
|
|
|
|
TAIL,
|
|
|
|
DECREASE
|
|
|
|
};
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::execute()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
if (run)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
int i;
|
2024-07-05 21:25:41 -04:00
|
|
|
double sig, siglimit;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
for (i = 0; i < size; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
double trigr = trigsig[2 * i + 0];
|
|
|
|
double trigi = trigsig[2 * i + 1];
|
|
|
|
sig = sqrt (trigr*trigr + trigi*trigi);
|
|
|
|
avsig = avm * avsig + onem_avm * sig;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
switch (state)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
case MUTED:
|
2024-07-26 11:52:34 -04:00
|
|
|
if (avsig > unmute_thresh)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
state = INCREASE;
|
|
|
|
count = ntup;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
out[2 * i + 0] = muted_gain * in[2 * i + 0];
|
|
|
|
out[2 * i + 1] = muted_gain * in[2 * i + 1];
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
break;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
case INCREASE:
|
2024-07-26 11:52:34 -04:00
|
|
|
out[2 * i + 0] = in[2 * i + 0] * cup[ntup - count];
|
|
|
|
out[2 * i + 1] = in[2 * i + 1] * cup[ntup - count];
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
if (count-- == 0)
|
|
|
|
state = UNMUTED;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
break;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
case UNMUTED:
|
2024-07-26 11:52:34 -04:00
|
|
|
if (avsig < tail_thresh)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
state = TAIL;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
if ((siglimit = avsig) > 1.0)
|
2024-07-17 20:08:05 -04:00
|
|
|
siglimit = 1.0;
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
count = (int)((min_tail + (max_tail - min_tail) * (1.0 - siglimit)) * rate);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
out[2 * i + 0] = in[2 * i + 0];
|
|
|
|
out[2 * i + 1] = in[2 * i + 1];
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
break;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
case TAIL:
|
2024-07-26 11:52:34 -04:00
|
|
|
out[2 * i + 0] = in[2 * i + 0];
|
|
|
|
out[2 * i + 1] = in[2 * i + 1];
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
if (avsig > unmute_thresh)
|
2024-07-17 20:08:05 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
state = UNMUTED;
|
2024-07-17 20:08:05 -04:00
|
|
|
}
|
2024-07-26 11:52:34 -04:00
|
|
|
else if (count-- == 0)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
state = DECREASE;
|
|
|
|
count = ntdown;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
break;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
case DECREASE:
|
2024-07-26 11:52:34 -04:00
|
|
|
out[2 * i + 0] = in[2 * i + 0] * cdown[ntdown - count];
|
|
|
|
out[2 * i + 1] = in[2 * i + 1] * cdown[ntdown - count];
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
if (count-- == 0)
|
|
|
|
state = MUTED;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-26 11:52:34 -04:00
|
|
|
else if (in != out)
|
2024-07-17 20:08:05 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
std::copy( in, in + size * 2, out);
|
2024-07-17 20:08:05 -04:00
|
|
|
}
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::xcap()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-28 05:36:45 -04:00
|
|
|
std::copy(trigger, trigger + size * 2, trigsig.begin());
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setBuffers(float* _in, float* _out, float* _trigger)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
in = _in;
|
|
|
|
out = _out;
|
|
|
|
trigger = _trigger;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setSamplerate(int _rate)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
rate = _rate;
|
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setSize(int _size)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
size = _size;
|
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* RXA Properties *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setRun(int _run)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
run = _run;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setThreshold(double _threshold)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
double thresh = pow (10.0, _threshold / 20.0);
|
|
|
|
tail_thresh = 0.9 * thresh;
|
|
|
|
unmute_thresh = thresh;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setMaxTail(double _tail)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 11:52:34 -04:00
|
|
|
if (_tail < min_tail)
|
|
|
|
_tail = min_tail;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
max_tail = _tail;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 11:52:34 -04:00
|
|
|
void AMSQ::setMutedGain(double dBlevel)
|
2024-06-16 05:31:13 -04:00
|
|
|
{ // dBlevel is negative
|
2024-07-26 11:52:34 -04:00
|
|
|
muted_gain = pow (10.0, dBlevel / 20.0);
|
|
|
|
compute_slews();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WDSP
|