2024-06-16 09:31:13 +00:00
|
|
|
/* emph.c
|
|
|
|
|
|
|
|
This file is part of a program that implements a Software-Defined Radio.
|
|
|
|
|
|
|
|
Copyright (C) 2014, 2016, 2023 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 "emph.hpp"
|
|
|
|
#include "fcurve.hpp"
|
|
|
|
#include "firmin.hpp"
|
|
|
|
#include "TXA.hpp"
|
|
|
|
|
|
|
|
namespace WDSP {
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* Partitioned Overlap-Save FM Pre-Emphasis *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
2024-06-25 01:50:48 +00:00
|
|
|
EMPHP* EMPHP::create_emphp (int run, int position, int size, int nc, int mp, float* in, float* out, int rate, int ctype, float f_low, float f_high)
|
2024-06-16 09:31:13 +00:00
|
|
|
{
|
|
|
|
EMPHP *a = new EMPHP;
|
2024-06-25 01:50:48 +00:00
|
|
|
float* impulse;
|
2024-06-16 09:31:13 +00:00
|
|
|
a->run = run;
|
|
|
|
a->position = position;
|
|
|
|
a->size = size;
|
|
|
|
a->nc = nc;
|
|
|
|
a->mp = mp;
|
|
|
|
a->in = in;
|
|
|
|
a->out = out;
|
|
|
|
a->rate = rate;
|
|
|
|
a->ctype = ctype;
|
|
|
|
a->f_low = f_low;
|
|
|
|
a->f_high = f_high;
|
|
|
|
impulse = FCurve::fc_impulse (a->nc, a->f_low, a->f_high, -20.0 * log10(a->f_high / a->f_low), 0.0, a->ctype, a->rate, 1.0 / (2.0 * a->size), 0, 0);
|
|
|
|
a->p = FIRCORE::create_fircore (a->size, a->in, a->out, a->nc, a->mp, impulse);
|
|
|
|
delete[] (impulse);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::destroy_emphp (EMPHP *a)
|
|
|
|
{
|
|
|
|
FIRCORE::destroy_fircore (a->p);
|
|
|
|
delete (a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::flush_emphp (EMPHP *a)
|
|
|
|
{
|
|
|
|
FIRCORE::flush_fircore (a->p);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::xemphp (EMPHP *a, int position)
|
|
|
|
{
|
|
|
|
if (a->run && a->position == position)
|
|
|
|
FIRCORE::xfircore (a->p);
|
|
|
|
else if (a->in != a->out)
|
2024-06-24 20:23:10 +00:00
|
|
|
memcpy (a->out, a->in, a->size * sizeof (wcomplex));
|
2024-06-16 09:31:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 01:50:48 +00:00
|
|
|
void EMPHP::setBuffers_emphp (EMPHP *a, float* in, float* out)
|
2024-06-16 09:31:13 +00:00
|
|
|
{
|
|
|
|
a->in = in;
|
|
|
|
a->out = out;
|
|
|
|
FIRCORE::setBuffers_fircore (a->p, a->in, a->out);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::setSamplerate_emphp (EMPHP *a, int rate)
|
|
|
|
{
|
2024-06-25 01:50:48 +00:00
|
|
|
float* impulse;
|
2024-06-16 09:31:13 +00:00
|
|
|
a->rate = rate;
|
|
|
|
impulse = FCurve::fc_impulse (a->nc, a->f_low, a->f_high, -20.0 * log10(a->f_high / a->f_low), 0.0, a->ctype, a->rate, 1.0 / (2.0 * a->size), 0, 0);
|
|
|
|
FIRCORE::setImpulse_fircore (a->p, impulse, 1);
|
|
|
|
delete[] (impulse);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::setSize_emphp (EMPHP *a, int size)
|
|
|
|
{
|
2024-06-25 01:50:48 +00:00
|
|
|
float* impulse;
|
2024-06-16 09:31:13 +00:00
|
|
|
a->size = size;
|
|
|
|
FIRCORE::setSize_fircore (a->p, a->size);
|
|
|
|
impulse = FCurve::fc_impulse (a->nc, a->f_low, a->f_high, -20.0 * log10(a->f_high / a->f_low), 0.0, a->ctype, a->rate, 1.0 / (2.0 * a->size), 0, 0);
|
|
|
|
FIRCORE::setImpulse_fircore (a->p, impulse, 1);
|
|
|
|
delete[] (impulse);
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* Partitioned Overlap-Save FM Pre-Emphasis: TXA Properties *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
|
|
|
void EMPHP::SetFMEmphPosition (TXA& txa, int position)
|
|
|
|
{
|
|
|
|
txa.csDSP.lock();
|
|
|
|
txa.preemph.p->position = position;
|
|
|
|
txa.csDSP.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::SetFMEmphMP (TXA& txa, int mp)
|
|
|
|
{
|
|
|
|
EMPHP *a;
|
|
|
|
a = txa.preemph.p;
|
|
|
|
if (a->mp != mp)
|
|
|
|
{
|
|
|
|
a->mp = mp;
|
|
|
|
FIRCORE::setMp_fircore (a->p, a->mp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPHP::SetFMEmphNC (TXA& txa, int nc)
|
|
|
|
{
|
|
|
|
EMPHP *a;
|
2024-06-25 01:50:48 +00:00
|
|
|
float* impulse;
|
2024-06-16 09:31:13 +00:00
|
|
|
txa.csDSP.lock();
|
|
|
|
a = txa.preemph.p;
|
|
|
|
if (a->nc != nc)
|
|
|
|
{
|
|
|
|
a->nc = nc;
|
|
|
|
impulse = FCurve::fc_impulse (a->nc, a->f_low, a->f_high, -20.0 * log10(a->f_high / a->f_low), 0.0, a->ctype, a->rate, 1.0 / (2.0 * a->size), 0, 0);
|
|
|
|
FIRCORE::setNc_fircore (a->p, a->nc, impulse);
|
|
|
|
delete[] (impulse);
|
|
|
|
}
|
|
|
|
txa.csDSP.unlock();
|
|
|
|
}
|
|
|
|
|
2024-06-25 01:50:48 +00:00
|
|
|
void EMPHP::SetFMPreEmphFreqs (TXA& txa, float low, float high)
|
2024-06-16 09:31:13 +00:00
|
|
|
{
|
|
|
|
EMPHP *a;
|
2024-06-25 01:50:48 +00:00
|
|
|
float* impulse;
|
2024-06-16 09:31:13 +00:00
|
|
|
txa.csDSP.lock();
|
|
|
|
a = txa.preemph.p;
|
|
|
|
if (a->f_low != low || a->f_high != high)
|
|
|
|
{
|
|
|
|
a->f_low = low;
|
|
|
|
a->f_high = high;
|
|
|
|
impulse = FCurve::fc_impulse (a->nc, a->f_low, a->f_high, -20.0 * log10(a->f_high / a->f_low), 0.0, a->ctype, a->rate, 1.0 / (2.0 * a->size), 0, 0);
|
|
|
|
FIRCORE::setImpulse_fircore (a->p, impulse, 1);
|
|
|
|
delete[] (impulse);
|
|
|
|
}
|
|
|
|
txa.csDSP.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* Overlap-Save FM Pre-Emphasis *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
|
|
|
|
void EMPH::calc_emph (EMPH *a)
|
|
|
|
{
|
2024-06-25 01:50:48 +00:00
|
|
|
a->infilt = new float[2 * a->size * 2]; // (float *)malloc0(2 * a->size * sizeof(complex));
|
|
|
|
a->product = new float[2 * a->size * 2]; // (float *)malloc0(2 * a->size * sizeof(complex));
|
2024-06-16 09:31:13 +00:00
|
|
|
a->mults = FCurve::fc_mults(a->size, a->f_low, a->f_high, -20.0 * log10(a->f_high / a->f_low), 0.0, a->ctype, a->rate, 1.0 / (2.0 * a->size), 0, 0);
|
2024-06-25 01:50:48 +00: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 09:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EMPH::decalc_emph (EMPH *a)
|
|
|
|
{
|
2024-06-25 01:50:48 +00:00
|
|
|
fftwf_destroy_plan(a->CRev);
|
|
|
|
fftwf_destroy_plan(a->CFor);
|
2024-06-16 09:31:13 +00:00
|
|
|
delete[] (a->mults);
|
|
|
|
delete[] (a->product);
|
|
|
|
delete[] (a->infilt);
|
|
|
|
}
|
|
|
|
|
2024-06-25 01:50:48 +00:00
|
|
|
EMPH* EMPH::create_emph (int run, int position, int size, float* in, float* out, int rate, int ctype, float f_low, float f_high)
|
2024-06-16 09:31:13 +00:00
|
|
|
{
|
|
|
|
EMPH *a = new EMPH;
|
|
|
|
a->run = run;
|
|
|
|
a->position = position;
|
|
|
|
a->size = size;
|
|
|
|
a->in = in;
|
|
|
|
a->out = out;
|
2024-06-25 01:50:48 +00:00
|
|
|
a->rate = (float)rate;
|
2024-06-16 09:31:13 +00:00
|
|
|
a->ctype = ctype;
|
|
|
|
a->f_low = f_low;
|
|
|
|
a->f_high = f_high;
|
|
|
|
calc_emph (a);
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPH::destroy_emph (EMPH *a)
|
|
|
|
{
|
|
|
|
decalc_emph (a);
|
|
|
|
delete (a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPH::flush_emph (EMPH *a)
|
|
|
|
{
|
2024-06-24 20:23:10 +00:00
|
|
|
memset (a->infilt, 0, 2 * a->size * sizeof (wcomplex));
|
2024-06-16 09:31:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void EMPH::xemph (EMPH *a, int position)
|
|
|
|
{
|
|
|
|
int i;
|
2024-06-25 01:50:48 +00:00
|
|
|
float I, Q;
|
2024-06-16 09:31:13 +00:00
|
|
|
if (a->run && a->position == position)
|
|
|
|
{
|
2024-06-24 20:23:10 +00:00
|
|
|
memcpy (&(a->infilt[2 * a->size]), a->in, a->size * sizeof (wcomplex));
|
2024-06-25 01:50:48 +00:00
|
|
|
fftwf_execute (a->CFor);
|
2024-06-16 09:31:13 +00:00
|
|
|
for (i = 0; i < 2 * a->size; i++)
|
|
|
|
{
|
|
|
|
I = a->product[2 * i + 0];
|
|
|
|
Q = 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-25 01:50:48 +00:00
|
|
|
fftwf_execute (a->CRev);
|
2024-06-24 20:23:10 +00:00
|
|
|
memcpy (a->infilt, &(a->infilt[2 * a->size]), a->size * sizeof(wcomplex));
|
2024-06-16 09:31:13 +00:00
|
|
|
}
|
|
|
|
else if (a->in != a->out)
|
2024-06-24 20:23:10 +00:00
|
|
|
memcpy (a->out, a->in, a->size * sizeof (wcomplex));
|
2024-06-16 09:31:13 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 01:50:48 +00:00
|
|
|
void EMPH::setBuffers_emph (EMPH *a, float* in, float* out)
|
2024-06-16 09:31:13 +00:00
|
|
|
{
|
|
|
|
decalc_emph (a);
|
|
|
|
a->in = in;
|
|
|
|
a->out = out;
|
|
|
|
calc_emph (a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPH::setSamplerate_emph (EMPH *a, int rate)
|
|
|
|
{
|
|
|
|
decalc_emph (a);
|
|
|
|
a->rate = rate;
|
|
|
|
calc_emph (a);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EMPH::setSize_emph (EMPH *a, int size)
|
|
|
|
{
|
|
|
|
decalc_emph(a);
|
|
|
|
a->size = size;
|
|
|
|
calc_emph(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
/********************************************************************************************************
|
|
|
|
* *
|
|
|
|
* Overlap-Save FM Pre-Emphasis: TXA Properties *
|
|
|
|
* *
|
|
|
|
********************************************************************************************************/
|
|
|
|
/* // Uncomment when needed
|
|
|
|
PORT
|
|
|
|
void SetTXAFMEmphPosition (int channel, int position)
|
|
|
|
{
|
|
|
|
ch.csDSP.lock();
|
|
|
|
txa.preemph.p->position = position;
|
|
|
|
ch.csDSP.unlock();
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
} // namespace WDSP
|