Extract spectrum collector from BasebandProcessor.

Code size improvement, and less confused classes. :-)
This commit is contained in:
Jared Boone 2015-12-11 12:26:45 -08:00
parent 75b1cc25ff
commit 088f60f2bc
15 changed files with 193 additions and 86 deletions

View File

@ -137,6 +137,7 @@ CPPSRC = main.cpp \
matched_filter.cpp \ matched_filter.cpp \
proc_am_audio.cpp \ proc_am_audio.cpp \
proc_nfm_audio.cpp \ proc_nfm_audio.cpp \
spectrum_collector.cpp \
proc_wfm_audio.cpp \ proc_wfm_audio.cpp \
proc_ais.cpp \ proc_ais.cpp \
proc_wideband_spectrum.cpp \ proc_wideband_spectrum.cpp \

View File

@ -34,31 +34,6 @@
#include <cstddef> #include <cstddef>
#include <algorithm> #include <algorithm>
void BasebandProcessor::update_spectrum() {
// Called from idle thread (after EVT_MASK_SPECTRUM is flagged)
if( channel_spectrum_request_update ) {
/* Decimated buffer is full. Compute spectrum. */
channel_spectrum_request_update = false;
fft_c_preswapped(channel_spectrum);
ChannelSpectrumMessage spectrum_message;
for(size_t i=0; i<spectrum_message.spectrum.db.size(); i++) {
const auto mag2 = magnitude_squared(channel_spectrum[i]);
const float db = complex16_mag_squared_to_dbv_norm(mag2);
constexpr float mag_scale = 5.0f;
const unsigned int v = (db * mag_scale) + 255.0f;
spectrum_message.spectrum.db[i] = std::max(0U, std::min(255U, v));
}
/* TODO: Rename .db -> .magnitude, or something more (less!) accurate. */
spectrum_message.spectrum.db_count = spectrum_message.spectrum.db.size();
spectrum_message.spectrum.sampling_rate = channel_spectrum_sampling_rate;
spectrum_message.spectrum.channel_filter_pass_frequency = channel_filter_pass_frequency;
spectrum_message.spectrum.channel_filter_stop_frequency = channel_filter_stop_frequency;
shared_memory.application_queue.push(spectrum_message);
}
}
void BasebandProcessor::feed_channel_stats(const buffer_c16_t& channel) { void BasebandProcessor::feed_channel_stats(const buffer_c16_t& channel) {
channel_stats.feed( channel_stats.feed(
channel, channel,
@ -69,21 +44,6 @@ void BasebandProcessor::feed_channel_stats(const buffer_c16_t& channel) {
); );
} }
void BasebandProcessor::feed_channel_spectrum(
const buffer_c16_t& channel,
const uint32_t filter_pass_frequency,
const uint32_t filter_stop_frequency
) {
channel_filter_pass_frequency = filter_pass_frequency;
channel_filter_stop_frequency = filter_stop_frequency;
channel_spectrum_decimator.feed(
channel,
[this](const buffer_c16_t& data) {
this->post_channel_spectrum_message(data);
}
);
}
void BasebandProcessor::fill_audio_buffer(const buffer_s16_t& audio) { void BasebandProcessor::fill_audio_buffer(const buffer_s16_t& audio) {
auto audio_buffer = audio::dma::tx_empty_buffer();; auto audio_buffer = audio::dma::tx_empty_buffer();;
for(size_t i=0; i<audio_buffer.count; i++) { for(size_t i=0; i<audio_buffer.count; i++) {
@ -94,15 +54,6 @@ void BasebandProcessor::fill_audio_buffer(const buffer_s16_t& audio) {
feed_audio_stats(audio); feed_audio_stats(audio);
} }
void BasebandProcessor::post_channel_spectrum_message(const buffer_c16_t& data) {
if( !channel_spectrum_request_update ) {
fft_swap(data, channel_spectrum);
channel_spectrum_sampling_rate = data.sampling_rate;
channel_spectrum_request_update = true;
events_flag(EVT_MASK_SPECTRUM);
}
}
void BasebandProcessor::feed_audio_stats(const buffer_s16_t& audio) { void BasebandProcessor::feed_audio_stats(const buffer_s16_t& audio) {
audio_stats.feed( audio_stats.feed(
audio, audio,

View File

@ -25,7 +25,6 @@
#include "dsp_types.hpp" #include "dsp_types.hpp"
#include "complex.hpp" #include "complex.hpp"
#include "block_decimator.hpp"
#include "channel_stats_collector.hpp" #include "channel_stats_collector.hpp"
#include "audio_stats_collector.hpp" #include "audio_stats_collector.hpp"
@ -39,32 +38,17 @@ public:
virtual void execute(const buffer_c8_t& buffer) = 0; virtual void execute(const buffer_c8_t& buffer) = 0;
void update_spectrum(); virtual void on_update_spectrum() { };
protected: protected:
void feed_channel_stats(const buffer_c16_t& channel); void feed_channel_stats(const buffer_c16_t& channel);
void feed_channel_spectrum(
const buffer_c16_t& channel,
const uint32_t filter_pass_frequency,
const uint32_t filter_stop_frequency
);
void fill_audio_buffer(const buffer_s16_t& audio); void fill_audio_buffer(const buffer_s16_t& audio);
volatile bool channel_spectrum_request_update { false };
std::array<std::complex<float>, 256> channel_spectrum;
uint32_t channel_spectrum_sampling_rate { 0 };
uint32_t channel_filter_pass_frequency { 0 };
uint32_t channel_filter_stop_frequency { 0 };
private: private:
BlockDecimator<256> channel_spectrum_decimator { 4 };
ChannelStatsCollector channel_stats; ChannelStatsCollector channel_stats;
AudioStatsCollector audio_stats; AudioStatsCollector audio_stats;
void post_channel_spectrum_message(const buffer_c16_t& data);
void feed_audio_stats(const buffer_s16_t& audio); void feed_audio_stats(const buffer_s16_t& audio);
}; };

View File

@ -68,6 +68,12 @@ void BasebandThread::set_configuration(const BasebandConfiguration& new_configur
baseband_configuration = new_configuration; baseband_configuration = new_configuration;
} }
void BasebandThread::on_update_spectrum() {
if( baseband_processor ) {
baseband_processor->on_update_spectrum();
}
}
void BasebandThread::run() { void BasebandThread::run() {
baseband::dma::init(); baseband::dma::init();

View File

@ -39,6 +39,8 @@ public:
void set_configuration(const BasebandConfiguration& new_configuration); void set_configuration(const BasebandConfiguration& new_configuration);
void on_update_spectrum();
// This getter should die, it's just here to leak information to code that // This getter should die, it's just here to leak information to code that
// isn't in the right place to begin with. // isn't in the right place to begin with.
baseband::Direction direction() const { baseband::Direction direction() const {

View File

@ -187,9 +187,8 @@ private:
} }
void handle_spectrum() { void handle_spectrum() {
if( baseband_thread.baseband_processor ) { // TODO: Send this via another message?!
baseband_thread.baseband_processor->update_spectrum(); baseband_thread.on_update_spectrum();
}
} }
}; };

View File

@ -38,7 +38,7 @@ void NarrowbandAMAudio::execute(const buffer_c8_t& buffer) {
// TODO: Feed channel_stats post-decimation data? // TODO: Feed channel_stats post-decimation data?
feed_channel_stats(channel); feed_channel_stats(channel);
feed_channel_spectrum( channel_spectrum.feed(
channel, channel,
decimator_out.sampling_rate * channel_filter_taps.pass_frequency_normalized, decimator_out.sampling_rate * channel_filter_taps.pass_frequency_normalized,
decimator_out.sampling_rate * channel_filter_taps.stop_frequency_normalized decimator_out.sampling_rate * channel_filter_taps.stop_frequency_normalized

View File

@ -31,6 +31,8 @@
#include "dsp_iir.hpp" #include "dsp_iir.hpp"
#include "dsp_iir_config.hpp" #include "dsp_iir_config.hpp"
#include "spectrum_collector.hpp"
class NarrowbandAMAudio : public BasebandProcessor { class NarrowbandAMAudio : public BasebandProcessor {
public: public:
NarrowbandAMAudio() { NarrowbandAMAudio() {
@ -40,12 +42,16 @@ public:
void execute(const buffer_c8_t& buffer) override; void execute(const buffer_c8_t& buffer) override;
void on_update_spectrum() override { channel_spectrum.update(); }
private: private:
ChannelDecimator decimator; ChannelDecimator decimator;
const fir_taps_real<64>& channel_filter_taps = taps_64_lp_031_070_tfilter; const fir_taps_real<64>& channel_filter_taps = taps_64_lp_031_070_tfilter;
dsp::decimate::FIRAndDecimateComplex channel_filter; dsp::decimate::FIRAndDecimateComplex channel_filter;
dsp::demodulate::AM demod; dsp::demodulate::AM demod;
IIRBiquadFilter audio_hpf { audio_hpf_config }; IIRBiquadFilter audio_hpf { audio_hpf_config };
SpectrumCollector channel_spectrum;
}; };
#endif/*__PROC_AM_AUDIO_H__*/ #endif/*__PROC_AM_AUDIO_H__*/

View File

@ -41,7 +41,7 @@ void NarrowbandFMAudio::execute(const buffer_c8_t& buffer) {
// TODO: Feed channel_stats post-decimation data? // TODO: Feed channel_stats post-decimation data?
feed_channel_stats(channel); feed_channel_stats(channel);
feed_channel_spectrum( channel_spectrum.feed(
channel, channel,
decimator_out.sampling_rate * channel_filter_taps.pass_frequency_normalized, decimator_out.sampling_rate * channel_filter_taps.pass_frequency_normalized,
decimator_out.sampling_rate * channel_filter_taps.stop_frequency_normalized decimator_out.sampling_rate * channel_filter_taps.stop_frequency_normalized

View File

@ -32,6 +32,8 @@
#include "dsp_iir_config.hpp" #include "dsp_iir_config.hpp"
#include "dsp_squelch.hpp" #include "dsp_squelch.hpp"
#include "spectrum_collector.hpp"
class NarrowbandFMAudio : public BasebandProcessor { class NarrowbandFMAudio : public BasebandProcessor {
public: public:
NarrowbandFMAudio() { NarrowbandFMAudio() {
@ -41,6 +43,8 @@ public:
void execute(const buffer_c8_t& buffer) override; void execute(const buffer_c8_t& buffer) override;
void on_update_spectrum() override { channel_spectrum.update(); }
private: private:
ChannelDecimator decimator; ChannelDecimator decimator;
const fir_taps_real<64>& channel_filter_taps = taps_64_lp_042_078_tfilter; const fir_taps_real<64>& channel_filter_taps = taps_64_lp_042_078_tfilter;
@ -49,6 +53,8 @@ private:
IIRBiquadFilter audio_hpf { audio_hpf_config }; IIRBiquadFilter audio_hpf { audio_hpf_config };
FMSquelch squelch; FMSquelch squelch;
SpectrumCollector channel_spectrum;
}; };
#endif/*__PROC_NFM_AUDIO_H__*/ #endif/*__PROC_NFM_AUDIO_H__*/

View File

@ -43,24 +43,24 @@ void WidebandSpectrum::execute(const buffer_c8_t& buffer) {
std::fill(spectrum.begin(), spectrum.end(), 0); std::fill(spectrum.begin(), spectrum.end(), 0);
} }
if( (phase & 7) == 0 ) { for(size_t i=0; i<spectrum.size(); i++) {
// TODO: Removed window-presum windowing, due to lack of available code RAM. // TODO: Removed window-presum windowing, due to lack of available code RAM.
// TODO: Apply window to improve spectrum bin sidelobes. // TODO: Apply window to improve spectrum bin sidelobes.
for(size_t i=0; i<channel_spectrum.size(); i++) { spectrum[i] += buffer.p[i + 0];
spectrum[i] += buffer.p[i]; spectrum[i] += buffer.p[i + 1024];
}
} }
if( phase == 23 ) { if( phase == 127 ) {
if( channel_spectrum_request_update == false ) { const buffer_c16_t buffer_c16 {
fft_swap(spectrum, channel_spectrum); spectrum.data(),
channel_spectrum_sampling_rate = buffer.sampling_rate; spectrum.size(),
channel_filter_pass_frequency = 0; buffer.sampling_rate
channel_filter_stop_frequency = 0; };
channel_spectrum_request_update = true; channel_spectrum.feed(
events_flag(EVT_MASK_SPECTRUM); buffer_c16,
0, 0
);
phase = 0; phase = 0;
}
} else { } else {
phase++; phase++;
} }

View File

@ -23,6 +23,7 @@
#define __PROC_WIDEBAND_SPECTRUM_H__ #define __PROC_WIDEBAND_SPECTRUM_H__
#include "baseband_processor.hpp" #include "baseband_processor.hpp"
#include "spectrum_collector.hpp"
#include <cstddef> #include <cstddef>
#include <array> #include <array>
@ -32,10 +33,14 @@ class WidebandSpectrum : public BasebandProcessor {
public: public:
void execute(const buffer_c8_t& buffer) override; void execute(const buffer_c8_t& buffer) override;
void on_update_spectrum() override { channel_spectrum.update(); }
private: private:
size_t sample_count = 0; size_t sample_count = 0;
std::array<std::complex<float>, 256> spectrum; SpectrumCollector channel_spectrum { 1 };
std::array<complex16_t, 256> spectrum;
}; };
#endif/*__PROC_WIDEBAND_SPECTRUM_H__*/ #endif/*__PROC_WIDEBAND_SPECTRUM_H__*/

View File

@ -0,0 +1,79 @@
/*
* Copyright (C) 2015 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 "spectrum_collector.hpp"
#include "dsp_fft.hpp"
#include "utility.hpp"
#include "event_m4.hpp"
#include "portapack_shared_memory.hpp"
#include <algorithm>
void SpectrumCollector::update() {
// Called from idle thread (after EVT_MASK_SPECTRUM is flagged)
if( channel_spectrum_request_update ) {
/* Decimated buffer is full. Compute spectrum. */
channel_spectrum_request_update = false;
fft_c_preswapped(channel_spectrum);
ChannelSpectrumMessage spectrum_message;
for(size_t i=0; i<spectrum_message.spectrum.db.size(); i++) {
const auto mag2 = magnitude_squared(channel_spectrum[i]);
const float db = complex16_mag_squared_to_dbv_norm(mag2);
constexpr float mag_scale = 5.0f;
const unsigned int v = (db * mag_scale) + 255.0f;
spectrum_message.spectrum.db[i] = std::max(0U, std::min(255U, v));
}
/* TODO: Rename .db -> .magnitude, or something more (less!) accurate. */
spectrum_message.spectrum.db_count = spectrum_message.spectrum.db.size();
spectrum_message.spectrum.sampling_rate = channel_spectrum_sampling_rate;
spectrum_message.spectrum.channel_filter_pass_frequency = channel_filter_pass_frequency;
spectrum_message.spectrum.channel_filter_stop_frequency = channel_filter_stop_frequency;
shared_memory.application_queue.push(spectrum_message);
}
}
void SpectrumCollector::feed(
const buffer_c16_t& channel,
const uint32_t filter_pass_frequency,
const uint32_t filter_stop_frequency
) {
channel_filter_pass_frequency = filter_pass_frequency;
channel_filter_stop_frequency = filter_stop_frequency;
channel_spectrum_decimator.feed(
channel,
[this](const buffer_c16_t& data) {
this->post_message(data);
}
);
}
void SpectrumCollector::post_message(const buffer_c16_t& data) {
if( !channel_spectrum_request_update ) {
fft_swap(data, channel_spectrum);
channel_spectrum_sampling_rate = data.sampling_rate;
channel_spectrum_request_update = true;
events_flag(EVT_MASK_SPECTRUM);
}
}

View File

@ -0,0 +1,61 @@
/*
* Copyright (C) 2015 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.
*/
#ifndef __SPECTRUM_COLLECTOR_H__
#define __SPECTRUM_COLLECTOR_H__
#include "dsp_types.hpp"
#include "complex.hpp"
#include "block_decimator.hpp"
#include <cstdint>
#include <array>
class SpectrumCollector {
public:
constexpr SpectrumCollector(
const size_t decimation_factor = 4
) : channel_spectrum_decimator { decimation_factor }
{
}
void update();
void feed(
const buffer_c16_t& channel,
const uint32_t filter_pass_frequency,
const uint32_t filter_stop_frequency
);
private:
BlockDecimator<256> channel_spectrum_decimator;
volatile bool channel_spectrum_request_update { false };
std::array<std::complex<float>, 256> channel_spectrum;
uint32_t channel_spectrum_sampling_rate { 0 };
uint32_t channel_filter_pass_frequency { 0 };
uint32_t channel_filter_stop_frequency { 0 };
void post_message(const buffer_c16_t& data);
};
#endif/*__SPECTRUM_COLLECTOR_H__*/

View File

@ -101,6 +101,13 @@ public:
void real(int16_t v) { _v[0] = v; } void real(int16_t v) { _v[0] = v; }
void imag(int16_t v) { _v[1] = v; } void imag(int16_t v) { _v[1] = v; }
template<class X>
complex<int16_t>& operator+=(const complex<X>& other) {
_v[0] += other.real();
_v[1] += other.imag();
return *this;
}
constexpr uint32_t __rep() const { constexpr uint32_t __rep() const {
return _rep; return _rep;
} }