diff --git a/FMCTCSSRX.cpp b/FMCTCSSRX.cpp index 9391ceb..1224358 100644 --- a/FMCTCSSRX.cpp +++ b/FMCTCSSRX.cpp @@ -22,67 +22,73 @@ const struct CTCSS_TABLE { uint8_t frequency; - q31_t coeff; + long coeff; } CTCSS_TABLE_DATA[] = { - { 67U, 131052}, - { 69U, 131051}, - { 71U, 131049}, - { 74U, 131048}, - { 77U, 131046}, - { 79U, 131044}, - { 82U, 131042}, - { 85U, 131040}, - { 88U, 131037}, - { 91U, 131035}, - { 94U, 131032}, - { 97U, 131030}, - {100U, 131028}, - {103U, 131024}, - {107U, 131021}, - {110U, 131017}, - {114U, 131013}, - {118U, 131009}, - {123U, 131005}, - {127U, 131000}, - {131U, 130994}, - {136U, 130989}, - {141U, 130983}, - {146U, 130977}, - {151U, 130970}, - {156U, 130962}, - {159U, 130958}, - {162U, 130954}, - {165U, 130949}, - {167U, 130946}, - {171U, 130941}, - {173U, 130937}, - {177U, 130931}, - {179U, 130927}, - {183U, 130921}, - {186U, 130917}, - {189U, 130911}, - {192U, 130906}, - {196U, 130899}, - {199U, 130894}, - {203U, 130887}, - {206U, 130881}, - {210U, 130873}, - {218U, 130859}, - {225U, 130844}, - {229U, 130837}, - {233U, 130827}, - {241U, 130810}, - {250U, 130791}, - {254U, 130783}}; + {67, 65527}, +{69, 65526}, +{71, 65525}, +{74, 65524}, +{77, 65523}, +{79, 65522}, +{82, 65521}, +{85, 65520}, +{88, 65519}, +{91, 65518}, +{94, 65517}, +{97, 65516}, +{100, 65514}, +{103, 65513}, +{107, 65511}, +{110, 65509}, +{114, 65507}, +{123, 65503}, +{127, 65501}, +{131, 65498}, +{136, 65495}, +{141, 65492}, +{146, 65489}, +{150, 65487}, +{151, 65486}, +{156, 65482}, +{159, 65480}, +{162, 65478}, +{165, 65476}, +{167, 65474}, +{171, 65472}, +{173, 65470}, +{177, 65467}, +{179, 65465}, +{183, 65462}, +{186, 65460}, +{188, 65458}, +{189, 65457}, +{192, 65454}, +{196, 65451}, +{199, 65448}, +{203, 65445}, +{206, 65442}, +{210, 65438}, +{213, 65435}, +{218, 65431}, +{221, 65428}, +{225, 65424}, +{229, 65420}, +{233, 65416}, +{237, 65412}, +{241, 65407}, +{245, 65403}, +{250, 65398}, +{254, 65393} +}; -const uint8_t CTCSS_TABLE_DATA_LEN = 50U; +const uint8_t CTCSS_TABLE_DATA_LEN = 55U; // 4Hz bandwidth const uint16_t N = 24000U / 4U; CFMCTCSSRX::CFMCTCSSRX() : m_coeff(0), -m_threshold(0U), +m_thresholdSquared(0U), m_count(0U), m_q0(0), m_q1(0), @@ -102,7 +108,7 @@ uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold) if (m_coeff == 0) return 4U; - m_threshold = threshold * threshold; + m_thresholdSquared = (long)(threshold * threshold); return 0U; } @@ -110,39 +116,24 @@ uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold) bool CFMCTCSSRX::process(const q15_t* samples, uint8_t length) { for (unsigned int i = 0U; i < length; i++) { - q31_t q2 = m_q1; + + long sampleLong = (long)samples[i]; + + long q2 = m_q1; m_q1 = m_q0; - - // Q31 multiplication, t2 = m_coeff * m_q1 - q63_t t1 = m_coeff * m_q1; - q31_t t2 = __SSAT(t1 >> 32, 31); - - // m_q0 = m_coeff * m_q1 - q2 + samples[i] - m_q0 = t2 - q2 + samples[i]; + m_q0 = m_coeff * m_q1 - q2 + sampleLong; m_count++; - if (m_count == N) { - // Q31 multiplication, t2 = m_q0 * m_q0 - q63_t t1 = m_q0 * m_q0; - q31_t t2 = __SSAT(t1 >> 32, 31); - - // Q31 multiplication, t4 = m_q0 * m_q0 - q63_t t3 = m_q1 * m_q1; - q31_t t4 = __SSAT(t3 >> 32, 31); - - // Q31 multiplication, t8 = m_q0 * m_q1 * m_coeff - q63_t t5 = m_q0 * m_q1; - q31_t t6 = __SSAT(t5 >> 32, 31); - q63_t t7 = t6 * m_coeff; - q31_t t8 = __SSAT(t7 >> 32, 31); - - // value = m_q0 * m_q0 + m_q1 * m_q1 - m_q0 * m_q1 * m_coeff - q31_t value = t2 + t4 - t8; - - m_result = value >= m_threshold; - m_count = 0U; - m_q0 = 0; - m_q1 = 0; + if(m_count == N) { + m_count = 0; + if(!m_result) { + long magnitudeSquared = m_q0 * m_q0 + m_q1 * m_q1 - m_q0 * m_q1 * m_coeff; + if(magnitudeSquared >= m_thresholdSquared) { + m_result = magnitudeSquared >= m_thresholdSquared; + m_q0 = 0; + m_q1 = 0; + } + } } } diff --git a/FMCTCSSRX.h b/FMCTCSSRX.h index c7fcef9..5d28a58 100644 --- a/FMCTCSSRX.h +++ b/FMCTCSSRX.h @@ -32,11 +32,11 @@ public: void reset(); private: - q31_t m_coeff; - uint16_t m_threshold; + long m_coeff; + long m_thresholdSquared; uint16_t m_count; - q31_t m_q0; - q31_t m_q1; + long m_q0; + long m_q1; bool m_result; };