diff --git a/CMakeLists.txt b/CMakeLists.txt index 045a10b06..6a0a9ff51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -397,7 +397,7 @@ if (BUILD_DEBIAN) endif (BUILD_DEBIAN) set_target_properties(sdrbase PROPERTIES DEFINE_SYMBOL "sdrangel_EXPORTS") -target_compile_features(sdrbase PRIVATE cxx_generalized_initializers) # cmake >= 3.1.0 +target_compile_features(sdrbase PUBLIC cxx_generalized_initializers) # cmake >= 3.1.0 qt5_use_modules(sdrbase Core Widgets OpenGL Multimedia) diff --git a/sdrbase/dsp/inthalfbandfilter.h b/sdrbase/dsp/inthalfbandfilter.h index 0d1e589a6..97ce9e857 100644 --- a/sdrbase/dsp/inthalfbandfilter.h +++ b/sdrbase/dsp/inthalfbandfilter.h @@ -10,9 +10,105 @@ /* * supported filter orders: 64, 48, 32 */ -#define HB_FILTERORDER 32 +#define HB_FILTERORDER 64 #define HB_SHIFT 14 +template +struct HBFIRFilterTraits +{ + static const qint32 hbOrder = 32; + static const qint32 hbShift = 14; + static constexpr qint16 hbMod[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2}; + /* Coefficents. This is a sinc function: + * Half of the half of coefficients are stored because: + * - half of the coefficients are 0 + * - there is a symmertry around the central 1.0 coefficient (not stored either) + * There are actually order+1 coefficients + */ + static constexpr qint32 hbCoeffs[8] = { + (qint32)(-0.015956912844043127236437484839370881673 * (1 << hbShift)), + (qint32)( 0.013023031678944928940522274274371739011 * (1 << hbShift)), + (qint32)(-0.01866942273717486777684371190844103694 * (1 << hbShift)), + (qint32)( 0.026550887571157304190005987720724078827 * (1 << hbShift)), + (qint32)(-0.038350314277854319344740474662103224546 * (1 << hbShift)), + (qint32)( 0.058429248652825838128421764849917963147 * (1 << hbShift)), + (qint32)(-0.102889802028955756885153505209018476307 * (1 << hbShift)), + (qint32)( 0.317237706405931241260276465254719369113 * (1 << hbShift)) + }; +}; + +template<> +struct HBFIRFilterTraits<32> +{ + static const qint32 hbOrder = 32; + static const qint32 hbShift = 14; + static constexpr qint16 hbMod[32+6] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2}; + static constexpr qint32 hbCoeffs[8] = { + (qint32)(-0.015956912844043127236437484839370881673 * (1 << hbShift)), + (qint32)( 0.013023031678944928940522274274371739011 * (1 << hbShift)), + (qint32)(-0.01866942273717486777684371190844103694 * (1 << hbShift)), + (qint32)( 0.026550887571157304190005987720724078827 * (1 << hbShift)), + (qint32)(-0.038350314277854319344740474662103224546 * (1 << hbShift)), + (qint32)( 0.058429248652825838128421764849917963147 * (1 << hbShift)), + (qint32)(-0.102889802028955756885153505209018476307 * (1 << hbShift)), + (qint32)( 0.317237706405931241260276465254719369113 * (1 << hbShift)) + }; +}; + +template<> +struct HBFIRFilterTraits<48> +{ + static const qint32 hbOrder = 48; + static const qint32 hbShift = 14; + static constexpr qint16 hbMod[48+6] = { 47,48,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41, + 42,43,44,45,46,47,48,0,1,2} ; + static constexpr qint32 hbCoeffs[12] = { + (qint32)(-0.004102576237611492253332112767338912818 * (1 << hbShift)), + (qint32)( 0.003950551047979387886410762575906119309 * (1 << hbShift)), + (qint32)(-0.005807875789391703583164350277456833282 * (1 << hbShift)), + (qint32)( 0.00823497890520805998770814682075069868 * (1 << hbShift)), + (qint32)(-0.011372226513199541059195851744334504474 * (1 << hbShift)), + (qint32)( 0.015471557140973646315984524335362948477 * (1 << hbShift)), + (qint32)(-0.020944996398689276484450516591095947661 * (1 << hbShift)), + (qint32)( 0.028568078132034283034279553703527199104 * (1 << hbShift)), + (qint32)(-0.040015143905614086738964374490024056286 * (1 << hbShift)), + (qint32)( 0.059669519431831075095828964549582451582 * (1 << hbShift)), + (qint32)(-0.103669138691865420076609893840213771909 * (1 << hbShift)), + (qint32)( 0.317491986549921390015072120149852707982 * (1 << hbShift)) + }; +}; + +template<> +struct HBFIRFilterTraits<64> +{ + static const qint32 hbOrder = 64; + static const qint32 hbShift = 14; + static constexpr qint16 hbMod[64+6] = { 63,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44, + 45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,0,1,2} ; + static constexpr qint32 hbCoeffs[16] = { + (qint32)(-0.001114417441601693505720538368564120901 * (1 << hbShift)), + (qint32)( 0.001268007827185253051302527005361753254 * (1 << hbShift)), + (qint32)(-0.001959831378850490895410230152151598304 * (1 << hbShift)), + (qint32)( 0.002878308307661380308073439948657323839 * (1 << hbShift)), + (qint32)(-0.004071361818258721100571850826099762344 * (1 << hbShift)), + (qint32)( 0.005597288494657440618973431867289036745 * (1 << hbShift)), + (qint32)(-0.007532345003308904551886371336877346039 * (1 << hbShift)), + (qint32)( 0.009980346844667375288961963519795972388 * (1 << hbShift)), + (qint32)(-0.013092614174300500062830820979797863401 * (1 << hbShift)), + (qint32)( 0.01710934914871829748417297878404497169 * (1 << hbShift)), + (qint32)(-0.022443558692997273018576720460259821266 * (1 << hbShift)), + (qint32)( 0.029875811511593811098386197500076377764 * (1 << hbShift)), + (qint32)(-0.041086352085710403647667021687084343284 * (1 << hbShift)), + (qint32)( 0.060465467462665789533104998554335907102 * (1 << hbShift)), + (qint32)(-0.104159517495977321788203084906854201108 * (1 << hbShift)), + (qint32)( 0.317657589850154464805598308885237202048 * (1 << hbShift)), + }; +}; + class SDRANGEL_API IntHalfbandFilter { public: IntHalfbandFilter(); @@ -235,75 +331,85 @@ public: // upsample by 2, from lower half of original spectrum bool workInterpolateLowerHalf(Sample* sampleIn, Sample *sampleOut) { + Sample s; + switch(m_state) { - case 0: - // insert sample into ring-buffer - m_samples[m_ptr][0] = 0; - m_samples[m_ptr][1] = 0; + case 0: + // insert sample into ring-buffer + m_samples[m_ptr][0] = 0; + m_samples[m_ptr][1] = 0; - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(s.imag()); + sampleOut->setImag(-s.real()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 1; + // next state + m_state = 1; - // tell caller we didn't consume the sample - return false; + // tell caller we don't have a new sample + return false; - case 1: - // insert sample into ring-buffer - m_samples[m_ptr][0] = -sampleIn->real(); - m_samples[m_ptr][1] = -sampleIn->imag(); + case 1: + // insert sample into ring-buffer + m_samples[m_ptr][0] = sampleIn->real(); + m_samples[m_ptr][1] = sampleIn->imag(); - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(-s.real()); + sampleOut->setImag(-s.imag()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 2; + // next state + m_state = 2; - // tell caller we consumed the sample - return true; + // tell caller we have a new sample + return true; - case 2: - // insert sample into ring-buffer - m_samples[m_ptr][0] = 0; - m_samples[m_ptr][1] = 0; + case 2: + // insert sample into ring-buffer + m_samples[m_ptr][0] = 0; + m_samples[m_ptr][1] = 0; - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(-s.imag()); + sampleOut->setImag(s.real()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 3; + // next state + m_state = 3; - // tell caller we didn't consume the sample - return false; + // tell caller we don't have a new sample + return false; - default: - // insert sample into ring-buffer - m_samples[m_ptr][0] = sampleIn->real(); - m_samples[m_ptr][1] = sampleIn->imag(); + default: + // insert sample into ring-buffer + m_samples[m_ptr][0] = sampleIn->real(); + m_samples[m_ptr][1] = sampleIn->imag(); - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(s.real()); + sampleOut->setImag(s.imag()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 0; + // next state + m_state = 0; - // tell caller we consumed the sample - return true; + // tell caller we have a new sample + return true; } } @@ -379,179 +485,145 @@ public: // upsample by 2, move original spectrum to upper half bool workInterpolateUpperHalf(Sample* sampleIn, Sample *sampleOut) { + Sample s; + switch(m_state) { - case 0: - // insert sample into ring-buffer - m_samples[m_ptr][0] = 0; - m_samples[m_ptr][1] = 0; + case 0: + // insert sample into ring-buffer + m_samples[m_ptr][0] = 0; + m_samples[m_ptr][1] = 0; - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(-s.imag()); + sampleOut->setImag(s.real()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 1; + // next state + m_state = 1; - // tell caller we didn't consume the sample - return false; + // tell caller we don't have a new sample + return false; - case 1: - // insert sample into ring-buffer - m_samples[m_ptr][0] = sampleIn->imag(); - m_samples[m_ptr][1] = -sampleIn->real(); + case 1: + // insert sample into ring-buffer + m_samples[m_ptr][0] = sampleIn->real(); + m_samples[m_ptr][1] = sampleIn->imag(); - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(-s.real()); + sampleOut->setImag(-s.imag()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 2; + // next state + m_state = 2; - // tell caller we consumed the sample - return true; + // tell caller we have a new sample + return true; - case 2: - // insert sample into ring-buffer - m_samples[m_ptr][0] = 0; - m_samples[m_ptr][1] = 0; + case 2: + // insert sample into ring-buffer + m_samples[m_ptr][0] = 0; + m_samples[m_ptr][1] = 0; - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(s.imag()); + sampleOut->setImag(-s.real()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 3; + // next state + m_state = 3; - // tell caller we didn't consume the sample - return false; + // tell caller we don't have a new sample + return false; - case 3: - // insert sample into ring-buffer - m_samples[m_ptr][0] = -sampleIn->real(); - m_samples[m_ptr][1] = -sampleIn->imag(); + default: + // insert sample into ring-buffer + m_samples[m_ptr][0] = sampleIn->real(); + m_samples[m_ptr][1] = sampleIn->imag(); - // save result - doFIR(sampleOut); + // save result + doFIR(&s); + sampleOut->setReal(s.real()); + sampleOut->setImag(s.imag()); - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); + // advance write-pointer + m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - // next state - m_state = 4; + // next state + m_state = 0; - // tell caller we consumed the sample - return true; - - case 4: - // insert sample into ring-buffer - m_samples[m_ptr][0] = 0; - m_samples[m_ptr][1] = 0; - - // save result - doFIR(sampleOut); - - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - - // next state - m_state = 5; - - // tell caller we didn't consume the sample - return false; - - case 5: - // insert sample into ring-buffer - m_samples[m_ptr][0] = -sampleIn->imag(); - m_samples[m_ptr][1] = sampleIn->real(); - - // save result - doFIR(sampleOut); - - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - - // next state - m_state = 6; - - // tell caller we consumed the sample - return true; - - case 6: - // insert sample into ring-buffer - m_samples[m_ptr][0] = 0; - m_samples[m_ptr][1] = 0; - - // save result - doFIR(sampleOut); - - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - - // next state - m_state = 7; - - // tell caller we didn't consume the sample - return false; - - default: - // insert sample into ring-buffer - m_samples[m_ptr][0] = sampleIn->real(); - m_samples[m_ptr][1] = sampleIn->imag(); - - // save result - doFIR(sampleOut); - - // advance write-pointer - m_ptr = (m_ptr + HB_FILTERORDER) % (HB_FILTERORDER + 1); - - // next state - m_state = 0; - - // tell caller we consumed the sample - return true; + // tell caller we have a new sample + return true; } } void myDecimate(const Sample* sample1, Sample* sample2) { - static const qint16 mod33[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, - 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2}; + #if HB_FILTERORDER == 64 + static const qint16 HB_MOD[64+6] = { 63,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44, + 45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,0,1,2} ; + #elif HB_FILTERORDER == 48 + static const qint16 HB_MOD[48+6] = { 47,48,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41, + 42,43,44,45,46,47,48,0,1,2} ; + #elif HB_FILTERORDER == 32 + static const qint16 HB_MOD[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2}; + #else + #error unsupported filter order + #endif - m_samples[m_ptr][0] = sample1->real(); - m_samples[m_ptr][1] = sample1->imag(); - m_ptr = mod33[m_ptr + 2 - 1]; + m_samples[m_ptr][0] = sample1->real(); + m_samples[m_ptr][1] = sample1->imag(); + m_ptr = HB_MOD[m_ptr + 2 - 1]; - m_samples[m_ptr][0] = sample2->real(); - m_samples[m_ptr][1] = sample2->imag(); + m_samples[m_ptr][0] = sample2->real(); + m_samples[m_ptr][1] = sample2->imag(); - doFIR(sample2); + doFIR(sample2); - m_ptr = mod33[m_ptr + 2 - 1]; - } + m_ptr = HB_MOD[m_ptr + 2 - 1]; + } - void myDecimate(qint32 x1, qint32 y1, qint32 *x2, qint32 *y2) - { - static const qint16 mod33[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, - 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2}; + void myDecimate(qint32 x1, qint32 y1, qint32 *x2, qint32 *y2) + { + #if HB_FILTERORDER == 64 + static const qint16 HB_MOD[64+6] = { 63,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44, + 45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,0,1,2} ; + #elif HB_FILTERORDER == 48 + static const qint16 HB_MOD[48+6] = { 47,48,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41, + 42,43,44,45,46,47,48,0,1,2} ; + #elif HB_FILTERORDER == 32 + static const qint16 HB_MOD[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2}; + #else + #error unsupported filter order + #endif - m_samples[m_ptr][0] = x1; - m_samples[m_ptr][1] = y1; - m_ptr = mod33[m_ptr + 2 - 1]; + m_samples[m_ptr][0] = x1; + m_samples[m_ptr][1] = y1; + m_ptr = HB_MOD[m_ptr + 2 - 1]; - m_samples[m_ptr][0] = *x2; - m_samples[m_ptr][1] = *y2; + m_samples[m_ptr][0] = *x2; + m_samples[m_ptr][1] = *y2; - doFIR(x2, y2); + doFIR(x2, y2); - m_ptr = mod33[m_ptr + 2 - 1]; - } + m_ptr = HB_MOD[m_ptr + 2 - 1]; + } protected: qint32 m_samples[HB_FILTERORDER + 1][2]; // Valgrind optim (from qint16) @@ -560,44 +632,54 @@ protected: void doFIR(Sample* sample) { - // coefficents + // Coefficents. This is a sinc function: + // Half of the half of coefficients are stored because: + // - half of the coefficients are 0 + // - there is a symmertry around the central 1.0 coefficient (not stored either) + // There are actually order+1 coefficients #if HB_FILTERORDER == 64 + static const qint16 HB_MOD[64+6] = { 63,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44, + 45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,0,1,2} ; static const qint32 COEFF[16] = { - -0.001114417441601693505720538368564120901 * (1 << HB_SHIFT), - 0.001268007827185253051302527005361753254 * (1 << HB_SHIFT), - -0.001959831378850490895410230152151598304 * (1 << HB_SHIFT), - 0.002878308307661380308073439948657323839 * (1 << HB_SHIFT), - -0.004071361818258721100571850826099762344 * (1 << HB_SHIFT), - 0.005597288494657440618973431867289036745 * (1 << HB_SHIFT), - -0.007532345003308904551886371336877346039 * (1 << HB_SHIFT), - 0.009980346844667375288961963519795972388 * (1 << HB_SHIFT), - -0.013092614174300500062830820979797863401 * (1 << HB_SHIFT), - 0.01710934914871829748417297878404497169 * (1 << HB_SHIFT), - -0.022443558692997273018576720460259821266 * (1 << HB_SHIFT), - 0.029875811511593811098386197500076377764 * (1 << HB_SHIFT), - -0.041086352085710403647667021687084343284 * (1 << HB_SHIFT), - 0.060465467462665789533104998554335907102 * (1 << HB_SHIFT), - -0.104159517495977321788203084906854201108 * (1 << HB_SHIFT), - 0.317657589850154464805598308885237202048 * (1 << HB_SHIFT), + (qint32)(-0.001114417441601693505720538368564120901 * (1 << HB_SHIFT)), + (qint32)( 0.001268007827185253051302527005361753254 * (1 << HB_SHIFT)), + (qint32)(-0.001959831378850490895410230152151598304 * (1 << HB_SHIFT)), + (qint32)( 0.002878308307661380308073439948657323839 * (1 << HB_SHIFT)), + (qint32)(-0.004071361818258721100571850826099762344 * (1 << HB_SHIFT)), + (qint32)( 0.005597288494657440618973431867289036745 * (1 << HB_SHIFT)), + (qint32)(-0.007532345003308904551886371336877346039 * (1 << HB_SHIFT)), + (qint32)( 0.009980346844667375288961963519795972388 * (1 << HB_SHIFT)), + (qint32)(-0.013092614174300500062830820979797863401 * (1 << HB_SHIFT)), + (qint32)( 0.01710934914871829748417297878404497169 * (1 << HB_SHIFT)), + (qint32)(-0.022443558692997273018576720460259821266 * (1 << HB_SHIFT)), + (qint32)( 0.029875811511593811098386197500076377764 * (1 << HB_SHIFT)), + (qint32)(-0.041086352085710403647667021687084343284 * (1 << HB_SHIFT)), + (qint32)( 0.060465467462665789533104998554335907102 * (1 << HB_SHIFT)), + (qint32)(-0.104159517495977321788203084906854201108 * (1 << HB_SHIFT)), + (qint32)( 0.317657589850154464805598308885237202048 * (1 << HB_SHIFT)), }; #elif HB_FILTERORDER == 48 + static const qint16 HB_MOD[48+6] = { 47,48,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41, + 42,43,44,45,46,47,48,0,1,2} ; static const qint32 COEFF[12] = { - -0.004102576237611492253332112767338912818 * (1 << HB_SHIFT), - 0.003950551047979387886410762575906119309 * (1 << HB_SHIFT), - -0.005807875789391703583164350277456833282 * (1 << HB_SHIFT), - 0.00823497890520805998770814682075069868 * (1 << HB_SHIFT), - -0.011372226513199541059195851744334504474 * (1 << HB_SHIFT), - 0.015471557140973646315984524335362948477 * (1 << HB_SHIFT), - -0.020944996398689276484450516591095947661 * (1 << HB_SHIFT), - 0.028568078132034283034279553703527199104 * (1 << HB_SHIFT), - -0.040015143905614086738964374490024056286 * (1 << HB_SHIFT), - 0.059669519431831075095828964549582451582 * (1 << HB_SHIFT), - -0.103669138691865420076609893840213771909 * (1 << HB_SHIFT), - 0.317491986549921390015072120149852707982 * (1 << HB_SHIFT) + (qint32)(-0.004102576237611492253332112767338912818 * (1 << HB_SHIFT)), + (qint32)( 0.003950551047979387886410762575906119309 * (1 << HB_SHIFT)), + (qint32)(-0.005807875789391703583164350277456833282 * (1 << HB_SHIFT)), + (qint32)( 0.00823497890520805998770814682075069868 * (1 << HB_SHIFT)), + (qint32)(-0.011372226513199541059195851744334504474 * (1 << HB_SHIFT)), + (qint32)( 0.015471557140973646315984524335362948477 * (1 << HB_SHIFT)), + (qint32)(-0.020944996398689276484450516591095947661 * (1 << HB_SHIFT)), + (qint32)( 0.028568078132034283034279553703527199104 * (1 << HB_SHIFT)), + (qint32)(-0.040015143905614086738964374490024056286 * (1 << HB_SHIFT)), + (qint32)( 0.059669519431831075095828964549582451582 * (1 << HB_SHIFT)), + (qint32)(-0.103669138691865420076609893840213771909 * (1 << HB_SHIFT)), + (qint32)( 0.317491986549921390015072120149852707982 * (1 << HB_SHIFT)) }; #elif HB_FILTERORDER == 32 - static const qint16 mod33[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + static const qint16 HB_MOD[32+6] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2} ; static const qint32 COEFF[8] = { (qint32)(-0.015956912844043127236437484839370881673 * (1 << HB_SHIFT)), @@ -615,8 +697,8 @@ protected: // init read-pointer - int a = mod33[m_ptr + 2 + 1]; // 0 + 1 - int b = mod33[m_ptr + 2 - 2]; //-1 - 1 + int a = HB_MOD[m_ptr + 2 + 1]; // 0 + 1 + int b = HB_MOD[m_ptr + 2 - 2]; //-1 - 1 // go through samples in buffer qint32 iAcc = 0; @@ -631,11 +713,11 @@ protected: qAcc += (m_samples[a][1] + m_samples[b][1]) * COEFF[i]; // update read-pointer - a = mod33[a + 2 + 2]; - b = mod33[b + 2 - 2]; + a = HB_MOD[a + 2 + 2]; + b = HB_MOD[b + 2 - 2]; } - a = mod33[a + 2 - 1]; + a = HB_MOD[a + 2 - 1]; iAcc += ((qint32)m_samples[a][0] + 1) << (HB_SHIFT - 1); qAcc += ((qint32)m_samples[a][1] + 1) << (HB_SHIFT - 1); @@ -646,44 +728,54 @@ protected: void doFIR(qint32 *x, qint32 *y) { - // coefficents + // Coefficents. This is a sinc function: + // Half of the half of coefficients are stored because: + // - half of the coefficients are 0 + // - there is a symmertry around the central 1.0 coefficient (not stored either) + // There are actually order+1 coefficients #if HB_FILTERORDER == 64 + static const qint16 HB_MOD[64+6] = { 63,64,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44, + 45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,0,1,2} ; static const qint32 COEFF[16] = { - -0.001114417441601693505720538368564120901 * (1 << HB_SHIFT), - 0.001268007827185253051302527005361753254 * (1 << HB_SHIFT), - -0.001959831378850490895410230152151598304 * (1 << HB_SHIFT), - 0.002878308307661380308073439948657323839 * (1 << HB_SHIFT), - -0.004071361818258721100571850826099762344 * (1 << HB_SHIFT), - 0.005597288494657440618973431867289036745 * (1 << HB_SHIFT), - -0.007532345003308904551886371336877346039 * (1 << HB_SHIFT), - 0.009980346844667375288961963519795972388 * (1 << HB_SHIFT), - -0.013092614174300500062830820979797863401 * (1 << HB_SHIFT), - 0.01710934914871829748417297878404497169 * (1 << HB_SHIFT), - -0.022443558692997273018576720460259821266 * (1 << HB_SHIFT), - 0.029875811511593811098386197500076377764 * (1 << HB_SHIFT), - -0.041086352085710403647667021687084343284 * (1 << HB_SHIFT), - 0.060465467462665789533104998554335907102 * (1 << HB_SHIFT), - -0.104159517495977321788203084906854201108 * (1 << HB_SHIFT), - 0.317657589850154464805598308885237202048 * (1 << HB_SHIFT), + (qint32)(-0.001114417441601693505720538368564120901 * (1 << HB_SHIFT)), + (qint32)( 0.001268007827185253051302527005361753254 * (1 << HB_SHIFT)), + (qint32)(-0.001959831378850490895410230152151598304 * (1 << HB_SHIFT)), + (qint32)( 0.002878308307661380308073439948657323839 * (1 << HB_SHIFT)), + (qint32)(-0.004071361818258721100571850826099762344 * (1 << HB_SHIFT)), + (qint32)( 0.005597288494657440618973431867289036745 * (1 << HB_SHIFT)), + (qint32)(-0.007532345003308904551886371336877346039 * (1 << HB_SHIFT)), + (qint32)( 0.009980346844667375288961963519795972388 * (1 << HB_SHIFT)), + (qint32)(-0.013092614174300500062830820979797863401 * (1 << HB_SHIFT)), + (qint32)( 0.01710934914871829748417297878404497169 * (1 << HB_SHIFT)), + (qint32)(-0.022443558692997273018576720460259821266 * (1 << HB_SHIFT)), + (qint32)( 0.029875811511593811098386197500076377764 * (1 << HB_SHIFT)), + (qint32)(-0.041086352085710403647667021687084343284 * (1 << HB_SHIFT)), + (qint32)( 0.060465467462665789533104998554335907102 * (1 << HB_SHIFT)), + (qint32)(-0.104159517495977321788203084906854201108 * (1 << HB_SHIFT)), + (qint32)( 0.317657589850154464805598308885237202048 * (1 << HB_SHIFT)), }; #elif HB_FILTERORDER == 48 - static const qint32 COEFF[12] = { - -0.004102576237611492253332112767338912818 * (1 << HB_SHIFT), - 0.003950551047979387886410762575906119309 * (1 << HB_SHIFT), - -0.005807875789391703583164350277456833282 * (1 << HB_SHIFT), - 0.00823497890520805998770814682075069868 * (1 << HB_SHIFT), - -0.011372226513199541059195851744334504474 * (1 << HB_SHIFT), - 0.015471557140973646315984524335362948477 * (1 << HB_SHIFT), - -0.020944996398689276484450516591095947661 * (1 << HB_SHIFT), - 0.028568078132034283034279553703527199104 * (1 << HB_SHIFT), - -0.040015143905614086738964374490024056286 * (1 << HB_SHIFT), - 0.059669519431831075095828964549582451582 * (1 << HB_SHIFT), - -0.103669138691865420076609893840213771909 * (1 << HB_SHIFT), - 0.317491986549921390015072120149852707982 * (1 << HB_SHIFT) - }; + static const qint16 HB_MOD[48+6] = { 47,48,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + 20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41, + 42,43,44,45,46,47,48,0,1,2} ; + static const qint32 COEFF[12] = { + (qint32)(-0.004102576237611492253332112767338912818 * (1 << HB_SHIFT)), + (qint32)( 0.003950551047979387886410762575906119309 * (1 << HB_SHIFT)), + (qint32)(-0.005807875789391703583164350277456833282 * (1 << HB_SHIFT)), + (qint32)( 0.00823497890520805998770814682075069868 * (1 << HB_SHIFT)), + (qint32)(-0.011372226513199541059195851744334504474 * (1 << HB_SHIFT)), + (qint32)( 0.015471557140973646315984524335362948477 * (1 << HB_SHIFT)), + (qint32)(-0.020944996398689276484450516591095947661 * (1 << HB_SHIFT)), + (qint32)( 0.028568078132034283034279553703527199104 * (1 << HB_SHIFT)), + (qint32)(-0.040015143905614086738964374490024056286 * (1 << HB_SHIFT)), + (qint32)( 0.059669519431831075095828964549582451582 * (1 << HB_SHIFT)), + (qint32)(-0.103669138691865420076609893840213771909 * (1 << HB_SHIFT)), + (qint32)( 0.317491986549921390015072120149852707982 * (1 << HB_SHIFT)) + }; #elif HB_FILTERORDER == 32 - static const int mod33[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, + static const int HB_MOD[38] = { 31,32,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20,21,22,23,24,25,26,27,28,29,30,31,32,0,1,2} ; static const qint32 COEFF[8] = { (qint32)(-0.015956912844043127236437484839370881673 * (1 << HB_SHIFT)), @@ -701,8 +793,8 @@ protected: // init read-pointer - int a = mod33[m_ptr + 2 + 1]; // 0 + 1 - int b = mod33[m_ptr + 2 - 2]; //-1 - 1 + int a = HB_MOD[m_ptr + 2 + 1]; // 0 + 1 + int b = HB_MOD[m_ptr + 2 - 2]; //-1 - 1 // go through samples in buffer qint32 iAcc = 0; @@ -717,11 +809,11 @@ protected: qAcc += (m_samples[a][1] + m_samples[b][1]) * COEFF[i]; // update read-pointer - a = mod33[a + 2 + 2]; - b = mod33[b + 2 - 2]; + a = HB_MOD[a + 2 + 2]; + b = HB_MOD[b + 2 - 2]; } - a = mod33[a + 2 - 1]; + a = HB_MOD[a + 2 - 1]; iAcc += ((qint32)m_samples[a][0] + 1) << (HB_SHIFT - 1); qAcc += ((qint32)m_samples[a][1] + 1) << (HB_SHIFT - 1); diff --git a/sdrbase/dsp/upchannelizer.cpp b/sdrbase/dsp/upchannelizer.cpp index 0f5054f6a..88ac0af7b 100644 --- a/sdrbase/dsp/upchannelizer.cpp +++ b/sdrbase/dsp/upchannelizer.cpp @@ -171,7 +171,8 @@ void UpChannelizer::applyConfiguration() m_currentInputSampleRate = m_outputSampleRate / (1 << m_filterStages.size()); - qDebug() << "UpChannelizer::applyConfiguration out=" << m_outputSampleRate + qDebug() << "UpChannelizer::applyConfiguration:" + << " out=" << m_outputSampleRate << ", req=" << m_requestedInputSampleRate << ", in=" << m_currentInputSampleRate << ", fc=" << m_currentCenterFrequency; @@ -225,34 +226,52 @@ Real UpChannelizer::createFilterChain(Real sigStart, Real sigEnd, Real chanStart safetyMargin = 0; - //fprintf(stderr, "Channelizer::createFilterChain: "); - //fprintf(stderr, "Signal [%.1f, %.1f] (BW %.1f), Channel [%.1f, %.1f], Rot %.1f, Safety %.1f\n", sigStart, sigEnd, sigBw, chanStart, chanEnd, rot, safetyMargin); -#if 1 + qDebug() << "UpChannelizer::createFilterChain: start:" + << " sig: [" << sigStart << ":" << sigEnd << "]" + << " BW: " << sigBw + << " chan: [" << chanStart << ":" << chanEnd << "]" + << " rot: " << rot + << " safety: " << safetyMargin; + // check if it fits into the left half - if(signalContainsChannel(sigStart + safetyMargin, sigStart + sigBw / 2.0 - safetyMargin, chanStart, chanEnd)) { - //fprintf(stderr, "-> take left half (rotate by +1/4 and decimate by 2)\n"); + if(signalContainsChannel(sigStart + safetyMargin, sigStart + sigBw / 2.0 - safetyMargin, chanStart, chanEnd)) + { + qDebug() << "UpChannelizer::createFilterChain: take left half (rotate by +1/4 and decimate by 2):" + << " [" << m_filterStages.size() << "]" + << " sig: [" << sigStart << ":" << sigStart + sigBw / 2.0 << "]"; m_filterStages.push_back(new FilterStage(FilterStage::ModeLowerHalf)); return createFilterChain(sigStart, sigStart + sigBw / 2.0, chanStart, chanEnd); } // check if it fits into the right half - if(signalContainsChannel(sigEnd - sigBw / 2.0f + safetyMargin, sigEnd - safetyMargin, chanStart, chanEnd)) { - //fprintf(stderr, "-> take right half (rotate by -1/4 and decimate by 2)\n"); + if(signalContainsChannel(sigEnd - sigBw / 2.0f + safetyMargin, sigEnd - safetyMargin, chanStart, chanEnd)) + { + qDebug() << "UpChannelizer::createFilterChain: take right half (rotate by -1/4 and decimate by 2):" + << " [" << m_filterStages.size() << "]" + << " sig: [" << sigEnd - sigBw / 2.0f << ":" << sigEnd << "]"; m_filterStages.push_back(new FilterStage(FilterStage::ModeUpperHalf)); return createFilterChain(sigEnd - sigBw / 2.0f, sigEnd, chanStart, chanEnd); } // check if it fits into the center // Was: if(signalContainsChannel(sigStart + rot + safetyMargin, sigStart + rot + sigBw / 2.0f - safetyMargin, chanStart, chanEnd)) { - if(signalContainsChannel(sigStart + rot + safetyMargin, sigEnd - rot - safetyMargin, chanStart, chanEnd)) { - //fprintf(stderr, "-> take center half (decimate by 2)\n"); + if(signalContainsChannel(sigStart + rot + safetyMargin, sigEnd - rot - safetyMargin, chanStart, chanEnd)) + { + qDebug() << "UpChannelizer::createFilterChain: take center half (decimate by 2):" + << " [" << m_filterStages.size() << "]" + << " sig: [" << sigStart + rot << ":" << sigEnd - rot << "]"; m_filterStages.push_back(new FilterStage(FilterStage::ModeCenter)); // Was: return createFilterChain(sigStart + rot, sigStart + sigBw / 2.0f + rot, chanStart, chanEnd); return createFilterChain(sigStart + rot, sigEnd - rot, chanStart, chanEnd); } -#endif + Real ofs = ((chanEnd - chanStart) / 2.0 + chanStart) - ((sigEnd - sigStart) / 2.0 + sigStart); - //fprintf(stderr, "-> complete (final BW %.1f, frequency offset %.1f)\n", sigBw, ofs); + + qDebug() << "UpChannelizer::createFilterChain: complete:" + << " #stages: " << m_filterStages.size() + << " BW: " << sigBw + << " ofs: " << ofs; + return ofs; }