Adding_WFM_AM_mode_to_Audio_App (#2644)

* Adding_WFM_AM_mode_to_Audio_App
* more precise values for cos and sin theta, fix sen_theta to sin_theta
* fix sen_theta to sin_theta
This commit is contained in:
Brumi-2021
2025-05-05 17:48:01 +02:00
committed by GitHub
parent bd781ce37b
commit 4342f5c0ee
18 changed files with 448 additions and 49 deletions

View File

@@ -52,6 +52,19 @@ void AudioOutput::write_unprocessed(const buffer_s16_t& audio) {
});
}
void AudioOutput::apt_write(const buffer_s16_t& audio) {
std::array<float, 32> audio_f;
for (size_t i = 0; i < audio.count; i++) {
cur = audio.p[i];
cur2 = cur * cur;
mag_am = sqrtf(prev2 + cur2 - (2 * prev * cur * cos_theta)) / sin_theta;
audio_f[i] = mag_am * ki; // normalize.
prev = cur;
prev2 = cur2;
}
write(buffer_f32_t{audio_f.data(), audio.count, audio.sampling_rate});
}
void AudioOutput::write(const buffer_s16_t& audio) {
std::array<float, 32> audio_f;
for (size_t i = 0; i < audio.count; i++) {
@@ -72,8 +85,8 @@ void AudioOutput::on_block(const buffer_f32_t& audio) {
if (do_processing) {
const auto audio_present_now = squelch.execute(audio);
hpf.execute_in_place(audio); // IIRBiquadFilter name is "hpf", but we will call with "hpf-coef" for all except AMFM (WFAX) with "lpf-coef"
deemph.execute_in_place(audio);
hpf.execute_in_place(audio); // IIRBiquadFilter name is "hpf", but we will call with "hpf-coef" for all except AMFM (WFAX) with "lpf-coef" and notch for WFMAM (NOAA)
deemph.execute_in_place(audio); // IIRBiquadFilter name is "deemph", but we will call LPF de-emphasis or other LPF for WFAM (NOAA).
audio_present_history = (audio_present_history << 1) | (audio_present_now ? 1 : 0);
audio_present = (audio_present_history != 0);

View File

@@ -46,6 +46,7 @@ class AudioOutput {
const float squelch_threshold = 0.0f);
void write_unprocessed(const buffer_s16_t& audio);
void apt_write(const buffer_s16_t& audio);
void write(const buffer_s16_t& audio);
void write(const buffer_f32_t& audio);
@@ -58,6 +59,10 @@ class AudioOutput {
private:
static constexpr float k = 32768.0f;
static constexpr float ki = 1.0f / k;
static constexpr float cos_theta = 0.30901699437494742410f;
static constexpr float sin_theta = 0.95105651629515357212f;
float cur = 0.0f, cur2 = 0.0f, prev = 0.0f, prev2 = 0.0f, mag_am = 0.0f;
BlockDecimator<int16_t, 32> block_buffer_s16{1};
BlockDecimator<float, 32> block_buffer{1};

View File

@@ -161,7 +161,7 @@ void WeFaxRx::configure(const WeFaxRxConfigureMessage& message) {
channel_filter_high_f = taps_2k6_usb_wefax_channel.high_frequency_normalized * channel_filter_input_fs;
channel_filter_transition = taps_2k6_usb_wefax_channel.transition_normalized * channel_filter_input_fs;
channel_spectrum.set_decimation_factor(1.0f);
audio_output.configure(audio_12k_lpf_1500hz_config); // hpf in all AM demod modes (AM-6K/9K, USB/LSB,DSB), except Wefax (lpf there).
audio_output.configure(apt_audio_12k_lpf_1500hz_config); // hpf in all AM demod modes (AM-6K/9K, USB/LSB,DSB), except Wefax (lpf there).
lpm = message.lpm;
ioc_mode = message.ioc;

View File

@@ -47,23 +47,37 @@ void WidebandFMAudio::execute(const buffer_c8_t& buffer) {
channel_spectrum.feed(channel, channel_filter_low_f, channel_filter_high_f, channel_filter_transition);
}
/* 384kHz complex<int16_t>[256]
/* 384kHz complex<int16_t>[256] for wfm
* -> FM demodulation
* -> 384kHz int16_t[256] */
/* TODO: To improve adjacent channel rejection, implement complex channel filter:
* pass < +/- 100kHz, stop > +/- 200kHz
*/
auto audio_oversampled = demod.execute(channel, work_audio_buffer);
/* 96kHz complex<int16_t>[64] for wfmam NOAA
* -> FM demodulation
* -> 96kHz int16_t[64] */
/* 384kHz int16_t[256]
auto audio_oversampled = demod.execute(channel, work_audio_buffer); // fs 384khz wfm , 96khz wfmam for NOAA
/* 384kHz int16_t[256] for wfm
* -> 4th order CIC decimation by 2, gain of 1
* -> 192kHz int16_t[128] */
/* 96kHz int16_t[64] for wfam
* -> 4th order CIC decimation by 2, gain of 1
* -> 48kHz int16_t[32] */
auto audio_4fs = audio_dec_1.execute(audio_oversampled, work_audio_buffer);
/* 192kHz int16_t[128]
/* 192kHz int16_t[128] for wfm
* -> 4th order CIC decimation by 2, gain of 1
* -> 96kHz int16_t[64] */
/* 48kHz int16_t[32] for wfman
* -> 4th order CIC decimation by 2, gain of 1
* -> 24kHz int16_t[16] */
auto audio_2fs = audio_dec_2.execute(audio_4fs, work_audio_buffer);
// Input: 96kHz int16_t[64]
@@ -118,13 +132,24 @@ void WidebandFMAudio::execute(const buffer_c8_t& buffer) {
break;
}
/* 96kHz int16_t[64]
/* 96kHz int16_t[64] for wfm
* -> FIR filter, <15kHz (0.156fs) pass, >19kHz (0.198fs) stop, gain of 1
* -> 48kHz int16_t[32] */
/* 24kHz int16_t[16] for wfmam
* -> FIR filter, <4.5kHz (0.1875fs) pass, >5.2kHz (0.2166fs) stop, gain of 1
* -> 12kHz int16_t[8] */
auto audio = audio_filter.execute(audio_2fs, work_audio_buffer);
/* -> 48kHz int16_t[32] */
audio_output.write(audio);
/* -> 48kHz int16_t[32] for wfm , */
/* -> 12kHz int16_t[8] for wfmam , */
if (decim_1.decimation_factor() == 2) {
audio_output.write(audio); // we are in original wfm , decim_1.decimation_factor == 2
} else {
audio_output.apt_write(audio); // we are in added wfmam (noaa), decim_1.decimation_factor == 8
}
}
void WidebandFMAudio::post_message(const buffer_c16_t& data) {
@@ -142,7 +167,11 @@ void WidebandFMAudio::on_message(const Message* const message) {
break;
case Message::ID::WFMConfigure:
configure(*reinterpret_cast<const WFMConfigureMessage*>(message));
configure_wfm(*reinterpret_cast<const WFMConfigureMessage*>(message));
break;
case Message::ID::WFMAMConfigure:
configure_wfmam(*reinterpret_cast<const WFMAMConfigureMessage*>(message));
break;
case Message::ID::CaptureConfig:
@@ -154,20 +183,56 @@ void WidebandFMAudio::on_message(const Message* const message) {
}
}
void WidebandFMAudio::configure(const WFMConfigureMessage& message) {
void WidebandFMAudio::configure_wfm(const WFMConfigureMessage& 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 decim_1_input_fs = decim_0_output_fs;
constexpr size_t decim_1_output_fs = decim_1_input_fs / decim_1.decimation_factor;
constexpr size_t demod_input_fs = decim_1_output_fs;
decim_0.configure(message.decim_0_filter.taps);
// decim_1.configure(message.decim_1_filter.taps); // Original .
// TODO dynamic decim1 , with decimation 2 / 8 and 16 x taps , / 32 taps .
// Temptatively , I splitted, in two WidebandFMAudio::configure_wfm / WidebandFMAudio::configure_wfmam and dynamically /2, /8 (here /2)
// decim_1.set<dsp::decimate::FIRC16xR16x16Decim2>().configure(message.decim_1_filter.taps); // for wfm
// decim_1.set<dsp::decimate::FIRC16xR16x32Decim8>().configure(taps_84k_wfmam_decim_1.taps); // for wfmam
decim_1.set<dsp::decimate::FIRC16xR16x16Decim2>().configure(message.decim_1_filter.taps); // for wfm
size_t decim_1_output_fs = decim_1_input_fs / decim_1.decimation_factor(); // wfm , decim_1.decimation_factor() = /2 , if applied after the line : decim_1.set<dsp::decimate::FIRC16xR16x16Decim2>().configure(message.decim_1_filter.taps);
size_t demod_input_fs = decim_1_output_fs;
spectrum_interval_samples = decim_1_output_fs / spectrum_rate_hz;
spectrum_samples = 0;
channel_filter_low_f = message.decim_1_filter.low_frequency_normalized * decim_1_input_fs;
channel_filter_high_f = message.decim_1_filter.high_frequency_normalized * decim_1_input_fs;
channel_filter_transition = message.decim_1_filter.transition_normalized * decim_1_input_fs;
demod.configure(demod_input_fs, message.deviation);
audio_filter.configure(message.audio_filter.taps);
audio_output.configure(message.audio_hpf_config, message.audio_deemph_config);
channel_spectrum.set_decimation_factor(1);
configured = true;
}
void WidebandFMAudio::configure_wfmam(const WFMAMConfigureMessage& 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 decim_1_input_fs = decim_0_output_fs;
decim_0.configure(message.decim_0_filter.taps);
decim_1.configure(message.decim_1_filter.taps);
// decim_1.configure(message.decim_1_filter.taps); // Original .
// TODO dynamic decim1 , with decimation 2 / 8 and 16 x taps , / 32 taps .
// Temptatively , I splitted, in two WidebandFMAudio::configure_wfm / WidebandFMAudio::configure_wfmam and dynamically /2, /8 . (here /8)
// decim_1.set<dsp::decimate::FIRC16xR16x16Decim2>().configure(message.decim_1_filter.taps); // for wfm
// decim_1.set<dsp::decimate::FIRC16xR16x32Decim8>().configure(message.decim_1_filter.taps); // for wfmam
decim_1.set<dsp::decimate::FIRC16xR16x32Decim8>().configure(message.decim_1_filter.taps); // for wfmam
size_t decim_1_output_fs = decim_1_input_fs / decim_1.decimation_factor(); // wfmam, decim_1.decimation_factor() = /8 ,if applied after the line, decim_1.set<dsp::decimate::FIRC16xR16x16Decim2>().configure(message.decim_1_filter.taps);
size_t demod_input_fs = decim_1_output_fs;
spectrum_interval_samples = decim_1_output_fs / spectrum_rate_hz;
spectrum_samples = 0;
channel_filter_low_f = message.decim_1_filter.low_frequency_normalized * decim_1_input_fs;
channel_filter_high_f = message.decim_1_filter.high_frequency_normalized * decim_1_input_fs;
channel_filter_transition = message.decim_1_filter.transition_normalized * decim_1_input_fs;

View File

@@ -35,6 +35,46 @@
#include "audio_output.hpp"
#include "spectrum_collector.hpp"
#include <array>
#include <memory>
#include <tuple>
#include <variant>
template <typename... Args>
class MultiDecimator {
public:
/* Dispatches to the underlying type's execute. */
template <typename Source, typename Destination>
Destination execute(
const Source& src,
const Destination& dst) {
return std::visit(
[&src, &dst](auto&& arg) -> Destination {
return arg.execute(src, dst);
},
decimator_);
}
size_t decimation_factor() const {
return std::visit(
[](auto&& arg) -> size_t {
return arg.decimation_factor;
},
decimator_);
}
/* Sets this decimator to a new instance of the specified decimator type.
* NB: The instance is returned by-ref so 'configure' can easily be called. */
template <typename Decimator>
Decimator& set() {
decimator_ = Decimator{};
return std::get<Decimator>(decimator_);
}
private:
std::variant<Args...> decimator_{};
};
class WidebandFMAudio : public BasebandProcessor {
public:
void execute(const buffer_c8_t& buffer) override;
@@ -59,7 +99,16 @@ class WidebandFMAudio : public BasebandProcessor {
complex_audio.size()};
dsp::decimate::FIRC8xR16x24FS4Decim4 decim_0{};
dsp::decimate::FIRC16xR16x16Decim2 decim_1{};
// dsp::decimate::FIRC16xR16x16Decim2 decim_1{}; //original condition , before adding wfmam
// decim_1 will handle different types of FIR filters depending on selection.
MultiDecimator<
dsp::decimate::FIRC16xR16x16Decim2,
dsp::decimate::FIRC16xR16x32Decim8>
decim_1{};
// dsp::decimate::FIRC16xR16x32Decim8 decim_1{}; // For FMAM
int32_t channel_filter_low_f = 0;
int32_t channel_filter_high_f = 0;
int32_t channel_filter_transition = 0;
@@ -94,7 +143,8 @@ class WidebandFMAudio : public BasebandProcessor {
BasebandThread baseband_thread{baseband_fs, this, baseband::Direction::Receive};
RSSIThread rssi_thread{};
void configure(const WFMConfigureMessage& message);
void configure_wfm(const WFMConfigureMessage& message);
void configure_wfmam(const WFMAMConfigureMessage& message);
void capture_config(const CaptureConfigMessage& message);
void post_message(const buffer_c16_t& data);
};