1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-11 02:46:12 -05:00
sdrangel/wdsp/meter.cpp

158 lines
3.7 KiB
C++
Raw Normal View History

2024-06-16 05:31:13 -04:00
/* meter.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 "meterlog10.hpp"
#include "meter.hpp"
namespace WDSP {
2024-07-24 09:32:21 -04:00
void METER::calc()
2024-06-16 05:31:13 -04:00
{
2024-07-24 09:32:21 -04:00
mult_average = exp(-1.0 / (rate * tau_average));
mult_peak = exp(-1.0 / (rate * tau_peak_decay));
flush();
2024-06-16 05:31:13 -04:00
}
2024-07-24 09:32:21 -04:00
METER::METER(
int _run,
int* _prun,
int _size,
float* _buff,
int _rate,
double _tau_av,
double _tau_decay,
double* _result,
int _enum_av,
int _enum_pk,
int _enum_gain,
double* _pgain
2024-06-16 05:31:13 -04:00
)
{
2024-07-24 09:32:21 -04:00
run = _run;
prun = _prun;
size = _size;
buff = _buff;
rate = (double) _rate;
tau_average = _tau_av;
tau_peak_decay = _tau_decay;
result = _result;
enum_av = _enum_av;
enum_pk = _enum_pk;
enum_gain = _enum_gain;
pgain = _pgain;
calc();
2024-06-16 05:31:13 -04:00
}
2024-07-24 09:32:21 -04:00
void METER::flush()
2024-06-16 05:31:13 -04:00
{
2024-07-24 09:32:21 -04:00
avg = 0.0;
peak = 0.0;
result[enum_av] = -100.0;
result[enum_pk] = -100.0;
2024-07-24 09:32:21 -04:00
if ((pgain != 0) && (enum_gain >= 0))
result[enum_gain] = 0.0;
2024-06-16 05:31:13 -04:00
}
2024-07-24 09:32:21 -04:00
void METER::execute()
2024-06-16 05:31:13 -04:00
{
int srun;
2024-07-13 17:59:46 -04:00
2024-07-24 09:32:21 -04:00
if (prun != 0)
srun = *(prun);
2024-06-16 05:31:13 -04:00
else
srun = 1;
2024-07-13 17:59:46 -04:00
2024-07-24 09:32:21 -04:00
if (run && srun)
2024-06-16 05:31:13 -04:00
{
int i;
double smag;
double np = 0.0;
2024-07-13 17:59:46 -04:00
2024-07-24 09:32:21 -04:00
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
2024-07-24 09:32:21 -04:00
double xr = buff[2 * i + 0];
double xi = buff[2 * i + 1];
2024-07-16 18:39:28 -04:00
smag = xr*xr + xi*xi;
2024-07-24 09:32:21 -04:00
avg = avg * mult_average + (1.0 - mult_average) * smag;
peak *= mult_peak;
if (smag > np)
np = smag;
2024-06-16 05:31:13 -04:00
}
2024-07-13 17:59:46 -04:00
2024-07-24 09:32:21 -04:00
if (np > peak)
peak = np;
2024-07-13 17:59:46 -04:00
2024-07-24 09:32:21 -04:00
result[enum_av] = 10.0 * MemLog::mlog10 (avg + 1.0e-40);
result[enum_pk] = 10.0 * MemLog::mlog10 (peak + 1.0e-40);
2024-07-13 17:59:46 -04:00
2024-07-24 09:32:21 -04:00
if ((pgain != 0) && (enum_gain >= 0))
result[enum_gain] = 20.0 * MemLog::mlog10 (*pgain + 1.0e-40);
2024-06-16 05:31:13 -04:00
}
else
{
2024-07-24 09:32:21 -04:00
if (enum_av >= 0)
result[enum_av] = -100.0;
if (enum_pk >= 0)
result[enum_pk] = -100.0;
if (enum_gain >= 0)
result[enum_gain] = 0.0;
2024-06-16 05:31:13 -04:00
}
}
2024-07-24 09:32:21 -04:00
void METER::setBuffers(float* in)
2024-06-16 05:31:13 -04:00
{
2024-07-24 09:32:21 -04:00
buff = in;
2024-06-16 05:31:13 -04:00
}
2024-07-24 09:32:21 -04:00
void METER::setSamplerate(int _rate)
2024-06-16 05:31:13 -04:00
{
2024-07-24 09:32:21 -04:00
rate = (double) _rate;
calc();
2024-06-16 05:31:13 -04:00
}
2024-07-24 09:32:21 -04:00
void METER::setSize(int _size)
2024-06-24 04:20:14 -04:00
{
2024-07-24 09:32:21 -04:00
size = _size;
flush();
2024-06-24 04:20:14 -04:00
}
2024-06-16 05:31:13 -04:00
/********************************************************************************************************
* *
2024-07-24 09:32:21 -04:00
* Public Properties *
2024-06-16 05:31:13 -04:00
* *
********************************************************************************************************/
2024-07-24 09:32:21 -04:00
double METER::getMeter(int mt)
2024-06-24 04:20:14 -04:00
{
2024-07-24 09:32:21 -04:00
return result[mt];
2024-06-24 04:20:14 -04:00
}
2024-06-16 05:31:13 -04:00
} // namespace WDSP