1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-22 08:04:49 -05:00

Compare commits

..

1 Commits

Author SHA1 Message Date
Edouard Griffiths
ed6d7a6980
Merge 130d40c218 into fcd43df711 2024-08-07 19:14:51 +00:00
41 changed files with 885 additions and 914 deletions

View File

@ -279,9 +279,6 @@ QByteArray WDSPRxSettings::serialize() const
s.writeDouble(163 + 100*i, m_profiles[i].m_ssqlTauMute);
s.writeDouble(164 + 100*i, m_profiles[i].m_ssqlTauUnmute);
s.writeDouble(165 + 100*i, m_profiles[i].m_amsqMaxTail);
// RIT
s.writeBool( 183 + 100*i, m_profiles[i].m_rit);
s.writeDouble(184 + 100*i, m_profiles[i].m_ritFrequency);
// Equalizer
s.writeBool( 190 + 100*i, m_profiles[i].m_equalizer);
s.writeFloat(4100 + 100*i, m_profiles[i].m_eqF[0]);
@ -406,15 +403,15 @@ bool WDSPRxSettings::deserialize(const QByteArray& data)
d.readU32( 74, &utmp, 0);
if ((utmp > 1023) && (utmp < 65535)) {
m_reverseAPIPort = (uint16_t) utmp;
m_reverseAPIPort = utmp;
} else {
m_reverseAPIPort = 8888;
}
d.readU32( 75, &utmp, 0);
m_reverseAPIDeviceIndex = utmp > 99 ? 99 : (uint16_t) utmp;
m_reverseAPIDeviceIndex = utmp > 99 ? 99 : utmp;
d.readU32( 76, &utmp, 0);
m_reverseAPIChannelIndex = utmp > 99 ? 99 : (uint16_t) utmp;
m_reverseAPIChannelIndex = utmp > 99 ? 99 : utmp;
d.readS32( 77, &m_streamIndex, 0);
if (m_rollupState)
@ -467,9 +464,9 @@ bool WDSPRxSettings::deserialize(const QByteArray& data)
// Filter
d.readS32 (100 + 100*i, &m_profiles[i].m_spanLog2, 3);
d.readS32 (101 + 100*i, &tmp, 30);
m_profiles[i].m_highCutoff = (float) tmp * 100.0f;
m_profiles[i].m_highCutoff = tmp * 100.0;
d.readS32 (102 + 100*i, &tmp, 3);
m_profiles[i].m_lowCutoff = (float) tmp * 100.0f;
m_profiles[i].m_lowCutoff = tmp * 100.0;
d.readS32 (103 + 100*i, &m_profiles[i].m_fftWindow, 0);
// AGC
d.readBool( 110 + 100*i, &m_profiles[i].m_agc, true);

View File

@ -1006,6 +1006,7 @@ void RXA::updateNBPFilters()
{
a->calc_impulse();
a->fircore->setImpulse(a->impulse, 1);
delete[] (a->impulse);
}
if (b->bpsnba->fnfrun)
{
@ -1097,6 +1098,7 @@ void RXA::nbpSetNotchesRun(int _run)
bpsnbaCheck(mode, _run);
b->calc_impulse(); // recalc nbp impulse response
b->fircore->setImpulse(b->impulse, 0); // calculate new filter masks
delete[] (b->impulse);
bpsnbaSet();
b->fircore->setUpdate(); // apply new filter masks
}
@ -1114,6 +1116,7 @@ void RXA::nbpSetWindow(int _wintype)
a->wintype = _wintype;
a->calc_impulse();
a->fircore->setImpulse(a->impulse, 1);
delete[] (a->impulse);
}
if (b->wintype != _wintype)
@ -1135,6 +1138,7 @@ void RXA::nbpSetAutoIncrease(int _autoincr)
a->autoincr = _autoincr;
a->calc_impulse();
a->fircore->setImpulse(a->impulse, 1);
delete[] (a->impulse);
}
if (b->autoincr != _autoincr)

View File

