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

280 lines
8.6 KiB
C++
Raw Normal View History

2024-06-16 05:31:13 -04:00
/* bandpass.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2013, 2016, 2017 Warren Pratt, NR0V
Copyright (C) 2024 Edouard Griffiths, F4EXB Adapted to SDRangel
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The author can be reached by email at
warren@wpratt.com
*/
#include "comm.hpp"
#include "bps.hpp"
#include "fir.hpp"
#include "bandpass.hpp"
#include "RXA.hpp"
#include "TXA.hpp"
namespace WDSP {
/********************************************************************************************************
* *
* Overlap-Save Bandpass *
* *
********************************************************************************************************/
void BPS::calc_bps (BPS *a)
{
2024-06-24 21:50:48 -04:00
float* impulse;
2024-07-16 17:15:13 -04:00
a->infilt = new float[2 * a->size * 2];
a->product = new float[2 * a->size * 2];
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults(2 * a->size, impulse);
2024-06-24 21:50:48 -04:00
a->CFor = fftwf_plan_dft_1d(2 * a->size, (fftwf_complex *)a->infilt, (fftwf_complex *)a->product, FFTW_FORWARD, FFTW_PATIENT);
a->CRev = fftwf_plan_dft_1d(2 * a->size, (fftwf_complex *)a->product, (fftwf_complex *)a->out, FFTW_BACKWARD, FFTW_PATIENT);
2024-06-16 05:31:13 -04:00
delete[](impulse);
}
void BPS::decalc_bps (BPS *a)
{
2024-06-24 21:50:48 -04:00
fftwf_destroy_plan(a->CRev);
fftwf_destroy_plan(a->CFor);
2024-06-16 05:31:13 -04:00
delete[] (a->mults);
delete[] (a->product);
delete[] (a->infilt);
}
2024-06-24 21:50:48 -04:00
BPS* BPS::create_bps (int run, int position, int size, float* in, float* out,
float f_low, float f_high, int samplerate, int wintype, float gain)
2024-06-16 05:31:13 -04:00
{
BPS *a = new BPS;
a->run = run;
a->position = position;
a->size = size;
2024-06-24 21:50:48 -04:00
a->samplerate = (float)samplerate;
2024-06-16 05:31:13 -04:00
a->wintype = wintype;
a->gain = gain;
a->in = in;
a->out = out;
a->f_low = f_low;
a->f_high = f_high;
calc_bps (a);
return a;
}
void BPS::destroy_bps (BPS *a)
{
decalc_bps (a);
delete a;
}
void BPS::flush_bps (BPS *a)
{
2024-07-16 17:15:13 -04:00
std::fill(a->infilt, a->infilt + 2 * a->size * 2, 0);
2024-06-16 05:31:13 -04:00
}
void BPS::xbps (BPS *a, int pos)
{
int i;
2024-06-24 21:50:48 -04:00
float I, Q;
2024-06-16 05:31:13 -04:00
if (a->run && pos == a->position)
{
2024-07-16 17:15:13 -04:00
std::copy(a->in, a->in + a->size * 2, &(a->infilt[2 * a->size]));
2024-06-24 21:50:48 -04:00
fftwf_execute (a->CFor);
2024-06-16 05:31:13 -04:00
for (i = 0; i < 2 * a->size; i++)
{
I = a->gain * a->product[2 * i + 0];
Q = a->gain * a->product[2 * i + 1];
a->product[2 * i + 0] = I * a->mults[2 * i + 0] - Q * a->mults[2 * i + 1];
a->product[2 * i + 1] = I * a->mults[2 * i + 1] + Q * a->mults[2 * i + 0];
}
2024-06-24 21:50:48 -04:00
fftwf_execute (a->CRev);
2024-07-16 17:15:13 -04:00
std::copy(&(a->infilt[2 * a->size]), &(a->infilt[2 * a->size]) + a->size * 2, a->infilt);
2024-06-16 05:31:13 -04:00
}
else if (a->in != a->out)
2024-07-16 17:15:13 -04:00
std::copy( a->in, a->in + a->size * 2, a->out);
2024-06-16 05:31:13 -04:00
}
2024-06-24 21:50:48 -04:00
void BPS::setBuffers_bps (BPS *a, float* in, float* out)
2024-06-16 05:31:13 -04:00
{
decalc_bps (a);
a->in = in;
a->out = out;
calc_bps (a);
}
void BPS::setSamplerate_bps (BPS *a, int rate)
{
decalc_bps (a);
a->samplerate = rate;
calc_bps (a);
}
void BPS::setSize_bps (BPS *a, int size)
{
decalc_bps (a);
a->size = size;
calc_bps (a);
}
2024-06-24 21:50:48 -04:00
void BPS::setFreqs_bps (BPS *a, float f_low, float f_high)
2024-06-16 05:31:13 -04:00
{
decalc_bps (a);
a->f_low = f_low;
a->f_high = f_high;
calc_bps (a);
}
/********************************************************************************************************
* *
* Overlap-Save Bandpass: RXA Properties *
* *
********************************************************************************************************/
void BPS::SetBPSRun (RXA& rxa, int run)
{
rxa.bp1.p->run = run;
}
2024-06-24 21:50:48 -04:00
void BPS::SetBPSFreqs (RXA& rxa, float f_low, float f_high)
2024-06-16 05:31:13 -04:00
{
2024-06-24 21:50:48 -04:00
float* impulse;
2024-06-16 05:31:13 -04:00
BPS *a1;
a1 = rxa.bps1.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if ((f_low != a1->f_low) || (f_high != a1->f_high))
{
a1->f_low = f_low;
a1->f_high = f_high;
delete[] (a1->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a1->size + 1, f_low, f_high, a1->samplerate, a1->wintype, 1, 1.0 / (float)(2 * a1->size));
2024-06-16 05:31:13 -04:00
a1->mults = FIR::fftcv_mults (2 * a1->size, impulse);
delete[] (impulse);
}
}
void BPS::SetBPSWindow (RXA& rxa, int wintype)
{
2024-06-24 21:50:48 -04:00
float* impulse;
2024-06-16 05:31:13 -04:00
BPS *a1;
a1 = rxa.bps1.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if ((a1->wintype != wintype))
{
a1->wintype = wintype;
delete[] (a1->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a1->size + 1, a1->f_low, a1->f_high, a1->samplerate, a1->wintype, 1, 1.0 / (float)(2 * a1->size));
2024-06-16 05:31:13 -04:00
a1->mults = FIR::fftcv_mults (2 * a1->size, impulse);
delete[] (impulse);
}
}
/********************************************************************************************************
* *
* TXA Properties *
* *
********************************************************************************************************/
// UNCOMMENT properties when pointers in place in txa
void BPS::SetBPSRun (TXA& txa, int run)
{
txa.bp1.p->run = run;
}
2024-06-24 21:50:48 -04:00
void BPS::SetBPSFreqs (TXA& txa, float f_low, float f_high)
2024-06-16 05:31:13 -04:00
{
2024-06-24 21:50:48 -04:00
float* impulse;
2024-06-16 05:31:13 -04:00
BPS *a;
a = txa.bps0.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if ((f_low != a->f_low) || (f_high != a->f_high))
{
a->f_low = f_low;
a->f_high = f_high;
delete[] (a->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a->size + 1, f_low, f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
a = txa.bps1.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if ((f_low != a->f_low) || (f_high != a->f_high))
{
a->f_low = f_low;
a->f_high = f_high;
delete[] (a->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a->size + 1, f_low, f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
a = txa.bps2.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if ((f_low != a->f_low) || (f_high != a->f_high))
{
a->f_low = f_low;
a->f_high = f_high;
delete[] (a->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a->size + 1, f_low, f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
}
void BPS::SetBPSWindow (TXA& txa, int wintype)
{
2024-06-24 21:50:48 -04:00
float* impulse;
2024-06-16 05:31:13 -04:00
BPS *a;
a = txa.bps0.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if (a->wintype != wintype)
{
a->wintype = wintype;
delete[] (a->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
a = txa.bps1.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if (a->wintype != wintype)
{
a->wintype = wintype;
delete[] (a->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass(a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
a = txa.bps2.p;
2024-07-13 17:59:46 -04:00
2024-06-16 05:31:13 -04:00
if (a->wintype != wintype)
{
a->wintype = wintype;
delete[] (a->mults);
2024-06-24 21:50:48 -04:00
impulse = FIR::fir_bandpass (a->size + 1, a->f_low, a->f_high, a->samplerate, a->wintype, 1, 1.0 / (float)(2 * a->size));
2024-06-16 05:31:13 -04:00
a->mults = FIR::fftcv_mults (2 * a->size, impulse);
delete[] (impulse);
}
}
} // namespace WDSP