Convert the Goertzel algorithm to Q15 and Q31.

This commit is contained in:
Jonathan Naylor 2020-04-19 16:52:11 +01:00
parent 85816d5bec
commit f7e5c2436c
2 changed files with 99 additions and 75 deletions

View File

@ -21,59 +21,59 @@
#include "FMCTCSSRX.h" #include "FMCTCSSRX.h"
const struct CTCSS_TABLE { const struct CTCSS_TABLE {
uint8_t frequency; uint8_t frequency;
float32_t coeff; q31_t coeffDivTwo;
} CTCSS_TABLE_DATA[] = { } CTCSS_TABLE_DATA[] = {
{ 67U, 1.999692}, { 67U, 2147153298},
{ 69U, 1.999671}, { 69U, 2147130228},
{ 71U, 1.999646}, { 71U, 2147103212},
{ 74U, 1.999621}, { 74U, 2147076297},
{ 77U, 1.999594}, { 77U, 2147047330},
{ 79U, 1.999565}, { 79U, 2147016195},
{ 82U, 1.999534}, { 82U, 2146982775},
{ 85U, 1.999500}, { 85U, 2146946945},
{ 88U, 1.999463}, { 88U, 2146907275},
{ 91U, 1.999426}, { 91U, 2146867538},
{ 94U, 1.999384}, { 94U, 2146822298},
{ 97U, 1.999350}, { 97U, 2146785526},
{100U, 1.999315}, {100U, 2146747759},
{103U, 1.999266}, {103U, 2146695349},
{107U, 1.999212}, {107U, 2146637984},
{110U, 1.999157}, {110U, 2146578604},
{114U, 1.999097}, {114U, 2146513835},
{118U, 1.999033}, {118U, 2146445080},
{123U, 1.998963}, {123U, 2146370355},
{127U, 1.998889}, {127U, 2146291161},
{131U, 1.998810}, {131U, 2146205372},
{136U, 1.998723}, {136U, 2146112589},
{141U, 1.998632}, {141U, 2146014479},
{146U, 1.998535}, {146U, 2145910829},
{151U, 1.998429}, {151U, 2145796971},
{156U, 1.998317}, {156U, 2145676831},
{159U, 1.998250}, {159U, 2145604646},
{162U, 1.998197}, {162U, 2145547790},
{165U, 1.998123}, {165U, 2145468230},
{167U, 1.998068}, {167U, 2145409363},
{171U, 1.997989}, {171U, 2145324517},
{173U, 1.997930}, {173U, 2145261046},
{177U, 1.997846}, {177U, 2145170643},
{179U, 1.997782}, {179U, 2145102321},
{183U, 1.997693}, {183U, 2145006080},
{186U, 1.997624}, {186U, 2144932648},
{189U, 1.997529}, {189U, 2144830280},
{192U, 1.997453}, {192U, 2144748638},
{196U, 1.997351}, {196U, 2144639788},
{199U, 1.997273}, {199U, 2144555290},
{203U, 1.997162}, {203U, 2144436713},
{206U, 1.997078}, {206U, 2144346237},
{210U, 1.996958}, {210U, 2144217348},
{218U, 1.996741}, {218U, 2143983951},
{225U, 1.996510}, {225U, 2143735870},
{229U, 1.996404}, {229U, 2143622139},
{233U, 1.996261}, {233U, 2143469001},
{241U, 1.995994}, {241U, 2143182299},
{250U, 1.995708}, {250U, 2142874683},
{254U, 1.995576}}; {254U, 2142733729}};
const uint8_t CTCSS_TABLE_DATA_LEN = 50U; const uint8_t CTCSS_TABLE_DATA_LEN = 50U;
@ -81,11 +81,11 @@ const uint8_t CTCSS_TABLE_DATA_LEN = 50U;
const uint16_t N = 24000U / 4U; const uint16_t N = 24000U / 4U;
CFMCTCSSRX::CFMCTCSSRX() : CFMCTCSSRX::CFMCTCSSRX() :
m_coeff(0.0), m_coeffDivTwo(0),
m_threshold(0.0), m_threshold(0U),
m_count(0U), m_count(0U),
m_q0(0.0), m_q0(0),
m_q1(0.0), m_q1(0),
m_result(false) m_result(false)
{ {
} }
@ -94,36 +94,60 @@ uint8_t CFMCTCSSRX::setParams(uint8_t frequency, uint8_t threshold)
{ {
for (uint8_t i = 0U; i < CTCSS_TABLE_DATA_LEN; i++) { for (uint8_t i = 0U; i < CTCSS_TABLE_DATA_LEN; i++) {
if (CTCSS_TABLE_DATA[i].frequency == frequency) { if (CTCSS_TABLE_DATA[i].frequency == frequency) {
m_coeff = CTCSS_TABLE_DATA[i].coeff; m_coeffDivTwo = CTCSS_TABLE_DATA[i].coeffDivTwo;
break; break;
} }
} }
if (m_coeff == 0.0) if (m_coeffDivTwo == 0)
return 4U; return 4U;
m_threshold = float32_t(threshold * threshold); m_threshold = threshold * threshold;
return 0U; return 0U;
} }
bool CFMCTCSSRX::process(q15_t* samples, uint8_t length) bool CFMCTCSSRX::process(const q15_t* samples, uint8_t length)
{ {
float32_t data[RX_BLOCK_SIZE];
::arm_q15_to_float(samples, data, length);
for (unsigned int i = 0U; i < length; i++) { for (unsigned int i = 0U; i < length; i++) {
float32_t q2 = m_q1; q31_t q2 = m_q1;
m_q1 = m_q0; m_q1 = m_q0;
m_q0 = m_coeff * m_q1 - q2 + data[i];
// Q31 multiplication, t3 = m_coeffDivTwo * 2 * m_q1
q63_t t1 = m_coeffDivTwo * m_q1;
q31_t t2 = __SSAT(t1 >> 32, 31);
q31_t t3 = t2 * 2;
// Convert input data to Q31 from Q15
q31_t t4 = __SSAT(samples[i] << 16, 31);
// m_q0 = m_coeffDivTwo * m_q1 * 2 - q2 + samples[i]
m_q0 = t3 - q2 + t4;
m_count++; m_count++;
if (m_count == N) { if (m_count == N) {
float32_t value = m_q0 * m_q0 + m_q1 * m_q1 - m_q0 * m_q1 * m_coeff; // 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, t9 = m_q0 * m_q1 * m_coeffDivTwo * 2
q63_t t5 = m_q0 * m_q1;
q31_t t6 = __SSAT(t5 >> 32, 31);
q63_t t7 = t6 * m_coeffDivTwo;
q31_t t8 = __SSAT(t7 >> 32, 31);
q31_t t9 = t8 * 2;
// value = m_q0 * m_q0 + m_q1 * m_q1 - m_q0 * m_q1 * m_coeffDivTwo * 2
q31_t value = t2 + t4 - t9;
m_result = value >= m_threshold; m_result = value >= m_threshold;
m_count = 0U; m_count = 0U;
m_q0 = 0.0F; m_q0 = 0;
m_q1 = 0.0F; m_q1 = 0;
} }
} }
@ -132,8 +156,8 @@ bool CFMCTCSSRX::process(q15_t* samples, uint8_t length)
void CFMCTCSSRX::reset() void CFMCTCSSRX::reset()
{ {
m_q0 = 0.0; m_q0 = 0;
m_q1 = 0.0; m_q1 = 0;
m_result = false; m_result = false;
m_count = 0U; m_count = 0U;
} }

View File

@ -27,16 +27,16 @@ public:
uint8_t setParams(uint8_t frequency, uint8_t threshold); uint8_t setParams(uint8_t frequency, uint8_t threshold);
bool process(q15_t* samples, uint8_t length); bool process(const q15_t* samples, uint8_t length);
void reset(); void reset();
private: private:
float32_t m_coeff; q31_t m_coeffDivTwo;
float32_t m_threshold; uint16_t m_threshold;
uint16_t m_count; uint16_t m_count;
float m_q0; q31_t m_q0;
float m_q1; q31_t m_q1;
bool m_result; bool m_result;
}; };