1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-21 12:26:34 -04:00
sdrangel/wdsp/resample.cpp

277 lines
6.4 KiB
C++
Raw Normal View History

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 *
* *
************************************************************************************************/
void RESAMPLE::calc_resample (RESAMPLE *a)
{
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-06-16 05:31:13 -04:00
a->fc = a->fcin;
a->ncoef = a->ncoefin;
x = a->in_rate;
y = a->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-06-16 05:31:13 -04:00
a->L = a->out_rate / x;
a->M = a->in_rate / x;
2024-07-17 15:50:58 -04:00
2024-07-21 08:20:48 -04:00
a->L = a->L <= 0 ? 1 : a->L;
a->M = a->M <= 0 ? 1 : a->M;
2024-07-19 02:10:54 -04:00
2024-07-17 15:50:58 -04:00
if (a->in_rate < a->out_rate)
min_rate = a->in_rate;
else
min_rate = a->out_rate;
if (a->fc == 0.0)
a->fc = 0.45 * (float)min_rate;
full_rate = (double) (a->in_rate * a->L);
2024-06-16 05:31:13 -04:00
fc_norm_high = a->fc / full_rate;
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
if (a->fc_low < 0.0)
fc_norm_low = - fc_norm_high;
else
fc_norm_low = a->fc_low / full_rate;
2024-07-17 15:50:58 -04:00
if (a->ncoef == 0)
a->ncoef = (int)(140.0 * full_rate / min_rate);
2024-06-16 05:31:13 -04:00
a->ncoef = (a->ncoef / a->L + 1) * a->L;
a->cpp = a->ncoef / a->L;
2024-07-06 16:42:07 -04:00
a->h = new double[a->ncoef]; // (float *)malloc0(a->ncoef * sizeof(float));
2024-07-17 15:50:58 -04:00
impulse = FIR::fir_bandpass(a->ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, a->gain * (double)a->L);
2024-06-16 05:31:13 -04:00
i = 0;
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
for (j = 0; j < a->L; j++)
2024-07-17 15:50:58 -04:00
{
2024-06-16 05:31:13 -04:00
for (k = 0; k < a->ncoef; k += a->L)
a->h[i++] = impulse[j + k];
2024-07-17 15:50:58 -04:00
}
2024-06-16 05:31:13 -04:00
a->ringsize = a->cpp;
2024-07-06 16:42:07 -04:00
a->ring = new double[a->ringsize]; // (float *)malloc0(a->ringsize * sizeof(complex));
2024-06-16 05:31:13 -04:00
a->idx_in = a->ringsize - 1;
a->phnum = 0;
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
delete[] (impulse);
}
void RESAMPLE::decalc_resample (RESAMPLE *a)
{
delete[] (a->ring);
delete[] (a->h);
}
2024-07-06 16:42:07 -04:00
RESAMPLE* RESAMPLE::create_resample (
int run,
int size,
float* in,
float* out,
int in_rate,
int out_rate,
double fc,
int ncoef,
double gain
)
2024-06-16 05:31:13 -04:00
{
RESAMPLE *a = new RESAMPLE;
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 = -1.0; // could add to create_resample() parameters
a->ncoefin = ncoef;
a->gain = gain;
calc_resample (a);
return a;
}
void RESAMPLE::destroy_resample (RESAMPLE *a)
{
decalc_resample (a);
delete (a);
}
void RESAMPLE::flush_resample (RESAMPLE *a)
{
2024-07-06 16:42:07 -04:00
std::fill(a->ring, a->ring + 2 * a->ringsize, 0);
2024-06-16 05:31:13 -04:00
a->idx_in = a->ringsize - 1;
a->phnum = 0;
}
int RESAMPLE::xresample (RESAMPLE *a)
{
int outsamps = 0;
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
if (a->run)
{
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
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];
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
while (a->phnum < a->L)
{
I = 0.0;
Q = 0.0;
n = a->cpp * a->phnum;
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
for (j = 0; j < a->cpp; j++)
{
2024-07-17 15:50:58 -04:00
if ((idx_out = a->idx_in + j) >= a->ringsize)
idx_out -= a->ringsize;
2024-06-16 05:31:13 -04:00
I += a->h[n + j] * a->ring[2 * idx_out + 0];
Q += a->h[n + j] * a->ring[2 * idx_out + 1];
}
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
a->out[2 * outsamps + 0] = I;
a->out[2 * outsamps + 1] = Q;
outsamps++;
a->phnum += a->M;
}
2024-07-17 15:50:58 -04:00
2024-06-16 05:31:13 -04:00
a->phnum -= a->L;
2024-07-17 15:50:58 -04:00
if (--a->idx_in < 0)
a->idx_in = a->ringsize - 1;
2024-06-16 05:31:13 -04:00
}
}
else if (a->in != a->out)
2024-07-17 15:50:58 -04:00
{
2024-07-16 17:15:13 -04:00
std::copy( a->in, a->in + a->size * 2, a->out);
2024-07-17 15:50:58 -04:00
}
2024-06-16 05:31:13 -04:00
return outsamps;
}
2024-06-24 21:50:48 -04:00
void RESAMPLE::setBuffers_resample(RESAMPLE *a, float* in, float* out)
2024-06-16 05:31:13 -04:00
{
a->in = in;
a->out = out;
}
void RESAMPLE::setSize_resample(RESAMPLE *a, int size)
{
a->size = size;
flush_resample (a);
}
void RESAMPLE::setInRate_resample(RESAMPLE *a, int rate)
{
decalc_resample (a);
a->in_rate = rate;
calc_resample (a);
}
void RESAMPLE::setOutRate_resample(RESAMPLE *a, int rate)
{
decalc_resample (a);
a->out_rate = rate;
calc_resample (a);
}
2024-07-17 15:50:58 -04:00
void RESAMPLE::setFCLow_resample (RESAMPLE *a, double fc_low)
2024-06-16 05:31:13 -04:00
{
if (fc_low != a->fc_low)
{
decalc_resample (a);
a->fc_low = fc_low;
calc_resample (a);
}
}
2024-07-17 15:50:58 -04:00
void RESAMPLE::setBandwidth_resample (RESAMPLE *a, double fc_low, double fc_high)
2024-06-16 05:31:13 -04:00
{
if (fc_low != a->fc_low || fc_high != a->fcin)
{
decalc_resample (a);
a->fc_low = fc_low;
a->fcin = fc_high;
calc_resample (a);
}
}
// exported calls
void* RESAMPLE::create_resampleV (int in_rate, int out_rate)
{
return (void *)create_resample (1, 0, 0, 0, in_rate, out_rate, 0.0, 0, 1.0);
}
2024-06-24 21:50:48 -04:00
void RESAMPLE::xresampleV (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;
*outsamps = xresample(a);
}
void RESAMPLE::destroy_resampleV (void* ptr)
{
destroy_resample ( (RESAMPLE*) ptr );
}
} // namespace WDSP