1
0
mirror of https://github.com/gnss-sdr/gnss-sdr synced 2025-06-24 01:14:08 +00:00

Improved multistate notch filter

Better managing of the system memory
This commit is contained in:
Antonio Ramos 2017-06-23 11:34:02 +02:00
parent 41e181e30d
commit d67d9b270e
2 changed files with 69 additions and 43 deletions

View File

@ -47,77 +47,97 @@ Notch::Notch(float pfa, float p_c_factor, int length_) : gr::block("Notch",
{ {
const int alignment_multiple = volk_get_alignment() / sizeof(gr_complex); const int alignment_multiple = volk_get_alignment() / sizeof(gr_complex);
set_alignment(std::max(1, alignment_multiple)); set_alignment(std::max(1, alignment_multiple));
set_history(2);
this->pfa = pfa; this->pfa = pfa;
noise_pow_est = 0.0; noise_pow_est = 0.0;
this->p_c_factor = p_c_factor; this->p_c_factor = p_c_factor;
this->length_ = length_; this->length_ = length_; //Set the number of samples per segment
filter_state_ = false; set_output_multiple(length_);
n_deg_fred = 2 * length_; filter_state_ = false; //Initial state of the filter
n_segments_est = 5; n_deg_fred = 2 * length_; //Number of dregrees of freedom
n_segments = 0; n_segments = 0;
n_segments_est = 8; // Set the number of segments for noise power estimation
n_segments_reset = 1000000; // Set the period (in segments) when the noise power is estimated
z_0 = gr_complex(0 , 0); z_0 = gr_complex(0 , 0);
boost::math::chi_squared_distribution<float> my_dist_(n_deg_fred); boost::math::chi_squared_distribution<float> my_dist_(n_deg_fred);
thres_ = boost::math::quantile(boost::math::complement(my_dist_, pfa)); thres_ = boost::math::quantile(boost::math::complement(my_dist_, pfa));
in = NULL;
out = NULL;
paux = NULL;
c_samples = static_cast<gr_complex *>(volk_malloc(length_ * sizeof(gr_complex), volk_get_alignment()));
angle_ = static_cast<float *>(volk_malloc(length_ * sizeof(float), volk_get_alignment()));
last_out = gr_complex(0,0);
} }
Notch::~Notch()
{
volk_free(c_samples);
volk_free(angle_);
}
int Notch::general_work(int noutput_items __attribute__((unused)), gr_vector_int &ninput_items __attribute__((unused)), int Notch::general_work(int noutput_items __attribute__((unused)), gr_vector_int &ninput_items __attribute__((unused)),
gr_vector_const_void_star &input_items, gr_vector_void_star &output_items) gr_vector_const_void_star &input_items, gr_vector_void_star &output_items)
{ {
gr_complex * in = (gr_complex *) input_items[0]; int index_in = 1;
gr_complex *out = (gr_complex *) output_items[0]; int index_out = 0;
gr_complex * paux; in = (gr_complex *) input_items[index_in];
int samples_proc = 0; out = (gr_complex *) output_items[index_out];
int aux = 0; int aux = 0;
gr_complex magnitude; gr_complex magnitude;
float sig2 = 0.0; float sig2 = 0.0;
float* angle_; while(((index_out + length_) < noutput_items) && (n_segments < n_segments_est) && (filter_state_ == false))
gr_complex * c_samples;
c_samples = static_cast<gr_complex *>(volk_malloc(length_ * sizeof(gr_complex), volk_get_alignment()));
angle_ = static_cast<float *>(volk_malloc(length_ * sizeof(float), volk_get_alignment()));
while(((samples_proc + length_) < noutput_items) && (n_segments < n_segments_est))
{ {
volk_32fc_x2_conjugate_dot_prod_32fc(&magnitude, in, in, length_); volk_32fc_x2_conjugate_dot_prod_32fc(&magnitude, in, in, length_);
sig2 = magnitude.real() / ((float) n_deg_fred); sig2 = magnitude.real() / ((float) n_deg_fred);
noise_pow_est = (((float) n_segments) * noise_pow_est + sig2) / ((float)(n_segments + 1)); noise_pow_est = (((float) n_segments) * noise_pow_est + sig2) / ((float)(n_segments + 1));
samples_proc = samples_proc + length_; index_out = index_out + length_;
index_in = index_in +length_;
n_segments++; n_segments++;
memcpy(out, in, sizeof(gr_complex)*length_); memcpy(out, in, sizeof(gr_complex) * length_);
in = (gr_complex *) input_items[samples_proc]; in = (gr_complex *) input_items[index_in];
out = (gr_complex *) output_items[samples_proc]; out = (gr_complex *) output_items[index_out];
} }
while((samples_proc + length_) < noutput_items) while((index_out + length_) < noutput_items)
{ {
n_segments++;
volk_32fc_x2_conjugate_dot_prod_32fc(&magnitude, in, in, length_); volk_32fc_x2_conjugate_dot_prod_32fc(&magnitude, in, in, length_);
if( (magnitude.real() / noise_pow_est) > thres_) if( (magnitude.real() / noise_pow_est) > thres_)
{ {
filter_state_ = true; if(filter_state_ == false)
paux = (gr_complex *) input_items[samples_proc-1]; {
filter_state_ = true;
last_out = gr_complex(0,0);
}
paux = (gr_complex *) input_items[index_in-1];
volk_32fc_x2_multiply_conjugate_32fc(c_samples, in, paux, length_); volk_32fc_x2_multiply_conjugate_32fc(c_samples, in, paux, length_);
volk_32fc_s32f_atan2_32f(angle_, c_samples, (float)1.0, length_); volk_32fc_s32f_atan2_32f(angle_, c_samples, (float)1.0, length_);
for(aux = 0; aux < length_; aux++) for(aux = 0; aux < length_; aux++)
{ {
z_0 = std::exp(gr_complex(0,1) *angle_[aux]); z_0 = std::exp(gr_complex(0,1) * angle_[aux]);
out[samples_proc] = in[samples_proc] - z_0 * in[samples_proc - 1] out[index_out] = in[index_in] - z_0 * in[index_in - 1]
+ gr_complex(p_c_factor,0) * z_0 * out[samples_proc -1]; + gr_complex(p_c_factor,0) * z_0 * last_out;
samples_proc++; last_out = out[index_out];
in = (gr_complex *) input_items[samples_proc]; index_out++;
out = (gr_complex *) output_items[samples_proc]; index_in++;
in = (gr_complex *) input_items[index_in];
out = (gr_complex *) output_items[index_out];
} }
} }
else else
{ {
if (n_segments > n_segments_reset)
{
n_segments = 0;
}
filter_state_ = false; filter_state_ = false;
samples_proc = samples_proc + length_; index_out = index_out + length_;
memcpy(out, in, sizeof(gr_complex)*length_); index_in = index_in +length_;
in = (gr_complex *) input_items[samples_proc]; memcpy(out, in, sizeof(gr_complex) * length_);
out = (gr_complex *) output_items[samples_proc]; in = (gr_complex *) input_items[index_in];
out = (gr_complex *) output_items[index_out];
} }
} }
volk_free(c_samples); consume_each(index_out);
volk_free(angle_); return index_out;
consume_each(samples_proc);
return samples_proc;
} }

View File

@ -54,20 +54,26 @@ private:
float p_c_factor; float p_c_factor;
float thres_; float thres_;
int length_; int length_;
int n_segments_est; unsigned int n_segments;
int n_segments; unsigned int n_segments_est;
unsigned int n_segments_reset;
int n_deg_fred; int n_deg_fred;
bool filter_state_; bool filter_state_;
gr_complex last_out;
gr_complex z_0; gr_complex z_0;
gr_complex* in;
gr_complex* out;
gr_complex* paux;
gr_complex* c_samples;
float* angle_;
public: public:
//friend notch_sptr make_notch_filter(float pfa, float p_c_factor,
// int length_);
Notch(float pfa, float p_c_factor, int length_); Notch(float pfa, float p_c_factor, int length_);
~Notch();
int general_work (int noutput_items, gr_vector_int &ninput_items, int general_work (int noutput_items, gr_vector_int &ninput_items,
gr_vector_const_void_star &input_items, gr_vector_const_void_star &input_items,
gr_vector_void_star &output_items); gr_vector_void_star &output_items);