2017-06-19 13:51:57 +00:00
|
|
|
/*!
|
|
|
|
* \file notch_cc.cc
|
|
|
|
* \brief Implements a multi state notch filter algorithm
|
|
|
|
* \author Antonio Ramos (antonio.ramosdet(at)gmail.com)
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010-2017 (see AUTHORS file for a list of contributors)
|
|
|
|
*
|
|
|
|
* GNSS-SDR is a software defined Global Navigation
|
|
|
|
* Satellite Systems receiver
|
|
|
|
*
|
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
|
|
|
* GNSS-SDR 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GNSS-SDR 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 GNSS-SDR. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* -------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notch_cc.h"
|
|
|
|
#include <boost/math/distributions/chi_squared.hpp>
|
|
|
|
#include <cmath>
|
|
|
|
#include <complex>
|
2017-07-07 09:48:54 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2017-06-19 13:51:57 +00:00
|
|
|
#include <gnuradio/io_signature.h>
|
2017-06-20 16:13:52 +00:00
|
|
|
#include <volk/volk.h>
|
2017-07-07 09:48:54 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
using google::LogMessage;
|
2017-06-19 13:51:57 +00:00
|
|
|
|
2017-06-20 16:13:52 +00:00
|
|
|
notch_sptr make_notch_filter(float pfa, float p_c_factor,
|
|
|
|
int length_)
|
2017-06-19 13:51:57 +00:00
|
|
|
{
|
|
|
|
return notch_sptr(new Notch(pfa, p_c_factor, length_));
|
|
|
|
}
|
|
|
|
|
2017-06-20 16:13:52 +00:00
|
|
|
Notch::Notch(float pfa, float p_c_factor, int length_) : gr::block("Notch",
|
2017-06-19 13:51:57 +00:00
|
|
|
gr::io_signature::make (1, 1, sizeof(gr_complex)),
|
|
|
|
gr::io_signature::make (1, 1, sizeof(gr_complex)))
|
|
|
|
{
|
2017-06-20 16:13:52 +00:00
|
|
|
const int alignment_multiple = volk_get_alignment() / sizeof(gr_complex);
|
|
|
|
set_alignment(std::max(1, alignment_multiple));
|
2017-06-23 09:34:02 +00:00
|
|
|
set_history(2);
|
2017-06-19 13:51:57 +00:00
|
|
|
this->pfa = pfa;
|
2017-06-20 16:13:52 +00:00
|
|
|
noise_pow_est = 0.0;
|
2017-07-07 09:48:54 +00:00
|
|
|
this->p_c_factor = gr_complex(p_c_factor , 0);
|
2017-06-23 09:34:02 +00:00
|
|
|
this->length_ = length_; //Set the number of samples per segment
|
|
|
|
set_output_multiple(length_);
|
|
|
|
filter_state_ = false; //Initial state of the filter
|
|
|
|
n_deg_fred = 2 * length_; //Number of dregrees of freedom
|
|
|
|
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
|
2017-06-19 13:51:57 +00:00
|
|
|
z_0 = gr_complex(0 , 0);
|
2017-06-20 16:13:52 +00:00
|
|
|
boost::math::chi_squared_distribution<float> my_dist_(n_deg_fred);
|
|
|
|
thres_ = boost::math::quantile(boost::math::complement(my_dist_, pfa));
|
2017-06-23 09:34:02 +00:00
|
|
|
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);
|
2017-06-19 13:51:57 +00:00
|
|
|
}
|
|
|
|
|
2017-06-23 09:34:02 +00:00
|
|
|
Notch::~Notch()
|
|
|
|
{
|
|
|
|
volk_free(c_samples);
|
|
|
|
volk_free(angle_);
|
|
|
|
}
|
2017-06-19 13:51:57 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2017-07-07 09:48:54 +00:00
|
|
|
|
2017-06-23 09:34:02 +00:00
|
|
|
int index_in = 1;
|
|
|
|
int index_out = 0;
|
2017-06-20 16:13:52 +00:00
|
|
|
int aux = 0;
|
2017-07-07 09:48:54 +00:00
|
|
|
lv_32fc_t magnitude;
|
2017-06-20 16:13:52 +00:00
|
|
|
float sig2 = 0.0;
|
2017-07-07 09:48:54 +00:00
|
|
|
gr_complex* in = (gr_complex *) input_items[0];
|
|
|
|
gr_complex* out = (gr_complex *) output_items[0];
|
|
|
|
gr_complex* paux;
|
|
|
|
in++;
|
2017-06-23 09:34:02 +00:00
|
|
|
while(((index_out + length_) < noutput_items) && (n_segments < n_segments_est) && (filter_state_ == false))
|
2017-06-20 16:13:52 +00:00
|
|
|
{
|
|
|
|
volk_32fc_x2_conjugate_dot_prod_32fc(&magnitude, in, in, length_);
|
2017-07-07 09:48:54 +00:00
|
|
|
sig2 = lv_creal(magnitude) / ((float) n_deg_fred);
|
2017-06-20 16:13:52 +00:00
|
|
|
noise_pow_est = (((float) n_segments) * noise_pow_est + sig2) / ((float)(n_segments + 1));
|
2017-07-07 09:48:54 +00:00
|
|
|
index_out += length_;
|
|
|
|
index_in += length_;
|
2017-06-20 16:13:52 +00:00
|
|
|
n_segments++;
|
2017-06-23 09:34:02 +00:00
|
|
|
memcpy(out, in, sizeof(gr_complex) * length_);
|
2017-07-07 09:48:54 +00:00
|
|
|
in += length_;
|
|
|
|
out += length_;
|
2017-06-20 16:13:52 +00:00
|
|
|
}
|
2017-06-23 09:34:02 +00:00
|
|
|
while((index_out + length_) < noutput_items)
|
2017-06-20 16:13:52 +00:00
|
|
|
{
|
2017-06-23 09:34:02 +00:00
|
|
|
n_segments++;
|
2017-06-20 16:13:52 +00:00
|
|
|
volk_32fc_x2_conjugate_dot_prod_32fc(&magnitude, in, in, length_);
|
2017-07-07 09:48:54 +00:00
|
|
|
if( (lv_creal(magnitude) / noise_pow_est) > thres_)
|
2017-06-20 16:13:52 +00:00
|
|
|
{
|
2017-06-23 09:34:02 +00:00
|
|
|
if(filter_state_ == false)
|
|
|
|
{
|
|
|
|
filter_state_ = true;
|
|
|
|
last_out = gr_complex(0,0);
|
|
|
|
}
|
2017-07-07 09:48:54 +00:00
|
|
|
paux = in - 1;
|
2017-06-20 16:13:52 +00:00
|
|
|
volk_32fc_x2_multiply_conjugate_32fc(c_samples, in, paux, length_);
|
2017-07-07 09:48:54 +00:00
|
|
|
volk_32fc_s32f_atan2_32f(angle_, c_samples, ((float)1.0), length_);
|
2017-06-20 16:13:52 +00:00
|
|
|
for(aux = 0; aux < length_; aux++)
|
|
|
|
{
|
2017-07-07 09:48:54 +00:00
|
|
|
z_0 = std::exp(gr_complex(0,1) * (*(angle_ + aux)));
|
|
|
|
*out = *in - z_0 * (*(in - 1))
|
|
|
|
+ p_c_factor * z_0 * last_out;
|
|
|
|
last_out = *out;
|
2017-06-23 09:34:02 +00:00
|
|
|
index_out++;
|
|
|
|
index_in++;
|
2017-07-07 09:48:54 +00:00
|
|
|
in ++;
|
|
|
|
out ++;
|
2017-06-20 16:13:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-06-23 09:34:02 +00:00
|
|
|
if (n_segments > n_segments_reset)
|
|
|
|
{
|
|
|
|
n_segments = 0;
|
|
|
|
}
|
2017-06-20 16:13:52 +00:00
|
|
|
filter_state_ = false;
|
2017-07-07 09:48:54 +00:00
|
|
|
index_out += length_;
|
|
|
|
index_in += length_;
|
2017-06-23 09:34:02 +00:00
|
|
|
memcpy(out, in, sizeof(gr_complex) * length_);
|
2017-07-07 09:48:54 +00:00
|
|
|
in += length_;
|
|
|
|
out += length_;
|
2017-06-20 16:13:52 +00:00
|
|
|
}
|
|
|
|
}
|
2017-06-23 09:34:02 +00:00
|
|
|
consume_each(index_out);
|
|
|
|
return index_out;
|
2017-06-19 13:51:57 +00:00
|
|
|
}
|