// file: extract_feature/extf_func_1.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
//  float_8 sample_freq: (input) sample frequency
//
// This method computes the Fourier transform of a signal
//
logical_1 Extract_feature::func_mfbank_cc(float_8* amplitude_a,
					  float_8* signal_a,
					  int_4 num_samples_a) {

  // compute the frequence pointer in mel frequence scale
  //
  int len = num_samples_a / 2;
  float_8* mel = new float_8[len+1];
  memset(mel, 0, (len+1) * sizeof(float_8));
  compute_mel_cc(len, mel);

  // 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, mel);

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

  // free memory
  //
  delete [] mel;
  delete [] cenf;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}






