From a60e2b7597708ac5ba5facb870caf95c0e79415b Mon Sep 17 00:00:00 2001 From: f4exb Date: Wed, 26 Apr 2017 01:34:27 +0200 Subject: [PATCH] IntHalfbandFilterEO1: Optimized versions of workInterpolateCenter and myInterpolate --- sdrbase/dsp/inthalfbandfilter.h | 1 + sdrbase/dsp/inthalfbandfilterdb.h | 2 +- sdrbase/dsp/inthalfbandfiltereo1.h | 103 +++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/sdrbase/dsp/inthalfbandfilter.h b/sdrbase/dsp/inthalfbandfilter.h index fc17a4ac5..488dfc1ff 100644 --- a/sdrbase/dsp/inthalfbandfilter.h +++ b/sdrbase/dsp/inthalfbandfilter.h @@ -587,6 +587,7 @@ public: m_ptr = HBFIRFilterTraits::hbMod[m_ptr + 2 - 1]; } + /** Optimized upsampler by 2 not calculating FIR with inserted null samples */ void myInterpolateOptimized(qint32 *x1, qint32 *y1, qint32 *x2, qint32 *y2) { // insert sample into ring double buffer diff --git a/sdrbase/dsp/inthalfbandfilterdb.h b/sdrbase/dsp/inthalfbandfilterdb.h index b9d2aee27..02ce76bd2 100644 --- a/sdrbase/dsp/inthalfbandfilterdb.h +++ b/sdrbase/dsp/inthalfbandfilterdb.h @@ -125,7 +125,6 @@ public: } } - bool workDecimateCenter(qint32 *x, qint32 *y) { // insert sample into ring-buffer @@ -459,6 +458,7 @@ public: advancePointer(); } + /** Optimized upsampler by 2 not calculating FIR with inserted null samples */ void myInterpolateOptimized(qint32 *x1, qint32 *y1, qint32 *x2, qint32 *y2) { // insert sample into ring double buffer diff --git a/sdrbase/dsp/inthalfbandfiltereo1.h b/sdrbase/dsp/inthalfbandfiltereo1.h index 4f9f44fba..33120362e 100644 --- a/sdrbase/dsp/inthalfbandfiltereo1.h +++ b/sdrbase/dsp/inthalfbandfiltereo1.h @@ -94,6 +94,40 @@ public: } } + /** Optimized upsampler by 2 not calculating FIR with inserted null samples */ + bool workInterpolateCenterOptimized(Sample* sampleIn, Sample *SampleOut) + { + switch(m_state) + { + case 0: + // return the middle peak + SampleOut->setReal(m_samples[m_ptr + (HBFIRFilterTraits::hbOrder/4) - 1][0]); + SampleOut->setImag(m_samples[m_ptr + (HBFIRFilterTraits::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_samples[m_ptr][0] = sampleIn->real(); + m_samples[m_ptr][1] = sampleIn->imag(); + m_samples[m_ptr + HBFIRFilterTraits::hbOrder/2][0] = sampleIn->real(); + m_samples[m_ptr + HBFIRFilterTraits::hbOrder/2][1] = sampleIn->imag(); + + // advance pointer + if (m_ptr < (HBFIRFilterTraits::hbOrder/2) - 1) { + m_ptr++; + } else { + m_ptr = 0; + } + + m_state = 0; // next state + return true; // tell caller we consumed the sample + } + } + bool workDecimateCenter(int32_t *x, int32_t *y) { // insert sample into ring-buffer @@ -427,9 +461,34 @@ public: advancePointer(); } + /** Optimized upsampler by 2 not calculating FIR with inserted null samples */ + void myInterpolateOptimized(qint32 *x1, qint32 *y1, qint32 *x2, qint32 *y2) + { + // insert sample into ring double buffer + m_samples[m_ptr][0] = *x1; + m_samples[m_ptr][1] = *y1; + m_samples[m_ptr + HBFIRFilterTraits::hbOrder/2][0] = *x1; + m_samples[m_ptr + HBFIRFilterTraits::hbOrder/2][1] = *y1; + + // advance pointer + if (m_ptr < (HBFIRFilterTraits::hbOrder/2) - 1) { + m_ptr++; + } else { + m_ptr = 0; + } + + // first output sample calculated with the middle peak + *x1 = m_samples[m_ptr + (HBFIRFilterTraits::hbOrder/4) - 1][0]; + *y1 = m_samples[m_ptr + (HBFIRFilterTraits::hbOrder/4) - 1][1]; + + // second sample calculated with the filter + doInterpolateFIR(x2, y2); + } + protected: int32_t m_even[2][HBFIRFilterTraits::hbOrder]; // double buffer technique int32_t m_odd[2][HBFIRFilterTraits::hbOrder]; // double buffer technique + int32_t m_samples[HBFIRFilterTraits::hbOrder][2]; // double buffer technique int m_ptr; int m_size; @@ -579,6 +638,48 @@ protected: *x = iAcc >> (HBFIRFilterTraits::hbShift -1); // HB_SHIFT incorrect do not loose the gained bit *y = qAcc >> (HBFIRFilterTraits::hbShift -1); } + + void doInterpolateFIR(Sample* sample) + { + qint32 iAcc = 0; + qint32 qAcc = 0; + + qint16 a = m_ptr; + qint16 b = m_ptr + (HBFIRFilterTraits::hbOrder / 2) - 1; + + // go through samples in buffer + for (int i = 0; i < HBFIRFilterTraits::hbOrder / 4; i++) + { + iAcc += (m_samples[a][0] + m_samples[b][0]) * HBFIRFilterTraits::hbCoeffs[i]; + qAcc += (m_samples[a][1] + m_samples[b][1]) * HBFIRFilterTraits::hbCoeffs[i]; + a++; + b--; + } + + sample->setReal(iAcc >> (HBFIRFilterTraits::hbShift -1)); + sample->setImag(qAcc >> (HBFIRFilterTraits::hbShift -1)); + } + + void doInterpolateFIR(qint32 *x, qint32 *y) + { + qint32 iAcc = 0; + qint32 qAcc = 0; + + qint16 a = m_ptr; + qint16 b = m_ptr + (HBFIRFilterTraits::hbOrder / 2) - 1; + + // go through samples in buffer + for (int i = 0; i < HBFIRFilterTraits::hbOrder / 4; i++) + { + iAcc += (m_samples[a][0] + m_samples[b][0]) * HBFIRFilterTraits::hbCoeffs[i]; + qAcc += (m_samples[a][1] + m_samples[b][1]) * HBFIRFilterTraits::hbCoeffs[i]; + a++; + b--; + } + + *x = iAcc >> (HBFIRFilterTraits::hbShift -1); + *y = qAcc >> (HBFIRFilterTraits::hbShift -1); + } }; template @@ -592,6 +693,8 @@ IntHalfbandFilterEO1::IntHalfbandFilterEO1() m_even[1][i] = 0; m_odd[0][i] = 0; m_odd[1][i] = 0; + m_samples[i][0] = 0; + m_samples[i][1] = 0; } m_ptr = 0;