1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-14 04:11:48 -05:00
sdrangel/wdsp/gen.cpp

600 lines
16 KiB
C++
Raw Normal View History

2024-06-16 05:31:13 -04:00
/* gen.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-07-13 17:59:46 -04:00
#include <chrono>
2024-06-16 05:31:13 -04:00
#include "comm.hpp"
#include "gen.hpp"
#include "TXA.hpp"
namespace WDSP {
void GEN::calc_tone()
2024-06-16 05:31:13 -04:00
{
tone.phs = 0.0;
tone.delta = TWOPI * tone.freq / rate;
tone.cosdelta = cos (tone.delta);
tone.sindelta = sin (tone.delta);
2024-06-16 05:31:13 -04:00
}
void GEN::calc_tt()
2024-06-16 05:31:13 -04:00
{
tt.phs1 = 0.0;
tt.phs2 = 0.0;
tt.delta1 = TWOPI * tt.f1 / rate;
tt.delta2 = TWOPI * tt.f2 / rate;
tt.cosdelta1 = cos (tt.delta1);
tt.cosdelta2 = cos (tt.delta2);
tt.sindelta1 = sin (tt.delta1);
tt.sindelta2 = sin (tt.delta2);
2024-06-16 05:31:13 -04:00
}
void GEN::calc_sweep()
2024-06-16 05:31:13 -04:00
{
sweep.phs = 0.0;
sweep.dphs = TWOPI * sweep.f1 / rate;
sweep.d2phs = TWOPI * sweep.sweeprate / (rate * rate);
sweep.dphsmax = TWOPI * sweep.f2 / rate;
2024-06-16 05:31:13 -04:00
}
void GEN::calc_sawtooth()
2024-06-16 05:31:13 -04:00
{
saw.period = 1.0 / saw.f;
saw.delta = 1.0 / rate;
saw.t = 0.0;
2024-06-16 05:31:13 -04:00
}
void GEN::calc_triangle()
2024-06-16 05:31:13 -04:00
{
tri.period = 1.0 / tri.f;
tri.half = 0.5 * tri.period;
tri.delta = 1.0 / rate;
tri.t = 0.0;
tri.t1 = 0.0;
2024-06-16 05:31:13 -04:00
}
void GEN::calc_pulse ()
2024-06-16 05:31:13 -04:00
{
int i;
2024-06-24 21:50:48 -04:00
float delta, theta;
pulse.pperiod = 1.0 / pulse.pf;
pulse.tphs = 0.0;
pulse.tdelta = TWOPI * pulse.tf / rate;
pulse.tcosdelta = cos (pulse.tdelta);
pulse.tsindelta = sin (pulse.tdelta);
pulse.pntrans = (int)(pulse.ptranstime * rate);
pulse.pnon = (int)(pulse.pdutycycle * pulse.pperiod * rate);
pulse.pnoff = (int)(pulse.pperiod * rate) - pulse.pnon - 2 * pulse.pntrans;
if (pulse.pnoff < 0)
pulse.pnoff = 0;
pulse.pcount = pulse.pnoff;
pulse.state = 0;
pulse.ctrans = new float[pulse.pntrans + 1]; // (float *) malloc0 ((pulse.pntrans + 1) * sizeof (float));
delta = PI / (float)pulse.pntrans;
2024-06-16 05:31:13 -04:00
theta = 0.0;
for (i = 0; i <= pulse.pntrans; i++)
2024-06-16 05:31:13 -04:00
{
pulse.ctrans[i] = 0.5 * (1.0 - cos (theta));
2024-06-16 05:31:13 -04:00
theta += delta;
}
}
void GEN::calc()
2024-06-16 05:31:13 -04:00
{
calc_tone();
calc_tt();
calc_sweep();
calc_sawtooth();
calc_triangle();
calc_pulse();
2024-06-16 05:31:13 -04:00
}
void GEN::decalc()
2024-06-16 05:31:13 -04:00
{
delete[] (pulse.ctrans);
2024-06-16 05:31:13 -04:00
}
GEN::GEN(
int _run,
int _size,
float* _in,
float* _out,
int _rate,
int _mode
) :
run(_run),
size(_size),
in(_in),
out(_out),
rate((double) _rate),
mode(_mode)
2024-06-16 05:31:13 -04:00
{
// tone
tone.mag = 1.0;
tone.freq = 1000.0;
2024-06-16 05:31:13 -04:00
// two-tone
tt.mag1 = 0.5;
tt.mag2 = 0.5;
tt.f1 = + 900.0;
tt.f2 = + 1700.0;
2024-06-16 05:31:13 -04:00
// noise
2024-07-13 17:59:46 -04:00
srand ((unsigned int) time (0));
noise.mag = 1.0;
2024-06-16 05:31:13 -04:00
// sweep
sweep.mag = 1.0;
sweep.f1 = -20000.0;
sweep.f2 = +20000.0;
sweep.sweeprate = +4000.0;
2024-06-16 05:31:13 -04:00
// sawtooth
saw.mag = 1.0;
saw.f = 500.0;
2024-06-16 05:31:13 -04:00
// triangle
tri.mag = 1.0;
tri.f = 500.0;
2024-06-16 05:31:13 -04:00
// pulse
pulse.mag = 1.0;
pulse.pf = 0.25;
pulse.pdutycycle = 0.25;
pulse.ptranstime = 0.002;
pulse.tf = 1000.0;
calc();
2024-06-16 05:31:13 -04:00
}
GEN::~GEN()
2024-06-16 05:31:13 -04:00
{
decalc();
2024-06-16 05:31:13 -04:00
}
void GEN::flush()
2024-06-16 05:31:13 -04:00
{
pulse.state = 0;
2024-06-16 05:31:13 -04:00
}
enum pstate
{
OFF,
UP,
ON,
DOWN
};
void GEN::execute()
2024-06-16 05:31:13 -04:00
{
if (run)
2024-06-16 05:31:13 -04:00
{
switch (mode)
2024-06-16 05:31:13 -04:00
{
case 0: // tone
{
int i;
2024-06-24 21:50:48 -04:00
float t1, t2;
float cosphase = cos (tone.phs);
float sinphase = sin (tone.phs);
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
out[2 * i + 0] = + tone.mag * cosphase;
out[2 * i + 1] = - tone.mag * sinphase;
2024-06-16 05:31:13 -04:00
t1 = cosphase;
t2 = sinphase;
cosphase = t1 * tone.cosdelta - t2 * tone.sindelta;
sinphase = t1 * tone.sindelta + t2 * tone.cosdelta;
tone.phs += tone.delta;
if (tone.phs >= TWOPI) tone.phs -= TWOPI;
if (tone.phs < 0.0 ) tone.phs += TWOPI;
2024-06-16 05:31:13 -04:00
}
break;
}
case 1: // two-tone
{
int i;
2024-06-24 21:50:48 -04:00
float tcos, tsin;
float cosphs1 = cos (tt.phs1);
float sinphs1 = sin (tt.phs1);
float cosphs2 = cos (tt.phs2);
float sinphs2 = sin (tt.phs2);
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
out[2 * i + 0] = + tt.mag1 * cosphs1 + tt.mag2 * cosphs2;
out[2 * i + 1] = - tt.mag1 * sinphs1 - tt.mag2 * sinphs2;
2024-06-16 05:31:13 -04:00
tcos = cosphs1;
tsin = sinphs1;
cosphs1 = tcos * tt.cosdelta1 - tsin * tt.sindelta1;
sinphs1 = tcos * tt.sindelta1 + tsin * tt.cosdelta1;
tt.phs1 += tt.delta1;
if (tt.phs1 >= TWOPI) tt.phs1 -= TWOPI;
if (tt.phs1 < 0.0 ) tt.phs1 += TWOPI;
2024-06-16 05:31:13 -04:00
tcos = cosphs2;
tsin = sinphs2;
cosphs2 = tcos * tt.cosdelta2 - tsin * tt.sindelta2;
sinphs2 = tcos * tt.sindelta2 + tsin * tt.cosdelta2;
tt.phs2 += tt.delta2;
if (tt.phs2 >= TWOPI) tt.phs2 -= TWOPI;
if (tt.phs2 < 0.0 ) tt.phs2 += TWOPI;
2024-06-16 05:31:13 -04:00
}
break;
}
case 2: // noise
{
int i;
2024-06-24 21:50:48 -04:00
float r1, r2, c, rad;
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
do
{
2024-06-24 21:50:48 -04:00
r1 = 2.0 * (float)rand() / (float)RAND_MAX - 1.0;
r2 = 2.0 * (float)rand() / (float)RAND_MAX - 1.0;
2024-06-16 05:31:13 -04:00
c = r1 * r1 + r2 * r2;
} while (c >= 1.0);
rad = sqrt (-2.0 * log (c) / c);
out[2 * i + 0] = noise.mag * rad * r1;
out[2 * i + 1] = noise.mag * rad * r2;
2024-06-16 05:31:13 -04:00
}
break;
}
case 3: // sweep
{
int i;
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
out[2 * i + 0] = + sweep.mag * cos(sweep.phs);
out[2 * i + 1] = - sweep.mag * sin(sweep.phs);
sweep.phs += sweep.dphs;
sweep.dphs += sweep.d2phs;
if (sweep.phs >= TWOPI) sweep.phs -= TWOPI;
if (sweep.phs < 0.0 ) sweep.phs += TWOPI;
if (sweep.dphs > sweep.dphsmax)
sweep.dphs = TWOPI * sweep.f1 / rate;
2024-06-16 05:31:13 -04:00
}
break;
}
case 4: // sawtooth (audio only)
{
int i;
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
if (saw.t > saw.period) saw.t -= saw.period;
out[2 * i + 0] = saw.mag * (saw.t * saw.f - 1.0);
out[2 * i + 1] = 0.0;
saw.t += saw.delta;
2024-06-16 05:31:13 -04:00
}
}
break;
case 5: // triangle (audio only)
{
int i;
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
if (tri.t > tri.period) tri.t1 = tri.t -= tri.period;
if (tri.t > tri.half) tri.t1 -= tri.delta;
else tri.t1 += tri.delta;
out[2 * i + 0] = tri.mag * (4.0 * tri.t1 * tri.f - 1.0);
out[2 * i + 1] = 0.0;
tri.t += tri.delta;
2024-06-16 05:31:13 -04:00
}
}
break;
case 6: // pulse (audio only)
{
int i;
2024-06-24 21:50:48 -04:00
float t1, t2;
float cosphase = cos (pulse.tphs);
float sinphase = sin (pulse.tphs);
for (i = 0; i < size; i++)
2024-06-16 05:31:13 -04:00
{
if (pulse.pnoff != 0)
switch (pulse.state)
2024-06-16 05:31:13 -04:00
{
case OFF:
out[2 * i + 0] = 0.0;
if (--pulse.pcount == 0)
2024-06-16 05:31:13 -04:00
{
pulse.state = UP;
pulse.pcount = pulse.pntrans;
2024-06-16 05:31:13 -04:00
}
break;
case UP:
out[2 * i + 0] = pulse.mag * cosphase * pulse.ctrans[pulse.pntrans - pulse.pcount];
if (--pulse.pcount == 0)
2024-06-16 05:31:13 -04:00
{
pulse.state = ON;
pulse.pcount = pulse.pnon;
2024-06-16 05:31:13 -04:00
}
break;
case ON:
out[2 * i + 0] = pulse.mag * cosphase;
if (--pulse.pcount == 0)
2024-06-16 05:31:13 -04:00
{
pulse.state = DOWN;
pulse.pcount = pulse.pntrans;
2024-06-16 05:31:13 -04:00
}
break;
case DOWN:
out[2 * i + 0] = pulse.mag * cosphase * pulse.ctrans[pulse.pcount];
if (--pulse.pcount == 0)
2024-06-16 05:31:13 -04:00
{
pulse.state = OFF;
pulse.pcount = pulse.pnoff;
2024-06-16 05:31:13 -04:00
}
break;
}
else
out[2 * i + 0] = 0.0;
out[2 * i + 1] = 0.0;
2024-06-16 05:31:13 -04:00
t1 = cosphase;
t2 = sinphase;
cosphase = t1 * pulse.tcosdelta - t2 * pulse.tsindelta;
sinphase = t1 * pulse.tsindelta + t2 * pulse.tcosdelta;
pulse.tphs += pulse.tdelta;
if (pulse.tphs >= TWOPI) pulse.tphs -= TWOPI;
if (pulse.tphs < 0.0 ) pulse.tphs += TWOPI;
2024-06-16 05:31:13 -04:00
}
}
break;
default: // silence
{
std::fill(out, out + size * 2, 0);
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 GEN::setBuffers(float* _in, float* _out)
2024-06-16 05:31:13 -04:00
{
in = _in;
out = _out;
2024-06-16 05:31:13 -04:00
}
void GEN::setSamplerate(int _rate)
2024-06-16 05:31:13 -04:00
{
decalc();
rate = _rate;
calc();
2024-06-16 05:31:13 -04:00
}
void GEN::setSize(int _size)
2024-06-16 05:31:13 -04:00
{
size = _size;
flush();
2024-06-16 05:31:13 -04:00
}
/********************************************************************************************************
* *
* Public Properties *
2024-06-16 05:31:13 -04:00
* *
********************************************************************************************************/
// 'PreGen', gen0
void GEN::SetPreRun(int _run)
2024-06-16 05:31:13 -04:00
{
run = _run;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreMode(int _mode)
2024-06-16 05:31:13 -04:00
{
mode = _mode;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreToneMag(float mag)
2024-06-16 05:31:13 -04:00
{
tone.mag = mag;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreToneFreq(float freq)
2024-06-16 05:31:13 -04:00
{
tone.freq = freq;
calc_tone();
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreNoiseMag (float mag)
2024-06-16 05:31:13 -04:00
{
noise.mag = mag;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreSweepMag(float mag)
2024-06-16 05:31:13 -04:00
{
sweep.mag = mag;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreSweepFreq(float freq1, float freq2)
2024-06-16 05:31:13 -04:00
{
sweep.f1 = freq1;
sweep.f2 = freq2;
calc_sweep();
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreSweepRate(float rate)
2024-06-16 05:31:13 -04:00
{
sweep.sweeprate = rate;
calc_sweep();
2024-06-16 05:31:13 -04:00
}
/********************************************************************************************************
* *
* TXA Properties *
* *
********************************************************************************************************/
// 'PreGen', gen0
void GEN::SetPreGenRun (TXA& txa, int run)
{
txa.gen0->run = run;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPreGenMode (TXA& txa, int mode)
{
txa.gen0->mode = mode;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenToneMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen0->tone.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenToneFreq (TXA& txa, float freq)
2024-06-16 05:31:13 -04:00
{
txa.gen0->tone.freq = freq;
txa.gen0->calc_tone();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenNoiseMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen0->noise.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenSweepMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen0->sweep.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenSweepFreq (TXA& txa, float freq1, float freq2)
2024-06-16 05:31:13 -04:00
{
txa.gen0->sweep.f1 = freq1;
txa.gen0->sweep.f2 = freq2;
txa.gen0->calc_sweep();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenSweepRate (TXA& txa, float rate)
2024-06-16 05:31:13 -04:00
{
txa.gen0->sweep.sweeprate = rate;
txa.gen0->calc_sweep();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenSawtoothMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen0->saw.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenSawtoothFreq (TXA& txa, float freq)
2024-06-16 05:31:13 -04:00
{
txa.gen0->saw.f = freq;
txa.gen0->calc_sawtooth();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenTriangleMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen0->tri.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenTriangleFreq (TXA& txa, float freq)
2024-06-16 05:31:13 -04:00
{
txa.gen0->tri.f = freq;
txa.gen0->calc_triangle();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenPulseMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen0->pulse.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenPulseFreq (TXA& txa, float freq)
2024-06-16 05:31:13 -04:00
{
txa.gen0->pulse.pf = freq;
txa.gen0->calc_pulse();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenPulseDutyCycle (TXA& txa, float dc)
2024-06-16 05:31:13 -04:00
{
txa.gen0->pulse.pdutycycle = dc;
txa.gen0->calc_pulse();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenPulseToneFreq (TXA& txa, float freq)
2024-06-16 05:31:13 -04:00
{
txa.gen0->pulse.tf = freq;
txa.gen0->calc_pulse();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPreGenPulseTransition (TXA& txa, float transtime)
2024-06-16 05:31:13 -04:00
{
txa.gen0->pulse.ptranstime = transtime;
txa.gen0->calc_pulse();
2024-06-16 05:31:13 -04:00
}
// 'PostGen', gen1
void GEN::SetPostGenRun (TXA& txa, int run)
{
txa.gen1->run = run;
2024-06-16 05:31:13 -04:00
}
void GEN::SetPostGenMode (TXA& txa, int mode)
{
txa.gen1->mode = mode;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenToneMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen1->tone.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenToneFreq (TXA& txa, float freq)
2024-06-16 05:31:13 -04:00
{
txa.gen1->tone.freq = freq;
txa.gen1->calc_tone();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenTTMag (TXA& txa, float mag1, float mag2)
2024-06-16 05:31:13 -04:00
{
txa.gen1->tt.mag1 = mag1;
txa.gen1->tt.mag2 = mag2;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenTTFreq (TXA& txa, float freq1, float freq2)
2024-06-16 05:31:13 -04:00
{
txa.gen1->tt.f1 = freq1;
txa.gen1->tt.f2 = freq2;
txa.gen1->calc_tt();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenSweepMag (TXA& txa, float mag)
2024-06-16 05:31:13 -04:00
{
txa.gen1->sweep.mag = mag;
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenSweepFreq (TXA& txa, float freq1, float freq2)
2024-06-16 05:31:13 -04:00
{
txa.gen1->sweep.f1 = freq1;
txa.gen1->sweep.f2 = freq2;
txa.gen1->calc_sweep();
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void GEN::SetPostGenSweepRate (TXA& txa, float rate)
2024-06-16 05:31:13 -04:00
{
txa.gen1->sweep.sweeprate = rate;
txa.gen1->calc_sweep();
2024-06-16 05:31:13 -04:00
}
} // namespace WDSP