2024-06-16 05:31:13 -04:00
|
|
|
/* fmsq.c
|
|
|
|
|
|
|
|
This file is part of a program that implements a Software-Defined Radio.
|
|
|
|
|
|
|
|
Copyright (C) 2013, 2016 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"
|
2024-07-12 21:20:26 -04:00
|
|
|
#include "fircore.hpp"
|
2024-07-26 23:32:45 -04:00
|
|
|
#include "eqp.hpp"
|
2024-06-16 05:31:13 -04:00
|
|
|
#include "fmsq.hpp"
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::calc()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
double delta;
|
|
|
|
double theta;
|
2024-08-08 03:09:40 -04:00
|
|
|
std::vector<float> impulse;
|
2024-06-16 05:31:13 -04:00
|
|
|
int i;
|
|
|
|
// noise filter
|
2024-08-03 05:05:12 -04:00
|
|
|
noise.resize(2 * size * 2);
|
2024-07-26 13:10:53 -04:00
|
|
|
F[0] = 0.0;
|
2024-08-03 05:05:12 -04:00
|
|
|
F[1] = (float) fc;
|
|
|
|
F[2] = (float) *pllpole;
|
2024-07-26 13:10:53 -04:00
|
|
|
F[3] = 20000.0;
|
|
|
|
G[0] = 0.0;
|
|
|
|
G[1] = 0.0;
|
|
|
|
G[2] = 3.0;
|
2024-08-03 05:05:12 -04:00
|
|
|
G[3] = (float) (+20.0 * log10(20000.0 / *pllpole));
|
2024-08-08 03:09:40 -04:00
|
|
|
EQP::eq_impulse (impulse, nc, 3, F.data(), G.data(), rate, 1.0 / (2.0 * size), 0, 0);
|
|
|
|
p = new FIRCORE(size, trigger, noise.data(), nc, mp, impulse.data());
|
2024-06-16 05:31:13 -04:00
|
|
|
// noise averaging
|
2024-07-26 13:10:53 -04:00
|
|
|
avm = exp(-1.0 / (rate * avtau));
|
|
|
|
onem_avm = 1.0 - avm;
|
|
|
|
avnoise = 100.0;
|
|
|
|
longavm = exp(-1.0 / (rate * longtau));
|
|
|
|
onem_longavm = 1.0 - longavm;
|
|
|
|
longnoise = 1.0;
|
2024-06-16 05:31:13 -04:00
|
|
|
// level change
|
2024-07-26 13:10:53 -04:00
|
|
|
ntup = (int)(tup * rate);
|
|
|
|
ntdown = (int)(tdown * rate);
|
2024-08-03 05:05:12 -04:00
|
|
|
cup.resize(ntup + 1);
|
|
|
|
cdown.resize(ntdown + 1);
|
2024-07-26 13:10:53 -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 13:10:53 -04:00
|
|
|
for (i = 0; i <= ntup; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
cup[i] = 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 13:10:53 -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 13:10:53 -04:00
|
|
|
for (i = 0; i <= ntdown; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
cdown[i] = 0.5 * (1 + cos(theta));
|
2024-06-16 05:31:13 -04:00
|
|
|
theta += delta;
|
|
|
|
}
|
|
|
|
// control
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::MUTED;
|
2024-07-26 13:10:53 -04:00
|
|
|
ready = 0;
|
|
|
|
ramp = 0.0;
|
|
|
|
rstep = 1.0 / rate;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::decalc()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-06 00:26:53 -04:00
|
|
|
delete (p);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
FMSQ::FMSQ(
|
|
|
|
int _run,
|
|
|
|
int _size,
|
|
|
|
float* _insig,
|
|
|
|
float* _outsig,
|
|
|
|
float* _trigger,
|
|
|
|
int _rate,
|
|
|
|
double _fc,
|
|
|
|
double* _pllpole,
|
|
|
|
double _tdelay,
|
|
|
|
double _avtau,
|
|
|
|
double _longtau,
|
|
|
|
double _tup,
|
|
|
|
double _tdown,
|
|
|
|
double _tail_thresh,
|
|
|
|
double _unmute_thresh,
|
|
|
|
double _min_tail,
|
|
|
|
double _max_tail,
|
|
|
|
int _nc,
|
|
|
|
int _mp
|
|
|
|
) :
|
|
|
|
run(_run),
|
|
|
|
size(_size),
|
|
|
|
insig(_insig),
|
|
|
|
outsig(_outsig),
|
|
|
|
trigger(_trigger),
|
|
|
|
rate((double) _rate),
|
|
|
|
fc(_fc),
|
|
|
|
pllpole(_pllpole),
|
|
|
|
avtau(_avtau),
|
|
|
|
longtau(_longtau),
|
|
|
|
tup(_tup),
|
|
|
|
tdown(_tdown),
|
|
|
|
tail_thresh(_tail_thresh),
|
|
|
|
unmute_thresh(_unmute_thresh),
|
|
|
|
min_tail(_min_tail),
|
|
|
|
max_tail(_max_tail),
|
2024-07-26 23:32:45 -04:00
|
|
|
tdelay(_tdelay),
|
2024-07-26 13:10:53 -04:00
|
|
|
nc(_nc),
|
|
|
|
mp(_mp)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
FMSQ::~FMSQ()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
decalc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::flush()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-06 00:26:53 -04:00
|
|
|
p->flush();
|
2024-07-26 13:10:53 -04:00
|
|
|
avnoise = 100.0;
|
|
|
|
longnoise = 1.0;
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::MUTED;
|
2024-07-26 13:10:53 -04:00
|
|
|
ready = 0;
|
|
|
|
ramp = 0.0;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::execute()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
if (run)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
double _noise;
|
|
|
|
double lnlimit;
|
2024-08-06 00:26:53 -04:00
|
|
|
p->execute();
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-08-03 05:05:12 -04:00
|
|
|
for (int i = 0; i < size; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
double noise0 = noise[2 * i + 0];
|
|
|
|
double noise1 = noise[2 * i + 1];
|
|
|
|
_noise = sqrt(noise0 * noise0 + noise1 * noise1);
|
|
|
|
avnoise = avm * avnoise + onem_avm * _noise;
|
|
|
|
longnoise = longavm * longnoise + onem_longavm * _noise;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if (!ready)
|
|
|
|
ramp += rstep;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if (ramp >= tdelay)
|
|
|
|
ready = 1;
|
2024-06-16 05:31:13 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
switch (state)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
case FMSQState::MUTED:
|
2024-07-26 13:10:53 -04:00
|
|
|
if (avnoise < unmute_thresh && ready)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::INCREASE;
|
2024-07-26 13:10:53 -04:00
|
|
|
count = ntup;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
outsig[2 * i + 0] = 0.0;
|
|
|
|
outsig[2 * i + 1] = 0.0;
|
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-08-03 05:05:12 -04:00
|
|
|
case FMSQState::INCREASE:
|
|
|
|
outsig[2 * i + 0] = (float) (insig[2 * i + 0] * cup[ntup - count]);
|
|
|
|
outsig[2 * i + 1] = (float) (insig[2 * i + 1] * cup[ntup - count]);
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if (count-- == 0)
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::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-08-03 05:05:12 -04:00
|
|
|
case FMSQState::UNMUTED:
|
2024-07-26 13:10:53 -04:00
|
|
|
if (avnoise > tail_thresh)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::TAIL;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if ((lnlimit = longnoise) > 1.0)
|
2024-07-17 20:08:05 -04:00
|
|
|
lnlimit = 1.0;
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
count = (int)((min_tail + (max_tail - min_tail) * lnlimit) * rate);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
outsig[2 * i + 0] = insig[2 * i + 0];
|
|
|
|
outsig[2 * i + 1] = insig[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-08-03 05:05:12 -04:00
|
|
|
case FMSQState::TAIL:
|
2024-07-26 13:10:53 -04:00
|
|
|
outsig[2 * i + 0] = insig[2 * i + 0];
|
|
|
|
outsig[2 * i + 1] = insig[2 * i + 1];
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if (avnoise < unmute_thresh)
|
2024-07-17 20:08:05 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::UNMUTED;
|
2024-07-17 20:08:05 -04:00
|
|
|
}
|
2024-07-26 13:10:53 -04:00
|
|
|
else if (count-- == 0)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::DECREASE;
|
2024-07-26 13:10:53 -04:00
|
|
|
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-08-03 05:05:12 -04:00
|
|
|
case FMSQState::DECREASE:
|
|
|
|
outsig[2 * i + 0] = (float) (insig[2 * i + 0] * cdown[ntdown - count]);
|
|
|
|
outsig[2 * i + 1] = (float) (insig[2 * i + 1] * cdown[ntdown - count]);
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if (count-- == 0)
|
2024-08-03 05:05:12 -04:00
|
|
|
state = FMSQState::MUTED;
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-26 13:10:53 -04:00
|
|
|
else if (insig != outsig)
|
2024-07-17 20:08:05 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
std::copy(insig, insig + size * 2, outsig);
|
2024-07-17 20:08:05 -04:00
|
|
|
}
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setBuffers(float* in, float* out, float* trig)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
insig = in;
|
|
|
|
outsig = out;
|
|
|
|
trigger = trig;
|
2024-08-06 00:26:53 -04:00
|
|
|
p->setBuffers(trigger, noise.data());
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setSamplerate(int _rate)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
decalc();
|
|
|
|
rate = _rate;
|
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setSize(int _size)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
decalc();
|
|
|
|
size = _size;
|
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* RXA Properties *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setRun(int _run)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
run = _run;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setThreshold(double threshold)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
tail_thresh = threshold;
|
|
|
|
unmute_thresh = 0.9 * threshold;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setNC(int _nc)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-08-08 03:09:40 -04:00
|
|
|
std::vector<float> impulse;
|
2024-07-13 17:59:46 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
if (nc != _nc)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
nc = _nc;
|
2024-08-08 03:09:40 -04:00
|
|
|
EQP::eq_impulse (impulse, nc, 3, F.data(), G.data(), rate, 1.0 / (2.0 * size), 0, 0);
|
|
|
|
p->setNc(nc, impulse.data());
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
void FMSQ::setMP(int _mp)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
if (mp != _mp)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
mp = _mp;
|
2024-08-06 00:26:53 -04:00
|
|
|
p->setMp(mp);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WDSP
|