1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-21 15:51:47 -05:00

WDSP: split RESAMPLE and RESAMPLEF

This commit is contained in:
f4exb 2024-07-17 21:50:58 +02:00
parent da7b3fbd41
commit 37a36532d0
5 changed files with 297 additions and 180 deletions

View File

@ -42,6 +42,7 @@ set(wdsp_SOURCES
osctrl.cpp
patchpanel.cpp
resample.cpp
resamplef.cpp
rmatch.cpp
RXA.cpp
sender.cpp
@ -98,6 +99,7 @@ set(wdsp_HEADERS
osctrl.hpp
patchpanel.hpp
resample.hpp
resamplef.hpp
rmatch.hpp
RXA.hpp
sender.hpp

View File

@ -42,43 +42,60 @@ void RESAMPLE::calc_resample (RESAMPLE *a)
int x, y, z;
int i, j, k;
int min_rate;
float full_rate;
float fc_norm_high, fc_norm_low;
double full_rate;
double fc_norm_high, fc_norm_low;
float* impulse;
a->fc = a->fcin;
a->ncoef = a->ncoefin;
x = a->in_rate;
y = a->out_rate;
while (y != 0)
{
z = y;
y = x % y;
x = z;
}
a->L = a->out_rate / x;
a->M = a->in_rate / x;
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 = (float)(a->in_rate * a->L);
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);
fc_norm_high = a->fc / full_rate;
if (a->fc_low < 0.0)
fc_norm_low = - fc_norm_high;
else
fc_norm_low = a->fc_low / full_rate;
if (a->ncoef == 0) a->ncoef = (int)(140.0 * full_rate / min_rate);
if (a->ncoef == 0)
a->ncoef = (int)(140.0 * full_rate / min_rate);
a->ncoef = (a->ncoef / a->L + 1) * a->L;
a->cpp = a->ncoef / a->L;
a->h = new double[a->ncoef]; // (float *)malloc0(a->ncoef * sizeof(float));
impulse = FIR::fir_bandpass(a->ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, a->gain * (float)a->L);
impulse = FIR::fir_bandpass(a->ncoef, fc_norm_low, fc_norm_high, 1.0, 1, 0, a->gain * (double)a->L);
i = 0;
for (j = 0; j < a->L; j++)
{
for (k = 0; k < a->ncoef; k += a->L)
a->h[i++] = impulse[j + k];
}
a->ringsize = a->cpp;
a->ring = new double[a->ringsize]; // (float *)malloc0(a->ringsize * sizeof(complex));
a->idx_in = a->ringsize - 1;
a->phnum = 0;
delete[] (impulse);
}
@ -135,6 +152,7 @@ void RESAMPLE::flush_resample (RESAMPLE *a)
int RESAMPLE::xresample (RESAMPLE *a)
{
int outsamps = 0;
if (a->run)
{
int i, j, n;
@ -145,28 +163,39 @@ int RESAMPLE::xresample (RESAMPLE *a)
{
a->ring[2 * a->idx_in + 0] = a->in[2 * i + 0];
a->ring[2 * a->idx_in + 1] = a->in[2 * i + 1];
while (a->phnum < a->L)
{
I = 0.0;
Q = 0.0;
n = a->cpp * a->phnum;
for (j = 0; j < a->cpp; j++)
{
if ((idx_out = a->idx_in + j) >= a->ringsize) idx_out -= a->ringsize;
if ((idx_out = a->idx_in + j) >= a->ringsize)
idx_out -= a->ringsize;
I += a->h[n + j] * a->ring[2 * idx_out + 0];
Q += a->h[n + j] * a->ring[2 * idx_out + 1];
}
a->out[2 * outsamps + 0] = I;
a->out[2 * outsamps + 1] = Q;
outsamps++;
a->phnum += a->M;
}
a->phnum -= a->L;
if (--a->idx_in < 0) a->idx_in = a->ringsize - 1;
if (--a->idx_in < 0)
a->idx_in = a->ringsize - 1;
}
}
else if (a->in != a->out)
{
std::copy( a->in, a->in + a->size * 2, a->out);
}
return outsamps;
}
@ -196,7 +225,7 @@ void RESAMPLE::setOutRate_resample(RESAMPLE *a, int rate)
calc_resample (a);
}
void RESAMPLE::setFCLow_resample (RESAMPLE *a, float fc_low)
void RESAMPLE::setFCLow_resample (RESAMPLE *a, double fc_low)
{
if (fc_low != a->fc_low)
{
@ -206,7 +235,7 @@ void RESAMPLE::setFCLow_resample (RESAMPLE *a, float fc_low)
}
}
void RESAMPLE::setBandwidth_resample (RESAMPLE *a, float fc_low, float fc_high)
void RESAMPLE::setBandwidth_resample (RESAMPLE *a, double fc_low, double fc_high)
{
if (fc_low != a->fc_low || fc_high != a->fcin)
{
@ -241,130 +270,4 @@ void RESAMPLE::destroy_resampleV (void* ptr)
destroy_resample ( (RESAMPLE*) ptr );
}
/************************************************************************************************
* *
* VERSION FOR NON-COMPLEX FLOATS *
* *
************************************************************************************************/
RESAMPLEF* RESAMPLEF::create_resampleF ( int run, int size, float* in, float* out, int in_rate, int out_rate)
{
RESAMPLEF *a = new RESAMPLEF;
int x, y, z;
int i, j, k;
int min_rate;
float full_rate;
float fc;
float fc_norm;
float* impulse;
a->run = run;
a->size = size;
a->in = in;
a->out = out;
x = in_rate;
y = out_rate;
while (y != 0)
{
z = y;
y = x % y;
x = z;
}
a->L = out_rate / x;
a->M = in_rate / x;
if (in_rate < out_rate) min_rate = in_rate;
else min_rate = out_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]; // (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 (j = 0; j < a->L; j ++)
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]; //(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);
}
void RESAMPLEF::flush_resampleF (RESAMPLEF *a)
{
memset (a->ring, 0, a->ringsize * sizeof (float));
a->idx_in = a->ringsize - 1;
a->phnum = 0;
}
int RESAMPLEF::xresampleF (RESAMPLEF *a)
{
int outsamps = 0;
if (a->run)
{
int i, j, n;
int idx_out;
float I;
for (i = 0; i < a->size; i++)
{
a->ring[a->idx_in] = (float)a->in[i];
while (a->phnum < a->L)
{
I = 0.0;
n = a->cpp * a->phnum;
for (j = 0; j < a->cpp; j++)
{
if ((idx_out = a->idx_in + j) >= a->ringsize) idx_out -= a->ringsize;
I += a->h[n + j] * a->ring[idx_out];
}
a->out[outsamps] = (float)I;
outsamps++;
a->phnum += a->M;
}
a->phnum -= a->L;
if (--a->idx_in < 0) a->idx_in = a->ringsize - 1;
}
}
else if (a->in != a->out)
memcpy (a->out, a->in, a->size * sizeof (float));
return outsamps;
}
// Exported calls
void* RESAMPLEF::create_resampleFV (int in_rate, int 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)
{
RESAMPLEF *a = (RESAMPLEF*) ptr;
a->in = input;
a->out = output;
a->size = numsamps;
*outsamps = xresampleF(a);
}
void RESAMPLEF::destroy_resampleFV (void* ptr)
{
destroy_resampleF ( (RESAMPLEF*) ptr );
}
} // namespace WDSP