@ -915,9 +915,7 @@ void TXA::setBandpassNC(int _nc)
if (a->nc != _nc)
{
a->nc = _nc;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
a->nc,
a->f_low,
a->f_high,
@ -926,7 +924,8 @@ void TXA::setBandpassNC(int _nc)
1,
a->gain / (double)(2 * a->size)
);
a->fircore->setNc(impulse);
a->fircore->setNc(a->nc, impulse);
delete[] impulse;
}
a = bp1;
@ -934,9 +933,7 @@ void TXA::setBandpassNC(int _nc)
if (a->nc != _nc)
{
a->nc = _nc;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
a->nc,
a->f_low,
a->f_high,
@ -945,7 +942,8 @@ void TXA::setBandpassNC(int _nc)
1,
a->gain / (double)(2 * a->size)
);
a->fircore->setNc(impulse);
a->fircore->setNc(a->nc, impulse);
delete[] impulse;
}
a = bp2;
@ -953,9 +951,7 @@ void TXA::setBandpassNC(int _nc)
if (a->nc != _nc)
{
a->nc = _nc;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
a->nc,
a->f_low,
a->f_high,
@ -964,7 +960,8 @@ void TXA::setBandpassNC(int _nc)
1,
a->gain / (double)(2 * a->size)
);
a->fircore->setNc(impulse);
a->fircore->setNc(a->nc, impulse);
delete[] impulse;
}
}
@ -1035,7 +1032,7 @@ void TXA::SetBPSRun (TXA& txa, int _run)
void TXA::SetBPSFreqs (TXA& txa, double _f_low, double _f_high)
{
std::vector<float> impulse;
float* impulse;
BPS *a;
a = txa.bps0;
@ -1043,8 +1040,10 @@ void TXA::SetBPSFreqs (TXA& txa, double _f_low, double _f_high)
{
a->f_low = _f_low;
a->f_high = _f_high;
FIR::fir_bandpass(impulse, a->size + 1, _f_low, _f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
FIR::fftcv_mults (a->mults, 2 * a->size, impulse.data());
delete[] (a->mults);
impulse = FIR::fir_bandpass(a->size + 1, _f_low, _f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
a = txa.bps1;
@ -1053,8 +1052,10 @@ void TXA::SetBPSFreqs (TXA& txa, double _f_low, double _f_high)
{
a->f_low = _f_low;
a->f_high = _f_high;
FIR::fir_bandpass(impulse, a->size + 1, _f_low, _f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
FIR::fftcv_mults (a->mults, 2 * a->size, impulse.data());
delete[] (a->mults);
impulse = FIR::fir_bandpass(a->size + 1, _f_low, _f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
a = txa.bps2;
@ -1063,22 +1064,26 @@ void TXA::SetBPSFreqs (TXA& txa, double _f_low, double _f_high)
{
a->f_low = _f_low;
a->f_high = _f_high;
FIR::fir_bandpass(impulse, a->size + 1, _f_low, _f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
FIR::fftcv_mults (a->mults, 2 * a->size, impulse.data());
delete[] (a->mults);
impulse = FIR::fir_bandpass(a->size + 1, _f_low, _f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
}
void TXA::SetBPSWindow (TXA& txa, int _wintype)
{
std::vector<float> impulse;
float* impulse;
BPS *a;
a = txa.bps0;
if (a->wintype != _wintype)
{
a->wintype = _wintype;
FIR::fir_bandpass(impulse, a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
FIR::fftcv_mults (a->mults, 2 * a->size, impulse.data());
delete[] (a->mults);
impulse = FIR::fir_bandpass(a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
a = txa.bps1;
@ -1086,8 +1091,10 @@ void TXA::SetBPSWindow (TXA& txa, int _wintype)
if (a->wintype != _wintype)
{
a->wintype = _wintype;
FIR::fir_bandpass(impulse, a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
FIR::fftcv_mults (a->mults, 2 * a->size, impulse.data());
delete[] (a->mults);
impulse = FIR::fir_bandpass(a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] impulse;
}
a = txa.bps2;
@ -1095,8 +1102,10 @@ void TXA::SetBPSWindow (TXA& txa, int _wintype)
if (a->wintype != _wintype)
{
a->wintype = _wintype;
FIR::fir_bandpass (impulse, a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
FIR::fftcv_mults (a->mults, 2 * a->size, impulse.data());
delete[] (a->mults);
impulse = FIR::fir_bandpass (a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] impulse;
}
}

View File

@ -66,9 +66,7 @@ BANDPASS::BANDPASS(
wintype(_wintype),
gain(_gain)
{
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
f_low,
f_high,
@ -77,7 +75,8 @@ BANDPASS::BANDPASS(
1,
gain / (double)(2 * size)
);
fircore = new FIRCORE(size, in, out, mp, impulse);
fircore = new FIRCORE(size, in, out, nc, mp, impulse);
delete[] impulse;
}
BANDPASS::~BANDPASS()
@ -108,9 +107,7 @@ void BANDPASS::setBuffers(float* _in, float* _out)
void BANDPASS::setSamplerate(int _rate)
{
samplerate = _rate;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
f_low,
f_high,
@ -120,6 +117,7 @@ void BANDPASS::setSamplerate(int _rate)
gain / (double) (2 * size)
);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void BANDPASS::setSize(int _size)
@ -128,9 +126,7 @@ void BANDPASS::setSize(int _size)
size = _size;
fircore->setSize(size);
// recalc impulse because scale factor is a function of size
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
f_low,
f_high,
@ -140,14 +136,13 @@ void BANDPASS::setSize(int _size)
gain / (double) (2 * size)
);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void BANDPASS::setGain(double _gain, int _update)
{
gain = _gain;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
f_low,
f_high,
@ -157,6 +152,7 @@ void BANDPASS::setGain(double _gain, int _update)
gain / (double) (2 * size)
);
fircore->setImpulse(impulse, _update);
delete[] impulse;
}
void BANDPASS::calcBandpassFilter(double _f_low, double _f_high, double _gain)
@ -166,9 +162,7 @@ void BANDPASS::calcBandpassFilter(double _f_low, double _f_high, double _gain)
f_low = _f_low;
f_high = _f_high;
gain = _gain;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
f_low,
f_high,
@ -178,6 +172,7 @@ void BANDPASS::calcBandpassFilter(double _f_low, double _f_high, double _gain)
gain / (double)(2 * size)
);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
}
@ -191,9 +186,7 @@ void BANDPASS::setBandpassFreqs(double _f_low, double _f_high)
{
if ((_f_low != f_low) || (_f_high != f_high))
{
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
_f_low,
_f_high,
@ -204,6 +197,7 @@ void BANDPASS::setBandpassFreqs(double _f_low, double _f_high)
);
fircore->setImpulse(impulse, 0);
delete[] impulse;
f_low = _f_low;
f_high = _f_high;
fircore->setUpdate();
@ -216,9 +210,7 @@ void BANDPASS::SetBandpassNC(int _nc)
if (_nc != nc)
{
nc = _nc;
std::vector<float> impulse;
FIR::fir_bandpass (
impulse,
float* impulse = FIR::fir_bandpass (
nc,
f_low,
f_high,
@ -227,7 +219,8 @@ void BANDPASS::SetBandpassNC(int _nc)
1,
gain / (double)( 2 * size)
);
fircore->setNc(impulse);
fircore->setNc(nc, impulse);
delete[] impulse;
}
}

View File

@ -42,28 +42,21 @@ namespace WDSP {
void BPS::calc()
{
float* impulse;
infilt.resize(2 * size * 2);
product.resize(2 * size * 2);
std::vector<float> impulse;
FIR::fir_bandpass(
impulse,
size + 1,
f_low,
f_high,
samplerate,
wintype,
1,
1.0 / (float)(2 * size)
);
FIR::fftcv_mults(mults, 2 * size, impulse.data());
impulse = FIR::fir_bandpass(size + 1, f_low, f_high, samplerate, wintype, 1, 1.0 / (float)(2 * size));
mults = FIR::fftcv_mults(2 * size, impulse);
CFor = fftwf_plan_dft_1d(2 * size, (fftwf_complex *) infilt.data(), (fftwf_complex *) product.data(), FFTW_FORWARD, FFTW_PATIENT);
CRev = fftwf_plan_dft_1d(2 * size, (fftwf_complex *) product.data(), (fftwf_complex *) out, FFTW_BACKWARD, FFTW_PATIENT);
delete[]impulse;
}
void BPS::decalc()
{
fftwf_destroy_plan(CRev);
fftwf_destroy_plan(CFor);
delete[] mults;
}
BPS::BPS(

View File

@ -56,7 +56,7 @@ public:
double f_high;
std::vector<float> infilt;
std::vector<float> product;
std::vector<float> mults;
float* mults;
double samplerate;
int wintype;
double gain;

View File

@ -177,6 +177,7 @@ void BPSNBA::recalc_bpsnba_filter(int update)
b->autoincr = autoincr;
b->calc_impulse();
b->fircore->setImpulse(b->impulse, update);
delete[] (b->impulse);
}
/********************************************************************************************************

View File

@ -34,10 +34,11 @@ namespace WDSP {
void CFIR::calc()
{
std::vector<float> impulse;
float* impulse;
scale = 1.0 / (float)(2 * size);
cfir_impulse (impulse, nc, DD, R, Pairs, runrate, cicrate, cutoff, xtype, xbw, 1, scale, wintype);
p = new FIRCORE(size, in, out, mp, impulse);
impulse = cfir_impulse (nc, DD, R, Pairs, runrate, cicrate, cutoff, xtype, xbw, 1, scale, wintype);
p = new FIRCORE(size, in, out, nc, mp, impulse);
delete[] impulse;
}
void CFIR::decalc()
@ -141,8 +142,7 @@ void CFIR::setOutRate(int rate)
calc();
}
void CFIR::cfir_impulse (
std::vector<float>& impulse,
float* CFIR::cfir_impulse (
int _N,
int _DD,
int _R,
@ -175,6 +175,7 @@ void CFIR::cfir_impulse (
double ri;
double mag = 0;
double fn;
float* impulse;
std::vector<float> A(_N);
double ft = _cutoff / _cicrate; // normalized cutoff frequency
int u_samps = (_N + 1) / 2; // number of unique samples, OK for odd or even N
@ -253,8 +254,8 @@ void CFIR::cfir_impulse (
else
for (i = u_samps, j = 1; i < _N; i++, j++)
A[i] = A[u_samps - j];
impulse.resize(2 * _N);
FIR::fir_fsamp (impulse, _N, A.data(), _rtype, 1.0, _wintype);
impulse = FIR::fir_fsamp (_N, A.data(), _rtype, 1.0, _wintype);
return impulse;
}
/********************************************************************************************************

View File

@ -28,8 +28,6 @@ warren@wpratt.com
#ifndef wdsp_cfir_h
#define wdsp_cfir_h
#include <vector>
#include "export.h"
namespace WDSP {
@ -85,8 +83,7 @@ public:
void setSamplerate(int rate);
void setSize(int size);
void setOutRate(int rate);
static void cfir_impulse (
std::vector<float>& impulse,
static float* cfir_impulse (
int N,
int DD,
int R,

View File

@ -31,84 +31,81 @@ warren@wpratt.com
namespace WDSP {
DELAY::DELAY(
int _run,
int _size,
float* _in,
float* _out,
int _rate,
float _tdelta,
float _tdelay
)
DELAY* create_delay (int run, int size, float* in, float* out, int rate, float tdelta, float tdelay)
{
run = _run;
size = _size;
in = _in;
out = _out;
rate = _rate;
tdelta = _tdelta;
tdelay = _tdelay;
L = (int)(0.5 + 1.0 / (tdelta * (float)rate));
adelta = 1.0f / (float) (rate * L);
ft = 0.45f / (float)L;
ncoef = (int)(60.0 / ft);
ncoef = (ncoef / L + 1) * L;
cpp = ncoef / L;
phnum = (int)(0.5 + tdelay / adelta);
snum = phnum / L;
phnum %= L;
idx_in = 0;
adelay = adelta * (float) (snum * L + phnum);
FIR::fir_bandpass (h, ncoef,-ft, +ft, 1.0, 1, 0, (float)L);
rsize = cpp + (WSDEL - 1);
ring.resize(rsize * 2);
DELAY *a = new DELAY;
a->run = run;
a->size = size;
a->in = in;
a->out = out;
a->rate = rate;
a->tdelta = tdelta;
a->tdelay = tdelay;
a->L = (int)(0.5 + 1.0 / (a->tdelta * (float)a->rate));
a->adelta = 1.0 / (a->rate * a->L);
a->ft = 0.45 / (float)a->L;
a->ncoef = (int)(60.0 / a->ft);
a->ncoef = (a->ncoef / a->L + 1) * a->L;
a->cpp = a->ncoef / a->L;
a->phnum = (int)(0.5 + a->tdelay / a->adelta);
a->snum = a->phnum / a->L;
a->phnum %= a->L;
a->idx_in = 0;
a->adelay = a->adelta * (a->snum * a->L + a->phnum);
a->h = FIR::fir_bandpass (a->ncoef,-a->ft, +a->ft, 1.0, 1, 0, (float)a->L);
a->rsize = a->cpp + (WSDEL - 1);
a->ring = new float[a->rsize * 2]; // (float *) malloc0 (a->rsize * sizeof (complex));
return a;
}
void DELAY::flush()
void DELAY::destroy_delay (DELAY *a)
{
std::fill(ring.begin(), ring.end(), 0);
idx_in = 0;
delete[] (a->ring);
delete[] (a->h);
delete (a);
}
void DELAY::execute()
void DELAY::flush_delay (DELAY *a)
{
if (run)
{
int j;
int k;
int idx;
int n;
float Itmp;
float Qtmp;
std::fill(a->ring, a->ring + a->cpp * 2, 0);
a->idx_in = 0;
}
for (int i = 0; i < size; i++)
void DELAY::xdelay (DELAY *a)
{
if (a->run)
{
ring[2 * idx_in + 0] = in[2 * i + 0];
ring[2 * idx_in + 1] = in[2 * i + 1];
int i, j, k, idx, n;
float Itmp, Qtmp;
for (i = 0; i < a->size; i++)
{
a->ring[2 * a->idx_in + 0] = a->in[2 * i + 0];
a->ring[2 * a->idx_in + 1] = a->in[2 * i + 1];
Itmp = 0.0;
Qtmp = 0.0;
if ((n = idx_in + snum) >= rsize)
n -= rsize;
if ((n = a->idx_in + a->snum) >= a->rsize)
n -= a->rsize;
for (j = 0, k = L - 1 - phnum; j < cpp; j++, k+= L)
for (j = 0, k = a->L - 1 - a->phnum; j < a->cpp; j++, k+= a->L)
{
if ((idx = n + j) >= rsize)
idx -= rsize;
if ((idx = n + j) >= a->rsize)
idx -= a->rsize;
Itmp += ring[2 * idx + 0] * h[k];
Qtmp += ring[2 * idx + 1] * h[k];
Itmp += a->ring[2 * idx + 0] * a->h[k];
Qtmp += a->ring[2 * idx + 1] * a->h[k];
}
out[2 * i + 0] = Itmp;
out[2 * i + 1] = Qtmp;
a->out[2 * i + 0] = Itmp;
a->out[2 * i + 1] = Qtmp;
if (--idx_in < 0)
idx_in = rsize - 1;
if (--a->idx_in < 0)
a->idx_in = a->rsize - 1;
}
}
else if (out != in)
std::copy( in, in + size * 2, out);
else if (a->out != a->in)
std::copy( a->in, a->in + a->size * 2, a->out);
}
/********************************************************************************************************
@ -117,28 +114,28 @@ void DELAY::execute()
* *
********************************************************************************************************/
void DELAY::setRun(int _run)
void DELAY::SetDelayRun (DELAY *a, int run)
{
run = _run;
a->run = run;
}
float DELAY::setValue(float _tdelay)
float DELAY::SetDelayValue (DELAY *a, float tdelay)
{
float _adelay;
tdelay = _tdelay;
phnum = (int)(0.5 + tdelay / adelta);
snum = phnum / L;
phnum %= L;
_adelay = adelta * (float) (snum * L + phnum);
adelay = _adelay;
float adelay;
a->tdelay = tdelay;
a->phnum = (int)(0.5 + a->tdelay / a->adelta);
a->snum = a->phnum / a->L;
a->phnum %= a->L;
a->adelay = a->adelta * (a->snum * a->L + a->phnum);
adelay = a->adelay;
return adelay;
}
void DELAY::setBuffs(int _size, float* _in, float* _out)
void DELAY::SetDelayBuffs (DELAY *a, int size, float* in, float* out)
{
size = _size;
in = _in;
out = _out;
a->size = size;
a->in = in;
a->out = out;
}
} // namespace WDSP

View File

@ -28,8 +28,6 @@ warren@wpratt.com
#ifndef wdsp_delay_h
#define wdsp_delay_h
#include <vector>
#include "export.h"
#define WSDEL 1025 // number of supported whole sample delays
@ -51,36 +49,25 @@ public:
int ncoef; // number of coefficients
int cpp; // coefficients per phase
float ft; // normalized cutoff frequency
std::vector<float> h; // coefficients
float* h; // coefficients
int snum; // starting sample number (0 for sub-sample delay)
int phnum; // phase number
int idx_in; // index for input into ring
int rsize; // ring size in complex samples
std::vector<float> ring; // ring buffer
float* ring; // ring buffer
float adelta; // actual delay increment
float adelay; // actual delay
DELAY(
int run,
int size,
float* in,
float* out,
int rate,
float tdelta,
float tdelay
);
DELAY(const DELAY&) = delete;
DELAY& operator=(DELAY& other) = delete;
~DELAY() = default;
void flush();
void execute();
static DELAY* create_delay (int run, int size, float* in, float* out, int rate, float tdelta, float tdelay);
static void destroy_delay (DELAY *a);
static void flush_delay (DELAY *a);
static void xdelay (DELAY *a);
// Properties
void setRun(int run);
float setValue(float delay); // returns actual delay in seconds
void setBuffs(int size, float* in, float* out);
static void SetDelayRun (DELAY *a, int run);
static float SetDelayValue (DELAY *a, float delay); // returns actual delay in seconds
static void SetDelayBuffs (DELAY *a, int size, float* in, float* out);
};
} // namespace WDSP

View File

@ -43,8 +43,7 @@ void EMPH::calc()
{
infilt = new float[2 * size * 2];
product = new float[2 * size * 2];
FCurve::fc_mults(
mults,
mults = FCurve::fc_mults(
size,
f_low,
f_high,
@ -64,6 +63,7 @@ void EMPH::decalc()
{
fftwf_destroy_plan(CRev);
fftwf_destroy_plan(CFor);
delete[] mults;
delete[] product;
delete[] infilt;
}

View File

@ -34,8 +34,6 @@ warren@wpratt.com
#ifndef _emph_h
#define _emph_h
#include <vector>
#include "fftw3.h"
#include "export.h"
@ -54,7 +52,7 @@ public:
double f_high;
float* infilt;
float* product;
std::vector<float> mults;
float* mults;
double rate;
fftwf_plan CFor;
fftwf_plan CRev;

View File

@ -53,6 +53,7 @@ EMPHP::EMPHP(
double _f_high
)
{
float* impulse;
run = _run;
position = _position;
size = _size;
@ -64,20 +65,19 @@ EMPHP::EMPHP(
ctype = _ctype;
f_low = _f_low;
f_high = _f_high;
std::vector<float> impulse(2 * nc);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc,
(float) f_low,
(float) f_high,
(float) (-20.0 * log10(f_high / f_low)),
f_low,
f_high,
-20.0 * log10(f_high / f_low),
0.0,
ctype,
(float) rate,
(float) (1.0 / (2.0 * size)),
rate,
1.0 / (2.0 * size),
0, 0
);
p = new FIRCORE(size, in, out, mp, impulse);
p = new FIRCORE(size, in, out, nc, mp, impulse);
delete[] (impulse);
}
EMPHP::~EMPHP()
@ -107,10 +107,9 @@ void EMPHP::setBuffers(float* _in, float* _out)
void EMPHP::setSamplerate(int _rate)
{
float* impulse;
rate = _rate;
std::vector<float> impulse(2 * nc);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc,
f_low,
f_high,
@ -122,15 +121,15 @@ void EMPHP::setSamplerate(int _rate)
0, 0
);
p->setImpulse(impulse, 1);
delete[] (impulse);
}
void EMPHP::setSize(int _size)
{
float* impulse;
size = _size;
p->setSize(size);
std::vector<float> impulse(2 * nc);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc,
f_low,
f_high,
@ -143,6 +142,7 @@ void EMPHP::setSize(int _size)
0
);
p->setImpulse(impulse, 1);
delete[] (impulse);
}
/********************************************************************************************************
@ -167,36 +167,37 @@ void EMPHP::setMP(int _mp)
void EMPHP::setNC(int _nc)
{
float* impulse;
if (nc != _nc)
{
nc = _nc;
std::vector<float> impulse(2 * nc);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc,
(float) f_low,
(float) f_high,
(float) (-20.0 * log10(f_high / f_low)),
f_low,
f_high,
-20.0 * log10(f_high / f_low),
0.0,
ctype,
(float) rate,
(float) (1.0 / (2.0 * size)),
rate,
1.0 / (2.0 * size),
0,
0
);
p->setNc(impulse);
p->setNc(nc, impulse);
delete[] (impulse);
}
}
void EMPHP::setFreqs(double low, double high)
{
float* impulse;
if (f_low != low || f_high != high)
{
f_low = low;
f_high = high;
std::vector<float> impulse(2 * nc);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc,
f_low,
f_high,
@ -209,6 +210,7 @@ void EMPHP::setFreqs(double low, double high)
0
);
p->setImpulse(impulse, 1);
delete[] (impulse);
}
}

View File

@ -44,12 +44,12 @@ namespace WDSP {
void EQ::eq_mults (std::vector<float>& mults, int size, int nfreqs, float* F, float* G, float samplerate, float scale, int ctfmode, int wintype)
{
std::vector<float> impulse;
EQP::eq_impulse (impulse, size + 1, nfreqs, F, G, samplerate, scale, ctfmode, wintype);
std::vector<float> _mults;
FIR::fftcv_mults(_mults, 2 * size, impulse.data());
float* impulse = EQP::eq_impulse (size + 1, nfreqs, F, G, samplerate, scale, ctfmode, wintype);
float* _mults = FIR::fftcv_mults(2 * size, impulse);
mults.resize(2 * size * 2);
std::copy(_mults.begin(), _mults.end(), mults.begin());
std::copy(_mults, _mults + 2*size*2, mults.begin());
delete[] _mults;
delete[] impulse;
}
void EQ::calc()

View File

@ -42,34 +42,25 @@ int EQP::fEQcompare (const void * a, const void * b)
return 1;
}
void EQP::eq_impulse (
std::vector<float>& impulse,
int N,
int _nfreqs,
const float* F,
const float* G,
double samplerate,
double scale,
int ctfmode,
int wintype
)
float* EQP::eq_impulse (int N, int nfreqs, const float* F, const float* G, double samplerate, double scale, int ctfmode, int wintype)
{
std::vector<float> fp(_nfreqs + 2);
std::vector<float> gp(_nfreqs + 2);
std::vector<float> fp(nfreqs + 2);
std::vector<float> gp(nfreqs + 2);
std::vector<float> A(N / 2 + 1);
float* sary = new float[2 * _nfreqs];
float* sary = new float[2 * nfreqs];
double gpreamp;
double f;
double frac;
float* impulse;
int i;
int j;
int mid;
fp[0] = 0.0;
fp[_nfreqs + 1] = 1.0;
fp[nfreqs + 1] = 1.0;
gpreamp = G[0];
for (i = 1; i <= _nfreqs; i++)
for (i = 1; i <= nfreqs; i++)
{
fp[i] = (float) (2.0 * F[i] / samplerate);
@ -82,22 +73,22 @@ void EQP::eq_impulse (
gp[i] = G[i];
}
for (i = 1, j = 0; i <= _nfreqs; i++, j+=2)
for (i = 1, j = 0; i <= nfreqs; i++, j+=2)
{
sary[j + 0] = fp[i];
sary[j + 1] = gp[i];
}
qsort (sary, _nfreqs, 2 * sizeof (float), fEQcompare);
qsort (sary, nfreqs, 2 * sizeof (float), fEQcompare);
for (i = 1, j = 0; i <= _nfreqs; i++, j+=2)
for (i = 1, j = 0; i <= nfreqs; i++, j+=2)
{
fp[i] = sary[j + 0];
gp[i] = sary[j + 1];
}
gp[0] = gp[1];
gp[_nfreqs + 1] = gp[_nfreqs];
gp[nfreqs + 1] = gp[nfreqs];
mid = N / 2;
j = 0;
@ -107,7 +98,7 @@ void EQP::eq_impulse (
{
f = (double)i / (double)mid;
while ((f > fp[j + 1]) && (j < _nfreqs))
while ((f > fp[j + 1]) && (j < nfreqs))
j++;
frac = (f - fp[j]) / (fp[j + 1] - fp[j]);
@ -120,7 +111,7 @@ void EQP::eq_impulse (
{
f = ((double)i + 0.5) / (double)mid;
while ((f > fp[j + 1]) && (j < _nfreqs))
while ((f > fp[j + 1]) && (j < nfreqs))
j++;
frac = (f - fp[j]) / (fp[j + 1] - fp[j]);
@ -141,7 +132,7 @@ void EQP::eq_impulse (
if (N & 1)
{
low = (int)(fp[1] * mid);
high = (int)(fp[_nfreqs] * mid + 0.5);
high = (int)(fp[nfreqs] * mid + 0.5);
lowmag = A[low];
highmag = A[high];
flow4 = pow((double)low / (double)mid, 4.0);
@ -169,7 +160,7 @@ void EQP::eq_impulse (
else
{
low = (int)(fp[1] * mid - 0.5);
high = (int)(fp[_nfreqs] * mid - 0.5);
high = (int)(fp[nfreqs] * mid - 0.5);
lowmag = A[low];
highmag = A[high];
flow4 = pow((double)low / (double)mid, 4.0);
@ -196,14 +187,13 @@ void EQP::eq_impulse (
}
}
impulse.resize(2 * N);
if (N & 1)
FIR::fir_fsamp_odd(impulse, N, A.data(), 1, 1.0, wintype);
impulse = FIR::fir_fsamp_odd(N, A.data(), 1, 1.0, wintype);
else
FIR::fir_fsamp(impulse, N, A.data(), 1, 1.0, wintype);
impulse = FIR::fir_fsamp(N, A.data(), 1, 1.0, wintype);
delete[] sary;
return impulse;
}
/********************************************************************************************************
@ -228,7 +218,7 @@ EQP::EQP(
)
{
// NOTE: 'nc' must be >= 'size'
std::vector<float> impulse;
float* impulse;
run = _run;
size = _size;
nc = _nc;
@ -243,8 +233,9 @@ EQP::EQP(
ctfmode = _ctfmode;
wintype = _wintype;
samplerate = (double) _samplerate;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore = new FIRCORE(size, in, out, mp, impulse);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore = new FIRCORE(size, in, out, nc, mp, impulse);
delete[] impulse;
}
EQP::~EQP()
@ -274,19 +265,21 @@ void EQP::setBuffers(float* _in, float* _out)
void EQP::setSamplerate(int rate)
{
std::vector<float> impulse;
float* impulse;
samplerate = rate;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void EQP::setSize(int _size)
{
std::vector<float> impulse;
float* impulse;
size = _size;
fircore->setSize(size);
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
/********************************************************************************************************
@ -302,13 +295,14 @@ void EQP::setRun(int _run)
void EQP::setNC(int _nc)
{
std::vector<float> impulse;
float* impulse;
if (nc != _nc)
{
nc = _nc;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setNc(impulse);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setNc(nc, impulse);
delete[] impulse;
}
}
@ -323,35 +317,38 @@ void EQP::setMP(int _mp)
void EQP::setProfile(int _nfreqs, const float* _F, const float* _G)
{
std::vector<float> impulse;
float* impulse;
nfreqs = _nfreqs;
F.resize(nfreqs + 1);
G.resize(nfreqs + 1);
std::copy(_F, _F + (_nfreqs + 1), F.begin());
std::copy(_G, _G + (_nfreqs + 1), G.begin());
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void EQP::setCtfmode(int _mode)
{
std::vector<float> impulse;
float* impulse;
ctfmode = _mode;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void EQP::setWintype(int _wintype)
{
std::vector<float> impulse;
float* impulse;
wintype = _wintype;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void EQP::setGrphEQ(const int *rxeq)
{ // three band equalizer (legacy compatibility)
std::vector<float> impulse;
float* impulse;
nfreqs = 4;
F.resize(nfreqs + 1);
G.resize(nfreqs + 1);
@ -365,13 +362,14 @@ void EQP::setGrphEQ(const int *rxeq)
G[3] = (float)rxeq[2];
G[4] = (float)rxeq[3];
ctfmode = 0;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void EQP::setGrphEQ10(const int *rxeq)
{ // ten band equalizer (legacy compatibility)
std::vector<float> impulse;
float* impulse;
nfreqs = 10;
F.resize(nfreqs + 1);
G.resize(nfreqs + 1);
@ -388,8 +386,9 @@ void EQP::setGrphEQ10(const int *rxeq)
for (int i = 0; i <= nfreqs; i++)
G[i] = (float)rxeq[i];
ctfmode = 0;
eq_impulse (impulse, nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
impulse = eq_impulse (nc, nfreqs, F.data(), G.data(), samplerate, 1.0 / (2.0 * size), ctfmode, wintype);
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
} // namespace WDSP

View File

@ -92,17 +92,7 @@ public:
void setGrphEQ(const int *rxeq);
void setGrphEQ10(const int *rxeq);
static void eq_impulse (
std::vector<float>& impulse,
int N,
int nfreqs,
const float* F,
const float* G,
double samplerate,
double scale,
int ctfmode,
int wintype
);
static float* eq_impulse (int N, int nfreqs, const float* F, const float* G, double samplerate, double scale, int ctfmode, int wintype);
private:
static int fEQcompare (const void * a, const void * b);

View File

@ -31,11 +31,12 @@ warren@wpratt.com
namespace WDSP {
void FCurve::fc_impulse (std::vector<float>& impulse, int nc, float f0, float f1, float g0, float, int curve, float samplerate, float scale, int ctfmode, int wintype)
float* FCurve::fc_impulse (int nc, float f0, float f1, float g0, float, int curve, float samplerate, float scale, int ctfmode, int wintype)
{
float* A = new float[nc / 2 + 1]; // (float *) malloc0 ((nc / 2 + 1) * sizeof (float));
int i;
float fn, f;
float* impulse;
int mid = nc / 2;
float g0_lin = pow(10.0, g0 / 20.0);
if (nc & 1)
@ -139,21 +140,22 @@ void FCurve::fc_impulse (std::vector<float>& impulse, int nc, float f0, float f1
}
}
}
if (nc & 1)
FIR::fir_fsamp_odd(impulse, nc, A, 1, 1.0, wintype);
impulse = FIR::fir_fsamp_odd(nc, A, 1, 1.0, wintype);
else
FIR::fir_fsamp(impulse, nc, A, 1, 1.0, wintype);
impulse = FIR::fir_fsamp(nc, A, 1, 1.0, wintype);
// print_impulse ("emph.txt", size + 1, impulse, 1, 0);
delete[] (A);
return impulse;
}
// generate mask for Overlap-Save Filter
void FCurve::fc_mults (std::vector<float>& mults, int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype)
float* FCurve::fc_mults (int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype)
{
std::vector<float> impulse(2 * (size + 1));
fc_impulse (impulse, size + 1, f0, f1, g0, g1, curve, samplerate, scale, ctfmode, wintype);
FIR::fftcv_mults(mults, 2 * size, impulse.data());
float* impulse = fc_impulse (size + 1, f0, f1, g0, g1, curve, samplerate, scale, ctfmode, wintype);
float* mults = FIR::fftcv_mults(2 * size, impulse);
delete[] (impulse);
return mults;
}
} // namespace WDSP

View File

@ -28,8 +28,6 @@ warren@wpratt.com
#ifndef wdsp_fcurve_h
#define wdsp_fcurve_h
#include <vector>
#include "export.h"
namespace WDSP {
@ -37,8 +35,8 @@ namespace WDSP {
class WDSP_API FCurve
{
public:
static void fc_impulse (std::vector<float>& impulse, int nc, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype);
static void fc_mults (std::vector<float>& mults, int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype);
static float* fc_impulse (int nc, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype);
static float* fc_mults (int size, float f0, float f1, float g0, float g1, int curve, float samplerate, float scale, int ctfmode, int wintype);
};
} // namespace WDSP

View File

@ -35,14 +35,14 @@ warren@pratt.one
namespace WDSP {
void FIR::fftcv_mults (std::vector<float>& mults, int NM, const float* c_impulse)
float* FIR::fftcv_mults (int NM, float* c_impulse)
{
mults.resize(NM * 2);
auto mults = new float[NM * 2];
std::vector<float> cfft_impulse(NM * 2);
fftwf_plan ptmp = fftwf_plan_dft_1d(
NM,
(fftwf_complex *) cfft_impulse.data(),
(fftwf_complex *) mults.data(),
(fftwf_complex *) mults,
FFTW_FORWARD,
FFTW_PATIENT
);
@ -51,13 +51,14 @@ void FIR::fftcv_mults (std::vector<float>& mults, int NM, const float* c_impulse
std::copy(c_impulse, c_impulse + (NM / 2 + 1) * 2, &(cfft_impulse[NM - 2]));
fftwf_execute (ptmp);
fftwf_destroy_plan (ptmp);
return mults;
}
void FIR::get_fsamp_window(std::vector<float>& window, int N, int wintype)
float* FIR::get_fsamp_window(int N, int wintype)
{
double arg0;
double arg1;
window.resize(N);
auto window = new float[N];
switch (wintype)
{
case 0:
@ -91,18 +92,20 @@ void FIR::get_fsamp_window(std::vector<float>& window, int N, int wintype)
for (int i = 0; i < N; i++)
window[i] = 1.0;
}
return window;
}
void FIR::fir_fsamp_odd (std::vector<float>& c_impulse, int N, const float* A, int rtype, double scale, int wintype)
float* FIR::fir_fsamp_odd (int N, const float* A, int rtype, double scale, int wintype)
{
int mid = (N - 1) / 2;
double mag;
double phs;
std::vector<float> fcoef(N * 2);
auto *c_impulse = new float[N * 2];
fftwf_plan ptmp = fftwf_plan_dft_1d(
N,
(fftwf_complex *)fcoef.data(),
(fftwf_complex *)c_impulse.data(),
(fftwf_complex *)c_impulse,
FFTW_BACKWARD,
FFTW_PATIENT
);
@ -121,8 +124,7 @@ void FIR::fir_fsamp_odd (std::vector<float>& c_impulse, int N, const float* A, i
}
fftwf_execute (ptmp);
fftwf_destroy_plan (ptmp);
std::vector<float> window;
get_fsamp_window(window, N, wintype);
float* window = get_fsamp_window(N, wintype);
switch (rtype)
{
case 0:
@ -139,11 +141,14 @@ void FIR::fir_fsamp_odd (std::vector<float>& c_impulse, int N, const float* A, i
default:
break;
}
delete[] window;
return c_impulse;
}
void FIR::fir_fsamp (std::vector<float>& c_impulse, int N, const float* A, int rtype, double scale, int wintype)
float* FIR::fir_fsamp (int N, const float* A, int rtype, double scale, int wintype)
{
double sum;
auto c_impulse = new float[N * 2];
if (N & 1)
{
@ -179,8 +184,7 @@ void FIR::fir_fsamp (std::vector<float>& c_impulse, int N, const float* A, int r
c_impulse[2 * n + 1] = 0.0;
}
}
std::vector<float> window;
get_fsamp_window (window, N, wintype);
float* window = get_fsamp_window (N, wintype);
switch (rtype)
{
case 0:
@ -197,11 +201,13 @@ void FIR::fir_fsamp (std::vector<float>& c_impulse, int N, const float* A, int r
default:
break;
}
delete[] window;
return c_impulse;
}
void FIR::fir_bandpass (std::vector<float>& c_impulse, int N, double f_low, double f_high, double samplerate, int wintype, int rtype, double scale)
float* FIR::fir_bandpass (int N, double f_low, double f_high, double samplerate, int wintype, int rtype, double scale)
{
c_impulse.resize(N * 2);
auto *c_impulse = new float[N * 2];
double ft = (f_high - f_low) / (2.0 * samplerate);
double ft_rad = TWOPI * ft;
double w_osc = PI * (f_high + f_low) / samplerate;
@ -273,9 +279,10 @@ void FIR::fir_bandpass (std::vector<float>& c_impulse, int N, double f_low, doub
break;
}
}
return c_impulse;
}
void FIR::fir_read (std::vector<float>& c_impulse, int N, const char *filename, int rtype, float scale)
float *FIR::fir_read (int N, const char *filename, int rtype, float scale)
// N = number of real or complex coefficients (see rtype)
// *filename = filename
// rtype = 0: real coefficients
@ -287,12 +294,12 @@ void FIR::fir_read (std::vector<float>& c_impulse, int N, const char *filename,
FILE *file;
float I;
float Q;
c_impulse.resize(N * 2);
std::fill(c_impulse.begin(), c_impulse.end(), 0);
auto c_impulse = new float[N * 2];
std::fill(c_impulse, c_impulse + N*2, 0);
file = fopen (filename, "r");
if (!file) {
return;
return c_impulse;
}
for (int i = 0; i < N; i++)
@ -323,6 +330,7 @@ void FIR::fir_read (std::vector<float>& c_impulse, int N, const char *filename,
}
}
fclose (file);
return c_impulse;
}
void FIR::analytic (int N, float* in, float* out)
@ -427,7 +435,7 @@ void FIR::mp_imp (int N, std::vector<float>& fir, std::vector<float>& mpfir, int
// impulse response of a zero frequency filter comprising a cascade of two resonators,
// each followed by a detrending filter
void FIR::zff_impulse(std::vector<float>& c_dresdet, int nc, float scale)
float* FIR::zff_impulse(int nc, float scale)
{
// nc = number of coefficients (power of two)
int n_resdet = nc / 2 - 1; // size of single zero-frequency resonator with detrender
@ -441,7 +449,7 @@ void FIR::zff_impulse(std::vector<float>& c_dresdet, int nc, float scale)
// allocate the float and complex versions and make the values
std::vector<float> dresdet(n_dresdet);
auto div = (float) ((nc / 2 + 1) * (nc / 2 + 1)); // calculate divisor
c_dresdet.resize(nc * 2);
auto c_dresdet = new float[nc * 2];
for (int n = 0; n < n_dresdet; n++) // convolve to make the cascade
{
for (int k = 0; k < n_resdet; k++)
@ -451,6 +459,8 @@ void FIR::zff_impulse(std::vector<float>& c_dresdet, int nc, float scale)
c_dresdet[2 * n + 0] = dresdet[n] * scale;
c_dresdet[2 * n + 1] = 0.0;
}
return c_dresdet;
}
} // namespace WDSP

View File

@ -36,17 +36,17 @@ namespace WDSP {
class WDSP_API FIR
{
public:
static void fftcv_mults (std::vector<float>& mults, int NM, const float* impulse);
static void fir_fsamp_odd (std::vector<float>& c_impulse, int N, const float* A, int rtype, double scale, int wintype);
static void fir_fsamp (std::vector<float>& c_impulse, int N, const float* A, int rtype, double scale, int wintype);
static void fir_bandpass (std::vector<float>& impulse, int N, double f_low, double f_high, double samplerate, int wintype, int rtype, double scale);
static float* fftcv_mults (int NM, float* c_impulse);
static float* fir_fsamp_odd (int N, const float* A, int rtype, double scale, int wintype);
static float* fir_fsamp (int N, const float* A, int rtype, double scale, int wintype);
static float* fir_bandpass (int N, double f_low, double f_high, double samplerate, int wintype, int rtype, double scale);
static void mp_imp (int N, std::vector<float>& fir, std::vector<float>& mpfir, int pfactor, int polarity);
private:
static void analytic (int N, float* in, float* out);
static void get_fsamp_window(std::vector<float>& window, int N, int wintype);
static void fir_read (std::vector<float>& impulse, int N, const char *filename, int rtype, float scale);
static void zff_impulse(std::vector<float>& impulse, int nc, float scale);
static float* get_fsamp_window(int N, int wintype);
static float *fir_read (int N, const char *filename, int rtype, float scale);
static float* zff_impulse(int nc, float scale);
};
#endif

View File

@ -99,7 +99,7 @@ void FIRCORE::calc(int _flip)
if (mp)
FIR::mp_imp (nc, impulse, imp, 16, 0);
else
std::copy(impulse.begin(), impulse.end(), imp.begin());
std::copy(impulse.begin(), impulse.begin() + nc * 2, imp.begin());
for (int i = 0; i < nfor; i++)
{
@ -122,19 +122,20 @@ FIRCORE::FIRCORE(
int _size,
float* _in,
float* _out,
int _nc,
int _mp,
const std::vector<float>& _impulse
float* _impulse
)
{
size = _size;
in = _in;
out = _out;
nc = (int) (_impulse.size() / 2);
nc = _nc;
mp = _mp;
plan();
impulse.resize(_impulse.size());
imp.resize(_impulse.size());
std::copy(_impulse.begin(), _impulse.end(), impulse.begin());
impulse.resize(nc * 2);
imp.resize(nc * 2);
std::copy(_impulse, _impulse + nc * 2, impulse.begin());
calc(1);
}
@ -203,29 +204,21 @@ void FIRCORE::setSize(int _size)
calc(1);
}
void FIRCORE::setImpulse(const std::vector<float>& _impulse, int _update)
void FIRCORE::setImpulse(float* _impulse, int _update)
{
auto imp_nc = (int) (_impulse.size() / 2);
if (imp_nc == nc) // to be on the safe side but setNc would be called if impulse size changes
{
std::copy(_impulse.begin(), _impulse.end(), impulse.begin());
std::copy(_impulse, _impulse + nc * 2, impulse.begin());
calc(_update);
}
else{
setNc(_impulse);
}
}
void FIRCORE::setNc(const std::vector<float>& _impulse)
void FIRCORE::setNc(int _nc, float* _impulse)
{
// because of FFT planning, this will probably cause a glitch in audio if done during dataflow
deplan();
nc = (int) (_impulse.size() / 2);
nc = _nc;
plan();
imp.resize(nc * 2);
impulse.resize(nc * 2);
std::copy(_impulse.begin(), _impulse.end(), impulse.begin());
std::copy(_impulse, _impulse + nc * 2, impulse.begin());
calc(1);
}

View File

@ -70,8 +70,10 @@ public:
int size,
float* in,
float* out,
int nc,
int mp,
const std::vector<float>& impulse
float*
impulse
);
FIRCORE(const FIRCORE&) = delete;
FIRCORE& operator=(const FIRCORE& other) = delete;
@ -81,8 +83,8 @@ public:
void execute();
void setBuffers(float* in, float* out);
void setSize(int size);
void setImpulse(const std::vector<float>& impulse, int update);
void setNc(const std::vector<float>& impulse);
void setImpulse(float* impulse, int update);
void setNc(int nc, float* impulse);
void setMp(int mp);
void setUpdate();

View File

@ -37,96 +37,94 @@ namespace WDSP {
* *
********************************************************************************************************/
void FIRMIN::calc()
void FIRMIN::calc_firmin (FIRMIN *a)
{
FIR::fir_bandpass (h, nc, f_low, f_high, samplerate, wintype, 1, gain);
rsize = nc;
mask = rsize - 1;
ring.resize(rsize * 2);
idx = 0;
a->h = FIR::fir_bandpass (a->nc, a->f_low, a->f_high, a->samplerate, a->wintype, 1, a->gain);
a->rsize = a->nc;
a->mask = a->rsize - 1;
a->ring = new float[a->rsize * 2]; // (float *) malloc0 (a->rsize * sizeof (complex));
a->idx = 0;
}
FIRMIN::FIRMIN(
int _run,
int _position,
int _size,
float* _in,
float* _out,
int _nc,
float _f_low,
float _f_high,
int _samplerate,
int _wintype,
float _gain
)
FIRMIN* FIRMIN::create_firmin (int run, int position, int size, float* in, float* out,
int nc, float f_low, float f_high, int samplerate, int wintype, float gain)
{
run = _run;
position = _position;
size = _size;
in = _in;
out = _out;
nc = _nc;
f_low = _f_low;
f_high = _f_high;
samplerate = (float) _samplerate;
wintype = _wintype;
gain = _gain;
calc();
FIRMIN *a = new FIRMIN;
a->run = run;
a->position = position;
a->size = size;
a->in = in;
a->out = out;
a->nc = nc;
a->f_low = f_low;
a->f_high = f_high;
a->samplerate = samplerate;
a->wintype = wintype;
a->gain = gain;
calc_firmin (a);
return a;
}
void FIRMIN::flush()
void FIRMIN::destroy_firmin (FIRMIN *a)
{
std::fill(ring.begin(), ring.end(), 0);
idx = 0;
delete[] (a->ring);
delete[] (a->h);
delete (a);
}
void FIRMIN::execute(int _pos)
void FIRMIN::flush_firmin (FIRMIN *a)
{
if (run && position == _pos)
std::fill(a->ring, a->ring + a->rsize * 2, 0);
a->idx = 0;
}
void FIRMIN::xfirmin (FIRMIN *a, int pos)
{
if (a->run && a->position == pos)
{
int k;
for (int i = 0; i < size; i++)
int i, j, k;
for (i = 0; i < a->size; i++)
{
ring[2 * idx + 0] = in[2 * i + 0];
ring[2 * idx + 1] = in[2 * i + 1];
out[2 * i + 0] = 0.0;
out[2 * i + 1] = 0.0;
k = idx;
for (int j = 0; j < nc; j++)
a->ring[2 * a->idx + 0] = a->in[2 * i + 0];
a->ring[2 * a->idx + 1] = a->in[2 * i + 1];
a->out[2 * i + 0] = 0.0;
a->out[2 * i + 1] = 0.0;
k = a->idx;
for (j = 0; j < a->nc; j++)
{
out[2 * i + 0] += h[2 * j + 0] * ring[2 * k + 0] - h[2 * j + 1] * ring[2 * k + 1];
out[2 * i + 1] += h[2 * j + 0] * ring[2 * k + 1] + h[2 * j + 1] * ring[2 * k + 0];
k = (k + mask) & mask;
a->out[2 * i + 0] += a->h[2 * j + 0] * a->ring[2 * k + 0] - a->h[2 * j + 1] * a->ring[2 * k + 1];
a->out[2 * i + 1] += a->h[2 * j + 0] * a->ring[2 * k + 1] + a->h[2 * j + 1] * a->ring[2 * k + 0];
k = (k + a->mask) & a->mask;
}
idx = (idx + 1) & mask;
a->idx = (a->idx + 1) & a->mask;
}
}
else if (in != out)
std::copy( in, in + size * 2, out);
else if (a->in != a->out)
std::copy( a->in, a->in + a->size * 2, a->out);
}
void FIRMIN::setBuffers(float* _in, float* _out)
void FIRMIN::setBuffers_firmin (FIRMIN *a, float* in, float* out)
{
in = _in;
out = _out;
a->in = in;
a->out = out;
}
void FIRMIN::setSamplerate(int _rate)
void FIRMIN::setSamplerate_firmin (FIRMIN *a, int rate)
{
samplerate = (float) _rate;
calc();
a->samplerate = (float)rate;
calc_firmin (a);
}
void FIRMIN::setSize(int _size)
void FIRMIN::setSize_firmin (FIRMIN *a, int size)
{
size = _size;
a->size = size;
}
void FIRMIN::setFreqs(float _f_low, float _f_high)
void FIRMIN::setFreqs_firmin (FIRMIN *a, float f_low, float f_high)
{
f_low = _f_low;
f_high = _f_high;
calc();
a->f_low = f_low;
a->f_high = f_high;
calc_firmin (a);
}
} // namespace WDSP

View File

@ -34,8 +34,6 @@ warren@wpratt.com
#ifndef wdsp_firmin_h
#define wdsp_firmin_h
#include <vector>
#include "fftw3.h"
#include "export.h"
@ -52,8 +50,8 @@ public:
int nc; // number of filter coefficients, power of two
float f_low; // low cutoff frequency
float f_high; // high cutoff frequency
std::vector<float> ring; // internal complex ring buffer
std::vector<float> h; // complex filter coefficients
float* ring; // internal complex ring buffer
float* h; // complex filter coefficients
int rsize; // ring size, number of complex samples, power of two
int mask; // mask to update indexes
int idx; // ring input/output index
@ -61,32 +59,18 @@ public:
int wintype; // filter window type
float gain; // filter gain
FIRMIN(
int run,
int position,
int size,
float* in,
float* out,
int nc,
float f_low,
float f_high,
int samplerate,
int wintype,
float gain
);
FIRMIN(const FIRMIN&) = delete;
FIRMIN& operator=(const FIRMIN& other) = delete;
~FIRMIN() = default;
void flush();
void execute(int pos);
void setBuffers(float* in, float* out);
void setSamplerate(int rate);
void setSize(int size);
void setFreqs(float f_low, float f_high);
static FIRMIN* create_firmin (int run, int position, int size, float* in, float* out,
int nc, float f_low, float f_high, int samplerate, int wintype, float gain);
static void destroy_firmin (FIRMIN *a);
static void flush_firmin (FIRMIN *a);
static void xfirmin (FIRMIN *a, int pos);
static void setBuffers_firmin (FIRMIN *a, float* in, float* out);
static void setSamplerate_firmin (FIRMIN *a, int rate);
static void setSize_firmin (FIRMIN *a, int size);
static void setFreqs_firmin (FIRMIN *a, float f_low, float f_high);
private:
void calc();
static void calc_firmin (FIRMIN *a);
};
} // namespace WDSP

View File

@ -37,169 +37,174 @@ namespace WDSP {
* *
********************************************************************************************************/
void FIROPT::plan()
void FIROPT::plan_firopt (FIROPT *a)
{
// must call for change in 'nc', 'size', 'out'
nfor = nc / size;
buffidx = 0;
idxmask = nfor - 1;
fftin.resize(2 * size * 2);
fftout.resize(nfor);
fmask.resize(nfor);
maskgen.resize(2 * size * 2);
pcfor.resize(nfor);
maskplan.resize(nfor);
for (int i = 0; i < nfor; i++)
int i;
a->nfor = a->nc / a->size;
a->buffidx = 0;
a->idxmask = a->nfor - 1;
a->fftin = new float[2 * a->size * 2]; // (float *) malloc0 (2 * a->size * sizeof (complex));
a->fftout = new float*[a->nfor]; // (float **) malloc0 (a->nfor * sizeof (float *));
a->fmask = new float*[a->nfor]; // (float **) malloc0 (a->nfor * sizeof (float *));
a->maskgen = new float[2 * a->size * 2]; // (float *) malloc0 (2 * a->size * sizeof (complex));
a->pcfor = new fftwf_plan[a->nfor]; // (fftwf_plan *) malloc0 (a->nfor * sizeof (fftwf_plan));
a->maskplan = new fftwf_plan[a->nfor]; // (fftwf_plan *) malloc0 (a->nfor * sizeof (fftwf_plan));
for (i = 0; i < a->nfor; i++)
{
fftout[i].resize(2 * size * 2);
fmask[i].resize(2 * size * 2);
pcfor[i] = fftwf_plan_dft_1d(
2 * size,
(fftwf_complex *)fftin.data(),
(fftwf_complex *)fftout[i].data(),
a->fftout[i] = new float[2 * a->size * 2]; // (float *) malloc0 (2 * a->size * sizeof (complex));
a->fmask[i] = new float[2 * a->size * 2]; // (float *) malloc0 (2 * a->size * sizeof (complex));
a->pcfor[i] = fftwf_plan_dft_1d(
2 * a->size,
(fftwf_complex *)a->fftin,
(fftwf_complex *)a->fftout[i],
FFTW_FORWARD,
FFTW_PATIENT
);
maskplan[i] = fftwf_plan_dft_1d(
2 * size,
(fftwf_complex *)maskgen.data(),
(fftwf_complex *)fmask[i].data(),
a->maskplan[i] = fftwf_plan_dft_1d(
2 * a->size,
(fftwf_complex *)a->maskgen,
(fftwf_complex *)a->fmask[i],
FFTW_FORWARD,
FFTW_PATIENT
);
}
accum.resize(2 * size * 2);
crev = fftwf_plan_dft_1d(
2 * size,
(fftwf_complex *)accum.data(),
(fftwf_complex *)out,
a->accum = new float[2 * a->size * 2]; // (float *) malloc0 (2 * a->size * sizeof (complex));
a->crev = fftwf_plan_dft_1d(
2 * a->size,
(fftwf_complex *)a->accum,
(fftwf_complex *)a->out,
FFTW_BACKWARD,
FFTW_PATIENT
);
}
void FIROPT::calc()
void FIROPT::calc_firopt (FIROPT *a)
{
// call for change in frequency, rate, wintype, gain
// must also call after a call to plan_firopt()
std::vector<float> impulse;
FIR::fir_bandpass (impulse, nc, f_low, f_high, samplerate, wintype, 1, gain);
buffidx = 0;
for (int i = 0; i < nfor; i++)
int i;
float* impulse = FIR::fir_bandpass (a->nc, a->f_low, a->f_high, a->samplerate, a->wintype, 1, a->gain);
a->buffidx = 0;
for (i = 0; i < a->nfor; i++)
{
// I right-justified the impulse response => take output from left side of output buff, discard right side
// Be careful about flipping an asymmetrical impulse response.
std::copy(&(impulse[2 * size * i]), &(impulse[2 * size * i]) + size * 2, &(maskgen[2 * size]));
fftwf_execute (maskplan[i]);
std::copy(&(impulse[2 * a->size * i]), &(impulse[2 * a->size * i]) + a->size * 2, &(a->maskgen[2 * a->size]));
fftwf_execute (a->maskplan[i]);
}
delete[] (impulse);
}
FIROPT::FIROPT(
int _run,
int _position,
int _size,
float* _in,
float* _out,
int _nc,
float _f_low,
float _f_high,
int _samplerate,
int _wintype,
float _gain
)
FIROPT* FIROPT::create_firopt (int run, int position, int size, float* in, float* out,
int nc, float f_low, float f_high, int samplerate, int wintype, float gain)
{
run = _run;
position = _position;
size = _size;
in = _in;
out = _out;
nc = _nc;
f_low = _f_low;
f_high = _f_high;
samplerate = (float) _samplerate;
wintype = _wintype;
gain = _gain;
plan();
calc();
FIROPT *a = new FIROPT;
a->run = run;
a->position = position;
a->size = size;
a->in = in;
a->out = out;
a->nc = nc;
a->f_low = f_low;
a->f_high = f_high;
a->samplerate = samplerate;
a->wintype = wintype;
a->gain = gain;
plan_firopt (a);
calc_firopt (a);
return a;
}
void FIROPT::deplan()
void FIROPT::deplan_firopt (FIROPT *a)
{
fftwf_destroy_plan (crev);
for (int i = 0; i < nfor; i++)
int i;
fftwf_destroy_plan (a->crev);
delete[] (a->accum);
for (i = 0; i < a->nfor; i++)
{
fftwf_destroy_plan (pcfor[i]);
fftwf_destroy_plan (maskplan[i]);
delete[] (a->fftout[i]);
delete[] (a->fmask[i]);
fftwf_destroy_plan (a->pcfor[i]);
fftwf_destroy_plan (a->maskplan[i]);
}
delete[] (a->maskplan);
delete[] (a->pcfor);
delete[] (a->maskgen);
delete[] (a->fmask);
delete[] (a->fftout);
delete[] (a->fftin);
}
FIROPT::~FIROPT()
void FIROPT::destroy_firopt (FIROPT *a)
{
deplan();
deplan_firopt (a);
delete (a);
}
void FIROPT::flush()
void FIROPT::flush_firopt (FIROPT *a)
{
std::fill(fftin.begin(), fftin.end(), 0);
for (int i = 0; i < nfor; i++)
std::fill(fftout[i].begin(), fftout[i].end(), 0);
buffidx = 0;
int i;
std::fill(a->fftin, a->fftin + 2 * a->size * 2, 0);
for (i = 0; i < a->nfor; i++)
std::fill(a->fftout[i], a->fftout[i] + 2 * a->size * 2, 0);
a->buffidx = 0;
}
void FIROPT::execute(int pos)
void FIROPT::xfiropt (FIROPT *a, int pos)
{
if (run && (position == pos))
if (a->run && (a->position == pos))
{
int k;
std::copy(in, in + size * 2, &(fftin[2 * size]));
fftwf_execute (pcfor[buffidx]);
k = buffidx;
std::fill(accum.begin(), accum.end(), 0);
for (int j = 0; j < nfor; j++)
int i, j, k;
std::copy(a->in, a->in + a->size * 2, &(a->fftin[2 * a->size]));
fftwf_execute (a->pcfor[a->buffidx]);
k = a->buffidx;
std::fill(a->accum, a->accum + 2 * a->size * 2, 0);
for (j = 0; j < a->nfor; j++)
{
for (int i = 0; i < 2 * size; i++)
for (i = 0; i < 2 * a->size; i++)
{
accum[2 * i + 0] += fftout[k][2 * i + 0] * fmask[j][2 * i + 0] - fftout[k][2 * i + 1] * fmask[j][2 * i + 1];
accum[2 * i + 1] += fftout[k][2 * i + 0] * fmask[j][2 * i + 1] + fftout[k][2 * i + 1] * fmask[j][2 * i + 0];
a->accum[2 * i + 0] += a->fftout[k][2 * i + 0] * a->fmask[j][2 * i + 0] - a->fftout[k][2 * i + 1] * a->fmask[j][2 * i + 1];
a->accum[2 * i + 1] += a->fftout[k][2 * i + 0] * a->fmask[j][2 * i + 1] + a->fftout[k][2 * i + 1] * a->fmask[j][2 * i + 0];
}
k = (k + idxmask) & idxmask;
k = (k + a->idxmask) & a->idxmask;
}
buffidx = (buffidx + 1) & idxmask;
fftwf_execute (crev);
std::copy(&(fftin[2 * size]), &(fftin[2 * size]) + size * 2, fftin.begin());
a->buffidx = (a->buffidx + 1) & a->idxmask;
fftwf_execute (a->crev);
std::copy(&(a->fftin[2 * a->size]), &(a->fftin[2 * a->size]) + a->size * 2, a->fftin);
}
else if (in != out)
std::copy( in, in + size * 2, out);
else if (a->in != a->out)
std::copy( a->in, a->in + a->size * 2, a->out);
}
void FIROPT::setBuffers(float* _in, float* _out)
void FIROPT::setBuffers_firopt (FIROPT *a, float* in, float* out)
{
in = _in;
out = _out;
deplan();
plan();
calc();
a->in = in;
a->out = out;
deplan_firopt (a);
plan_firopt (a);
calc_firopt (a);
}
void FIROPT::setSamplerate(int _rate)
void FIROPT::setSamplerate_firopt (FIROPT *a, int rate)
{
samplerate = (float) _rate;
calc();
a->samplerate = rate;
calc_firopt (a);
}
void FIROPT::setSize(int _size)
void FIROPT::setSize_firopt (FIROPT *a, int size)
{
size = _size;
deplan();
plan();
calc();
a->size = size;
deplan_firopt (a);
plan_firopt (a);
calc_firopt (a);
}
void FIROPT::setFreqs(float _f_low, float _f_high)
void FIROPT::setFreqs_firopt (FIROPT *a, float f_low, float f_high)
{
f_low = _f_low;
f_high = _f_high;
calc();
a->f_low = f_low;
a->f_high = f_high;
calc_firopt (a);
}

View File

@ -34,8 +34,6 @@ warren@wpratt.com
#ifndef wdsp_firopt_h
#define wdsp_firopt_h
#include <vector>
#include "fftw3.h"
#include "export.h"
@ -43,7 +41,6 @@ namespace WDSP {
class WDSP_API FIROPT
{
public:
int run; // run control
int position; // position at which to execute
int size; // input/output buffer size, power of two
@ -56,46 +53,31 @@ public:
int wintype; // filter window type
float gain; // filter gain
int nfor; // number of buffers in delay line
std::vector<float> fftin; // fft input buffer
std::vector<std::vector<float>> fmask; // frequency domain masks
std::vector<std::vector<float>> fftout; // fftout delay line
std::vector<float> accum; // frequency domain accumulator
float* fftin; // fft input buffer
float** fmask; // frequency domain masks
float** fftout; // fftout delay line
float* accum; // frequency domain accumulator
int buffidx; // fft out buffer index
int idxmask; // mask for index computations
std::vector<float> maskgen; // input for mask generation FFT
std::vector<fftwf_plan> pcfor; // array of forward FFT plans
float* maskgen; // input for mask generation FFT
fftwf_plan* pcfor; // array of forward FFT plans
fftwf_plan crev; // reverse fft plan
std::vector<fftwf_plan> maskplan; // plans for frequency domain masks
fftwf_plan* maskplan; // plans for frequency domain masks
FIROPT(
int run,
int position,
int size,
float* in,
float* out,
int nc,
float f_low,
float f_high,
int samplerate,
int wintype,
float gain
);
FIROPT(const FIROPT&) = delete;
FIROPT& operator=(const FIROPT& other) = delete;
~FIROPT();
void destroy();
void flush();
void execute(int pos);
void setBuffers(float* in, float* out);
void setSamplerate(int rate);
void setSize(int size);
void setFreqs(float f_low, float f_high);
static FIROPT* create_firopt (int run, int position, int size, float* in, float* out,
int nc, float f_low, float f_high, int samplerate, int wintype, float gain);
static void xfiropt (FIROPT *a, int pos);
static void destroy_firopt (FIROPT *a);
static void flush_firopt (FIROPT *a);
static void setBuffers_firopt (FIROPT *a, float* in, float* out);
static void setSamplerate_firopt (FIROPT *a, int rate);
static void setSize_firopt (FIROPT *a, int size);
static void setFreqs_firopt (FIROPT *a, float f_low, float f_high);
private:
void plan();
void calc();
void deplan();
static void plan_firopt (FIROPT *a);
static void calc_firopt (FIROPT *a);
static void deplan_firopt (FIROPT *a);
};
} // namespace WDSP

View File

@ -143,12 +143,11 @@ FMD::FMD(
lim_gain(0.0001), // 2.5
lim_pre_gain(0.01) // 0.4
{
float* impulse;
calc();
// de-emphasis filter
audio.resize(size * 2);
std::vector<float> impulse(2 * nc_de);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc_de,
(float) f_low,
(float) f_high,
@ -159,17 +158,18 @@ FMD::FMD(
0,
0
);
pde = new FIRCORE(size, audio.data(), out, mp_de, impulse);
pde = new FIRCORE(size, audio.data(), out, nc_de, mp_de, impulse);
delete[] impulse;
// audio filter
std::vector<float> impulseb;
FIR::fir_bandpass(impulseb, nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud = new FIRCORE(size, out, out, mp_aud, impulseb);
impulse = FIR::fir_bandpass(nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud = new FIRCORE(size, out, out, nc_aud, mp_aud, impulse);
delete[] impulse;
}
FMD::~FMD()
{
delete paud;
delete pde;
delete (paud);
delete (pde);
decalc();
}
@ -248,13 +248,12 @@ void FMD::setBuffers(float* _in, float* _out)
void FMD::setSamplerate(int _rate)
{
float* impulse;
decalc();
rate = _rate;
calc();
// de-emphasis filter
std::vector<float> impulse(2 * nc_de);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc_de,
(float) f_low,
(float) f_high,
@ -267,24 +266,24 @@ void FMD::setSamplerate(int _rate)
0
);
pde->setImpulse(impulse, 1);
delete[] impulse;
// audio filter
std::vector<float> impulseb;
FIR::fir_bandpass(impulseb, nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud->setImpulse(impulseb, 1);
impulse = FIR::fir_bandpass(nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud->setImpulse(impulse, 1);
delete[] impulse;
plim->setSamplerate((int) rate);
}
void FMD::setSize(int _size)
{
float* impulse;
decalc();
size = _size;
calc();
audio.resize(size * 2);
// de-emphasis filter
delete pde;
std::vector<float> impulse(2 * nc_de);
FCurve::fc_impulse (
impulse,
delete (pde);
impulse = FCurve::fc_impulse (
nc_de,
(float) f_low,
(float) f_high,
@ -296,12 +295,13 @@ void FMD::setSize(int _size)
0,
0
);
pde = new FIRCORE(size, audio.data(), out, mp_de, impulse);
pde = new FIRCORE(size, audio.data(), out, nc_de, mp_de, impulse);
delete[] impulse;
// audio filter
delete paud;
std::vector<float> impulseb;
FIR::fir_bandpass(impulseb, nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud = new FIRCORE(size, out, out, mp_aud, impulseb);
delete (paud);
impulse = FIR::fir_bandpass(nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud = new FIRCORE(size, out, out, nc_aud, mp_aud, impulse);
delete[] impulse;
plim->setSize(size);
}
@ -331,12 +331,12 @@ void FMD::setCTCSSRun(int _run)
void FMD::setNCde(int nc)
{
float* impulse;
if (nc_de != nc)
{
nc_de = nc;
std::vector<float> impulse(2 * nc_de);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc_de,
(float) f_low,
(float) f_high,
@ -348,7 +348,8 @@ void FMD::setNCde(int nc)
0,
0
);
pde->setNc(impulse);
pde->setNc(nc_de, impulse);
delete[] impulse;
}
}
@ -363,12 +364,14 @@ void FMD::setMPde(int mp)
void FMD::setNCaud(int nc)
{
float* impulse;
if (nc_aud != nc)
{
nc_aud = nc;
std::vector<float> impulse;
FIR::fir_bandpass(impulse, nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud->setNc(impulse);
impulse = FIR::fir_bandpass(nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud->setNc(nc_aud, impulse);
delete[] impulse;
}
}
@ -402,14 +405,14 @@ void FMD::setLimGain(double gaindB)
void FMD::setAFFilter(double low, double high)
{
float* impulse;
if (f_low != low || f_high != high)
{
f_low = low;
f_high = high;
// de-emphasis filter
std::vector<float> impulse(2 * nc_de);
FCurve::fc_impulse (
impulse,
impulse = FCurve::fc_impulse (
nc_de,
(float) f_low,
(float) f_high,
@ -422,10 +425,11 @@ void FMD::setAFFilter(double low, double high)
0
);
pde->setImpulse(impulse, 1);
delete[] impulse;
// audio filter
std::vector<float> impulseb;
FIR::fir_bandpass (impulseb, nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud->setImpulse(impulseb, 1);
impulse = FIR::fir_bandpass (nc_aud, 0.8 * f_low, 1.1 * f_high, rate, 0, 1, afgain / (2.0 * size));
paud->setImpulse(impulse, 1);
delete[] impulse;
}
}

View File

@ -25,8 +25,6 @@ warren@wpratt.com
*/
#include <vector>
#include "comm.hpp"
#include "fircore.hpp"
#include "fir.hpp"
@ -65,7 +63,7 @@ FMMOD::FMMOD(
int _mp
)
{
std::vector<float> impulse;
float* impulse;
run = _run;
size = _size;
in = _in;
@ -81,8 +79,9 @@ FMMOD::FMMOD(
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);
impulse = FIR::fir_bandpass(nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p = new FIRCORE(size, out, out, nc, mp, impulse);
delete[] impulse;
}
FMMOD::~FMMOD()
@ -139,21 +138,23 @@ void FMMOD::setBuffers(float* _in, float* _out)
void FMMOD::setSamplerate(int _rate)
{
std::vector<float> impulse;
float* impulse;
samplerate = _rate;
calc();
FIR::fir_bandpass(impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
impulse = FIR::fir_bandpass(nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 1);
delete[] impulse;
}
void FMMOD::setSize(int _size)
{
std::vector<float> impulse;
float* impulse;
size = _size;
calc();
p->setSize(size);
FIR::fir_bandpass(impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
impulse = FIR::fir_bandpass(nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 1);
delete[] impulse;
}
/********************************************************************************************************
@ -165,9 +166,9 @@ void FMMOD::setSize(int _size)
void FMMOD::setDeviation(float _deviation)
{
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));
float* impulse = FIR::fir_bandpass (nc, -_bp_fc, +_bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 0);
delete[] impulse;
deviation = _deviation;
// mod
sphase = 0.0;
@ -191,13 +192,14 @@ void FMMOD::setCTCSSRun (int _run)
void FMMOD::setNC(int _nc)
{
std::vector<float> impulse;
float* impulse;
if (nc != _nc)
{
nc = _nc;
FIR::fir_bandpass (impulse, nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setNc(impulse);
impulse = FIR::fir_bandpass (nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setNc(nc, impulse);
delete[] impulse;
}
}
@ -212,15 +214,16 @@ void FMMOD::setMP(int _mp)
void FMMOD::setAFFreqs(float _low, float _high)
{
std::vector<float> impulse;
float* impulse;
if (f_low != _low || f_high != _high)
{
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));
impulse = FIR::fir_bandpass (nc, -bp_fc, +bp_fc, samplerate, 0, 1, 1.0 / (2 * size));
p->setImpulse(impulse, 1);
delete[] impulse;
}
}

View File

@ -36,7 +36,7 @@ void FMSQ::calc()
{
double delta;
double theta;
std::vector<float> impulse;
float* impulse;
int i;
// noise filter
noise.resize(2 * size * 2);
@ -48,8 +48,9 @@ void FMSQ::calc()
G[1] = 0.0;
G[2] = 3.0;
G[3] = (float) (+20.0 * log10(20000.0 / *pllpole));
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(), mp, impulse);
impulse = EQP::eq_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);
delete[] impulse;
// noise averaging
avm = exp(-1.0 / (rate * avtau));
onem_avm = 1.0 - avm;
@ -285,13 +286,14 @@ void FMSQ::setThreshold(double threshold)
void FMSQ::setNC(int _nc)
{
std::vector<float> impulse;
float* impulse;
if (nc != _nc)
{
nc = _nc;
EQP::eq_impulse (impulse, nc, 3, F.data(), G.data(), rate, 1.0 / (2.0 * size), 0, 0);
p->setNc(impulse);
impulse = EQP::eq_impulse (nc, 3, F.data(), G.data(), rate, 1.0 / (2.0 * size), 0, 0);
p->setNc(nc, impulse);
delete[] impulse;
}
}

View File

@ -34,10 +34,11 @@ namespace WDSP {
void ICFIR::calc_icfir (ICFIR *a)
{
std::vector<float> impulse;
float* impulse;
a->scale = 1.0f / (float)(2 * a->size);
icfir_impulse (impulse, a->nc, a->DD, a->R, a->Pairs, (float) a->runrate, (float) a->cicrate, a->cutoff, a->xtype, a->xbw, 1, a->scale, a->wintype);
a->p = new FIRCORE(a->size, a->in, a->out, a->mp, impulse);
impulse = icfir_impulse (a->nc, a->DD, a->R, a->Pairs, (float) a->runrate, (float) a->cicrate, a->cutoff, a->xtype, a->xbw, 1, a->scale, a->wintype);
a->p = new FIRCORE(a->size, a->in, a->out, a->nc, a->mp, impulse);
delete[] (impulse);
}
void ICFIR::decalc_icfir (ICFIR *a)
@ -144,8 +145,7 @@ void ICFIR::setOutRate_icfir (ICFIR *a, int rate)
calc_icfir (a);
}
void ICFIR::icfir_impulse (
std::vector<float>& impulse,
float* ICFIR::icfir_impulse (
int N,
int DD,
int R,
@ -178,6 +178,7 @@ void ICFIR::icfir_impulse (
float ri;
float mag;
float fn;
float* impulse;
auto* A = new float[N];
float ft = cutoff / cicrate; // normalized cutoff frequency
int u_samps = (N + 1) / 2; // number of unique samples, OK for odd or even N
@ -238,10 +239,10 @@ void ICFIR::icfir_impulse (
else
for (i = u_samps, j = 1; i < N; i++, j++)
A[i] = A[u_samps - j];
impulse.resize(2 * N);
FIR::fir_fsamp (impulse, N, A, rtype, 1.0, wintype);
impulse = FIR::fir_fsamp (N, A, rtype, 1.0, wintype);
delete[] (A);
delete[] xistion;
return impulse;
}

View File

@ -79,8 +79,7 @@ public:
static void setSamplerate_icfir (ICFIR *a, int rate);
static void setSize_icfir (ICFIR *a, int size);
static void setOutRate_icfir (ICFIR *a, int rate);
static void icfir_impulse (
std::vector<float>& impulse,
static float* icfir_impulse (
int N,
int DD,
int R,

View File

@ -158,20 +158,21 @@ void NOTCHDB::getNumNotches(int* _nnotches) const
* *
********************************************************************************************************/
void NBP::fir_mbandpass (std::vector<float>& impulse, int N, int nbp, const double* flow, const double* fhigh, double rate, double scale, int wintype)
float* NBP::fir_mbandpass (int N, int nbp, const double* flow, const double* fhigh, double rate, double scale, int wintype)
{
impulse.resize(N * 2);
std::fill(impulse.begin(), impulse.end(), 0);
auto* impulse = new float[N * 2];
std::fill(impulse, impulse + N*2, 0);
for (int k = 0; k < nbp; k++)
{
std::vector<float> imp;
FIR::fir_bandpass (imp, N, flow[k], fhigh[k], rate, wintype, 1, scale);
float* imp = FIR::fir_bandpass (N, flow[k], fhigh[k], rate, wintype, 1, scale);
for (int i = 0; i < N; i++)
{
impulse[2 * i + 0] += imp[2 * i + 0];
impulse[2 * i + 1] += imp[2 * i + 1];
}
delete[] imp;
}
return impulse;
}
double NBP::min_notch_width() const
@ -323,8 +324,7 @@ void NBP::calc_lightweight()
bplow[i] -= offset;
bphigh[i] -= offset;
}
fir_mbandpass (
impulse,
impulse = fir_mbandpass (
nc,
numpb,
bplow.data(),
@ -335,6 +335,7 @@ void NBP::calc_lightweight()
);
fircore->setImpulse(impulse, 1);
// print_impulse ("nbp.txt", size + 1, impulse, 1, 0);
delete[] impulse;
}
hadnotch = havnotch;
}
@ -374,8 +375,7 @@ void NBP::calc_impulse ()
bplow[i] -= offset;
bphigh[i] -= offset;
}
fir_mbandpass (
impulse,
impulse = fir_mbandpass (
nc,
numpb,
bplow.data(),
@ -387,8 +387,7 @@ void NBP::calc_impulse ()
}
else
{
FIR::fir_bandpass(
impulse,
impulse = FIR::fir_bandpass(
nc,
flow,
fhigh,
@ -438,12 +437,14 @@ NBP::NBP(
bplow.resize(maxpb);
bphigh.resize(maxpb);
calc_impulse ();
fircore = new FIRCORE(size, in, out, mp, impulse);
fircore = new FIRCORE(size, in, out, nc, mp, impulse);
// print_impulse ("nbp.txt", size + 1, impulse, 1, 0);
delete[]impulse;
}
NBP::~NBP()
{
delete fircore;
delete (fircore);
}
void NBP::flush()
@ -471,6 +472,7 @@ void NBP::setSamplerate(int _rate)
rate = _rate;
calc_impulse ();
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void NBP::setSize(int _size)
@ -480,12 +482,14 @@ void NBP::setSize(int _size)
fircore->setSize(size);
calc_impulse ();
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
void NBP::setNc()
{
calc_impulse();
fircore->setNc(impulse);
fircore->setNc(nc, impulse);
delete[] impulse;
}
void NBP::setMp()
@ -514,6 +518,7 @@ void NBP::SetFreqs(double _flow, double _fhigh)
fhigh = _fhigh;
calc_impulse();
fircore->setImpulse(impulse, 1);
delete[] impulse;
}
}

View File

@ -80,7 +80,7 @@ public:
int autoincr; // auto-increment notch width
double flow; // low bandpass cutoff freq
double fhigh; // high bandpass cutoff freq
std::vector<float> impulse; // filter impulse response
float* impulse; // filter impulse response
int maxpb; // maximum number of passbands
NOTCHDB* notchdb; // ptr to addr of notch-database data structure
std::vector<double> bplow; // array of passband lows
@ -129,7 +129,7 @@ public:
void calc_lightweight();
private:
static void fir_mbandpass (std::vector<float>& impulse, int N, int nbp, const double* flow, const double* fhigh, double rate, double scale, int wintype);
static float* fir_mbandpass (int N, int nbp, const double* flow, const double* fhigh, double rate, double scale, int wintype);
double min_notch_width () const;
static int make_nbp (
int nn,

View File

@ -47,7 +47,7 @@ void RESAMPLE::calc()
double full_rate;
double fc_norm_high;
double fc_norm_low;
std::vector<float> impulse;
float* impulse;
fc = fcin;
ncoef = ncoefin;
x = in_rate;
@ -88,7 +88,7 @@ void RESAMPLE::calc()
ncoef = (ncoef / L + 1) * L;
cpp = ncoef / L;
h.resize(ncoef);
FIR::fir_bandpass(impulse, ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, gain * (double)L);
impulse = FIR::fir_bandpass(ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, gain * (double)L);
i = 0;
for (int j = 0; j < L; j++)
@ -101,6 +101,8 @@ void RESAMPLE::calc()
ring.resize(ringsize);
idx_in = ringsize - 1;
phnum = 0;
delete[] impulse;
}
RESAMPLE::RESAMPLE (

View File

@ -25,8 +25,6 @@ warren@wpratt.com
*/
#include <vector>
#include "comm.hpp"
#include "fir.hpp"
#include "resamplef.hpp"
@ -41,16 +39,14 @@ namespace WDSP {
RESAMPLEF* RESAMPLEF::create_resampleF ( int run, int size, float* in, float* out, int in_rate, int out_rate)
{
auto *a = new RESAMPLEF;
int x;
int y;
int z;
int i;
RESAMPLEF *a = new RESAMPLEF;
int x, y, z;
int i, j, k;
int min_rate;
float full_rate;
float fc;
float fc_norm;
std::vector<float> impulse;
float* impulse;
a->run = run;
a->size = size;
a->in = in;
@ -76,35 +72,36 @@ RESAMPLEF* RESAMPLEF::create_resampleF ( int run, int size, float* in, float* ou
else
min_rate = out_rate;
fc = 0.45f * (float)min_rate;
fc = 0.45 * (float)min_rate;
full_rate = (float)(in_rate * a->L);
fc_norm = fc / full_rate;
a->ncoef = (int)(60.0 / fc_norm);
a->ncoef = (a->ncoef / a->L + 1) * a->L;
a->cpp = a->ncoef / a->L;
a->h = new float[a->ncoef];
FIR::fir_bandpass (impulse, a->ncoef, -fc_norm, +fc_norm, 1.0, 1, 0, (float)a->L);
a->h = new float[a->ncoef]; // (float *) malloc0 (a->ncoef * sizeof (float));
impulse = FIR::fir_bandpass (a->ncoef, -fc_norm, +fc_norm, 1.0, 1, 0, (float)a->L);
i = 0;
for (int j = 0; j < a->L; j ++)
for (j = 0; j < a->L; j ++)
{
for (int k = 0; k < a->ncoef; k += a->L)
for (k = 0; k < a->ncoef; k += a->L)
a->h[i++] = impulse[j + k];
}
a->ringsize = a->cpp;
a->ring = new float[a->ringsize];
a->ring = new float[a->ringsize]; //(float *) malloc0 (a->ringsize * sizeof (float));
a->idx_in = a->ringsize - 1;
a->phnum = 0;
delete[] (impulse);
return a;
}
void RESAMPLEF::destroy_resampleF (RESAMPLEF *a)
{
delete[] a->ring;
delete[] a->h;
delete a;
delete[] (a->ring);
delete[] (a->h);
delete (a);
}
void RESAMPLEF::flush_resampleF (RESAMPLEF *a)
@ -120,20 +117,20 @@ int RESAMPLEF::xresampleF (RESAMPLEF *a)
if (a->run)
{
int n;
int i, j, n;
int idx_out;
float I;
for (int i = 0; i < a->size; i++)
for (i = 0; i < a->size; i++)
{
a->ring[a->idx_in] = a->in[i];
a->ring[a->idx_in] = (float)a->in[i];
while (a->phnum < a->L)
{
I = 0.0;
n = a->cpp * a->phnum;
for (int j = 0; j < a->cpp; j++)
for (j = 0; j < a->cpp; j++)
{
if ((idx_out = a->idx_in + j) >= a->ringsize)
idx_out -= a->ringsize;
@ -141,7 +138,7 @@ int RESAMPLEF::xresampleF (RESAMPLEF *a)
I += a->h[n + j] * a->ring[idx_out];
}
a->out[outsamps] = I;
a->out[outsamps] = (float)I;
outsamps++;
a->phnum += a->M;
@ -166,13 +163,13 @@ int RESAMPLEF::xresampleF (RESAMPLEF *a)
void* RESAMPLEF::create_resampleFV (int in_rate, int out_rate)
{
return (void *) create_resampleF (1, 0, nullptr, nullptr, in_rate, out_rate);
return (void *) create_resampleF (1, 0, 0, 0, in_rate, out_rate);
}
void RESAMPLEF::xresampleFV (float* input, float* output, int numsamps, int* outsamps, void* ptr)
{
auto *a = (RESAMPLEF*) ptr;
RESAMPLEF *a = (RESAMPLEF*) ptr;
a->in = input;
a->out = output;
a->size = numsamps;

View File

@ -36,11 +36,11 @@ namespace WDSP {
MAV* MAV::create_mav (int ringmin, int ringmax, float nom_value)
{
auto *a = new MAV;
MAV *a = new MAV;
a->ringmin = ringmin;
a->ringmax = ringmax;
a->nom_value = nom_value;
a->ring = new int[a->ringmax];
a->ring = new int[a->ringmax]; // (int *) malloc0 (a->ringmax * sizeof (int));
a->mask = a->ringmax - 1;
a->i = 0;
a->load = 0;
@ -50,8 +50,8 @@ MAV* MAV::create_mav (int ringmin, int ringmax, float nom_value)
void MAV::destroy_mav (MAV *a)
{
delete[] a->ring;
delete a;
delete[] (a->ring);
delete (a);
}
void MAV::flush_mav (MAV *a)
@ -79,11 +79,11 @@ void MAV::xmav (MAV *a, int input, float* output)
AAMAV* AAMAV::create_aamav (int ringmin, int ringmax, float nom_ratio)
{
auto *a = new AAMAV;
AAMAV *a = new AAMAV;
a->ringmin = ringmin;
a->ringmax = ringmax;
a->nom_ratio = nom_ratio;
a->ring = new int[a->ringmax];
a->ring = new int[a->ringmax]; // (int *) malloc0 (a->ringmax * sizeof (int));
a->mask = a->ringmax - 1;
a->i = 0;
a->load = 0;
@ -94,8 +94,8 @@ AAMAV* AAMAV::create_aamav (int ringmin, int ringmax, float nom_ratio)
void AAMAV::destroy_aamav (AAMAV *a)
{
delete[] a->ring;
delete[] a;
delete[] (a->ring);
delete[] (a);
}
void AAMAV::flush_aamav (AAMAV *a)
@ -137,38 +137,37 @@ void AAMAV::xaamav (AAMAV *a, int input, float* output)
void RMATCH::calc_rmatch (RMATCH *a)
{
int m;
float theta;
float dtheta;
float theta, dtheta;
int max_ring_insize;
a->nom_ratio = (float)a->nom_outrate / (float)a->nom_inrate;
max_ring_insize = (int)(1.0 + (float)a->insize * (1.05 * a->nom_ratio));
if (a->ringsize < 2 * max_ring_insize) a->ringsize = 2 * max_ring_insize;
if (a->ringsize < 2 * a->outsize) a->ringsize = 2 * a->outsize;
a->ring = new float[a->ringsize * 2];
a->ring = new float[a->ringsize * 2]; // (float *) malloc0 (a->ringsize * sizeof (complex));
a->rsize = a->ringsize;
a->n_ring = a->rsize / 2;
a->iin = a->rsize / 2;
a->iout = 0;
a->resout = new float[max_ring_insize * 2];
a->v = new VARSAMP(1, a->insize, a->in, a->resout, a->nom_inrate, a->nom_outrate,
a->resout = new float[max_ring_insize * 2]; // (float *) malloc0 (max_ring_insize * sizeof (complex));
a->v = VARSAMP::create_varsamp (1, a->insize, a->in, a->resout, a->nom_inrate, a->nom_outrate,
a->fc_high, a->fc_low, a->R, a->gain, a->var, a->varmode);
a->ffmav = AAMAV::create_aamav (a->ff_ringmin, a->ff_ringmax, a->nom_ratio);
a->propmav = MAV::create_mav (a->prop_ringmin, a->prop_ringmax, 0.0);
a->pr_gain = a->prop_gain * 48000.0f / (float)a->nom_outrate; // adjust gain for rate
a->pr_gain = a->prop_gain * 48000.0 / (float)a->nom_outrate; // adjust gain for rate
a->inv_nom_ratio = (float)a->nom_inrate / (float)a->nom_outrate;
a->feed_forward = 1.0;
a->av_deviation = 0.0;
a->ntslew = (int)(a->tslew * (float) a->nom_outrate);
a->ntslew = (int)(a->tslew * a->nom_outrate);
if (a->ntslew + 1 > a->rsize / 2) a->ntslew = a->rsize / 2 - 1;
a->cslew = new float[a->ntslew + 1];
dtheta = (float) PI / (float) a->ntslew;
a->cslew = new float[a->ntslew + 1]; // (float *) malloc0 ((a->ntslew + 1) * sizeof (float));
dtheta = PI / (float)a->ntslew;
theta = 0.0;
for (m = 0; m <= a->ntslew; m++)
{
a->cslew[m] = 0.5f * (1.0f - cos (theta));
a->cslew[m] = 0.5 * (1.0 - cos (theta));
theta += dtheta;
}
a->baux = new float[a->ringsize / 2 * 2];
a->baux = new float[a->ringsize / 2 * 2]; // (float *) malloc0 (a->ringsize / 2 * sizeof (complex));
a->readsamps = 0;
a->writesamps = 0;
a->read_startup = (unsigned int)((float)a->nom_outrate * a->startup_delay);
@ -185,7 +184,7 @@ void RMATCH::decalc_rmatch (RMATCH *a)
delete[] (a->cslew);
MAV::destroy_mav (a->propmav);
AAMAV::destroy_aamav (a->ffmav);
delete a->v;
VARSAMP::destroy_varsamp (a->v);
delete[] (a->resout);
delete[] (a->ring);
}
@ -216,7 +215,7 @@ RMATCH* RMATCH::create_rmatch (
float tslew // slew/blend time (seconds)
)
{
auto *a = new RMATCH;
RMATCH *a = new RMATCH;
a->run = run;
a->in = in;
a->out = out;
@ -247,7 +246,7 @@ RMATCH* RMATCH::create_rmatch (
void RMATCH::destroy_rmatch (RMATCH *a)
{
decalc_rmatch (a);
delete a;
delete (a);
}
void RMATCH::reset_rmatch (RMATCH *a)
@ -265,32 +264,30 @@ void RMATCH::control (RMATCH *a, int change)
float current_ratio;
AAMAV::xaamav (a->ffmav, change, &current_ratio);
current_ratio *= a->inv_nom_ratio;
a->feed_forward = a->ff_alpha * current_ratio + (1.0f - a->ff_alpha) * a->feed_forward;
a->feed_forward = a->ff_alpha * current_ratio + (1.0 - a->ff_alpha) * a->feed_forward;
}
{
int deviation = a->n_ring - a->rsize / 2;
MAV::xmav (a->propmav, deviation, &a->av_deviation);
}
a->var = a->feed_forward - a->pr_gain * a->av_deviation;
if (a->var > 1.04) a->var = 1.04f;
if (a->var < 0.96) a->var = 0.96f;
if (a->var > 1.04) a->var = 1.04;
if (a->var < 0.96) a->var = 0.96;
}
void RMATCH::blend (RMATCH *a)
{
int i;
int j;
int i, j;
for (i = 0, j = a->iout; i <= a->ntslew; i++, j = (j + 1) % a->rsize)
{
a->ring[2 * j + 0] = a->cslew[i] * a->ring[2 * j + 0] + (1.0f - a->cslew[i]) * a->baux[2 * i + 0];
a->ring[2 * j + 1] = a->cslew[i] * a->ring[2 * j + 1] + (1.0f - a->cslew[i]) * a->baux[2 * i + 1];
a->ring[2 * j + 0] = a->cslew[i] * a->ring[2 * j + 0] + (1.0 - a->cslew[i]) * a->baux[2 * i + 0];
a->ring[2 * j + 1] = a->cslew[i] * a->ring[2 * j + 1] + (1.0 - a->cslew[i]) * a->baux[2 * i + 1];
}
}
void RMATCH::upslew (RMATCH *a, int newsamps)
{
int i;
int j;
int i, j;
i = 0;
j = a->iin;
while (a->ucnt >= 0 && i < newsamps)
@ -305,13 +302,10 @@ void RMATCH::upslew (RMATCH *a, int newsamps)
void RMATCH::xrmatchIN (void* b, float* in)
{
auto *a = (RMATCH*) b;
RMATCH *a = (RMATCH*) b;
if (a->run == 1)
{
int newsamps;
int first;
int second;
int ovfl;
int newsamps, first, second, ovfl;
float var;
a->v->in = a->in = in;
@ -320,12 +314,13 @@ void RMATCH::xrmatchIN (void* b, float* in)
else
var = a->fvar;
newsamps = a->v->execute(var);
newsamps = VARSAMP::xvarsamp (a->v, var);
a->n_ring += newsamps;
if ((ovfl = a->n_ring - a->rsize) > 0)
{
a->overflows += 1;
// a->n_ring = a->rsize / 2;
a->n_ring = a->rsize; //
if ((a->ntslew + 1) > (a->rsize - a->iout))
@ -341,6 +336,7 @@ void RMATCH::xrmatchIN (void* b, float* in)
std::copy(a->ring + 2 * a->iout, a->ring + 2 * a->iout + first * 2, a->baux);
std::copy(a->ring, a->ring + second * 2, a->baux + 2 * first);
// a->iout = (a->iout + ovfl + a->rsize / 2) % a->rsize;
a->iout = (a->iout + ovfl) % a->rsize; //
}
@ -380,13 +376,8 @@ void RMATCH::xrmatchIN (void* b, float* in)
void RMATCH::dslew (RMATCH *a)
{
int i;
int j;
int k;
int n;
int zeros;
int first;
int second;
int i, j, k, n;
int zeros, first, second;
if (a->n_ring > a->ntslew + 1)
{
i = (a->iout + (a->n_ring - (a->ntslew + 1))) % a->rsize;
@ -423,7 +414,7 @@ void RMATCH::dslew (RMATCH *a)
j--;
n++;
}
// zeros = a->outsize + a->rsize / 2 - n;
if ((zeros = a->outsize - n) > 0) //
{ //
if (zeros > a->rsize - i)
@ -438,20 +429,21 @@ void RMATCH::dslew (RMATCH *a)
}
std::fill(a->ring + 2 * i, a->ring + 2 * i + first * 2, 0);
std::fill(a->ring, a->ring + second * 2, 0);
n += zeros;
}
a->n_ring = n;
a->iin = (a->iout + a->n_ring) % a->rsize;
n += zeros; //
} //
// a->n_ring = a->outsize + a->rsize / 2;
a->n_ring = n; //
// a->iin = (a->iout + a->outsize + a->rsize/2) % a->rsize;
a->iin = (a->iout + a->n_ring) % a->rsize; //
}
void RMATCH::xrmatchOUT (void* b, float* out)
{
auto *a = (RMATCH*) b;
RMATCH *a = (RMATCH*) b;
if (a->run == 1)
{
int first;
int second;
int first, second;
a->out = out;
if (a->n_ring < a->outsize)
@ -495,7 +487,7 @@ void RMATCH::xrmatchOUT (void* b, float* out)
void RMATCH::getRMatchDiags (void* b, int* underflows, int* overflows, float* var, int* ringsize, int* nring)
{
auto *a = (RMATCH*) b;
RMATCH *a = (RMATCH*) b;
*underflows = a->underflows;
*overflows = a->overflows;
a->underflows &= 0xFFFFFFFF;
@ -508,12 +500,15 @@ void RMATCH::getRMatchDiags (void* b, int* underflows, int* overflows, float* va
void RMATCH::resetRMatchDiags (void*)
{
// RMATCH *a = (RMATCH*) b;
// InterlockedExchange (&a->underflows, 0);
// InterlockedExchange (&a->overflows, 0);
}
void RMATCH::forceRMatchVar (void* b, int force, float fvar)
{
auto *a = (RMATCH*) b;
RMATCH *a = (RMATCH*) b;
a->force = force;
a->fvar = fvar;
}
@ -523,8 +518,8 @@ void* RMATCH::create_rmatchV(int in_size, int out_size, int nom_inrate, int nom_
{
return (void*)create_rmatch (
1, // run
nullptr, // input buffer, stuffed in other calls
nullptr, // output buffer, stuffed in other calls
0, // input buffer, stuffed in other calls
0, // output buffer, stuffed in other calls
in_size, // input buffer size (complex samples)
out_size, // output buffer size (complex samples)
nom_inrate, // nominal input sample-rate
@ -539,25 +534,25 @@ void* RMATCH::create_rmatchV(int in_size, int out_size, int nom_inrate, int nom_
var, // initial variable ratio
4096, // feed-forward moving average min size
262144, // feed-forward moving average max size - POWER OF TWO!
0.01f, // feed-forward exponential smoothing
0.01, // feed-forward exponential smoothing
4096, // proportional feedback min moving av ringsize
16384, // proportional feedback max moving av ringsize - POWER OF TWO!
4.0e-06f, // proportional feedback gain
4.0e-06, // proportional feedback gain
1, // linearly interpolate cvar by sample
0.003f ); // slew time (seconds)
0.003 ); // slew time (seconds)
}
void RMATCH::destroy_rmatchV (void* ptr)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
destroy_rmatch (a);
}
void RMATCH::setRMatchInsize (void* ptr, int insize)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
std::this_thread::sleep_for(std::chrono::seconds(10));
decalc_rmatch(a);
@ -569,7 +564,7 @@ void RMATCH::setRMatchInsize (void* ptr, int insize)
void RMATCH::setRMatchOutsize (void* ptr, int outsize)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
std::this_thread::sleep_for(std::chrono::seconds(10));
decalc_rmatch(a);
@ -581,7 +576,7 @@ void RMATCH::setRMatchOutsize (void* ptr, int outsize)
void RMATCH::setRMatchNomInrate (void* ptr, int nom_inrate)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
std::this_thread::sleep_for(std::chrono::seconds(10));
decalc_rmatch(a);
@ -593,7 +588,7 @@ void RMATCH::setRMatchNomInrate (void* ptr, int nom_inrate)
void RMATCH::setRMatchNomOutrate (void* ptr, int nom_outrate)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
std::this_thread::sleep_for(std::chrono::seconds(10));
decalc_rmatch(a);
@ -605,7 +600,7 @@ void RMATCH::setRMatchNomOutrate (void* ptr, int nom_outrate)
void RMATCH::setRMatchRingsize (void* ptr, int ringsize)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
std::this_thread::sleep_for(std::chrono::seconds(10));
decalc_rmatch(a);
@ -617,7 +612,7 @@ void RMATCH::setRMatchRingsize (void* ptr, int ringsize)
void RMATCH::setRMatchFeedbackGain (void* b, float feedback_gain)
{
auto *a = (RMATCH*) b;
RMATCH *a = (RMATCH*) b;
a->prop_gain = feedback_gain;
a->pr_gain = a->prop_gain * 48000.0 / (float)a->nom_outrate;
}
@ -625,8 +620,9 @@ void RMATCH::setRMatchFeedbackGain (void* b, float feedback_gain)
void RMATCH::setRMatchSlewTime (void* b, float slew_time)
{
auto *a = (RMATCH*) b;
a->run = 0;
RMATCH *a = (RMATCH*) b;
a->run = 0; // InterlockedBitTestAndReset(&a->run, 0); // turn OFF new data coming into the rmatch
// Sleep(10); // wait for processing to cease
decalc_rmatch(a); // deallocate all memory EXCEPT the data structure holding all current parameters
a->tslew = slew_time; // change the value of 'slew_time'
calc_rmatch(a); // recalculate/reallocate everything in the RMATCH
@ -636,20 +632,21 @@ void RMATCH::setRMatchSlewTime (void* b, float slew_time)
void RMATCH::setRMatchSlewTime1(void* b, float slew_time)
{
auto *a = (RMATCH*) b;
float theta;
float dtheta;
RMATCH *a = (RMATCH*) b;
float theta, dtheta;
int m;
a->run = 0;
delete[] a->cslew;
// Sleep(10);
delete[](a->cslew);
a->tslew = slew_time;
a->ntslew = (int)(a->tslew * (float) a->nom_outrate);
a->ntslew = (int)(a->tslew * a->nom_outrate);
if (a->ntslew + 1 > a->rsize / 2) a->ntslew = a->rsize / 2 - 1;
a->cslew = new float[a->ntslew + 1]; // (float*)malloc0((a->ntslew + 1) * sizeof(float));
dtheta = (float) PI / (float)a->ntslew;
dtheta = PI / (float)a->ntslew;
theta = 0.0;
for (int m = 0; m <= a->ntslew; m++)
for (m = 0; m <= a->ntslew; m++)
{
a->cslew[m] = 0.5f * (1.0f - cos(theta));
a->cslew[m] = 0.5 * (1.0 - cos(theta));
theta += dtheta;
}
a->run = 1;
@ -658,8 +655,9 @@ void RMATCH::setRMatchSlewTime1(void* b, float slew_time)
void RMATCH::setRMatchPropRingMin(void* ptr, int prop_min)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
// Sleep(10);
decalc_rmatch(a);
a->prop_ringmin = prop_min;
calc_rmatch(a);
@ -669,8 +667,9 @@ void RMATCH::setRMatchPropRingMin(void* ptr, int prop_min)
void RMATCH::setRMatchPropRingMax(void* ptr, int prop_max)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
// Sleep(10);
decalc_rmatch(a);
a->prop_ringmax = prop_max; // must be a power of two
calc_rmatch(a);
@ -680,8 +679,9 @@ void RMATCH::setRMatchPropRingMax(void* ptr, int prop_max)
void RMATCH::setRMatchFFRingMin(void* ptr, int ff_ringmin)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
// Sleep(10);
decalc_rmatch(a);
a->ff_ringmin = ff_ringmin;
calc_rmatch(a);
@ -691,8 +691,9 @@ void RMATCH::setRMatchFFRingMin(void* ptr, int ff_ringmin)
void RMATCH::setRMatchFFRingMax(void* ptr, int ff_ringmax)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
// Sleep(10);
decalc_rmatch(a);
a->ff_ringmax = ff_ringmax; // must be a power of two
calc_rmatch(a);
@ -702,7 +703,7 @@ void RMATCH::setRMatchFFRingMax(void* ptr, int ff_ringmax)
void RMATCH::setRMatchFFAlpha(void* ptr, float ff_alpha)
{
auto *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
a->run = 0;
std::this_thread::sleep_for(std::chrono::seconds(10));
a->ff_alpha = ff_alpha;
@ -712,7 +713,7 @@ void RMATCH::setRMatchFFAlpha(void* ptr, float ff_alpha)
void RMATCH::getControlFlag(void* ptr, int* control_flag)
{
RMATCH const *a = (RMATCH*) ptr;
RMATCH *a = (RMATCH*) ptr;
*control_flag = a->control_flag;
}
@ -723,8 +724,8 @@ void* RMATCH::create_rmatchLegacyV(int in_size, int out_size, int nom_inrate, in
{
return (void*) create_rmatch(
1, // run
nullptr, // input buffer, stuffed in other calls
nullptr, // output buffer, stuffed in other calls
0, // input buffer, stuffed in other calls
0, // output buffer, stuffed in other calls
in_size, // input buffer size (complex samples)
out_size, // output buffer size (complex samples)
nom_inrate, // nominal input sample-rate
@ -739,12 +740,12 @@ void* RMATCH::create_rmatchLegacyV(int in_size, int out_size, int nom_inrate, in
1.0, // initial variable ratio
4096, // feed-forward moving average min size
262144, // feed-forward moving average max size - POWER OF TWO!
0.01f, // feed-forward exponential smoothing
0.01, // feed-forward exponential smoothing
4096, // proportional feedback min moving av ringsize
16384, // proportional feedback max moving av ringsize - POWER OF TWO!
1.0e-06f, // proportional feedback gain ***W4WMT - reduce loop gain a bit for PowerSDR to help Primary buffers > 512
1.0e-06, // proportional feedback gain ***W4WMT - reduce loop gain a bit for PowerSDR to help Primary buffers > 512
0, // linearly interpolate cvar by sample ***W4WMT - set varmode = 0 for PowerSDR (doesn't work otherwise!?!)
0.003f); // slew time (seconds)
0.003); // slew time (seconds)
}
} // namespace WDSP

View File

@ -157,6 +157,7 @@ void USLEW::setBuffers(float* _in, float* _out)
void USLEW::setSamplerate(int _rate)
{
decalc();
rate = _rate;
calc();
}
@ -176,6 +177,7 @@ void USLEW::setSize(int _size)
void USLEW::setuSlewTime(double _time)
{
// NOTE: 'time' is in seconds
decalc();
tupslew = _time;
calc();
}

View File

@ -85,6 +85,7 @@ public:
private:
void calc();
void decalc();
};
} // namespace WDSP

View File

@ -31,220 +31,236 @@ warren@wpratt.com
namespace WDSP {
void VARSAMP::calc()
void VARSAMP::calc_varsamp (VARSAMP *a)
{
float min_rate;
float max_rate;
float norm_rate;
float fc_norm_high;
float fc_norm_low;
nom_ratio = (float)out_rate / (float)in_rate;
cvar = var * nom_ratio;
inv_cvar = 1.0f / cvar;
old_inv_cvar = inv_cvar;
dicvar = 0.0;
delta = (float) fabs (1.0 / cvar - 1.0);
fc = fcin;
if (out_rate >= in_rate)
float min_rate, max_rate, norm_rate;
float fc_norm_high, fc_norm_low;
a->nom_ratio = (float)a->out_rate / (float)a->in_rate;
a->cvar = a->var * a->nom_ratio;
a->inv_cvar = 1.0 / a->cvar;
a->old_inv_cvar = a->inv_cvar;
a->dicvar = 0.0;
a->delta = fabs (1.0 / a->cvar - 1.0);
a->fc = a->fcin;
if (a->out_rate >= a->in_rate)
{
min_rate = (float)in_rate;
min_rate = (float)a->in_rate;
max_rate = (float)a->out_rate;
norm_rate = min_rate;
}
else
{
min_rate = (float)out_rate;
max_rate = (float)in_rate;
min_rate = (float)a->out_rate;
max_rate = (float)a->in_rate;
norm_rate = max_rate;
}
if (fc == 0.0) fc = 0.95f * 0.45f * min_rate;
fc_norm_high = fc / norm_rate;
if (fc_low < 0.0)
if (a->fc == 0.0) a->fc = 0.95 * 0.45 * min_rate;
fc_norm_high = a->fc / norm_rate;
if (a->fc_low < 0.0)
fc_norm_low = - fc_norm_high;
else
fc_norm_low = fc_low / norm_rate;
rsize = (int)(140.0 * norm_rate / min_rate);
ncoef = rsize + 1;
ncoef += (R - 1) * (ncoef - 1);
FIR::fir_bandpass(h, ncoef, fc_norm_low, fc_norm_high, (float)R, 1, 0, (float)R * gain);
ring.resize(rsize * 2);
idx_in = rsize - 1;
h_offset = 0.0;
hs.resize(rsize);
isamps = 0.0;
fc_norm_low = a->fc_low / norm_rate;
a->rsize = (int)(140.0 * norm_rate / min_rate);
a->ncoef = a->rsize + 1;
a->ncoef += (a->R - 1) * (a->ncoef - 1);
a->h = FIR::fir_bandpass(a->ncoef, fc_norm_low, fc_norm_high, (float)a->R, 1, 0, (float)a->R * a->gain);
// print_impulse ("imp.txt", a->ncoef, a->h, 0, 0);
a->ring = new float[a->rsize * 2]; // (float *)malloc0(a->rsize * sizeof(complex));
a->idx_in = a->rsize - 1;
a->h_offset = 0.0;
a->hs = new float[a->rsize]; // (float *)malloc0 (a->rsize * sizeof (float));
a->isamps = 0.0;
}
VARSAMP::VARSAMP(
int _run,
int _size,
float* _in,
float* _out,
int _in_rate,
int _out_rate,
float _fc,
float _fc_low,
int _R,
float _gain,
float _var,
int _varmode
void VARSAMP::decalc_varsamp (VARSAMP *a)
{
delete[] (a->hs);
delete[] (a->ring);
delete[] (a->h);
}
VARSAMP* VARSAMP::create_varsamp (
int run,
int size,
float* in,
float* out,
int in_rate,
int out_rate,
float fc,
float fc_low,
int R,
float gain,
float var,
int varmode
)
{
run = _run;
size = _size;
in = _in;
out = _out;
in_rate = _in_rate;
out_rate = _out_rate;
fcin = _fc;
fc_low = _fc_low;
R = _R;
gain = _gain;
var = _var;
varmode = _varmode;
calc();
VARSAMP *a = new VARSAMP;
a->run = run;
a->size = size;
a->in = in;
a->out = out;
a->in_rate = in_rate;
a->out_rate = out_rate;
a->fcin = fc;
a->fc_low = fc_low;
a->R = R;
a->gain = gain;
a->var = var;
a->varmode = varmode;
calc_varsamp (a);
return a;
}
void VARSAMP::flush()
void VARSAMP::destroy_varsamp (VARSAMP *a)
{
std::fill(ring.begin(), ring.end(), 0);
idx_in = rsize - 1;
h_offset = 0.0;
isamps = 0.0;
decalc_varsamp (a);
delete (a);
}
void VARSAMP::hshift()
void VARSAMP::flush_varsamp (VARSAMP *a)
{
int i;
int j;
int k;
std::fill(a->ring, a->ring + a->rsize * 2, 0);
a->idx_in = a->rsize - 1;
a->h_offset = 0.0;
a->isamps = 0.0;
}
void VARSAMP::hshift (VARSAMP *a)
{
int i, j, k;
int hidx;
float frac;
float pos;
pos = (float)R * h_offset;
float frac, pos;
pos = (float)a->R * a->h_offset;
hidx = (int)(pos);
frac = pos - (float)hidx;
for (i = rsize - 1, j = hidx, k = hidx + 1; i >= 0; i--, j += R, k += R)
hs[i] = h[j] + frac * (h[k] - h[j]);
for (i = a->rsize - 1, j = hidx, k = hidx + 1; i >= 0; i--, j += a->R, k += a->R)
a->hs[i] = a->h[j] + frac * (a->h[k] - a->h[j]);
}
int VARSAMP::execute(float _var)
int VARSAMP::xvarsamp (VARSAMP *a, float var)
{
int outsamps = 0;
uint64_t const* picvar;
uint64_t* picvar;
uint64_t N;
var = _var;
old_inv_cvar = inv_cvar;
cvar = var * nom_ratio;
inv_cvar = 1.0f / cvar;
if (varmode)
a->var = var;
a->old_inv_cvar = a->inv_cvar;
a->cvar = a->var * a->nom_ratio;
a->inv_cvar = 1.0 / a->cvar;
if (a->varmode)
{
dicvar = (inv_cvar - old_inv_cvar) / (float)size;
inv_cvar = old_inv_cvar;
a->dicvar = (a->inv_cvar - a->old_inv_cvar) / (float)a->size;
a->inv_cvar = a->old_inv_cvar;
}
else dicvar = 0.0;
if (run)
else a->dicvar = 0.0;
if (a->run)
{
int i, j;
int idx_out;
float I;
float Q;
for (int i = 0; i < size; i++)
float I, Q;
for (i = 0; i < a->size; i++)
{
ring[2 * idx_in + 0] = in[2 * i + 0];
ring[2 * idx_in + 1] = in[2 * i + 1];
inv_cvar += dicvar;
picvar = (uint64_t*)(&inv_cvar);
a->ring[2 * a->idx_in + 0] = a->in[2 * i + 0];
a->ring[2 * a->idx_in + 1] = a->in[2 * i + 1];
a->inv_cvar += a->dicvar;
picvar = (uint64_t*)(&a->inv_cvar);
N = *picvar & 0xffffffffffff0000;
inv_cvar = static_cast<float>(N);
delta = 1.0f - inv_cvar;
while (isamps < 1.0)
a->inv_cvar = static_cast<float>(N);
a->delta = 1.0 - a->inv_cvar;
while (a->isamps < 1.0)
{
I = 0.0;
Q = 0.0;
hshift();
h_offset += delta;
while (h_offset >= 1.0) h_offset -= 1.0f;
while (h_offset < 0.0) h_offset += 1.0f;
for (int j = 0; j < rsize; j++)
hshift (a);
a->h_offset += a->delta;
while (a->h_offset >= 1.0) a->h_offset -= 1.0;
while (a->h_offset < 0.0) a->h_offset += 1.0;
for (j = 0; j < a->rsize; j++)
{
if ((idx_out = idx_in + j) >= rsize) idx_out -= rsize;
I += hs[j] * ring[2 * idx_out + 0];
Q += hs[j] * ring[2 * idx_out + 1];
if ((idx_out = a->idx_in + j) >= a->rsize) idx_out -= a->rsize;
I += a->hs[j] * a->ring[2 * idx_out + 0];
Q += a->hs[j] * a->ring[2 * idx_out + 1];
}
out[2 * outsamps + 0] = I;
out[2 * outsamps + 1] = Q;
a->out[2 * outsamps + 0] = I;
a->out[2 * outsamps + 1] = Q;
outsamps++;
isamps += inv_cvar;
a->isamps += a->inv_cvar;
}
isamps -= 1.0f;
if (--idx_in < 0) idx_in = rsize - 1;
a->isamps -= 1.0;
if (--a->idx_in < 0) a->idx_in = a->rsize - 1;
}
}
else if (in != out)
std::copy( in, in + size * 2, out);
else if (a->in != a->out)
std::copy( a->in, a->in + a->size * 2, a->out);
return outsamps;
}
void VARSAMP::setBuffers(float* _in, float* _out)
void VARSAMP::setBuffers_varsamp (VARSAMP *a, float* in, float* out)
{
in = _in;
out = _out;
a->in = in;
a->out = out;
}
void VARSAMP::setSize(int _size)
void VARSAMP::setSize_varsamp (VARSAMP *a, int size)
{
size = _size;
flush();
a->size = size;
flush_varsamp (a);
}
void VARSAMP::setInRate(int _rate)
void VARSAMP::setInRate_varsamp (VARSAMP *a, int rate)
{
in_rate = _rate;
calc();
decalc_varsamp (a);
a->in_rate = rate;
calc_varsamp (a);
}
void VARSAMP::setOutRate(int _rate)
void VARSAMP::setOutRate_varsamp (VARSAMP *a, int rate)
{
out_rate = _rate;
calc();
decalc_varsamp (a);
a->out_rate = rate;
calc_varsamp (a);
}
void VARSAMP::setFCLow(float _fc_low)
void VARSAMP::setFCLow_varsamp (VARSAMP *a, float fc_low)
{
if (_fc_low != fc_low)
if (fc_low != a->fc_low)
{
fc_low = _fc_low;
calc();
decalc_varsamp (a);
a->fc_low = fc_low;
calc_varsamp (a);
}
}
void VARSAMP::setBandwidth(float _fc_low, float _fc_high)
void VARSAMP::setBandwidth_varsamp (VARSAMP *a, float fc_low, float fc_high)
{
if (_fc_low != fc_low || _fc_high != fcin)
if (fc_low != a->fc_low || fc_high != a->fcin)
{
fc_low = _fc_low;
fcin = _fc_high;
calc();
decalc_varsamp (a);
a->fc_low = fc_low;
a->fcin = fc_high;
calc_varsamp (a);
}
}
// exported calls
void* VARSAMP::create_varsampV (int _in_rate, int _out_rate, int R)
void* VARSAMP::create_varsampV (int in_rate, int out_rate, int R)
{
return (void *) new VARSAMP(1, 0, nullptr, nullptr, _in_rate, _out_rate, 0.0, -1.0, R, 1.0, 1.0, 1);
return (void *)create_varsamp (1, 0, 0, 0, in_rate, out_rate, 0.0, -1.0, R, 1.0, 1.0, 1);
}
void VARSAMP::xvarsampV (float* input, float* output, int numsamps, float var, int* outsamps, void* ptr)
{
auto *a = (VARSAMP*) ptr;
VARSAMP *a = (VARSAMP*) ptr;
a->in = input;
a->out = output;
a->size = numsamps;
*outsamps = a->execute(var);
*outsamps = xvarsamp(a, var);
}
void VARSAMP::destroy_varsampV (void* ptr)
{
delete (VARSAMP*) ptr;
destroy_varsamp ( (VARSAMP*) ptr );
}
} // namespace WDSP

View File

@ -28,8 +28,6 @@ warren@wpratt.com
#ifndef wdsp_varsamp_h
#define wdsp_varsamp_h
#include <vector>
#include "export.h"
namespace WDSP {
@ -49,9 +47,9 @@ public:
float gain;
int idx_in;
int ncoef;
std::vector<float> h;
float* h;
int rsize;
std::vector<float> ring;
float* ring;
float var;
int varmode;
float cvar;
@ -59,13 +57,13 @@ public:
float old_inv_cvar;
float dicvar;
float delta;
std::vector<float> hs;
float* hs;
int R;
float h_offset;
float isamps;
float nom_ratio;
VARSAMP(
static VARSAMP* create_varsamp (
int run,
int size,
float* in,
@ -79,26 +77,24 @@ public:
float var,
int varmode
);
VARSAMP(const VARSAMP&) = delete;
VARSAMP& operator=(VARSAMP& other) = delete;
~VARSAMP() = default;
void flush();
int execute(float var);
void setBuffers(float* in, float* out);
void setSize(int size);
void setInRate(int rate);
void setOutRate(int rate);
void setFCLow(float fc_low);
void setBandwidth(float fc_low, float fc_high);
static void destroy_varsamp (VARSAMP *a);
static void flush_varsamp (VARSAMP *a);
static int xvarsamp (VARSAMP *a, float var);
static void setBuffers_varsamp (VARSAMP *a, float* in, float* out);
static void setSize_varsamp (VARSAMP *a, int size);
static void setInRate_varsamp (VARSAMP *a, int rate);
static void setOutRate_varsamp (VARSAMP *a, int rate);
static void setFCLow_varsamp (VARSAMP *a, float fc_low);
static void setBandwidth_varsamp (VARSAMP *a, float fc_low, float fc_high);
// Exported calls
static void* create_varsampV (int in_rate, int out_rate, int R);
static void xvarsampV (float* input, float* output, int numsamps, float var, int* outsamps, void* ptr);
static void destroy_varsampV (void* ptr);
private:
void calc();
void hshift();
static void calc_varsamp (VARSAMP *a);
static void decalc_varsamp (VARSAMP *a);
static void hshift (VARSAMP *a);
};
} // namespace WDSP