/* -*- c++ -*- */
/*
 * Copyright 2007 Free Software Foundation, Inc.
 * 
 * This file is part of GNU Radio
 * 
 * GNU Radio 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.
 * 
 * GNU Radio 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 GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gr_iir_resonator_bandpass_fff.h>
#include <gr_io_signature.h>
#include <stdio.h>


gr_iir_resonator_bandpass_fff_sptr 
gr_make_iir_resonator_bandpass_fff (const float gain, const float fbtap0,const float fbtap1,const float intrinsic_gain) throw (std::invalid_argument)
{
  return gr_iir_resonator_bandpass_fff_sptr (new gr_iir_resonator_bandpass_fff (gain,fbtap0,fbtap1,intrinsic_gain));
}

gr_iir_resonator_bandpass_fff::gr_iir_resonator_bandpass_fff (const float gain, const float fbtap0,const float fbtap1,const float intrinsic_gain) throw (std::invalid_argument)

  : gr_sync_block ("iir_resonator_bandpass_fff",
		   gr_make_io_signature (1, 1, sizeof (float)),
		   gr_make_io_signature (1, 1, sizeof (float))),
    //d_iir_resonator_bandpass ( fbtaps,intrinsic_gain),
    d_updated (false)
{
  set_taps(fbtap0,fbtap1,intrinsic_gain);
  set_gain(gain);
  reset();
  // fprintf (stderr, "gr_iir_resonator_bandpass_fff::ctor\n");
}

gr_iir_resonator_bandpass_fff::~gr_iir_resonator_bandpass_fff ()
{
  // nop
}

void
gr_iir_resonator_bandpass_fff::reset ()
{
  d_v0=0.0;
  d_v1=0.0;
  d_v2=0.0;
}


void
gr_iir_resonator_bandpass_fff::set_gain (const float gain) throw (std::invalid_argument)
{
  
  d_gain = gain;
  d_inv_gain=d_gain/d_intrinsic_gain;//TODO check for invalid value 0.0
  d_updated = true;
}

void
gr_iir_resonator_bandpass_fff::set_taps (const float fbtap0, const float fbtap1,const float intrinsic_gain, bool do_reset) throw (std::invalid_argument)
{
  
  d_fb0 = fbtap0;
  d_fb1 = fbtap1;
  d_intrinsic_gain=intrinsic_gain;
  set_gain(d_gain);
  d_updated = true;
  if(do_reset) reset();
}

int
gr_iir_resonator_bandpass_fff::work (int noutput_items,
			 gr_vector_const_void_star &input_items,
			 gr_vector_void_star &output_items)
{
  const float *in = (const float *) input_items[0];
  float *out = (float *) output_items[0];


  /*if (d_updated){
    set_taps (d_new_fbtaps);
    set_intrinsic_gain(d_new_intrinsic_gain);
    set_gain(d_new_gain);
    d_updated = false;
  }*/

  filter_n (out, in, noutput_items);
  return noutput_items;
};


float gr_iir_resonator_bandpass_fff::filter_one(float input)
{
  d_v0 = d_v1; d_v1 = d_v2; 
  d_v2 = (d_inv_gain * input)
          + ( d_fb0 * d_v0) + (  d_fb1 * d_v1);
  return    (d_v2 - d_v0);
}


void 
gr_iir_resonator_bandpass_fff::filter_n (float output[],
					     const float input[],
					     long n)
{
  for (int i = 0; i < n; i++)
    output[i] = filter_one (input[i]);
}

