// file: extf_norm_0.cc
//
// this program calculates the snr of a signal using a probabilistic measure
// 

// system include files
//
#include <string.h>

// isip include files
//
#include "extract_feature.h"
#include "extract_feature_constants.h"
#include <signal_constants.h>

// function: normalize_cc
//
// arguments:
//
//  float_8** vectors: (input/output) pointer to working data
//  int_4 num_frames: (input) number of frames
//
// return value: a logical_1 indicating status
//
// this function parses command line arguments
//
logical_1 Extract_feature::normalize_cc(float_8** vectors_a,
					int_4 num_frames_a) {
  
  float_8 mean[vector_first_d];  
  memset(mean, 0, vector_first_d * sizeof(float_8));
  float_8 min_energy = max_energy_d - (EXTF_SIL_FLOOR*log(10))/10.0;
  int mfcc_start_pos = 0;

  // clamp energy to silence floor
  //
  if (include_energy_d == ISIP_TRUE) {
    for (int_4 i=0; i < num_frames_a; i++) {
      if (vectors_a[i][0] <  min_energy) {
	vectors_a[i][0] =  min_energy;
      }
      if (energy_norm_d == ISIP_TRUE) {
	vectors_a[i][0] =  1 - (max_energy_d - vectors_a[i][0]);
      }
    }
    mfcc_start_pos++;
  }
  
  // if cms is required
  //
  if (cepstral_mean_subtract_d == ISIP_TRUE) {
    // outerloop first helps locality of memory accesses
    //
    for (int_4 i=0; i < num_frames_a; i++) {
      for (int_4 j=mfcc_start_pos; j < vector_first_d; j++) {
	mean[j] += vectors_a[i][j];
      }
    }

    // scale the means by the number of samples
    //
    for (int_4 j=mfcc_start_pos; j < vector_first_d; j++) {
      mean[j] /= (float_8)(num_frames_a);
    }

    // normalize the samples by the mean
    //
    for (int_4 i=0; i < num_frames_a; i++) {
      for (int_4 j=mfcc_start_pos; j < vector_first_d; j++) {
	vectors_a[i][j] -= mean[j];
      }
    }
  }

  // return without error
  //
  return ISIP_TRUE;
    
}
