Files
mayhem-firmware/firmware/baseband/proc_am_audio.cpp
T
Mo b8ad44c431 Opt-in channel-power squelch for AM audio RX (#3315)
* AM: add opt-in channel-power squelch for AM audio RX

Add an SDR++-style power squelch on the complex channel signal for the
Analog Audio RX app in AM modes (DSB/SSB/CW). The squelch measures the
mean channel power, converts it to dBFS, and mutes the demodulated audio
when the signal is below a user-set threshold. Being carrier-based it
keys on the AM carrier (e.g. airband/ATC) rather than on audio-band
noise, which the existing FM squelch could not do for AM.

A new 'SQ' field (0-99, 0 = off) is added to the AM options view and is
persisted per app via app_settings. Default is 0, so existing AM
behaviour is unchanged unless the user enables it. Threshold maps 1..99
to -80..-20 dBFS.

Touches: message (AMConfigureMessage.squelch_level), baseband_api
(AMConfig::apply), receiver_model (am_squelch_level get/set),
app_settings (persistence), proc_am_audio (DSP), analog_audio_app (UI).

* Refactor audio power calculation to use magnitude squared

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-09-14 20:54:54 +02:00

206 lines
8.1 KiB
C++

/*
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* 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, 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; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "proc_am_audio.hpp"
#include "audio_output.hpp"
#include "audio_dma.hpp"
#include "event_m4.hpp"
#include <array>
#include <cmath>
#include "dsp_hilbert.hpp"
// Phase 2: Constructor to start threads AFTER object is fully initialized
NarrowbandAMAudio::NarrowbandAMAudio() {
// Initialize members that threads might access
channel_spectrum.set_decimation_factor(1);
// Start threads AFTER everything initialized
baseband_thread.start();
rssi_thread.start();
}
void NarrowbandAMAudio::execute(const buffer_c8_t& buffer) {
if (!configured) {
return;
}
const auto decim_0_out = decim_0.execute(buffer, dst_buffer);
const auto audio_decim_0_out = audio_decim_0.execute(decim_0_out, dst_buffer);
spectrum_samples += decim_0_out.count;
if (!spectrum_capture_active &&
spectrum_samples >= spectrum_interval_samples) {
spectrum_samples -= spectrum_interval_samples;
spectrum_capture_active =
channel_spectrum.start_capture(spectrum_decimation_factor);
}
if (spectrum_capture_active &&
channel_spectrum.feed(
audio_decim_0_out,
channel_filter_low_f,
channel_filter_high_f,
channel_filter_transition)) {
spectrum_capture_active = false;
}
const auto decim_1_out = translating_decim_1.execute(audio_decim_0_out, dst_buffer);
const auto decim_2_out = decim_2.execute(decim_1_out, dst_buffer);
const auto channel_out = channel_filter.execute(decim_2_out, dst_buffer);
// TODO: Feed channel_stats post-decimation data?
feed_channel_stats(channel_out);
auto audio = demodulate(channel_out); // now 3 AM demodulation types : demod_am, demod_ssb, demod_ssb_fm (for Wefax)
// SDR++-style power squelch on the complex channel signal (opt-in; 0 = off).
// Measures mean channel power, converts to dBFS, and mutes the audio when
// the signal is below the user threshold. Carrier-based, so it keys on the
// AM carrier (e.g. airband/ATC) rather than on audio-band noise.
if (squelch_level > 0 && channel_out.count > 0) {
uint64_t sum_mag_sq = 0;
for (size_t i = 0; i < channel_out.count; i++) {
const int32_t re = channel_out.p[i].real();
const int32_t im = channel_out.p[i].imag();
sum_mag_sq += static_cast<uint64_t>(re) * re + static_cast<uint64_t>(im) * im;
}
constexpr float full_scale_mag2 = 32768.0f * 32768.0f;
const float mean_mag2_norm =
static_cast<float>(sum_mag_sq) / (static_cast<float>(channel_out.count) * full_scale_mag2);
const float level_dbfs = (mean_mag2_norm > 0.0f) ? mag2_to_dbv_norm(mean_mag2_norm) : -200.0f;
// Map squelch_level 1..99 to a threshold of -80..-20 dBFS.
const float threshold_dbfs = -80.0f + (squelch_level - 1) * (60.0f / 98.0f);
if (level_dbfs < threshold_dbfs) {
for (size_t i = 0; i < audio.count; i++)
audio.p[i] = 0.0f;
}
}
audio_compressor.execute_in_place(audio);
audio_output.write(audio);
}
buffer_f32_t NarrowbandAMAudio::demodulate(const buffer_c16_t& channel) {
switch (modulation_ssb) { // enum class Modulation : int32_t {DSB = 0, SSB = 1, SSB_FM = 2}
case (int)(AMConfigureMessage::Modulation::DSB):
return demod_am.execute(channel, audio_buffer);
break;
case (int)(AMConfigureMessage::Modulation::SSB):
return demod_ssb.execute(channel, audio_buffer);
break;
case (int)(AMConfigureMessage::Modulation::SSB_FM): // Added to handle Weather Fax mode.
// chDbgPanic("case SSB_FM demodulation"); // Debug.
return demod_ssb_fm.execute(channel, audio_buffer); // Calling a derivative of demod_ssb (USB) , but with different FIR taps + FM audio tones demod.
break;
// return demod am as a default
default:
return demod_am.execute(channel, audio_buffer);
break;
}
}
void NarrowbandAMAudio::on_message(const Message* const message) {
switch (message->id) {
case Message::ID::UpdateSpectrum:
case Message::ID::SpectrumStreamingConfig:
channel_spectrum.on_message(message);
break;
case Message::ID::AMConfigure:
configure(*reinterpret_cast<const AMConfigureMessage*>(message));
break;
case Message::ID::CaptureConfig:
capture_config(*reinterpret_cast<const CaptureConfigMessage*>(message));
break;
case Message::ID::AudioDDCConfig:
ddc_config(*reinterpret_cast<const AudioDDCConfigMessage*>(message));
break;
default:
break;
}
}
void NarrowbandAMAudio::configure(const AMConfigureMessage& message) {
constexpr size_t decim_0_input_fs = baseband_fs;
constexpr size_t decim_0_output_fs = decim_0_input_fs / decim_0.decimation_factor;
constexpr size_t audio_decim_0_output_fs = decim_0_output_fs / 2;
constexpr size_t decim_1_output_fs =
audio_decim_0_output_fs / translating_decim_1.decimation_factor;
constexpr size_t decim_2_input_fs = decim_1_output_fs;
constexpr size_t decim_2_output_fs = decim_2_input_fs / decim_2_decimation_factor;
constexpr size_t channel_filter_input_fs = decim_2_output_fs;
// const size_t channel_filter_output_fs = channel_filter_input_fs / channel_filter_decimation_factor;
decim_0.configure(message.decim_0_filter.taps, 33554432);
audio_decim_0.configure(taps_audio_wide_halfband_0.taps);
translating_decim_1.configure(
message.decim_1_filter.taps, audio_decim_0_output_fs);
decim_2.configure(message.decim_2_filter.taps, decim_2_decimation_factor);
channel_filter.configure(message.channel_filter.taps, channel_filter_decimation_factor);
channel_filter_low_f = message.channel_filter.low_frequency_normalized * channel_filter_input_fs;
channel_filter_high_f = message.channel_filter.high_frequency_normalized * channel_filter_input_fs;
channel_filter_transition = message.channel_filter.transition_normalized * channel_filter_input_fs;
modulation_ssb = (int)message.modulation; // now sending by message , 3 types of AM demod : enum class Modulation : int32_t {DSB = 0, SSB = 1, SSB_FM = 2}
spectrum_decimation_factor = 2 * message.channel_spectrum_decimation_factor;
channel_spectrum.set_decimation_factor(1);
spectrum_interval_samples =
decim_0_output_fs / spectrum_rate_hz;
audio_output.configure(message.audio_hpf_lpf_config); // hpf in all AM demod modes (AM-6K/9K, USB/LSB,DSB), except Wefax (lpf there).
squelch_level = message.squelch_level;
configured = true;
}
void NarrowbandAMAudio::ddc_config(const AudioDDCConfigMessage& message) {
translating_decim_1.set_frequency(message.frequency);
channel_spectrum.set_channel_filter_offset(message.frequency);
}
void NarrowbandAMAudio::capture_config(const CaptureConfigMessage& message) {
if (message.config) {
audio_output.set_stream(std::make_unique<StreamInput>(message.config));
} else {
audio_output.set_stream(nullptr);
}
}
int main() {
audio::dma::init_audio_out();
EventDispatcher event_dispatcher{std::make_unique<NarrowbandAMAudio>()};
event_dispatcher.run();
return 0;
}