Fix FM audio noise floor, TX gain, and sample wrapping

The 300-2700 Hz bandpass rounded to integer counts after each stage.
Stage 1 is -14 dB and stage 2 is +13.6 dB, which left a broadband floor
of about 7 counts RMS and a signal-dependent DC offset. It is now one
CFMBandpass class that carries 12 fractional bits between stages, uses
an int64 accumulate, and rounds once at the output. RF and ext-audio
paths both use it.

The ext-audio upsampler now multiplies by 3 to restore the 1/3 lost to
zero stuffing.

The RF/ext audio boost multiplies now saturate instead of wrapping,
and the DAC write clamps to 0..4095 rather than only counting overflow.

Deploy with ExtAudioBoost=1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Cort Buffington
2026-10-30 10:23:49 -05:00
co-authored by Claude Opus 5.5
parent e8061763bf
commit 7c736e17ef
5 changed files with 64 additions and 34 deletions
+11 -17
View File
@@ -65,12 +65,8 @@ m_hangTimer(),
m_reverseTimer(), m_reverseTimer(),
m_extGapTimer(), m_extGapTimer(),
m_needReverse(false), 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_filter(),
m_filterStage2(32768, 0,-32768, 32768, -50339, 19052), m_dsFilter(),
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_blanking(), m_blanking(),
m_accessMode(1U), m_accessMode(1U),
m_linkMode(false), 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) { 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); currentSample = m_blanking.process(currentSample);
if (m_extEnabled && (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF)) { 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); m_downSampler.addSample(dsSample);
} }
currentSample *= currentBoost; currentSample = q15_t(__SSAT(q31_t(currentSample) * currentBoost, 16));
} else { } else {
currentSample = 0; currentSample = 0;
} }
} else { } else {
if (m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT) { if (m_state == FS_RELAYING_EXT || m_state == FS_KERCHUNK_EXT) {
currentSample *= currentBoost; currentSample = q15_t(__SSAT(q31_t(currentSample) * currentBoost, 16));
} else { } else {
if (m_extEnabled && (m_state == FS_RELAYING_RF || m_state == FS_KERCHUNK_RF)) { 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); m_downSampler.addSample(dsSample);
} }
continue; continue;
@@ -240,7 +236,7 @@ void CFM::repeaterSamples(bool cos, q15_t* samples, const uint16_t* rssi, uint8_
currentSample += m_callsign.getLowAudio(); 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()) if (!m_callsign.isRunning() && !m_rfAck.isRunning() && !m_extAck.isRunning())
currentSample += m_timeoutTone.getAudio(); currentSample += m_timeoutTone.getAudio();
@@ -339,16 +335,16 @@ void CFM::linkSamples(bool cos, q15_t* samples, uint8_t length)
if (m_rfSignal && m_extEnabled) { if (m_rfSignal && m_extEnabled) {
q15_t currentSample = m_blanking.process(currentRFSample); 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); m_downSampler.addSample(dsSample);
} }
if (!m_extSignal) if (!m_extSignal)
continue; 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()); currentSample += m_ctcssTX.getAudio(m_reverseTimer.isRunning());
@@ -420,9 +416,7 @@ void CFM::reset()
m_inputExtRB.reset(); m_inputExtRB.reset();
m_downSampler.reset(); m_downSampler.reset();
m_dsFilterStage1.reset(); m_dsFilter.reset();
m_dsFilterStage2.reset();
m_dsFilterStage3.reset();
m_squelch.reset(); m_squelch.reset();
m_needReverse = false; m_needReverse = false;
+2 -6
View File
@@ -77,12 +77,8 @@ private:
CFMTimer m_reverseTimer; CFMTimer m_reverseTimer;
CFMTimer m_extGapTimer; // debounce for link-mode ext-audio underrun, see linkStateMachine() CFMTimer m_extGapTimer; // debounce for link-mode ext-audio underrun, see linkStateMachine()
bool m_needReverse; bool m_needReverse;
CFMDirectFormI m_filterStage1; CFMBandpass m_filter;
CFMDirectFormI m_filterStage2; CFMBandpass m_dsFilter; // anti-alias filter for the downsampler
CFMDirectFormI m_filterStage3;
CFMDirectFormI m_dsFilterStage1; // anti-alias filter for the downsampler, own state
CFMDirectFormI m_dsFilterStage2;
CFMDirectFormI m_dsFilterStage3;
CFMBlanking m_blanking; CFMBlanking m_blanking;
uint8_t m_accessMode; uint8_t m_accessMode;
bool m_linkMode; bool m_linkMode;
+39 -4
View File
@@ -37,6 +37,9 @@ THE SOFTWARE.
#ifndef DIRECTFORMI_H_ #ifndef DIRECTFORMI_H_
#define DIRECTFORMI_H_ #define DIRECTFORMI_H_
const q31_t FM_FILTER_FRAC_BITS = 12;
class CFMDirectFormI class CFMDirectFormI
{ {
public: public:
@@ -78,13 +81,13 @@ public:
m_y2 = 0; m_y2 = 0;
} }
// filtering operation: one sample in and one out // filtering operation: one sample in and one out, both with FM_FILTER_FRAC_BITS fractional bits
inline q15_t filter(const q15_t in) inline q31_t filter(const q31_t in)
{ {
// calculate the output // 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 // update the delay lines
m_x2 = m_x1; m_x2 = m_x1;
@@ -107,6 +110,38 @@ private:
q31_t c_a1,c_a2; // IIR q31_t c_a1,c_a2; // IIR
}; };
// 3rd order Cheby Filter 300 to 2700Hz, 0.2dB passband ripple, sampling rate 24kHz
class CFMBandpass
{
public:
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 reset()
{
m_stage1.reset();
m_stage2.reset();
m_stage3.reset();
}
// keep the fractional bits between the stages, round once at the end
inline q15_t filter(const 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));
}
private:
CFMDirectFormI m_stage1;
CFMDirectFormI m_stage2;
CFMDirectFormI m_stage3;
};
#endif /* DIRECTFORMI_H */ #endif /* DIRECTFORMI_H */
#endif #endif
+2 -2
View File
@@ -76,11 +76,11 @@ bool CFMUpSampler::getSample(q15_t& sample)
m_packPointer[1U] = pairPack.byte1; m_packPointer[1U] = pairPack.byte1;
m_packPointer[2U] = pairPack.byte2; 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; break;
} }
case 3: case 3:
sample = q15_t(m_pack & FM_UPSAMPLE_MASK) - 2048; sample = (q15_t(m_pack & FM_UPSAMPLE_MASK) - 2048) * 3;
break; break;
default: default:
sample = 0; sample = 0;
+10 -5
View File
@@ -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++) { for (uint16_t i = 0U; i < length; i++) {
q31_t res1 = samples[i] * txLevel; q31_t res1 = samples[i] * txLevel;
q15_t res2 = q15_t(__SSAT((res1 >> 15), 16)); 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 // Detect and clamp DAC overflow
if (res3 > 4095U) if (res3 > 4095) {
m_dacOverflow++; m_dacOverflow++;
res3 = 4095;
} else if (res3 < 0) {
m_dacOverflow++;
res3 = 0;
}
if (control == NULL) if (control == NULL)
m_txBuffer.put({res3, MARK_NONE}); m_txBuffer.put({uint16_t(res3), MARK_NONE});
else else
m_txBuffer.put({res3, control[i]}); m_txBuffer.put({uint16_t(res3), control[i]});
} }
} }