// file: tr_st_eval_1.cc
//

// isip include files
//
#include "train_state.h"
#include "train_state_constants.h"
 
// method: eval_mix_cc
//
// arguments:
//  int_4 mix_ind : (input) the mixture component index
//  float_8* data : (input) a data feature vector
//  int_4 frame : (input) the current frame index
//
// return: the score based on the input feature vector and mixture index
//
// this function evaluates the input feature vector against the Train_State
// distribution and computes a log-distance metric (i.e. a log score
// of the data given the model)
//
float_8 Train_State::eval_score_cc(int_4 mix_ind_a, float_8* data_a) {
			     
  // temporary variables
  //
  float_8 tmp_score = (float_8)0;
  float_8 tmp = (float_8)0;
  float_8* trans_data = new float_8[num_features_d];
  
  // initialize score
  //
  score_d = TRAIN_STATE_SCORE;
  
  // reset tmp score
  //
  tmp_score = (float_8)scale_d[mix_ind_a-1];
  
  // compute the likelihood of data given mixture
  //
  for (int_4 j = 0; j < num_features_d; j++) {
    
    // compute the translated data and take the product to evaluate
    // likelihood score
    //
    trans_data[j] = data_a[j] - (float_8)mean_d[mix_ind_a-1][j];
    tmp_score += trans_data[j] * trans_data[j] *
      (float_8)covar_d[mix_ind_a-1][j];
  }
  
  // add the mixture weight, use recursive sum to protect against
  // underflow
  //
  tmp_score = (float_8)weights_d[mix_ind_a-1] - (float_8)0.5 * tmp_score;
    
  if (score_d < tmp_score) {
    tmp = score_d;
    score_d = tmp_score;
    tmp_score = tmp;
  }
  tmp = tmp_score - score_d;
  if (tmp >= TRAIN_STATE_LOG_SCORE) {
    score_d += (float_8)log(1.0 + exp((float_8)tmp));
  }
  
  // free memory
  //
  delete [] trans_data;

  // return the log likelihood score
  //
  return score_d;
}