View File

@ -80,8 +80,8 @@ public:
static void setSize_resample(RESAMPLE *a, int size);
static void setInRate_resample(RESAMPLE *a, int rate);
static void setOutRate_resample(RESAMPLE *a, int rate);
static void setFCLow_resample (RESAMPLE *a, float fc_low);
static void setBandwidth_resample (RESAMPLE *a, float fc_low, float fc_high);
static void setFCLow_resample (RESAMPLE *a, double fc_low);
static void setBandwidth_resample (RESAMPLE *a, double fc_low, double fc_high);
// Exported calls
static void* create_resampleV (int in_rate, int out_rate);
static void xresampleV (float* input, float* output, int numsamps, int* outsamps, void* ptr);
@ -92,46 +92,6 @@ private:
static void decalc_resample (RESAMPLE *a);
};
#endif
/************************************************************************************************
* *
* VERSION FOR NON-COMPLEX FLOATS *
* *
************************************************************************************************/
#ifndef wdsp_resampleF_h
#define wdsp_resampleF_h
#include "export.h"
class WDSP_API RESAMPLEF
{
public:
int run; // run
int size; // number of input samples per buffer
float* in; // input buffer for resampler
float* out; // output buffer for resampler
int idx_in; // index for input into ring
int ncoef; // number of coefficients
int L; // interpolation factor
int M; // decimation factor
float* h; // coefficients
int ringsize; // number of values the ring buffer holds
float* ring; // ring buffer
int cpp; // coefficients of the phase
int phnum; // phase number
static RESAMPLEF* create_resampleF (int run, int size, float* in, float* out, int in_rate, int out_rate);
static void destroy_resampleF (RESAMPLEF *a);
static void flush_resampleF (RESAMPLEF *a);
static int xresampleF (RESAMPLEF *a);
// Exported calls
static void* create_resampleFV (int in_rate, int out_rate);
static void xresampleFV (float* input, float* output, int numsamps, int* outsamps, void* ptr);
static void destroy_resampleFV (void* ptr);
};
} // namespace WDSP
#endif

