diff --git a/FM.cpp b/FM.cpp index cfcd6ca..d923514 100644 --- a/FM.cpp +++ b/FM.cpp @@ -65,12 +65,8 @@ m_hangTimer(), m_reverseTimer(), m_extGapTimer(), m_needReverse(false), -m_filterStage1( 724, 1448, 724, 32768, -37895, 21352),//3rd order Cheby Filter 300 to 2700Hz, 0.2dB passband ripple, sampling rate 24kHz -m_filterStage2(32768, 0,-32768, 32768, -50339, 19052), -m_filterStage3(32768, -65536, 32768, 32768, -64075, 31460), -m_dsFilterStage1(m_filterStage1),//same coefficients, separate state -m_dsFilterStage2(m_filterStage2), -m_dsFilterStage3(m_filterStage3), +m_filter(), +m_dsFilter(), m_blanking(), m_accessMode(1U), m_linkMode(false), @@ -207,20 +203,20 @@ void CFM::repeaterSamples(bool cos, q15_t* samples, const uint16_t* rssi, uint8_ if (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF || m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT) { currentSample = m_blanking.process(currentSample); if (m_extEnabled && (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF)) { - q15_t dsSample = m_dsFilterStage3.filter(m_dsFilterStage2.filter(m_dsFilterStage1.filter(currentSample))); + q15_t dsSample = m_dsFilter.filter(currentSample); m_downSampler.addSample(dsSample); } - currentSample *= currentBoost; + currentSample = q15_t(__SSAT(q31_t(currentSample) * currentBoost, 16)); } else { currentSample = 0; } } else { if (m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT) { - currentSample *= currentBoost; + currentSample = q15_t(__SSAT(q31_t(currentSample) * currentBoost, 16)); } else { if (m_extEnabled && (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF)) { - q15_t dsSample = m_dsFilterStage3.filter(m_dsFilterStage2.filter(m_dsFilterStage1.filter(currentSample))); + q15_t dsSample = m_dsFilter.filter(currentSample); m_downSampler.addSample(dsSample); } continue; @@ -240,7 +236,7 @@ void CFM::repeaterSamples(bool cos, q15_t* samples, const uint16_t* rssi, uint8_ currentSample += m_callsign.getLowAudio(); } - currentSample = m_filterStage3.filter(m_filterStage2.filter(m_filterStage1.filter(currentSample))); + currentSample = m_filter.filter(currentSample); if (!m_callsign.isRunning() && !m_rfAck.isRunning() && !m_extAck.isRunning()) currentSample += m_timeoutTone.getAudio(); @@ -339,16 +335,16 @@ void CFM::linkSamples(bool cos, q15_t* samples, uint8_t length) if (m_rfSignal && m_extEnabled) { q15_t currentSample = m_blanking.process(currentRFSample); - q15_t dsSample = m_dsFilterStage3.filter(m_dsFilterStage2.filter(m_dsFilterStage1.filter(currentSample))); + q15_t dsSample = m_dsFilter.filter(currentSample); m_downSampler.addSample(dsSample); } if (!m_extSignal) continue; - q15_t currentSample = currentExtSample * m_extAudioBoost; + q15_t currentSample = q15_t(__SSAT(q31_t(currentExtSample) * m_extAudioBoost, 16)); - currentSample = m_filterStage3.filter(m_filterStage2.filter(m_filterStage1.filter(currentSample))); + currentSample = m_filter.filter(currentSample); currentSample += m_ctcssTX.getAudio(m_reverseTimer.isRunning()); @@ -420,9 +416,7 @@ void CFM::reset() m_inputExtRB.reset(); m_downSampler.reset(); - m_dsFilterStage1.reset(); - m_dsFilterStage2.reset(); - m_dsFilterStage3.reset(); + m_dsFilter.reset(); m_squelch.reset(); m_needReverse = false; diff --git a/FM.h b/FM.h index 8d5f49b..da837b3 100644 --- a/FM.h +++ b/FM.h @@ -30,7 +30,7 @@ #include "FMKeyer.h" #include "FMTimer.h" #include "RingBuffer.h" -#include "FMDirectForm1.h" +#include "FMBandpass.h" #include "FMDownSampler.h" #include "FMUpSampler.h" #include "FMNoiseSquelch.h" @@ -77,12 +77,8 @@ private: CFMTimer m_reverseTimer; CFMTimer m_extGapTimer; // debounce for link-mode ext-audio underrun, see linkStateMachine() bool m_needReverse; - CFMDirectFormI m_filterStage1; - CFMDirectFormI m_filterStage2; - CFMDirectFormI m_filterStage3; - CFMDirectFormI m_dsFilterStage1; // anti-alias filter for the downsampler, own state - CFMDirectFormI m_dsFilterStage2; - CFMDirectFormI m_dsFilterStage3; + CFMBandpass m_filter; + CFMBandpass m_dsFilter; // anti-alias filter for the downsampler CFMBlanking m_blanking; uint8_t m_accessMode; bool m_linkMode; diff --git a/FMBandpass.cpp b/FMBandpass.cpp new file mode 100644 index 0000000..8d960d4 --- /dev/null +++ b/FMBandpass.cpp @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2020 by Jonathan Naylor G4KLX + * Copyright (C) 2026 by Cort Buffington N0MJS + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "Config.h" + +#if defined(MODE_FM) + +#include "Globals.h" +#include "FMBandpass.h" + +CFMBandpass::CFMBandpass() : +m_stage1( 724, 1448, 724, 32768, -37895, 21352), +m_stage2(32768, 0,-32768, 32768, -50339, 19052), +m_stage3(32768, -65536, 32768, 32768, -64075, 31460) +{ +} + +void CFMBandpass::reset() +{ + m_stage1.reset(); + m_stage2.reset(); + m_stage3.reset(); +} + +// keep the fractional bits between the stages, round once at the end +q15_t CFMBandpass::filter(q15_t in) +{ + q31_t out = m_stage3.filter(m_stage2.filter(m_stage1.filter(q31_t(in) << FM_FILTER_FRAC_BITS))); + + return q15_t(__SSAT((out + (1 << (FM_FILTER_FRAC_BITS - 1))) >> FM_FILTER_FRAC_BITS, 16)); +} + +#endif + diff --git a/FMBandpass.h b/FMBandpass.h new file mode 100644 index 0000000..c3055a9 --- /dev/null +++ b/FMBandpass.h @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2020 by Jonathan Naylor G4KLX + * Copyright (C) 2026 by Cort Buffington N0MJS + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include "Config.h" + +#if defined(MODE_FM) + +#if !defined(FMBandpass_H) +#define FMBandpass_H + +#include "FMDirectForm1.h" + +// 3rd order Cheby Filter 300 to 2700Hz, 0.2dB passband ripple, sampling rate 24kHz +class CFMBandpass { +public: + CFMBandpass(); + + void reset(); + + q15_t filter(q15_t in); + +private: + CFMDirectFormI m_stage1; + CFMDirectFormI m_stage2; + CFMDirectFormI m_stage3; +}; + +#endif + +#endif + diff --git a/FMDirectForm1.h b/FMDirectForm1.h index 089b863..c1f3d4c 100644 --- a/FMDirectForm1.h +++ b/FMDirectForm1.h @@ -37,6 +37,9 @@ THE SOFTWARE. #ifndef DIRECTFORMI_H_ #define DIRECTFORMI_H_ + +const q31_t FM_FILTER_FRAC_BITS = 12; + class CFMDirectFormI { public: @@ -78,13 +81,13 @@ public: m_y2 = 0; } - // filtering operation: one sample in and one out - inline q15_t filter(const q15_t in) + // filtering operation: one sample in and one out, both with FM_FILTER_FRAC_BITS fractional bits + inline q31_t filter(const q31_t in) { // calculate the output - q31_t out_upscaled = c_b0 * in + c_b1 * m_x1 + c_b2 * m_x2 - c_a1 * m_y1 - c_a2 * m_y2; + int64_t out_upscaled = int64_t(c_b0) * in + int64_t(c_b1) * m_x1 + int64_t(c_b2) * m_x2 - int64_t(c_a1) * m_y1 - int64_t(c_a2) * m_y2; - q15_t out = __SSAT(out_upscaled >> 15, 15); + q31_t out = q31_t((out_upscaled + (1 << 14)) >> 15); // update the delay lines m_x2 = m_x1; diff --git a/FMUpSampler.cpp b/FMUpSampler.cpp index d177f59..14ab1f4 100644 --- a/FMUpSampler.cpp +++ b/FMUpSampler.cpp @@ -76,11 +76,11 @@ bool CFMUpSampler::getSample(q15_t& sample) m_packPointer[1U] = pairPack.byte1; m_packPointer[2U] = pairPack.byte2; - sample = q15_t(m_pack >> 12) - 2048; + sample = (q15_t(m_pack >> 12) - 2048) * 3;//restore the 1/3 lost to zero stuffing break; } case 3: - sample = q15_t(m_pack & FM_UPSAMPLE_MASK) - 2048; + sample = (q15_t(m_pack & FM_UPSAMPLE_MASK) - 2048) * 3; break; default: sample = 0; diff --git a/IO.cpp b/IO.cpp index dc3e0c5..54d5eef 100644 --- a/IO.cpp +++ b/IO.cpp @@ -622,16 +622,21 @@ void CIO::write(MMDVM_STATE mode, q15_t* samples, uint16_t length, const uint8_t for (uint16_t i = 0U; i < length; i++) { q31_t res1 = samples[i] * txLevel; q15_t res2 = q15_t(__SSAT((res1 >> 15), 16)); - uint16_t res3 = uint16_t(res2 + m_txDCOffset); + int32_t res3 = int32_t(res2) + m_txDCOffset; - // Detect DAC overflow - if (res3 > 4095U) + // Detect and clamp DAC overflow + if (res3 > 4095) { m_dacOverflow++; + res3 = 4095; + } else if (res3 < 0) { + m_dacOverflow++; + res3 = 0; + } if (control == NULL) - m_txBuffer.put({res3, MARK_NONE}); + m_txBuffer.put({uint16_t(res3), MARK_NONE}); else - m_txBuffer.put({res3, control[i]}); + m_txBuffer.put({uint16_t(res3), control[i]}); } }