Convert to using floats.

This commit is contained in:
Jonathan Naylor 2020-04-18 16:48:30 +01:00
parent 6d5b864cba
commit ebdba2bdb9
3 changed files with 102 additions and 62 deletions

1
FM.cpp
View File

@ -103,6 +103,7 @@ void CFM::process()
void CFM::reset() void CFM::reset()
{ {
m_ctcssRX.reset();
} }
uint8_t CFM::setCallsign(const char* callsign, uint8_t speed, uint16_t frequency, uint8_t time, uint8_t holdoff, uint8_t level, bool callsignAtStart, bool callsignAtEnd) uint8_t CFM::setCallsign(const char* callsign, uint8_t speed, uint16_t frequency, uint8_t time, uint8_t holdoff, uint8_t level, bool callsignAtStart, bool callsignAtEnd)

View File

@ -21,65 +21,72 @@
#include "FMCTCSSRX.h" #include "FMCTCSSRX.h"
const struct CTCSS_TABLE { const struct CTCSS_TABLE {
uint8_t frequency; uint8_t frequency;
q31_t coeffDivTwo; float32_t coeff;
} CTCSS_TABLE_DATA[] = { } CTCSS_TABLE_DATA[] = {
{ 67U, 2147475280}, { 67U, 1.999692},
{ 69U, 2147474696}, { 69U, 1.999671},
{ 71U, 2147474012}, { 71U, 1.999646},
{ 74U, 2147473330}, { 74U, 1.999621},
{ 77U, 2147472596}, { 77U, 1.999594},
{ 79U, 2147471807}, { 79U, 1.999565},
{ 82U, 2147470961}, { 82U, 1.999534},
{ 85U, 2147470053}, { 85U, 1.999500},
{ 88U, 2147469048}, { 88U, 1.999463},
{ 91U, 2147468042}, { 91U, 1.999426},
{ 94U, 2147466895}, { 94U, 1.999384},
{ 97U, 2147465964}, { 97U, 1.999350},
{100U, 2147465007}, {100U, 1.999315},
{103U, 2147463679}, {103U, 1.999266},
{107U, 2147462226}, {107U, 1.999212},
{110U, 2147460722}, {110U, 1.999157},
{114U, 2147459081}, {114U, 1.999097},
{118U, 2147457339}, {118U, 1.999033},
{123U, 2147455446}, {123U, 1.998963},
{127U, 2147453440}, {127U, 1.998889},
{131U, 2147451266}, {131U, 1.998810},
{136U, 2147448916}, {136U, 1.998723},
{141U, 2147446430}, {141U, 1.998632},
{146U, 2147443804}, {146U, 1.998535},
{151U, 2147440919}, {151U, 1.998429},
{156U, 2147437875}, {156U, 1.998317},
{159U, 2147436046}, {159U, 1.998250},
{162U, 2147434605}, {162U, 1.998197},
{165U, 2147432590}, {165U, 1.998123},
{167U, 2147431098}, {167U, 1.998068},
{171U, 2147428948}, {171U, 1.997989},
{173U, 2147427340}, {173U, 1.997930},
{177U, 2147425049}, {177U, 1.997846},
{179U, 2147423318}, {179U, 1.997782},
{183U, 2147420879}, {183U, 1.997693},
{186U, 2147419018}, {186U, 1.997624},
{189U, 2147416424}, {189U, 1.997529},
{192U, 2147414356}, {192U, 1.997453},
{196U, 2147411597}, {196U, 1.997351},
{199U, 2147409456}, {199U, 1.997273},
{203U, 2147406451}, {203U, 1.997162},
{206U, 2147404158}, {206U, 1.997078},
{210U, 2147400892}, {210U, 1.996958},
{218U, 2147394977}, {218U, 1.996741},
{225U, 2147388689}, {225U, 1.996510},
{229U, 2147385807}, {229U, 1.996404},
{233U, 2147381925}, {233U, 1.996261},
{241U, 2147374659}, {241U, 1.995994},
{250U, 2147366861}, {250U, 1.995708},
{254U, 2147363288}}; {254U, 1.995576}};
const uint8_t CTCSS_TABLE_DATA_LEN = 50U; const uint8_t CTCSS_TABLE_DATA_LEN = 50U;
// 4Hz bandwidth
const uint16_t N = 24000U / 4U;
CFMCTCSSRX::CFMCTCSSRX() : CFMCTCSSRX::CFMCTCSSRX() :
m_coeffDivTwo(0), m_coeff(0.0),
m_threshold(0U) m_threshold(0.0),
m_count(0U),
m_q0(0.0),
m_q1(0.0),
m_result(false)
{ {
} }
@ -87,20 +94,46 @@ 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_coeffDivTwo = CTCSS_TABLE_DATA[i].coeffDivTwo; m_coeff = CTCSS_TABLE_DATA[i].coeff;
break; break;
} }
} }
if (m_coeffDivTwo == 0) if (m_coeff == 0.0)
return 4U; return 4U;
m_threshold = threshold; m_threshold = float32_t(threshold * threshold);
return 0U; return 0U;
} }
bool CFMCTCSSRX::process(const q15_t* samples, uint8_t length) bool CFMCTCSSRX::process(q15_t* samples, uint8_t length)
{ {
return false; float32_t data[RX_BLOCK_SIZE];
::arm_q15_to_float(samples, data, length);
for (unsigned int i = 0U; i < length; i++) {
float32_t q2 = m_q1;
m_q1 = m_q0;
m_q0 = m_coeff * m_q1 - q2 + data[i];
m_count++;
if (m_count == N) {
float32_t value = m_q0 * m_q0 + m_q1 * m_q1 - m_q0 * m_q1 * m_coeff;
m_result = value >= m_threshold;
m_count = 0U;
m_q0 = 0.0F;
m_q1 = 0.0F;
}
}
return m_result;
}
void CFMCTCSSRX::reset()
{
m_q0 = 0.0;
m_q1 = 0.0;
m_result = false;
m_count = 0U;
} }

View File

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