2016-11-01 10:02:50 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2016 F4EXB //
|
|
|
|
// written by Edouard Griffiths //
|
|
|
|
// //
|
|
|
|
// Integer half-band FIR based interpolator and decimator //
|
|
|
|
// This is the double buffer variant //
|
|
|
|
// //
|
|
|
|
// 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 as version 3 of the License, or //
|
|
|
|
// //
|
|
|
|
// 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 V3 for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef INCLUDE_INTHALFBANDFILTER_DB_H
|
|
|
|
#define INCLUDE_INTHALFBANDFILTER_DB_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "dsp/dsptypes.h"
|
|
|
|
#include "dsp/hbfiltertraits.h"
|
2018-03-20 08:49:21 -04:00
|
|
|
#include "export.h"
|
2016-11-01 10:02:50 -04:00
|
|
|
|
2018-01-21 13:39:51 -05:00
|
|
|
template<typename AccuType, uint32_t HBFilterOrder>
|
2018-03-03 15:19:59 -05:00
|
|
|
class SDRBASE_API IntHalfbandFilterDB {
|
2016-11-01 10:02:50 -04:00
|
|
|
public:
|
|
|
|
IntHalfbandFilterDB();
|
|
|
|
|
|
|
|
// downsample by 2, return center part of original spectrum
|
|
|
|
bool workDecimateCenter(Sample* sample)
|
|
|
|
{
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample->real(), (FixReal) sample->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 1;
|
|
|
|
// tell caller we don't have a new sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// save result
|
|
|
|
doFIR(sample);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 0;
|
|
|
|
|
|
|
|
// tell caller we have a new sample
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsample by 2, return center part of original spectrum - double buffer variant
|
2017-04-25 19:57:11 -04:00
|
|
|
bool workInterpolateCenterZeroStuffing(Sample* sampleIn, Sample *SampleOut)
|
2016-11-01 10:02:50 -04:00
|
|
|
{
|
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) 0, (FixReal) 0);
|
2016-11-01 10:02:50 -04:00
|
|
|
// save result
|
|
|
|
doFIR(SampleOut);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 1;
|
|
|
|
// tell caller we didn't consume the sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sampleIn->real(), (FixReal) sampleIn->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
// save result
|
|
|
|
doFIR(SampleOut);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 0;
|
|
|
|
// tell caller we consumed the sample
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 18:22:27 -04:00
|
|
|
/** Optimized upsampler by 2 not calculating FIR with inserted null samples */
|
2017-04-25 19:57:11 -04:00
|
|
|
bool workInterpolateCenter(Sample* sampleIn, Sample *SampleOut)
|
2017-04-25 18:22:27 -04:00
|
|
|
{
|
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// return the middle peak
|
|
|
|
SampleOut->setReal(m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][0]);
|
|
|
|
SampleOut->setImag(m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][1]);
|
|
|
|
m_state = 1; // next state
|
|
|
|
return false; // tell caller we didn't consume the sample
|
|
|
|
|
|
|
|
default:
|
|
|
|
// calculate with non null samples
|
|
|
|
doInterpolateFIR(SampleOut);
|
|
|
|
|
|
|
|
// insert sample into ring double buffer
|
|
|
|
m_samplesDB[m_ptr][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr][1] = sampleIn->imag();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][1] = sampleIn->imag();
|
|
|
|
|
|
|
|
// advance pointer
|
|
|
|
if (m_ptr < (HBFIRFilterTraits<HBFilterOrder>::hbOrder/2) - 1) {
|
|
|
|
m_ptr++;
|
|
|
|
} else {
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = 0; // next state
|
|
|
|
return true; // tell caller we consumed the sample
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
// bool workDecimateCenter(qint32 *x, qint32 *y)
|
|
|
|
// {
|
|
|
|
// // insert sample into ring-buffer
|
|
|
|
// storeSample32(*x, *y);
|
|
|
|
//
|
|
|
|
// switch (m_state)
|
|
|
|
// {
|
|
|
|
// case 0:
|
|
|
|
// // advance write-pointer
|
|
|
|
// advancePointer();
|
|
|
|
// // next state
|
|
|
|
// m_state = 1;
|
|
|
|
// // tell caller we don't have a new sample
|
|
|
|
// return false;
|
|
|
|
//
|
|
|
|
// default:
|
|
|
|
// // save result
|
|
|
|
// doFIR(x, y);
|
|
|
|
// // advance write-pointer
|
|
|
|
// advancePointer();
|
|
|
|
// // next state
|
|
|
|
// m_state = 0;
|
|
|
|
// // tell caller we have a new sample
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// downsample by 2, return lower half of original spectrum
|
|
|
|
bool workDecimateLowerHalf(Sample* sample)
|
|
|
|
{
|
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) -sample->imag(), (FixReal) sample->real());
|
2016-11-01 10:02:50 -04:00
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 1;
|
|
|
|
// tell caller we don't have a new sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) -sample->real(), (FixReal) -sample->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
// save result
|
|
|
|
doFIR(sample);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 2;
|
|
|
|
// tell caller we have a new sample
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample->imag(), (FixReal) -sample->real());
|
2016-11-01 10:02:50 -04:00
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 3;
|
|
|
|
// tell caller we don't have a new sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample->real(), (FixReal) sample->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
// save result
|
|
|
|
doFIR(sample);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 0;
|
|
|
|
// tell caller we have a new sample
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 20:20:07 -04:00
|
|
|
/** Optimized upsampler by 2 not calculating FIR with inserted null samples */
|
2016-11-01 10:02:50 -04:00
|
|
|
bool workInterpolateLowerHalf(Sample* sampleIn, Sample *sampleOut)
|
|
|
|
{
|
|
|
|
Sample s;
|
|
|
|
|
2017-04-25 20:20:07 -04:00
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// return the middle peak
|
|
|
|
sampleOut->setReal(m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][1]); // imag
|
|
|
|
sampleOut->setImag(-m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][0]); // - real
|
|
|
|
m_state = 1; // next state
|
|
|
|
return false; // tell caller we didn't consume the sample
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// calculate with non null samples
|
|
|
|
doInterpolateFIR(&s);
|
|
|
|
sampleOut->setReal(-s.real());
|
|
|
|
sampleOut->setImag(-s.imag());
|
|
|
|
|
|
|
|
// insert sample into ring double buffer
|
|
|
|
m_samplesDB[m_ptr][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr][1] = sampleIn->imag();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][1] = sampleIn->imag();
|
|
|
|
|
|
|
|
// advance pointer
|
|
|
|
if (m_ptr < (HBFIRFilterTraits<HBFilterOrder>::hbOrder/2) - 1) {
|
|
|
|
m_ptr++;
|
|
|
|
} else {
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = 2; // next state
|
|
|
|
return true; // tell caller we consumed the sample
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// return the middle peak
|
|
|
|
sampleOut->setReal(-m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][1]); // - imag
|
|
|
|
sampleOut->setImag(m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][0]); // real
|
|
|
|
m_state = 3; // next state
|
|
|
|
return false; // tell caller we didn't consume the sample
|
|
|
|
|
|
|
|
default:
|
|
|
|
// calculate with non null samples
|
|
|
|
doInterpolateFIR(&s);
|
|
|
|
sampleOut->setReal(s.real());
|
|
|
|
sampleOut->setImag(s.imag());
|
|
|
|
|
|
|
|
// insert sample into ring double buffer
|
|
|
|
m_samplesDB[m_ptr][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr][1] = sampleIn->imag();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][1] = sampleIn->imag();
|
|
|
|
|
|
|
|
// advance pointer
|
|
|
|
if (m_ptr < (HBFIRFilterTraits<HBFilterOrder>::hbOrder/2) - 1) {
|
|
|
|
m_ptr++;
|
|
|
|
} else {
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = 0; // next state
|
|
|
|
return true; // tell caller we consumed the sample
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsample by 2, from lower half of original spectrum - double buffer variant
|
|
|
|
bool workInterpolateLowerHalfZeroStuffing(Sample* sampleIn, Sample *sampleOut)
|
|
|
|
{
|
|
|
|
Sample s;
|
|
|
|
|
2016-11-01 10:02:50 -04:00
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) 0, (FixReal) 0);
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(s.imag());
|
|
|
|
sampleOut->setImag(-s.real());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 1;
|
|
|
|
|
|
|
|
// tell caller we didn't consume the sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sampleIn->real(), (FixReal) sampleIn->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(-s.real());
|
|
|
|
sampleOut->setImag(-s.imag());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 2;
|
|
|
|
|
|
|
|
// tell caller we consumed the sample
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) 0, (FixReal) 0);
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(-s.imag());
|
|
|
|
sampleOut->setImag(s.real());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 3;
|
|
|
|
|
|
|
|
// tell caller we didn't consume the sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sampleIn->real(), (FixReal) sampleIn->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(s.real());
|
|
|
|
sampleOut->setImag(s.imag());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 0;
|
|
|
|
|
|
|
|
// tell caller we consumed the sample
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// downsample by 2, return upper half of original spectrum
|
|
|
|
bool workDecimateUpperHalf(Sample* sample)
|
|
|
|
{
|
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample->imag(), (FixReal) -sample->real());
|
2016-11-01 10:02:50 -04:00
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 1;
|
|
|
|
// tell caller we don't have a new sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) -sample->real(), (FixReal) -sample->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
// save result
|
|
|
|
doFIR(sample);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 2;
|
|
|
|
// tell caller we have a new sample
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) -sample->imag(), (FixReal) sample->real());
|
2016-11-01 10:02:50 -04:00
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 3;
|
|
|
|
// tell caller we don't have a new sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample->real(), (FixReal) sample->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
// save result
|
|
|
|
doFIR(sample);
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
// next state
|
|
|
|
m_state = 0;
|
|
|
|
// tell caller we have a new sample
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 20:20:07 -04:00
|
|
|
/** Optimized upsampler by 2 not calculating FIR with inserted null samples */
|
2016-11-01 10:02:50 -04:00
|
|
|
bool workInterpolateUpperHalf(Sample* sampleIn, Sample *sampleOut)
|
|
|
|
{
|
|
|
|
Sample s;
|
|
|
|
|
2017-04-25 20:20:07 -04:00
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// return the middle peak
|
|
|
|
sampleOut->setReal(-m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][1]); // - imag
|
|
|
|
sampleOut->setImag(m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][0]); // + real
|
|
|
|
m_state = 1; // next state
|
|
|
|
return false; // tell caller we didn't consume the sample
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// calculate with non null samples
|
|
|
|
doInterpolateFIR(&s);
|
|
|
|
sampleOut->setReal(-s.real());
|
|
|
|
sampleOut->setImag(-s.imag());
|
|
|
|
|
|
|
|
// insert sample into ring double buffer
|
|
|
|
m_samplesDB[m_ptr][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr][1] = sampleIn->imag();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][1] = sampleIn->imag();
|
|
|
|
|
|
|
|
// advance pointer
|
|
|
|
if (m_ptr < (HBFIRFilterTraits<HBFilterOrder>::hbOrder/2) - 1) {
|
|
|
|
m_ptr++;
|
|
|
|
} else {
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = 2; // next state
|
|
|
|
return true; // tell caller we consumed the sample
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// return the middle peak
|
|
|
|
sampleOut->setReal(m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][1]); // + imag
|
|
|
|
sampleOut->setImag(-m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][0]); // - real
|
|
|
|
m_state = 3; // next state
|
|
|
|
return false; // tell caller we didn't consume the sample
|
|
|
|
|
|
|
|
default:
|
|
|
|
// calculate with non null samples
|
|
|
|
doInterpolateFIR(&s);
|
|
|
|
sampleOut->setReal(s.real());
|
|
|
|
sampleOut->setImag(s.imag());
|
|
|
|
|
|
|
|
// insert sample into ring double buffer
|
|
|
|
m_samplesDB[m_ptr][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr][1] = sampleIn->imag();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][0] = sampleIn->real();
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][1] = sampleIn->imag();
|
|
|
|
|
|
|
|
// advance pointer
|
|
|
|
if (m_ptr < (HBFIRFilterTraits<HBFilterOrder>::hbOrder/2) - 1) {
|
|
|
|
m_ptr++;
|
|
|
|
} else {
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_state = 0; // next state
|
|
|
|
return true; // tell caller we consumed the sample
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// upsample by 2, move original spectrum to upper half - double buffer variant
|
|
|
|
bool workInterpolateUpperHalfZeroStuffing(Sample* sampleIn, Sample *sampleOut)
|
|
|
|
{
|
|
|
|
Sample s;
|
|
|
|
|
2016-11-01 10:02:50 -04:00
|
|
|
switch(m_state)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) 0, (FixReal) 0);
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(-s.imag());
|
|
|
|
sampleOut->setImag(s.real());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 1;
|
|
|
|
|
|
|
|
// tell caller we didn't consume the sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
case 1:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sampleIn->real(), (FixReal) sampleIn->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(-s.real());
|
|
|
|
sampleOut->setImag(-s.imag());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 2;
|
|
|
|
|
|
|
|
// tell caller we consumed the sample
|
|
|
|
return true;
|
|
|
|
|
|
|
|
case 2:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) 0, (FixReal) 0);
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(s.imag());
|
|
|
|
sampleOut->setImag(-s.real());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 3;
|
|
|
|
|
|
|
|
// tell caller we didn't consume the sample
|
|
|
|
return false;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// insert sample into ring-buffer
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sampleIn->real(), (FixReal) sampleIn->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// save result
|
|
|
|
doFIR(&s);
|
|
|
|
sampleOut->setReal(s.real());
|
|
|
|
sampleOut->setImag(s.imag());
|
|
|
|
|
|
|
|
// advance write-pointer
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
// next state
|
|
|
|
m_state = 0;
|
|
|
|
|
|
|
|
// tell caller we consumed the sample
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void myDecimate(const Sample* sample1, Sample* sample2)
|
|
|
|
{
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample1->real(), (FixReal) sample1->imag());
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample2->real(), (FixReal) sample2->imag());
|
2016-11-01 10:02:50 -04:00
|
|
|
doFIR(sample2);
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
}
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
void myDecimate(AccuType x1, AccuType y1, AccuType *x2, AccuType *y2)
|
2016-11-01 10:02:50 -04:00
|
|
|
{
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleAccu(x1, y1);
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleAccu(*x2, *y2);
|
|
|
|
doFIRAccu(x2, y2);
|
2016-11-08 21:27:30 -05:00
|
|
|
advancePointer();
|
2016-11-01 10:02:50 -04:00
|
|
|
}
|
|
|
|
|
2017-04-25 19:39:45 -04:00
|
|
|
/** Simple zero stuffing and filter */
|
|
|
|
void myInterpolateZeroStuffing(Sample* sample1, Sample* sample2)
|
2016-12-30 22:23:59 -05:00
|
|
|
{
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) sample1->real(), (FixReal) sample1->imag());
|
2016-12-30 22:23:59 -05:00
|
|
|
doFIR(sample1);
|
|
|
|
advancePointer();
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
storeSampleFixReal((FixReal) 0, (FixReal) 0);
|
2016-12-30 22:23:59 -05:00
|
|
|
doFIR(sample2);
|
|
|
|
advancePointer();
|
|
|
|
}
|
|
|
|
|
2017-04-25 19:39:45 -04:00
|
|
|
/** Simple zero stuffing and filter */
|
2018-01-21 20:49:06 -05:00
|
|
|
// void myInterpolateZeroStuffing(qint32 *x1, qint32 *y1, qint32 *x2, qint32 *y2)
|
|
|
|
// {
|
|
|
|
// storeSampleAccu(*x1, *y1);
|
|
|
|
// doFIR(x1, y1);
|
|
|
|
// advancePointer();
|
|
|
|
//
|
|
|
|
// storeSampleAccu(0, 0);
|
|
|
|
// doFIR(x2, y2);
|
|
|
|
// advancePointer();
|
|
|
|
// }
|
2016-12-30 22:23:59 -05:00
|
|
|
|
2017-04-25 19:34:27 -04:00
|
|
|
/** Optimized upsampler by 2 not calculating FIR with inserted null samples */
|
2017-04-25 19:39:45 -04:00
|
|
|
void myInterpolate(qint32 *x1, qint32 *y1, qint32 *x2, qint32 *y2)
|
2017-04-25 18:22:27 -04:00
|
|
|
{
|
|
|
|
// insert sample into ring double buffer
|
|
|
|
m_samplesDB[m_ptr][0] = *x1;
|
|
|
|
m_samplesDB[m_ptr][1] = *y1;
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][0] = *x1;
|
|
|
|
m_samplesDB[m_ptr + HBFIRFilterTraits<HBFilterOrder>::hbOrder/2][1] = *y1;
|
|
|
|
|
|
|
|
// advance pointer
|
|
|
|
if (m_ptr < (HBFIRFilterTraits<HBFilterOrder>::hbOrder/2) - 1) {
|
|
|
|
m_ptr++;
|
|
|
|
} else {
|
|
|
|
m_ptr = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first output sample calculated with the middle peak
|
|
|
|
*x1 = m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][0];
|
|
|
|
*y1 = m_samplesDB[m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder/4) - 1][1];
|
|
|
|
|
|
|
|
// second sample calculated with the filter
|
|
|
|
doInterpolateFIR(x2, y2);
|
|
|
|
}
|
|
|
|
|
2016-11-01 10:02:50 -04:00
|
|
|
protected:
|
2018-01-21 13:39:51 -05:00
|
|
|
AccuType m_samplesDB[2*(HBFIRFilterTraits<HBFilterOrder>::hbOrder - 1)][2]; // double buffer technique
|
2016-11-02 19:30:55 -04:00
|
|
|
int m_ptr;
|
|
|
|
int m_size;
|
2016-11-01 10:02:50 -04:00
|
|
|
int m_state;
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
void storeSampleFixReal(const FixReal& sampleI, const FixReal& sampleQ)
|
2016-11-02 19:30:55 -04:00
|
|
|
{
|
2016-11-03 20:12:39 -04:00
|
|
|
m_samplesDB[m_ptr][0] = sampleI;
|
|
|
|
m_samplesDB[m_ptr][1] = sampleQ;
|
|
|
|
m_samplesDB[m_ptr + m_size][0] = sampleI;
|
|
|
|
m_samplesDB[m_ptr + m_size][1] = sampleQ;
|
2016-11-02 19:30:55 -04:00
|
|
|
}
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
void storeSampleAccu(AccuType x, AccuType y)
|
2016-11-02 19:30:55 -04:00
|
|
|
{
|
2016-11-03 20:12:39 -04:00
|
|
|
m_samplesDB[m_ptr][0] = x;
|
|
|
|
m_samplesDB[m_ptr][1] = y;
|
|
|
|
m_samplesDB[m_ptr + m_size][0] = x;
|
|
|
|
m_samplesDB[m_ptr + m_size][1] = y;
|
2016-11-02 19:30:55 -04:00
|
|
|
}
|
|
|
|
|
2016-11-08 21:27:30 -05:00
|
|
|
void advancePointer()
|
|
|
|
{
|
|
|
|
m_ptr = m_ptr + 1 < m_size ? m_ptr + 1: 0;
|
|
|
|
}
|
|
|
|
|
2016-11-01 10:02:50 -04:00
|
|
|
void doFIR(Sample* sample)
|
|
|
|
{
|
2016-11-02 19:30:55 -04:00
|
|
|
int a = m_ptr + m_size; // tip pointer
|
|
|
|
int b = m_ptr + 1; // tail pointer
|
2018-01-21 13:39:51 -05:00
|
|
|
AccuType iAcc = 0;
|
|
|
|
AccuType qAcc = 0;
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < HBFIRFilterTraits<HBFilterOrder>::hbOrder / 4; i++)
|
|
|
|
{
|
|
|
|
iAcc += (m_samplesDB[a][0] + m_samplesDB[b][0]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
qAcc += (m_samplesDB[a][1] + m_samplesDB[b][1]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
a -= 2;
|
|
|
|
b += 2;
|
|
|
|
}
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
iAcc += m_samplesDB[b-1][0] << (HBFIRFilterTraits<HBFilterOrder>::hbShift - 1);
|
|
|
|
qAcc += m_samplesDB[b-1][1] << (HBFIRFilterTraits<HBFilterOrder>::hbShift - 1);
|
2016-11-01 10:02:50 -04:00
|
|
|
|
2016-11-21 12:01:55 -05:00
|
|
|
sample->setReal(iAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1));
|
|
|
|
sample->setImag(qAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1));
|
2016-11-01 10:02:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void doFIR(qint32 *x, qint32 *y)
|
|
|
|
{
|
2016-11-02 19:30:55 -04:00
|
|
|
int a = m_ptr + m_size; // tip pointer
|
|
|
|
int b = m_ptr + 1; // tail pointer
|
2018-01-21 13:39:51 -05:00
|
|
|
AccuType iAcc = 0;
|
|
|
|
AccuType qAcc = 0;
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < HBFIRFilterTraits<HBFilterOrder>::hbOrder / 4; i++)
|
|
|
|
{
|
|
|
|
iAcc += (m_samplesDB[a][0] + m_samplesDB[b][0]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
qAcc += (m_samplesDB[a][1] + m_samplesDB[b][1]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
a -= 2;
|
|
|
|
b += 2;
|
|
|
|
}
|
|
|
|
|
2018-01-21 20:49:06 -05:00
|
|
|
iAcc += m_samplesDB[b-1][0] << (HBFIRFilterTraits<HBFilterOrder>::hbShift - 1);
|
|
|
|
qAcc += m_samplesDB[b-1][1] << (HBFIRFilterTraits<HBFilterOrder>::hbShift - 1);
|
|
|
|
|
|
|
|
*x = iAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1); // HB_SHIFT incorrect do not loose the gained bit
|
|
|
|
*y = qAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void doFIRAccu(AccuType *x, AccuType *y)
|
|
|
|
{
|
|
|
|
int a = m_ptr + m_size; // tip pointer
|
|
|
|
int b = m_ptr + 1; // tail pointer
|
|
|
|
AccuType iAcc = 0;
|
|
|
|
AccuType qAcc = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < HBFIRFilterTraits<HBFilterOrder>::hbOrder / 4; i++)
|
|
|
|
{
|
|
|
|
iAcc += (m_samplesDB[a][0] + m_samplesDB[b][0]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
qAcc += (m_samplesDB[a][1] + m_samplesDB[b][1]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
a -= 2;
|
|
|
|
b += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
iAcc += m_samplesDB[b-1][0] << (HBFIRFilterTraits<HBFilterOrder>::hbShift - 1);
|
|
|
|
qAcc += m_samplesDB[b-1][1] << (HBFIRFilterTraits<HBFilterOrder>::hbShift - 1);
|
2016-11-01 10:02:50 -04:00
|
|
|
|
|
|
|
*x = iAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1); // HB_SHIFT incorrect do not loose the gained bit
|
|
|
|
*y = qAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1);
|
|
|
|
}
|
2017-04-25 18:22:27 -04:00
|
|
|
|
|
|
|
void doInterpolateFIR(Sample* sample)
|
|
|
|
{
|
|
|
|
qint16 a = m_ptr;
|
|
|
|
qint16 b = m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder / 2) - 1;
|
|
|
|
|
|
|
|
// go through samples in buffer
|
2018-01-21 13:39:51 -05:00
|
|
|
AccuType iAcc = 0;
|
|
|
|
AccuType qAcc = 0;
|
2017-04-25 18:22:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < HBFIRFilterTraits<HBFilterOrder>::hbOrder / 4; i++)
|
|
|
|
{
|
|
|
|
iAcc += (m_samplesDB[a][0] + m_samplesDB[b][0]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
qAcc += (m_samplesDB[a][1] + m_samplesDB[b][1]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
a++;
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
|
|
|
|
sample->setReal(iAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1));
|
|
|
|
sample->setImag(qAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void doInterpolateFIR(qint32 *x, qint32 *y)
|
|
|
|
{
|
|
|
|
qint16 a = m_ptr;
|
|
|
|
qint16 b = m_ptr + (HBFIRFilterTraits<HBFilterOrder>::hbOrder / 2) - 1;
|
|
|
|
|
|
|
|
// go through samples in buffer
|
2018-01-21 13:39:51 -05:00
|
|
|
AccuType iAcc = 0;
|
|
|
|
AccuType qAcc = 0;
|
2017-04-25 18:22:27 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < HBFIRFilterTraits<HBFilterOrder>::hbOrder / 4; i++)
|
|
|
|
{
|
|
|
|
iAcc += (m_samplesDB[a][0] + m_samplesDB[b][0]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
qAcc += (m_samplesDB[a][1] + m_samplesDB[b][1]) * HBFIRFilterTraits<HBFilterOrder>::hbCoeffs[i];
|
|
|
|
a++;
|
|
|
|
b--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*x = iAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1);
|
|
|
|
*y = qAcc >> (HBFIRFilterTraits<HBFilterOrder>::hbShift -1);
|
|
|
|
}
|
2016-11-01 10:02:50 -04:00
|
|
|
};
|
|
|
|
|
2018-01-21 13:39:51 -05:00
|
|
|
template<typename AccuType, uint32_t HBFilterOrder>
|
|
|
|
IntHalfbandFilterDB<AccuType, HBFilterOrder>::IntHalfbandFilterDB()
|
2016-11-01 10:02:50 -04:00
|
|
|
{
|
2016-11-02 19:30:55 -04:00
|
|
|
m_size = HBFIRFilterTraits<HBFilterOrder>::hbOrder - 1;
|
2016-11-01 10:02:50 -04:00
|
|
|
|
2016-11-02 19:30:55 -04:00
|
|
|
for (int i = 0; i < m_size; i++)
|
2016-11-01 10:02:50 -04:00
|
|
|
{
|
|
|
|
m_samplesDB[i][0] = 0;
|
|
|
|
m_samplesDB[i][1] = 0;
|
|
|
|
}
|
|
|
|
|
2016-11-02 19:30:55 -04:00
|
|
|
m_ptr = 0;
|
2016-11-01 10:02:50 -04:00
|
|
|
m_state = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // INCLUDE_INTHALFBANDFILTER_DB_H
|