// file: extract_feature/extf_func_5.cc
//

// isip include files
//
#include "extract_feature.h"
#include <Signal.h>
#include <memory.h>

#include "extract_feature_constants.h"
#include <signal_constants.h>

// method: func_fbank_cc
//
// arguments:
//  float_8* amplitude_a: (output) filter bank amplitudes
//  int_4 order_bank_a: (input) number of filters
//  float_8* signal_a: (input) signal to run filters upon
//  int_4 num_samples_a: (input) size of signal
//
// This method computes the filter bank amplitudes on the normal
// frequence scale
//
logical_1 Extract_feature::func_fbank_cc(float_8* amplitude_a, 
					 float_8* signal_a,
					 int_4 num_samples_a) {

  // compute the frequence pointers in the normal frequence scale
  //
  int len = num_samples_a/2;

  float_8* freq = new float_8[len+1];
  memset(freq, 0, (len+1) * sizeof(float_8));
  float_8 sf = 8000.0; 

  for (int k = 0; k<len+1; k++) {
    freq[k] = (sf/(float_8)2) * (k/(float_8)len);
  }

  // compute the center frequence
  //
  float_8* cenf = new float_8[num_fbanks_d+2];
  memset(cenf, 0, (num_fbanks_d+2) * sizeof(float_8));
  compute_cenf_cc(len, cenf, NULL);

  // compute the filter bank amplitude via triangle filters
  //
  for (int i = 0; i<num_fbanks_d; i++) {
    amplitude_a[i] = 0.0;
    for (int l = 0; l<len; l++) {
      if ((freq[l] >= cenf[i]) && (freq[l] < cenf[i+1])) {
      	amplitude_a[i] += signal_a[l]*(freq[l]-cenf[i])/
					  (cenf[i+1]-cenf[i]);
      }
      else if ((freq[l] >= cenf[i+1]) && (freq[l] < cenf[i+2])) {
	amplitude_a[i] += signal_a[l]*(freq[l]-cenf[i+1])/
					  (cenf[i+2]-cenf[i+1]);
      }
    }
  }

  // free memory
  //
  delete [] freq;
  delete [] cenf;

  // exit gracefully
  //
  return ISIP_TRUE;
}