182
wdsp/resamplef.cpp Normal file
View File

@ -0,0 +1,182 @@
/* 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 "resamplef.hpp"
namespace WDSP {
/************************************************************************************************
* *
* VERSION FOR NON-COMPLEX FLOATS *
* *
************************************************************************************************/
RESAMPLEF* RESAMPLEF::create_resampleF ( int run, int size, float* in, float* out, int in_rate, int out_rate)
{
RESAMPLEF *a = new RESAMPLEF;
int x, y, z;
int i, j, k;
int min_rate;
float full_rate;
float fc;
float fc_norm;
float* impulse;
a->run = run;
a->size = size;
a->in = in;
a->out = out;
x = in_rate;
y = out_rate;
while (y != 0)
{
z = y;
y = x % y;
x = z;
}
a->L = out_rate / x;
a->M = in_rate / x;
if (in_rate < out_rate)
min_rate = in_rate;
else
min_rate = out_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]; // (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 (j = 0; j < a->L; j ++)
{
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]; //(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);
}
void RESAMPLEF::flush_resampleF (RESAMPLEF *a)
{
memset (a->ring, 0, a->ringsize * sizeof (float));
a->idx_in = a->ringsize - 1;
a->phnum = 0;
}
int RESAMPLEF::xresampleF (RESAMPLEF *a)
{
int outsamps = 0;
if (a->run)
{
int i, j, n;
int idx_out;
float I;
for (i = 0; i < a->size; i++)
{
a->ring[a->idx_in] = (float)a->in[i];
while (a->phnum < a->L)
{
I = 0.0;
n = a->cpp * a->phnum;
for (j = 0; j < a->cpp; j++)
{
if ((idx_out = a->idx_in + j) >= a->ringsize)
idx_out -= a->ringsize;
I += a->h[n + j] * a->ring[idx_out];
}
a->out[outsamps] = (float)I;
outsamps++;
a->phnum += a->M;
}
a->phnum -= a->L;
if (--a->idx_in < 0)
a->idx_in = a->ringsize - 1;
}
}
else if (a->in != a->out)
{
memcpy (a->out, a->in, a->size * sizeof (float));
}
return outsamps;
}
// Exported calls
void* RESAMPLEF::create_resampleFV (int in_rate, int 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)
{
RESAMPLEF *a = (RESAMPLEF*) ptr;
a->in = input;
a->out = output;
a->size = numsamps;
*outsamps = xresampleF(a);
}
void RESAMPLEF::destroy_resampleFV (void* ptr)
{
destroy_resampleF ( (RESAMPLEF*) ptr );
}
} // namespace WDSP

70
wdsp/resamplef.hpp Normal file
View File

@ -0,0 +1,70 @@
/* resample.h
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
*/
/************************************************************************************************
* *
* VERSION FOR NON-COMPLEX FLOATS *
* *
************************************************************************************************/
#ifndef wdsp_resampleF_h
#define wdsp_resampleF_h
#include "export.h"
namespace WDSP {
class WDSP_API RESAMPLEF
{
public:
int run; // run
int size; // number of input samples per buffer
float* in; // input buffer for resampler
float* out; // output buffer for resampler
int idx_in; // index for input into ring
int ncoef; // number of coefficients
int L; // interpolation factor
int M; // decimation factor
float* h; // coefficients
int ringsize; // number of values the ring buffer holds
float* ring; // ring buffer
int cpp; // coefficients of the phase
int phnum; // phase number
static RESAMPLEF* create_resampleF (int run, int size, float* in, float* out, int in_rate, int out_rate);
static void destroy_resampleF (RESAMPLEF *a);
static void flush_resampleF (RESAMPLEF *a);
static int xresampleF (RESAMPLEF *a);
// Exported calls
static void* create_resampleFV (int in_rate, int out_rate);
static void xresampleFV (float* input, float* output, int numsamps, int* outsamps, void* ptr);
static void destroy_resampleFV (void* ptr);
};
} // namespace WDSP
#endif