2024-06-16 05:31:13 -04:00
|
|
|
/* resample.c
|
|
|
|
|
|
|
|
This file is part of a program that implements a Software-Defined Radio.
|
|
|
|
|
|
|
|
Copyright (C) 2013 Warren Pratt, NR0V
|
|
|
|
Copyright (C) 2024 Edouard Griffiths, F4EXB Adapted to SDRangel
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
The author can be reached by email at
|
|
|
|
|
|
|
|
warren@wpratt.com
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "comm.hpp"
|
|
|
|
#include "fir.hpp"
|
|
|
|
#include "resample.hpp"
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
|
|
|
/************************************************************************************************
|
|
|
|
* *
|
|
|
|
* VERSION FOR COMPLEX DOUBLE-PRECISION *
|
|
|
|
* *
|
|
|
|
************************************************************************************************/
|
|
|
|
|
2024-07-23 22:52:00 -04:00
|
|
|
void RESAMPLE::calc()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
int x, y, z;
|
|
|
|
int i, j, k;
|
|
|
|
int min_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
double full_rate;
|
|
|
|
double fc_norm_high, fc_norm_low;
|
2024-06-24 21:50:48 -04:00
|
|
|
float* impulse;
|
2024-07-23 22:33:42 -04:00
|
|
|
fc = fcin;
|
|
|
|
ncoef = ncoefin;
|
|
|
|
x = in_rate;
|
|
|
|
y = out_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
while (y != 0)
|
|
|
|
{
|
|
|
|
z = y;
|
|
|
|
y = x % y;
|
|
|
|
x = z;
|
|
|
|
}
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
L = out_rate / x;
|
|
|
|
M = in_rate / x;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
L = L <= 0 ? 1 : L;
|
|
|
|
M = M <= 0 ? 1 : M;
|
2024-07-19 02:10:54 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
if (in_rate < out_rate)
|
|
|
|
min_rate = in_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
else
|
2024-07-23 22:33:42 -04:00
|
|
|
min_rate = out_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
if (fc == 0.0)
|
|
|
|
fc = 0.45 * (float)min_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
full_rate = (double) (in_rate * L);
|
|
|
|
fc_norm_high = fc / full_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
if (fc_low < 0.0)
|
2024-06-16 05:31:13 -04:00
|
|
|
fc_norm_low = - fc_norm_high;
|
|
|
|
else
|
2024-07-23 22:33:42 -04:00
|
|
|
fc_norm_low = fc_low / full_rate;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
if (ncoef == 0)
|
|
|
|
ncoef = (int)(140.0 * full_rate / min_rate);
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
ncoef = (ncoef / L + 1) * L;
|
|
|
|
cpp = ncoef / L;
|
|
|
|
h = new double[ncoef]; // (float *)malloc0(ncoef * sizeof(float));
|
|
|
|
impulse = FIR::fir_bandpass(ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, gain * (double)L);
|
2024-06-16 05:31:13 -04:00
|
|
|
i = 0;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
for (j = 0; j < L; j++)
|
2024-07-17 15:50:58 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
for (k = 0; k < ncoef; k += L)
|
|
|
|
h[i++] = impulse[j + k];
|
2024-07-17 15:50:58 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
ringsize = cpp;
|
|
|
|
ring = new double[ringsize]; // (float *)malloc0(ringsize * sizeof(complex));
|
|
|
|
idx_in = ringsize - 1;
|
|
|
|
phnum = 0;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
delete[] (impulse);
|
|
|
|
}
|
|
|
|
|
2024-07-23 22:52:00 -04:00
|
|
|
void RESAMPLE::decalc()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
delete[] ring;
|
|
|
|
delete[] h;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
RESAMPLE::RESAMPLE (
|
|
|
|
int _run,
|
|
|
|
int _size,
|
|
|
|
float* _in,
|
|
|
|
float* _out,
|
|
|
|
int _in_rate,
|
|
|
|
int _out_rate,
|
|
|
|
double _fc,
|
|
|
|
int _ncoef,
|
|
|
|
double _gain
|
2024-07-23 22:52:00 -04:00
|
|
|
) :
|
|
|
|
run(_run),
|
|
|
|
size(_size),
|
|
|
|
in(_in),
|
|
|
|
out(_out),
|
|
|
|
in_rate(_in_rate),
|
|
|
|
out_rate(_out_rate),
|
|
|
|
fcin(_fc),
|
|
|
|
fc_low(-1.0), // could add to create_resample() parameters
|
|
|
|
ncoefin(_ncoef),
|
|
|
|
gain(_gain)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:52:00 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
RESAMPLE::~RESAMPLE()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:52:00 -04:00
|
|
|
decalc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::flush()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
std::fill(ring, ring + 2 * ringsize, 0);
|
|
|
|
idx_in = ringsize - 1;
|
|
|
|
phnum = 0;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
int RESAMPLE::execute()
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
int outsamps = 0;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
if (run)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
int i, j, n;
|
|
|
|
int idx_out;
|
2024-07-06 16:42:07 -04:00
|
|
|
double I, Q;
|
2024-06-16 05:31:13 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
for (i = 0; i < size; i++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
ring[2 * idx_in + 0] = in[2 * i + 0];
|
|
|
|
ring[2 * idx_in + 1] = in[2 * i + 1];
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
while (phnum < L)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
I = 0.0;
|
|
|
|
Q = 0.0;
|
2024-07-23 22:33:42 -04:00
|
|
|
n = cpp * phnum;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
for (j = 0; j < cpp; j++)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
if ((idx_out = idx_in + j) >= ringsize)
|
|
|
|
idx_out -= ringsize;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
I += h[n + j] * ring[2 * idx_out + 0];
|
|
|
|
Q += h[n + j] * ring[2 * idx_out + 1];
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
out[2 * outsamps + 0] = I;
|
|
|
|
out[2 * outsamps + 1] = Q;
|
2024-06-16 05:31:13 -04:00
|
|
|
outsamps++;
|
2024-07-23 22:33:42 -04:00
|
|
|
phnum += M;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
phnum -= L;
|
2024-07-17 15:50:58 -04:00
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
if (--idx_in < 0)
|
|
|
|
idx_in = ringsize - 1;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
}
|
2024-07-23 22:33:42 -04:00
|
|
|
else if (in != out)
|
2024-07-17 15:50:58 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
std::copy( in, in + size * 2, out);
|
2024-07-17 15:50:58 -04:00
|
|
|
}
|
|
|
|
|
2024-06-16 05:31:13 -04:00
|
|
|
return outsamps;
|
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::setBuffers(float* _in, float* _out)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
in = _in;
|
|
|
|
out = _out;
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::setSize(int _size)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
size = _size;
|
|
|
|
flush();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::setInRate(int _rate)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:52:00 -04:00
|
|
|
decalc();
|
2024-07-23 22:33:42 -04:00
|
|
|
in_rate = _rate;
|
2024-07-23 22:52:00 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::setOutRate(int _rate)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:52:00 -04:00
|
|
|
decalc();
|
2024-07-23 22:33:42 -04:00
|
|
|
out_rate = _rate;
|
2024-07-23 22:52:00 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::setFCLow(double _fc_low)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
if (fc_low != _fc_low)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:52:00 -04:00
|
|
|
decalc();
|
2024-07-23 22:33:42 -04:00
|
|
|
fc_low = _fc_low;
|
2024-07-23 22:52:00 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::setBandwidth(double _fc_low, double _fc_high)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
if (fc_low != _fc_low || _fc_high != fcin)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:52:00 -04:00
|
|
|
decalc();
|
2024-07-23 22:33:42 -04:00
|
|
|
fc_low = _fc_low;
|
|
|
|
fcin = _fc_high;
|
2024-07-23 22:52:00 -04:00
|
|
|
calc();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// exported calls
|
|
|
|
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void* RESAMPLE::createV (int in_rate, int out_rate)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
return (void *) new RESAMPLE(1, 0, 0, 0, in_rate, out_rate, 0.0, 0, 1.0);
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::executeV (float* input, float* output, int numsamps, int* outsamps, void* ptr)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
|
|
|
RESAMPLE *a = (RESAMPLE*) ptr;
|
|
|
|
a->in = input;
|
|
|
|
a->out = output;
|
|
|
|
a->size = numsamps;
|
2024-07-23 22:33:42 -04:00
|
|
|
*outsamps = a->execute();
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-07-23 22:33:42 -04:00
|
|
|
void RESAMPLE::destroyV (void* ptr)
|
2024-06-16 05:31:13 -04:00
|
|
|
{
|
2024-07-23 22:33:42 -04:00
|
|
|
delete ( (RESAMPLE*) ptr );
|
2024-06-16 05:31:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace WDSP
|