1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 16:08:39 -05:00
sdrangel/wdsp/amsq.cpp

259 lines
6.2 KiB
C++
Raw Normal View History

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
*/
2024-08-03 05:05:12 -04:00
#include <cstdio>
2024-06-16 05:31:13 -04:00
#include "comm.hpp"
#include "amsq.hpp"
namespace WDSP {
void AMSQ::compute_slews()
2024-06-16 05:31:13 -04:00
{
2024-08-02 02:01:46 -04:00
double delta;
double theta;
delta = PI / (double)ntup;
2024-06-16 05:31:13 -04:00
theta = 0.0;
2024-08-02 02:01:46 -04:00
for (int i = 0; i <= ntup; i++)
2024-06-16 05:31:13 -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;
}
delta = PI / (double)ntdown;
2024-06-16 05:31:13 -04:00
theta = 0.0;
2024-08-02 02:01:46 -04:00
for (int i = 0; i <= ntdown; i++)
2024-06-16 05:31:13 -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;
}
}
void AMSQ::calc()
2024-06-16 05:31:13 -04:00
{
// signal averaging
trigsig.resize(size * 2);
avm = exp(-1.0 / (rate * avtau));
onem_avm = 1.0 - avm;
avsig = 0.0;
2024-06-16 05:31:13 -04:00
// level change
ntup = (int)(tup * rate);
ntdown = (int)(tdown * rate);
2024-08-02 02:01:46 -04:00
cup.resize((ntup + 1) * 2);
cdown.resize((ntdown + 1) * 2);
compute_slews();
2024-06-16 05:31:13 -04:00
// control
2024-08-02 02:01:46 -04:00
state = AMSQState::MUTED;
2024-06-16 05:31:13 -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
{
calc();
2024-06-16 05:31:13 -04:00
}
void AMSQ::flush()
2024-06-16 05:31:13 -04:00
{
std::fill(trigsig.begin(), trigsig.end(), 0);
avsig = 0.0;
2024-08-02 02:01:46 -04:00
state = AMSQState::MUTED;
2024-06-16 05:31:13 -04:00
}
void AMSQ::execute()
2024-06-16 05:31:13 -04:00
{
if (run)
2024-06-16 05:31:13 -04:00
{
2024-08-02 02:01:46 -04:00
double sig;
double siglimit;
2024-08-02 02:01:46 -04:00
for (int i = 0; i < size; i++)
2024-06-16 05:31:13 -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;
switch (state)
2024-06-16 05:31:13 -04:00
{
2024-08-02 02:01:46 -04:00
case AMSQState::MUTED:
if (avsig > unmute_thresh)
2024-06-16 05:31:13 -04:00
{
2024-08-02 02:01:46 -04:00
state = AMSQState::INCREASE;
count = ntup;
2024-06-16 05:31:13 -04:00
}
2024-08-02 02:01:46 -04:00
out[2 * i + 0] = (float) (muted_gain * in[2 * i + 0]);
out[2 * i + 1] = (float) (muted_gain * in[2 * i + 1]);
2024-06-16 05:31:13 -04:00
break;
2024-08-02 02:01:46 -04:00
case AMSQState::INCREASE:
out[2 * i + 0] = (float) (in[2 * i + 0] * cup[ntup - count]);
out[2 * i + 1] = (float) (in[2 * i + 1] * cup[ntup - count]);
if (count-- == 0)
2024-08-02 02:01:46 -04:00
state = AMSQState::UNMUTED;
2024-06-16 05:31:13 -04:00
break;
2024-08-02 02:01:46 -04:00
case AMSQState::UNMUTED:
if (avsig < tail_thresh)
2024-06-16 05:31:13 -04:00
{
2024-08-02 02:01:46 -04:00
state = AMSQState::TAIL;
if ((siglimit = avsig) > 1.0)
siglimit = 1.0;
count = (int)((min_tail + (max_tail - min_tail) * (1.0 - siglimit)) * rate);
2024-06-16 05:31:13 -04:00
}
out[2 * i + 0] = in[2 * i + 0];
out[2 * i + 1] = in[2 * i + 1];
2024-06-16 05:31:13 -04:00
break;
2024-08-02 02:01:46 -04:00
case AMSQState::TAIL:
out[2 * i + 0] = in[2 * i + 0];
out[2 * i + 1] = in[2 * i + 1];
if (avsig > unmute_thresh)
{
2024-08-02 02:01:46 -04:00
state = AMSQState::UNMUTED;
}
else if (count-- == 0)
2024-06-16 05:31:13 -04:00
{
2024-08-03 05:05:12 -04:00
state = AMSQState::DECREASE;
count = ntdown;
2024-06-16 05:31:13 -04:00
}
2024-06-16 05:31:13 -04:00
break;
2024-08-02 02:01:46 -04:00
case AMSQState::DECREASE:
out[2 * i + 0] = (float) (in[2 * i + 0] * cdown[ntdown - count]);
out[2 * i + 1] = (float) (in[2 * i + 1] * cdown[ntdown - count]);
if (count-- == 0)
2024-08-02 02:01:46 -04:00
state = AMSQState::MUTED;
2024-06-16 05:31:13 -04:00
break;
}
}
}
else if (in != out)
{
std::copy( in, in + size * 2, out);
}
2024-06-16 05:31:13 -04:00
}
void AMSQ::xcap()
2024-06-16 05:31:13 -04:00
{
std::copy(trigger, trigger + size * 2, trigsig.begin());
2024-06-16 05:31:13 -04:00
}
void AMSQ::setBuffers(float* _in, float* _out, float* _trigger)
2024-06-16 05:31:13 -04:00
{
in = _in;
out = _out;
trigger = _trigger;
2024-06-16 05:31:13 -04:00
}
void AMSQ::setSamplerate(int _rate)
2024-06-16 05:31:13 -04:00
{
rate = _rate;
calc();
2024-06-16 05:31:13 -04:00
}
void AMSQ::setSize(int _size)
2024-06-16 05:31:13 -04:00
{
size = _size;
calc();
2024-06-16 05:31:13 -04:00
}
/********************************************************************************************************
* *
* RXA Properties *
* *
********************************************************************************************************/
void AMSQ::setRun(int _run)
2024-06-16 05:31:13 -04:00
{
run = _run;
2024-06-16 05:31:13 -04:00
}
void AMSQ::setThreshold(double _threshold)
2024-06-16 05:31:13 -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
}
void AMSQ::setMaxTail(double _tail)
2024-06-16 05:31:13 -04:00
{
if (_tail < min_tail)
_tail = min_tail;
max_tail = _tail;
2024-06-16 05:31:13 -04:00
}
void AMSQ::setMutedGain(double dBlevel)
2024-06-16 05:31:13 -04:00
{ // dBlevel is negative
muted_gain = pow (10.0, dBlevel / 20.0);
compute_slews();
2024-06-16 05:31:13 -04:00
}
} // namespace WDSP