From 5ec8fba83136117cbf43abdbc736bb7b636f1809 Mon Sep 17 00:00:00 2001 From: Carles Fernandez Date: Wed, 27 Oct 2021 21:01:28 +0200 Subject: [PATCH] Input filter: Prefer initialization to assignment in constructors --- .../adapters/beamformer_filter.cc | 10 +++-- .../input_filter/adapters/fir_filter.cc | 30 +++++++------ .../adapters/freq_xlating_fir_filter.cc | 27 +++++++----- .../input_filter/adapters/notch_filter.cc | 24 +++++++---- .../adapters/notch_filter_lite.cc | 32 ++++++++------ .../adapters/pulse_blanking_filter.cc | 34 +++++++++------ .../input_filter/gnuradio_blocks/notch_cc.cc | 29 +++++++------ .../gnuradio_blocks/notch_lite_cc.cc | 43 ++++++++++--------- .../gnuradio_blocks/pulse_blanking_cc.cc | 23 +++++----- 9 files changed, 146 insertions(+), 106 deletions(-) diff --git a/src/algorithms/input_filter/adapters/beamformer_filter.cc b/src/algorithms/input_filter/adapters/beamformer_filter.cc index 3d8707e20..2d18b2449 100644 --- a/src/algorithms/input_filter/adapters/beamformer_filter.cc +++ b/src/algorithms/input_filter/adapters/beamformer_filter.cc @@ -23,15 +23,18 @@ BeamformerFilter::BeamformerFilter( const ConfigurationInterface* configuration, const std::string& role, - unsigned int in_stream, unsigned int out_stream) : role_(role), in_stream_(in_stream), out_stream_(out_stream) + unsigned int in_stream, unsigned int out_stream) + : role_(role), + samples_(0ULL), + in_stream_(in_stream), + out_stream_(out_stream) { const std::string default_item_type("gr_complex"); const std::string default_dump_file("./data/input_filter.dat"); item_type_ = configuration->property(role + ".item_type", default_item_type); dump_ = configuration->property(role + ".dump", false); - DLOG(INFO) << "dump_ is " << dump_; dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file); - + DLOG(INFO) << "role " << role_; if (item_type_ == "gr_complex") { item_size_ = sizeof(gr_complex); @@ -51,7 +54,6 @@ BeamformerFilter::BeamformerFilter( file_sink_ = gr::blocks::file_sink::make(item_size_, dump_filename_.c_str()); DLOG(INFO) << "file_sink(" << file_sink_->unique_id() << ")"; } - samples_ = 0ULL; if (in_stream_ > 8) { LOG(ERROR) << "This implementation only supports eight input streams"; diff --git a/src/algorithms/input_filter/adapters/fir_filter.cc b/src/algorithms/input_filter/adapters/fir_filter.cc index 7831bccb9..ad76b5f6a 100644 --- a/src/algorithms/input_filter/adapters/fir_filter.cc +++ b/src/algorithms/input_filter/adapters/fir_filter.cc @@ -23,12 +23,18 @@ #include -FirFilter::FirFilter(const ConfigurationInterface* configuration, std::string role, - unsigned int in_streams, unsigned int out_streams) : role_(std::move(role)), in_streams_(in_streams), out_streams_(out_streams) +FirFilter::FirFilter(const ConfigurationInterface* configuration, + std::string role, + unsigned int in_streams, + unsigned int out_streams) + : config_(configuration), + role_(std::move(role)), + item_size_(0), + in_streams_(in_streams), + out_streams_(out_streams) { - config_ = configuration; (*this).init(); - item_size_ = 0; + DLOG(INFO) << "role " << role_; if ((taps_item_type_ == "float") && (input_item_type_ == "gr_complex") && (output_item_type_ == "gr_complex")) { item_size_ = sizeof(gr_complex); @@ -133,23 +139,24 @@ void FirFilter::init() const std::string default_output_item_type("gr_complex"); const std::string default_taps_item_type("float"); const std::string default_dump_filename("../data/input_filter.dat"); - const int default_number_of_taps = 6; - const unsigned int default_number_of_bands = 2; + const std::string default_filter_type("bandpass"); const std::vector default_bands = {0.0, 0.4, 0.6, 1.0}; const std::vector default_ampl = {1.0, 1.0, 0.0, 0.0}; const std::vector default_error_w = {1.0, 1.0}; - const std::string default_filter_type("bandpass"); const int default_grid_density = 16; + const int default_number_of_taps = 6; + const unsigned int default_number_of_bands = 2; - DLOG(INFO) << "role " << role_; + const int number_of_taps = config_->property(role_ + ".number_of_taps", default_number_of_taps); + const unsigned int number_of_bands = config_->property(role_ + ".number_of_bands", default_number_of_bands); + const std::string filter_type = config_->property(role_ + ".filter_type", default_filter_type); + const int grid_density = config_->property(role_ + ".grid_density", default_grid_density); input_item_type_ = config_->property(role_ + ".input_item_type", default_input_item_type); output_item_type_ = config_->property(role_ + ".output_item_type", default_output_item_type); taps_item_type_ = config_->property(role_ + ".taps_item_type", default_taps_item_type); dump_ = config_->property(role_ + ".dump", false); dump_filename_ = config_->property(role_ + ".dump_filename", default_dump_filename); - const int number_of_taps = config_->property(role_ + ".number_of_taps", default_number_of_taps); - const unsigned int number_of_bands = config_->property(role_ + ".number_of_bands", default_number_of_bands); std::vector bands; std::vector ampl; @@ -179,9 +186,6 @@ void FirFilter::init() error_w.push_back(option_value); } - const std::string filter_type = config_->property(role_ + ".filter_type", default_filter_type); - const int grid_density = config_->property(role_ + ".grid_density", default_grid_density); - // pm_remez implements the Parks-McClellan FIR filter design. // It calculates the optimal (in the Chebyshev/minimax sense) FIR filter // impulse response given a set of band edges, the desired response on diff --git a/src/algorithms/input_filter/adapters/freq_xlating_fir_filter.cc b/src/algorithms/input_filter/adapters/freq_xlating_fir_filter.cc index 6e4c03acc..1b7fb828c 100644 --- a/src/algorithms/input_filter/adapters/freq_xlating_fir_filter.cc +++ b/src/algorithms/input_filter/adapters/freq_xlating_fir_filter.cc @@ -25,8 +25,13 @@ #include -FreqXlatingFirFilter::FreqXlatingFirFilter(const ConfigurationInterface* configuration, std::string role, - unsigned int in_streams, unsigned int out_streams) : role_(std::move(role)), in_streams_(in_streams), out_streams_(out_streams) +FreqXlatingFirFilter::FreqXlatingFirFilter(const ConfigurationInterface* configuration, + std::string role, + unsigned int in_streams, + unsigned int out_streams) + : role_(std::move(role)), + in_streams_(in_streams), + out_streams_(out_streams) { const std::string default_input_item_type("gr_complex"); const std::string default_output_item_type("gr_complex"); @@ -43,19 +48,18 @@ FreqXlatingFirFilter::FreqXlatingFirFilter(const ConfigurationInterface* configu const int default_grid_density = 16; const int default_decimation_factor = 1; - DLOG(INFO) << "role " << role_; - - input_item_type_ = configuration->property(role_ + ".input_item_type", default_input_item_type); - output_item_type_ = configuration->property(role_ + ".output_item_type", default_output_item_type); - taps_item_type_ = configuration->property(role_ + ".taps_item_type", default_taps_item_type); - dump_ = configuration->property(role_ + ".dump", false); - dump_filename_ = configuration->property(role_ + ".dump_filename", default_dump_filename); - intermediate_freq_ = configuration->property(role_ + ".IF", default_intermediate_freq); - sampling_freq_ = configuration->property(role_ + ".sampling_frequency", default_sampling_freq); const int number_of_taps = configuration->property(role_ + ".number_of_taps", default_number_of_taps); const unsigned int number_of_bands = configuration->property(role_ + ".number_of_bands", default_number_of_bands); const std::string filter_type = configuration->property(role_ + ".filter_type", default_filter_type); + + dump_filename_ = configuration->property(role_ + ".dump_filename", default_dump_filename); + input_item_type_ = configuration->property(role_ + ".input_item_type", default_input_item_type); + output_item_type_ = configuration->property(role_ + ".output_item_type", default_output_item_type); + taps_item_type_ = configuration->property(role_ + ".taps_item_type", default_taps_item_type); + intermediate_freq_ = configuration->property(role_ + ".IF", default_intermediate_freq); + sampling_freq_ = configuration->property(role_ + ".sampling_frequency", default_sampling_freq); decimation_factor_ = configuration->property(role_ + ".decimation_factor", default_decimation_factor); + dump_ = configuration->property(role_ + ".dump", false); if (filter_type != "lowpass") { @@ -102,6 +106,7 @@ FreqXlatingFirFilter::FreqXlatingFirFilter(const ConfigurationInterface* configu } size_t item_size; + DLOG(INFO) << "role " << role_; LOG(INFO) << "Created freq_xlating_fir_filter with " << taps_.size() << " taps"; if ((taps_item_type_ == "float") && (input_item_type_ == "gr_complex") && (output_item_type_ == "gr_complex")) { diff --git a/src/algorithms/input_filter/adapters/notch_filter.cc b/src/algorithms/input_filter/adapters/notch_filter.cc index c3f1dfd0a..0f4c1ca82 100644 --- a/src/algorithms/input_filter/adapters/notch_filter.cc +++ b/src/algorithms/input_filter/adapters/notch_filter.cc @@ -22,25 +22,33 @@ #include -NotchFilter::NotchFilter(const ConfigurationInterface* configuration, const std::string& role, - unsigned int in_streams, unsigned int out_streams) : role_(role), in_streams_(in_streams), out_streams_(out_streams) +NotchFilter::NotchFilter(const ConfigurationInterface* configuration, + const std::string& role, + unsigned int in_streams, + unsigned int out_streams) + : role_(role), + in_streams_(in_streams), + out_streams_(out_streams) { + const std::string default_item_type("gr_complex"); + const std::string default_dump_file("./data/input_filter.dat"); const float default_pfa = 0.001; const float default_p_c_factor = 0.9; const int default_length_ = 32; const int default_n_segments_est = 12500; const int default_n_segments_reset = 5000000; - const std::string default_item_type("gr_complex"); - const std::string default_dump_file("./data/input_filter.dat"); - item_type_ = configuration->property(role + ".item_type", default_item_type); - dump_ = configuration->property(role + ".dump", false); - DLOG(INFO) << "dump_ is " << dump_; - dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file); + const float pfa = configuration->property(role + ".pfa", default_pfa); const float p_c_factor = configuration->property(role + ".p_c_factor", default_p_c_factor); const int length_ = configuration->property(role + ".length", default_length_); const int n_segments_est = configuration->property(role + ".segments_est", default_n_segments_est); const int n_segments_reset = configuration->property(role + ".segments_reset", default_n_segments_reset); + + dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file); + item_type_ = configuration->property(role + ".item_type", default_item_type); + dump_ = configuration->property(role + ".dump", false); + + DLOG(INFO) << "role " << role_; if (item_type_ == "gr_complex") { item_size_ = sizeof(gr_complex); diff --git a/src/algorithms/input_filter/adapters/notch_filter_lite.cc b/src/algorithms/input_filter/adapters/notch_filter_lite.cc index 530085a24..470a6881e 100644 --- a/src/algorithms/input_filter/adapters/notch_filter_lite.cc +++ b/src/algorithms/input_filter/adapters/notch_filter_lite.cc @@ -23,31 +23,39 @@ #include // for max -NotchFilterLite::NotchFilterLite(const ConfigurationInterface* configuration, const std::string& role, - unsigned int in_streams, unsigned int out_streams) : role_(role), in_streams_(in_streams), out_streams_(out_streams) +NotchFilterLite::NotchFilterLite(const ConfigurationInterface* configuration, + const std::string& role, + unsigned int in_streams, + unsigned int out_streams) + : role_(role), + in_streams_(in_streams), + out_streams_(out_streams) { - const float default_p_c_factor = 0.9; - const float default_pfa = 0.001; - const int default_length_ = 32; - const int default_n_segments_est = 12500; - const int default_n_segments_reset = 5000000; - const float default_samp_freq = 4000000; const std::string default_item_type("gr_complex"); const std::string default_dump_file("./data/input_filter.dat"); + const float default_p_c_factor = 0.9; + const float default_pfa = 0.001; + const float default_samp_freq = 4000000; + const int default_n_segments_reset = 5000000; + const int default_length_ = 32; + const int default_n_segments_est = 12500; + const float samp_freq = configuration->property("SignalSource.sampling_frequency", default_samp_freq); const float default_coeff_rate = samp_freq * 0.1F; - item_type_ = configuration->property(role + ".item_type", default_item_type); - dump_ = configuration->property(role + ".dump", false); - DLOG(INFO) << "dump_ is " << dump_; - dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file); const float p_c_factor = configuration->property(role + ".p_c_factor", default_p_c_factor); const float pfa = configuration->property(role + ".pfa", default_pfa); const float coeff_rate = configuration->property(role + ".coeff_rate", default_coeff_rate); const int length_ = configuration->property(role + ".length", default_length_); const int n_segments_est = configuration->property(role + ".segments_est", default_n_segments_est); const int n_segments_reset = configuration->property(role + ".segments_reset", default_n_segments_reset); + + dump_filename_ = configuration->property(role + ".dump_filename", default_dump_file); + item_type_ = configuration->property(role + ".item_type", default_item_type); + dump_ = configuration->property(role + ".dump", false); + int n_segments_coeff = static_cast((samp_freq / coeff_rate) / static_cast(length_)); n_segments_coeff = std::max(1, n_segments_coeff); + DLOG(INFO) << "role " << role_; if (item_type_ == "gr_complex") { item_size_ = sizeof(gr_complex); diff --git a/src/algorithms/input_filter/adapters/pulse_blanking_filter.cc b/src/algorithms/input_filter/adapters/pulse_blanking_filter.cc index f22ab676e..b28c13d47 100644 --- a/src/algorithms/input_filter/adapters/pulse_blanking_filter.cc +++ b/src/algorithms/input_filter/adapters/pulse_blanking_filter.cc @@ -26,19 +26,16 @@ #include -PulseBlankingFilter::PulseBlankingFilter(const ConfigurationInterface* configuration, std::string role, - unsigned int in_streams, unsigned int out_streams) : role_(std::move(role)), in_streams_(in_streams), out_streams_(out_streams) +PulseBlankingFilter::PulseBlankingFilter(const ConfigurationInterface* configuration, + std::string role, + unsigned int in_streams, + unsigned int out_streams) + : role_(std::move(role)), + in_streams_(in_streams), + out_streams_(out_streams) { - size_t item_size; - xlat_ = false; const std::string default_item_type("gr_complex"); const std::string default_dump_filename("../data/input_filter.dat"); - - DLOG(INFO) << "role " << role_; - - item_type_ = configuration->property(role_ + ".item_type", default_item_type); - dump_ = configuration->property(role_ + ".dump", false); - dump_filename_ = configuration->property(role_ + ".dump_filename", default_dump_filename); const float default_pfa_ = 0.04; const float pfa = configuration->property(role_ + ".pfa", default_pfa_); const int default_length_ = 32; @@ -47,6 +44,16 @@ PulseBlankingFilter::PulseBlankingFilter(const ConfigurationInterface* configura const int n_segments_est = configuration->property(role_ + ".segments_est", default_n_segments_est); const int default_n_segments_reset = 5000000; const int n_segments_reset = configuration->property(role_ + ".segments_reset", default_n_segments_reset); + const double default_if = 0.0; + const double if_aux = configuration->property(role_ + ".if", default_if); + const double if_ = configuration->property(role_ + ".IF", if_aux); + + dump_filename_ = configuration->property(role_ + ".dump_filename", default_dump_filename); + item_type_ = configuration->property(role_ + ".item_type", default_item_type); + dump_ = configuration->property(role_ + ".dump", false); + + DLOG(INFO) << "role " << role_; + size_t item_size; if (item_type_ == "gr_complex") { item_size = sizeof(gr_complex); // output @@ -59,9 +66,6 @@ PulseBlankingFilter::PulseBlankingFilter(const ConfigurationInterface* configura item_size = sizeof(gr_complex); // avoids uninitialization input_size_ = 0; // notify wrong configuration } - const double default_if = 0.0; - const double if_aux = configuration->property(role_ + ".if", default_if); - const double if_ = configuration->property(role_ + ".IF", if_aux); if (std::abs(if_) > 1.0) { xlat_ = true; @@ -74,6 +78,10 @@ PulseBlankingFilter::PulseBlankingFilter(const ConfigurationInterface* configura const std::vector taps = gr::filter::firdes::low_pass(1.0, sampling_freq_, bw_, tw_); freq_xlating_ = gr::filter::freq_xlating_fir_filter_ccf::make(1, taps, if_, sampling_freq_); } + else + { + xlat_ = false; + } if (dump_) { DLOG(INFO) << "Dumping output into file " << dump_filename_; diff --git a/src/algorithms/input_filter/gnuradio_blocks/notch_cc.cc b/src/algorithms/input_filter/gnuradio_blocks/notch_cc.cc index 797a4ce97..09c651cd8 100644 --- a/src/algorithms/input_filter/gnuradio_blocks/notch_cc.cc +++ b/src/algorithms/input_filter/gnuradio_blocks/notch_cc.cc @@ -34,28 +34,29 @@ Notch::Notch(float pfa, float p_c_factor, int32_t length, int32_t n_segments_est, - int32_t n_segments_reset) : gr::block("Notch", - gr::io_signature::make(1, 1, sizeof(gr_complex)), - gr::io_signature::make(1, 1, sizeof(gr_complex))) + int32_t n_segments_reset) + : gr::block("Notch", + gr::io_signature::make(1, 1, sizeof(gr_complex)), + gr::io_signature::make(1, 1, sizeof(gr_complex))), + last_out_(gr_complex(0.0, 0.0)), + z_0_(gr_complex(0.0, 0.0)), + p_c_factor_(gr_complex(p_c_factor, 0.0)), + pfa_(pfa), + noise_pow_est_(0.0), + length_(length), // Set the number of samples per segment + n_deg_fred_(2 * length), // Number of dregrees of freedom, + n_segments_(0), + n_segments_est_(n_segments_est), // Set the number of segments for noise power estimation + n_segments_reset_(n_segments_reset), // Set the period (in segments) when the noise power is estimated + filter_state_(false) { const int32_t alignment_multiple = volk_get_alignment() / sizeof(gr_complex); set_alignment(std::max(1, alignment_multiple)); - pfa_ = pfa; - noise_pow_est_ = 0.0; - p_c_factor_ = gr_complex(p_c_factor, 0.0); - length_ = length; // Set the number of samples per segment - filter_state_ = false; // Initial state of the filter - n_deg_fred_ = 2 * length_; // Number of dregrees of freedom - n_segments_ = 0; - n_segments_est_ = n_segments_est; // Set the number of segments for noise power estimation - n_segments_reset_ = n_segments_reset; // Set the period (in segments) when the noise power is estimated - z_0_ = gr_complex(0.0, 0.0); boost::math::chi_squared_distribution my_dist_(n_deg_fred_); thres_ = boost::math::quantile(boost::math::complement(my_dist_, pfa_)); c_samples_ = volk_gnsssdr::vector(length_); angle_ = volk_gnsssdr::vector(length_); power_spect_ = volk_gnsssdr::vector(length_); - last_out_ = gr_complex(0.0, 0.0); d_fft_ = gnss_fft_fwd_make_unique(length_); } diff --git a/src/algorithms/input_filter/gnuradio_blocks/notch_lite_cc.cc b/src/algorithms/input_filter/gnuradio_blocks/notch_lite_cc.cc index e365cfe13..c0902d724 100644 --- a/src/algorithms/input_filter/gnuradio_blocks/notch_lite_cc.cc +++ b/src/algorithms/input_filter/gnuradio_blocks/notch_lite_cc.cc @@ -35,32 +35,35 @@ NotchLite::NotchLite(float p_c_factor, int32_t length, int32_t n_segments_est, int32_t n_segments_reset, - int32_t n_segments_coeff) : gr::block("NotchLite", - gr::io_signature::make(1, 1, sizeof(gr_complex)), - gr::io_signature::make(1, 1, sizeof(gr_complex))) + int32_t n_segments_coeff) + : gr::block("NotchLite", + gr::io_signature::make(1, 1, sizeof(gr_complex)), + gr::io_signature::make(1, 1, sizeof(gr_complex))), + last_out_(gr_complex(0.0, 0.0)), + z_0_(gr_complex(0.0, 0.0)), + p_c_factor_(gr_complex(p_c_factor, 0.0)), + c_samples1_(gr_complex(0.0, 0.0)), + c_samples2_(gr_complex(0.0, 0.0)), + pfa_(pfa), + noise_pow_est_(0.0), + angle1_(0.0), + angle2_(0.0), + length_(length), + n_segments_(0), + n_segments_est_(n_segments_est), + n_segments_reset_(n_segments_reset), + n_segments_coeff_reset_(n_segments_coeff), + n_segments_coeff_(0), + n_deg_fred_(2 * length), + filter_state_(false) { const int32_t alignment_multiple = volk_get_alignment() / sizeof(gr_complex); set_alignment(std::max(1, alignment_multiple)); set_history(2); - p_c_factor_ = gr_complex(p_c_factor, 0.0); - n_segments_est_ = n_segments_est; - n_segments_reset_ = n_segments_reset; - n_segments_coeff_reset_ = n_segments_coeff; - n_segments_coeff_ = 0; - length_ = length; - pfa_ = pfa; - n_segments_ = 0; - n_deg_fred_ = 2 * length_; - noise_pow_est_ = 0.0; - filter_state_ = false; - z_0_ = gr_complex(0.0, 0.0); - last_out_ = gr_complex(0.0, 0.0); + boost::math::chi_squared_distribution my_dist_(n_deg_fred_); thres_ = boost::math::quantile(boost::math::complement(my_dist_, pfa_)); - c_samples1_ = gr_complex(0.0, 0.0); - c_samples2_ = gr_complex(0.0, 0.0); - angle1_ = 0.0; - angle2_ = 0.0; + power_spect_ = volk_gnsssdr::vector(length_); d_fft_ = gnss_fft_fwd_make_unique(length_); } diff --git a/src/algorithms/input_filter/gnuradio_blocks/pulse_blanking_cc.cc b/src/algorithms/input_filter/gnuradio_blocks/pulse_blanking_cc.cc index a8d06ed3f..d963f0b2b 100644 --- a/src/algorithms/input_filter/gnuradio_blocks/pulse_blanking_cc.cc +++ b/src/algorithms/input_filter/gnuradio_blocks/pulse_blanking_cc.cc @@ -33,20 +33,21 @@ pulse_blanking_cc_sptr make_pulse_blanking_cc(float pfa, int32_t length, pulse_blanking_cc::pulse_blanking_cc(float pfa, int32_t length, int32_t n_segments_est, - int32_t n_segments_reset) : gr::block("pulse_blanking_cc", - gr::io_signature::make(1, 1, sizeof(gr_complex)), - gr::io_signature::make(1, 1, sizeof(gr_complex))) + int32_t n_segments_reset) + : gr::block("pulse_blanking_cc", + gr::io_signature::make(1, 1, sizeof(gr_complex)), + gr::io_signature::make(1, 1, sizeof(gr_complex))), + noise_power_estimation_(0.0), + pfa_(pfa), + length_(length), + n_segments_(0), + n_segments_est_(n_segments_est), + n_segments_reset_(n_segments_reset), + n_deg_fred_(2 * length), + last_filtered_(false) { const int32_t alignment_multiple = volk_get_alignment() / sizeof(gr_complex); set_alignment(std::max(1, alignment_multiple)); - pfa_ = pfa; - length_ = length; - last_filtered_ = false; - n_segments_ = 0; - n_segments_est_ = n_segments_est; - n_segments_reset_ = n_segments_reset; - noise_power_estimation_ = 0.0; - n_deg_fred_ = 2 * length_; boost::math::chi_squared_distribution my_dist_(n_deg_fred_); thres_ = boost::math::quantile(boost::math::complement(my_dist_, pfa_)); zeros_ = volk_gnsssdr::vector(length_);