1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-25 01:18:38 -05:00
sdrangel/wdsp/fmmod.cpp

228 lines
5.4 KiB
C++
Raw Normal View History

2024-06-16 05:31:13 -04:00
/* fmmod.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2013, 2016, 2023 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 <vector>
2024-06-16 05:31:13 -04:00
#include "comm.hpp"
#include "fircore.hpp"
2024-06-16 05:31:13 -04:00
#include "fir.hpp"
#include "fmmod.hpp"
#include "TXA.hpp"
namespace WDSP {
2024-08-07 15:14:09 -04:00
void FMMOD::calc()
2024-06-16 05:31:13 -04:00
{
// ctcss gen
2024-08-07 15:14:09 -04:00
tscale = 1.0 / (1.0 + ctcss_level);
tphase = 0.0;
tdelta = TWOPI * ctcss_freq / samplerate;
2024-06-16 05:31:13 -04:00
// mod
2024-08-07 15:14:09 -04:00
sphase = 0.0;
sdelta = TWOPI * deviation / samplerate;
2024-06-16 05:31:13 -04:00
// bandpass
2024-08-07 15:14:09 -04:00
bp_fc = deviation + f_high;
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
FMMOD::FMMOD(
int _run,
int _size,
float* _in,
float* _out,
int _rate,
double _dev,
double _f_low,
double _f_high,
int _ctcss_run,
double _ctcss_level,
double _ctcss_freq,
int _bp_run,
int _nc,
int _mp
2024-06-16 05:31:13 -04:00
)
{
std::vector<float> impulse;
2024-08-07 15:14:09 -04:00
run = _run;
size = _size;
in = _in;
out = _out;
samplerate = (float) _rate;
deviation = _dev;
f_low = _f_low;
f_high = _f_high;
ctcss_run = _ctcss_run;
ctcss_level = _ctcss_level;
ctcss_freq = _ctcss_freq;
bp_run = _bp_run;
nc = _nc;
mp = _mp;
calc();
FIR::fir_bandpass(impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p = new FIRCORE(size, out, out, mp, impulse);
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
FMMOD::~FMMOD()
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
delete p;
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::flush()
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
tphase = 0.0;
sphase = 0.0;
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::execute()
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
double dp;
double magdp;
double peak;
if (run)
2024-06-16 05:31:13 -04:00
{
peak = 0.0;
2024-08-07 15:14:09 -04:00
for (int i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
if (ctcss_run)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
tphase += tdelta;
if (tphase >= TWOPI) tphase -= TWOPI;
out[2 * i + 0] = (float) (tscale * (in[2 * i + 0] + ctcss_level * cos (tphase)));
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
dp = out[2 * i + 0] * sdelta;
sphase += dp;
if (sphase >= TWOPI) sphase -= TWOPI;
if (sphase < 0.0 ) sphase += TWOPI;
out[2 * i + 0] = (float) (0.7071 * cos (sphase));
out[2 * i + 1] = (float) (0.7071 * sin (sphase));
2024-06-16 05:31:13 -04:00
if ((magdp = dp) < 0.0) magdp = - magdp;
if (magdp > peak) peak = magdp;
}
2024-08-07 15:14:09 -04:00
if (bp_run)
p->execute();
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
else if (in != out)
std::copy( in, in + size * 2, out);
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::setBuffers(float* _in, float* _out)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
in = _in;
out = _out;
calc();
p->setBuffers(out, out);
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::setSamplerate(int _rate)
2024-06-16 05:31:13 -04:00
{
std::vector<float> impulse;
2024-08-07 15:14:09 -04:00
samplerate = _rate;
calc();
FIR::fir_bandpass(impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 1);
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::setSize(int _size)
2024-06-16 05:31:13 -04:00
{
std::vector<float> impulse;
2024-08-07 15:14:09 -04:00
size = _size;
calc();
p->setSize(size);
FIR::fir_bandpass(impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 1);
2024-06-16 05:31:13 -04:00
}
/********************************************************************************************************
* *
* TXA Properties *
* *
********************************************************************************************************/
2024-08-07 15:14:09 -04:00
void FMMOD::setDeviation(float _deviation)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
double _bp_fc = f_high + _deviation;
std::vector<float> impulse;
FIR::fir_bandpass (impulse, nc, -_bp_fc, +_bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 0);
2024-08-07 15:14:09 -04:00
deviation = _deviation;
2024-06-16 05:31:13 -04:00
// mod
2024-08-07 15:14:09 -04:00
sphase = 0.0;
sdelta = TWOPI * deviation / samplerate;
2024-06-16 05:31:13 -04:00
// bandpass
2024-08-07 15:14:09 -04:00
bp_fc = _bp_fc;
p->setUpdate();
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::setCTCSSFreq (float _freq)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
ctcss_freq = _freq;
tphase = 0.0;
tdelta = TWOPI * ctcss_freq / samplerate;
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::setCTCSSRun (int _run)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
ctcss_run = _run;
2024-06-16 05:31:13 -04:00
}
2024-08-07 15:14:09 -04:00
void FMMOD::setNC(int _nc)
2024-06-16 05:31:13 -04:00
{
std::vector<float> impulse;
2024-07-13 17:59:46 -04:00
2024-08-07 15:14:09 -04:00
if (nc != _nc)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
nc = _nc;
FIR::fir_bandpass (impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setNc(impulse);
2024-06-16 05:31:13 -04:00
}
}
2024-08-07 15:14:09 -04:00
void FMMOD::setMP(int _mp)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
if (mp != _mp)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
mp = _mp;
p->setMp(mp);
2024-06-16 05:31:13 -04:00
}
}
2024-08-07 15:14:09 -04:00
void FMMOD::setAFFreqs(float _low, float _high)
2024-06-16 05:31:13 -04:00
{
std::vector<float> impulse;
2024-07-13 17:59:46 -04:00
2024-08-07 15:14:09 -04:00
if (f_low != _low || f_high != _high)
2024-06-16 05:31:13 -04:00
{
2024-08-07 15:14:09 -04:00
f_low = _low;
f_high = _high;
bp_fc = deviation + f_high;
FIR::fir_bandpass (impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 1);
2024-06-16 05:31:13 -04:00
}
}
} // namespace WDSP