/*
 *  Copyright 2002 by Soos, Antal
 *  All rights reserved. Property of Soos, Antal.
 *  Restricted rights to use, duplicate or disclose this code are
 *  granted through contract.  
 */
/***************************************************************************/
/*                                                                         */
/*     sysIdent . hpp	                                                   */
/*                                                                         */
/*     System Identification - Common definitions.			   */
/*                                                                         */
/*                                                                         */
/***************************************************************************/

// Include files definition:
#ifndef _SYSIDENT_HPP_
#define _SYSIDENT_HPP_

#include "global_def.h"
#include "MatrixCal.hpp"
#include <cstdlib>


// Declaration of the system identification paramethers:

#define STATISTIC_IS_IMPLEMENTED 1  // for no statistic 0

#define NxT NxTs    // the lenght beatwen two decision t=N*T
#define inv_NxT   1/NxT // 1/NxT for faster calculation NxT (leng for statistic)
//-----------------------------------------------------------------------------
// Declaration of the sysID object
class sysid {  // system identification class
 	
protected:
  	Matrix psy;	// the observed system data matrix
  	float psy_v[ARMAX_dim+2];		// dimension(psy) = [(m_s+m_a+m_b+m_c) x 1] 
			// psy = [1, -y_1...-y_a, u_1...u_b, v_1...v_c]^T
  	Matrix theta;	// the estimated system paramether matrix 
  	float theta_v[ARMAX_dim+2];	        // dimension(theta) = [(m_s+m_a+m_b+m_c) x 1] 
			// theta = [x_s, x_a...x_a, x_b...x_b, x_c...x_c]^T

 	float v_n;    // model's error - residuals: v_n = y_n - psy' * theta

//-----------------------------------------------------------------------------
	
     int count_n;	// counter for the sample lenght
  	float v_qume;	// quadratic nean (moving averadge) = mean squared value
    float v_qume_sum;	//sum for  quadratic nean (moving averadge) = squared value
 	float v_maxd;	// maximum deviation from the moving averadge
  	float v_mind;	// minimum deviation from the moving averadge
  	float v_mami;	// error range: v_mami = v_maxd - v_mind 

  	float performance_index ; // generalized performance index
  	int stable_coeficients;  // Flag for stability if 1, unstable if 0
public:

	sysid(void);
	~sysid(void);

	void update_psy(float y_n, float u_n, float v_n);
	void update_psy(float y_n, float v_n);
	void update_psy(float u_n_1);
	void get_psy(Matrix *gpsy);

	void get_theta(Matrix *gtheta);
	void set_theta(Matrix *gtheta);
  
	int update_stat(void);  // Statistic calculation for the identification 
    float get_v_n(void); // residual
    float get_v_qume(void);	// quadratic nean (moving averadge) = mean squared value
	float get_v_maxd(void);	// maximum deviation from the moving averadge
	float get_v_mind(void);	// minimum deviation from the moving averadge
	float get_v_mami(void);	// error range: v_mami = v_maxd - v_mind 


	float get_performance_index(void); // generalized performance index
	int   get_stable_coeficients(void); // stable_coeficients flag 1/0
    int stable(void); // model stability chek

};
//-----------------------------------------------------------------------------

#endif // SYSIDENT_HPP


