diff --git a/FM.cpp b/FM.cpp index 074de3d..85f9fb5 100644 --- a/FM.cpp +++ b/FM.cpp @@ -41,8 +41,7 @@ m_filterStage2(32768, 0,-32768, 32768, -50339, 19052), m_filterStage3(32768, -65536, 32768, 32768, -64075, 31460), m_blanking(), m_useCOS(true), -m_rxBoost(1U), -m_rxLevel(128 * 128) +m_rxBoost(1U) { } @@ -55,7 +54,7 @@ void CFM::samples(bool cos, q15_t* samples, uint8_t length) for (; i < length; i++) { q15_t currentSample = samples[i];//save to a local variable to avoid indirection on every access - CTCSSState ctcssState = m_ctcssRX.process(getUnscaledSample(currentSample)); + CTCSSState ctcssState = m_ctcssRX.process(currentSample); if (CTCSS_NOT_READY(ctcssState) && m_modemState != STATE_FM) { //Not enough samples to determine if you have CTCSS, just carry on @@ -149,7 +148,6 @@ uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFreque { m_useCOS = useCOS; m_rxBoost = q15_t(rxBoost); - m_rxLevel = q15_t(rxLevel * 128); m_timeoutTimer.setTimeout(timeout, 0U); m_kerchunkTimer.setTimeout(kerchunkTime, 0U); @@ -158,7 +156,7 @@ uint8_t CFM::setMisc(uint16_t timeout, uint8_t timeoutLevel, uint8_t ctcssFreque m_timeoutTone.setParams(timeoutLevel); m_blanking.setParams(maxDev, timeoutLevel); - uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold); + uint8_t ret = m_ctcssRX.setParams(ctcssFrequency, ctcssThreshold, q15_t(rxLevel * 128)); if (ret != 0U) return ret; @@ -400,17 +398,4 @@ void CFM::beginRelaying() m_ackMinTimer.start(); } -q15_t CFM::getUnscaledSample(q15_t sample) -{ - // sample / rxLevel - q31_t sample31 = q31_t(sample) << 16; - if (((sample31 >> 31) & 1) == ((m_rxLevel >> 15) & 1)) - sample31 += m_rxLevel >> 1; - else - sample31 -= m_rxLevel >> 1; - - sample31 /= m_rxLevel; - - return q15_t(sample31); -} diff --git a/FM.h b/FM.h index 1a07eb8..c56d462 100644 --- a/FM.h +++ b/FM.h @@ -78,7 +78,6 @@ private: CFMBlanking m_blanking; bool m_useCOS; q15_t m_rxBoost; - q15_t m_rxLevel; void stateMachine(bool validSignal, uint8_t length); void listeningState(bool validSignal); @@ -91,8 +90,6 @@ private: void sendCallsign(); void beginRelaying(); - - q15_t getUnscaledSample(q15_t sample); }; #endif diff --git a/FMCTCSSRX.cpp b/FMCTCSSRX.cpp index 57fbfbe..180f1f3 100644 --- a/FMCTCSSRX.cpp +++ b/FMCTCSSRX.cpp @@ -86,12 +86,16 @@ m_threshold(0), m_count(0U), m_q0(0), m_q1(0), -m_result(CTS_NONE) +m_result(CTS_NONE), +m_rxLevelInverse(1) { } -uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold) +uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold, q15_t rxLevel) { + // Calculate 1/rxLevel + m_rxLevelInverse = q31_t(q15Division(65535 /* This value should be 32767 (q15 1). But this does not work.*/, rxLevel)); + m_coeffDivTwo = 0; for (uint8_t i = 0U; i < CTCSS_TABLE_DATA_LEN; i++) { @@ -111,6 +115,8 @@ uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold) CTCSSState CFMCTCSSRX::process(q15_t sample) { + q63_t sample31 = q31_t(sample) * m_rxLevelInverse; + m_result = m_result & (~CTS_READY); q31_t q2 = m_q1; @@ -122,7 +128,7 @@ CTCSSState CFMCTCSSRX::process(q15_t sample) q31_t t3 = t2 * 2; // m_q0 = m_coeffDivTwo * m_q1 * 2 - q2 + sample - m_q0 = t3 - q2 + q31_t(sample); + m_q0 = t3 - q2 + sample31; m_count++; if (m_count == N) { diff --git a/FMCTCSSRX.h b/FMCTCSSRX.h index a848cb9..7f36b32 100644 --- a/FMCTCSSRX.h +++ b/FMCTCSSRX.h @@ -53,7 +53,7 @@ class CFMCTCSSRX { public: CFMCTCSSRX(); - uint8_t setParams(uint8_t frequency, uint8_t threshold); + uint8_t setParams(uint8_t frequency, uint8_t threshold, q15_t rxLevel); CTCSSState process(q15_t sample); @@ -62,12 +62,25 @@ public: void reset(); private: - q63_t m_coeffDivTwo; - q31_t m_threshold; - uint16_t m_count; - q31_t m_q0; - q31_t m_q1; - CTCSSState m_result; + static inline q15_t q15Division(q15_t a, q15_t divisor) + { + q31_t a31 = q31_t(a) << 16; + + if (((a >> 31) & 1) == ((divisor >> 15) & 1)) + a31 += divisor >> 1; + else + a31 -= divisor >> 1; + + return a31 / divisor; + } + + q63_t m_coeffDivTwo; + q31_t m_threshold; + uint16_t m_count; + q31_t m_q0; + q31_t m_q1; + CTCSSState m_result; + q31_t m_rxLevelInverse; }; #endif