howto_correlation_sink_c.cc


※上記の広告は60日以上更新のないWIKIに表示されています。更新することで広告が下部へ移動します。

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <howto_correlation_sink_c.h>
#include <algorithm>
#include <gr_io_signature.h>


static const int MIN_IN = 1;	// mininum number of input streams
static const int MAX_IN = -1;	// maximum number of input streams
static const int MIN_OUT = 1;
static const int MAX_OUT = -1;

howto_correlation_sink_c::howto_correlation_sink_c (unsigned decimation, int vlen)
  : gr_sync_decimator ("correlation_sink_c",
		   gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex) * vlen),
		   gr_make_io_signature (MIN_IN, MAX_IN, sizeof (float) * vlen),
		   //gr_make_io_signature (0, 0, 0),
		   decimation),
    d_vlen(vlen)
{
} 

int
howto_correlation_sink_c::work (int noutput_items,
		    gr_vector_const_void_star &input_items,
		    gr_vector_void_star &output_items)
{ 
	/*
	 * gr_complex *iptr = (gr_complex *) input_items[0];
	 * for (int i = 0; i < noutput_items * d_vlen; i++)
	 * d_data.push_back (iptr[i]);
    */
    const gr_complex *in0 = (const gr_complex *) input_items[0];
    const gr_complex *in1 = (const gr_complex *) input_items[1];
    float *out = (float *) output_items[0];
    gr_complex sum_in0, sum_in1;
    float avg_in0, avg_in1, covar, var_in0, var_in1, corr_coef;
     
	int ntaps = decimation ();
	for (int i = 0; i < noutput_items * d_vlen; i++){
		sum_in0 = gr_complex(0, 0);
		sum_in1 = gr_complex(0, 0);	
		covar = 0;
		var_in0 = 0;
		var_in1 = 0;
		corr_coef = 0;
		//calculate average of each input
		for(int j = 0; j < ntaps; j++){
			sum_in0 += in0[j];
			sum_in1 += in1[j];
		}
		avg_in0 = real(sum_in0) / ntaps;
		avg_in1 = real(sum_in1) / ntaps;
		for(int j = 0; j < ntaps; j++){
			covar += (real(in0[j]) - avg_in0) * (real(in1[j]) - avg_in1);
			var_in0 += (real(in0[j]) - avg_in0) * (real(in0[j]) - avg_in0);
			var_in1 += (real(in1[j]) - avg_in1) * (real(in1[j]) - avg_in1);
		}
		corr_coef = covar / (sqrt(var_in0) * sqrt(var_in1)); //calculate correlation coefficient
		out[i] = corr_coef;
	}
	return noutput_items;
}


howto_correlation_sink_c_sptr
howto_make_correlation_sink_c (unsigned decimation, int vlen)
{
  return howto_correlation_sink_c_sptr (new howto_correlation_sink_c (decimation, vlen));
}

std::vector<gr_complex>
howto_correlation_sink_c::data () const
{
  return d_data;
}