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-07-17 20:08:05 -04:00
|
|
|
double delta, theta;
|
2024-06-24 21:50:48 -04:00
|
|
|
float* impulse;
|
2024-06-16 05:31:13 -04:00
|
|
|
int i;
|
|
|
|
// noise filter
|
2024-07-26 13:10:53 -04:00
|
|
|
noise = new float[2 * size * 2]; // (float *)malloc0(2 * size * sizeof(complex));
|
|
|
|
F[0] = 0.0;
|
|
|
|
F[1] = fc;
|
|
|
|
F[2] = *pllpole;
|
|
|
|
F[3] = 20000.0;
|
|
|
|
G[0] = 0.0;
|
|
|
|
G[1] = 0.0;
|
|
|
|
G[2] = 3.0;
|
|
|
|
G[3] = +20.0 * log10(20000.0 / *pllpole);
|
|
|
|
impulse = EQP::eq_impulse (nc, 3, F, G, rate, 1.0 / (2.0 * size), 0, 0);
|
|
|
|
p = FIRCORE::create_fircore (size, trigger, noise, nc, mp, impulse);
|
2024-06-16 05:31:13 -04:00
|
|
|
delete[] (impulse);
|
|
|
|
// 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);
|
|
|
|
cup = new double[ntup + 1]; // (float *)malloc0 ((ntup + 1) * sizeof(float));
|
|
|
|
cdown = new double[ntdown + 1]; //(float *)malloc0 ((ntdown + 1) * sizeof(float));
|
|
|
|
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-07-26 13:10:53 -04:00
|
|
|
state = 0;
|
|
|
|
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-07-26 13:10:53 -04:00
|
|
|
delete[] (cdown);
|
|
|
|
delete[] (cup);
|
|
|
|
FIRCORE::destroy_fircore (p);
|
|
|
|
delete[] (noise);
|
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-07-26 13:10:53 -04:00
|
|
|
FIRCORE::flush_fircore (p);
|
|
|
|
avnoise = 100.0;
|
|
|
|
longnoise = 1.0;
|
|
|
|
state = 0;
|
|
|
|
ready = 0;
|
|
|
|
ramp = 0.0;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
enum _fmsqstate
|
|
|
|
{
|
|
|
|
MUTED,
|
|
|
|
INCREASE,
|
|
|
|
UNMUTED,
|
|
|
|
TAIL,
|
|
|
|
DECREASE
|
|
|
|
};
|
|
|
|
|
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
|
|
|
{
|
|
|
|
int i;
|
2024-07-26 13:10:53 -04:00
|
|
|
double _noise, lnlimit;
|
|
|
|
FIRCORE::xfircore (p);
|
2024-07-17 20:08:05 -04:00
|
|
|
|
2024-07-26 13:10:53 -04:00
|
|
|
for (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
|
|
|
{
|
|
|
|
case MUTED:
|
2024-07-26 13:10:53 -04:00
|
|
|
if (avnoise < unmute_thresh && ready)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -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 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-06-16 05:31:13 -04:00
|
|
|
case INCREASE:
|
2024-07-26 13:10:53 -04:00
|
|
|
outsig[2 * i + 0] = insig[2 * i + 0] * cup[ntup - count];
|
|
|
|
outsig[2 * i + 1] = 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)
|
|
|
|
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 13:10:53 -04:00
|
|
|
if (avnoise > tail_thresh)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-26 13:10:53 -04:00
|
|
|
state = 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-06-16 05:31:13 -04:00
|
|
|
case 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-07-26 13:10:53 -04:00
|
|
|
state = 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-07-26 13:10:53 -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 13:10:53 -04:00
|
|
|
outsig[2 * i + 0] = insig[2 * i + 0] * cdown[ntdown - count];
|
|
|
|
outsig[2 * i + 1] = 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)
|
|
|
|
state = 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;
|
|
|
|
FIRCORE::setBuffers_fircore (p, trigger, noise);
|
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-06-24 21:50:48 -04:00
|
|
|
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;
|
|
|
|
impulse = EQP::eq_impulse (nc, 3, F, G, rate, 1.0 / (2.0 * size), 0, 0);
|
|
|
|
FIRCORE::setNc_fircore (p, nc, impulse);
|
2024-06-16 05:31:13 -04:00
|
|
|
delete[] (impulse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
FIRCORE::setMp_fircore (p, mp);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WDSP
|