/***************************************************************************/
/*                                                                         */
/*                            main program of MMAC                         */
/*                            ====================                         */
/*                                                                         */
/***************************************************************************/
//------ Start Ganping code ------  ********************************************
#include <stdio.h>

#include <stdlib.h>
#include <signal.h>

#include "apss.h" 
#include "global.h"
//------  End Gamping code ------   ********************************************


#include <iostream>
#include <new>
#include <cstring>
//#include <conio>
#include <fstream>
using namespace std;



//*****************************************************************************
#include "sysIdent.hpp"
#include "MatrixCal.hpp" 

//*****************************************************************************
// Paramether estimation:
#include "FIX.hpp"
#include "RLS.hpp"
#include "LMS.hpp"
#include "WCE.hpp"
#include "KF.hpp"
#include "FQR_RLS.hpp"
#include "IQR_RLS.hpp"
#include "QR_LMS.hpp"
#include "KFA.hpp"
  // Control algorithm
#include "H8_MPC.hpp"

//*****************************************************************************

// Global variable definitions:
    int selectionflag;

//------ Start Ganping code ------  ********************************************
    int     queueid1, queueid2;             /* msg id number */

     MSGIN1  msgin ;                         /* sampled data structure */
     MSGOUT1 msgout ;                        /* control signal structure */

     double  T_sample ;                      /* sampling time */

     long unsigned int sample_no ;            /* sample number */

     long unsigned int cycle_no =0 ;            /* Gain calculation cycle  number */

     double time_s ;  // time from the control start

     int      sampledSignal ;                 /* sampled Signals :
						      1 -> Omega
						      2 -> dPe
		 				default -> dPe        */

         long int  idnum;    /* Random number generators  meory */   
#define SAVE_1  1

     int     prtFlag ;
//  FILE *fp10;
// System order definition:
#define MMAC_H8 5
#define MO_II  2

//------  End Gamping code ------   ********************************************
// COUNSTANT DEFINITION
//Model lover simension:
#define MMAC_DC 0        // DC component from ARMAX 
#define MMAC_a MMAC_H8   // AR components from ARMAX
#define MMAC_b MMAC_H8   // MA components from ARMAX
#define MMAC_c  MMAC_H8   // X  components from ARMAX

#define H8_m MMAC_H8     // H8MPC control system_dimension = m_a or  m_b
#define H8_l MMAC_H8     // H8MPC # of system disturbances 
#define H8_r 1           // H8MPC # of system control input 
#define H8_p H8_m + H8_r // H8MPC # of system controlled output
#define H8_q 1           // H8MPC # of system measured outputs 

  
#define ALG2ND 0  

#if ALG2ND
// Model dimension for  control 2nd:
#define MMAC_11 1     // DC component from ARMAX 
#define MMAC_aa MO_II    // AR components from ARMAX
#define MMAC_bb MO_II  // MA components from ARMAX
#define MMAC_cc 0     // X  components from ARMAX
#endif


    FILE   *fp_H8;
    FILE   *fp_H8x;
  int icomments=0;

int main()
{
  //   long int idnum[6];    /* Random number generators  meory */
   double   ut, yt ;                        /* sampled data at time t */
  float yt_filt;
     float ftemp,ftemp1, ftemp2, ftemp3, ftemp4, ftemp5;
     int itemp1, itemp5;
     int iflag_disturbance;   //Disturbance event signaling
     float performanceindex,performanceindex2 ;  
     enum algorithms_1 {Nan_1,FIX_1,RLS_1,LMS_1,WCE_1,KF_1,FQR_1,IQR_1,QRL_1,KFA_1};
     enum algorithms_1 sel_1;

#if ALG2ND
     enum algorithms_2 {Nan_2,FIX_2,RLS_2,LMS_2,WCE_2,KF_2,FQR_2,IQR_2,QRL_2,KFA_2};
     enum algorithms_2 sel_2;
#endif
     
     float gama1=1.0; 

     Matrix Mtemp;
     Matrix Mtemp2;
     Matrix Mtemp3;


//------ Start Ganping code ------  ********************************************
     char    Ftyp[] = "dat" ;
    double ttemp;
    void	on_signal(int) ; 
    void	initall(void) ;
    void	sampling(double *yt, double *ut );
    void	control(float) ;
    int i, itemp ;

  
    char S_FIX_1[16] ;
    char S_RLS_1[16] ;
    char S_LMS_1[16] ;
    char S_WCE_1[16] ;
    char S_KF_1[16] ;
    char S_FQR_1[16] ;
    char S_IQR_1[16] ;
    char S_QRL_1[16] ;
    char S_KFA_1[16] ;

    char U_FIX_1[16] ;
    char U_RLS_1[16] ;
    char U_LMS_1[16] ;
    char U_WCE_1[16] ;
    char U_KF_1[16] ;
    char U_FQR_1[16] ;
    char U_IQR_1[16] ;
    char U_QRL_1[16] ;
    char U_KFA_1[16] ;

#if ALG2ND
    char S_FIX_2[16] ;
    char S_RLS_2[16] ;
    char S_LMS_2[16] ;
    char S_WCE_2[16] ;
    char S_KF_2[16] ;
    char S_FQR_2[16] ;
    char S_IQR_2[16] ;
    char S_QRL_2[16] ;
    char S_KFA_2[16] ; 

    char U_FIX_2[16] ;
    char U_RLS_2[16] ;
    char U_LMS_2[16] ;
    char U_WCE_2[16] ;
    char U_KF_2[16] ;
    char U_FQR_2[16] ;
    char U_IQR_2[16] ;
    char U_QRL_2[16] ;
    char U_KFA_2[16] ;
#endif

    char C_H8[16] ; 
    char C_H8x[16] ; 

  

    FILE         *fp_FIX_1;
    FILE         *fp_RLS_1;
    FILE         *fp_LMS_1; 
    FILE         *fp_WCE_1;
    FILE         *fp_KF_1;
    FILE         *fp_FQR_1;
    FILE         *fp_IQR_1;
    FILE         *fp_QRL_1;
    FILE         *fp_KFA_1;

    FILE         *fp_FIX_U1;
    FILE         *fp_RLS_U1;
    FILE         *fp_LMS_U1; 
    FILE         *fp_WCE_U1;
    FILE         *fp_KF_U1;
    FILE         *fp_FQR_U1;
    FILE         *fp_IQR_U1;
    FILE         *fp_QRL_U1;
    FILE         *fp_KFA_U1;


#if ALG2ND
    FILE         *fp_FIX_2;
    FILE         *fp_RLS_2;
    FILE         *fp_LMS_2; 
    FILE         *fp_WCE_2;
    FILE         *fp_KF_2;
    FILE         *fp_FQR_2;
    FILE         *fp_IQR_2;
    FILE         *fp_QRL_2;
    FILE         *fp_KFA_2;

    FILE         *fp_FIX_U2;
    FILE         *fp_RLS_U2;
    FILE         *fp_LMS_U2; 
    FILE         *fp_WCE_U2;
    FILE         *fp_KF_U2;
    FILE         *fp_FQR_U2;
    FILE         *fp_IQR_U2;
    FILE         *fp_QRL_U2;
    FILE         *fp_KFA_U2;
#endif 
   
   
    
	
    signal(SIGUSR1, on_signal);
    // pidnum = idnum;
    initall() ;              // Sysytem initialization

    idnum  = msgout.mtype * 585 * msgout.mtype * msgout.mtype * msgout.mtype ;  // Random number distintive initialization
 
    // printf("\n >>>>  msgout.mtype = %li,  idnum  = %li  ||| \n",msgout.mtype, idnum);  // PRINT for test


    sprintf(S_FIX_1, "S_FIX_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_RLS_1, "S_RLS_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_LMS_1, "S_LMS_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_WCE_1, "S_WCE_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_KF_1, "S_KF_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_FQR_1, "S_FQR_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_IQR_1, "S_IQR_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_QRL_1, "S_QRL_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_KFA_1, "S_KFA_1.%d.%s",msgout.mtype,Ftyp) ;

    sprintf(U_FIX_1, "U_FIX_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_RLS_1, "U_RLS_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_LMS_1, "U_LMS_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_WCE_1, "U_WCE_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_KF_1, "U_KF_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_FQR_1, "U_FQR_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_IQR_1, "U_IQR_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_QRL_1, "U_QRL_1.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_KFA_1, "U_KFA_1.%d.%s",msgout.mtype,Ftyp) ;
   


#if ALG2ND
    sprintf(S_FIX_2, "S_FIX_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_RLS_2, "S_RLS_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_LMS_2, "S_LMS_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_WCE_2, "S_WCE_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_KF_2, "S_KF_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_FQR_2, "S_FQR_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_IQR_2, "S_IQR_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_QRL_2, "S_QRL_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(S_KFA_2, "S_KFA_2.%d.%s",msgout.mtype,Ftyp) ; 

    sprintf(U_FIX_2, "U_FIX_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_RLS_2, "U_RLS_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_LMS_2, "U_LMS_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_WCE_2, "U_WCE_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_KF_2, "U_KF_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_FQR_2, "U_FQR_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_IQR_2, "U_IQR_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_QRL_2, "U_QRL_2.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(U_KFA_2, "U_KFA_2.%d.%s",msgout.mtype,Ftyp) ;
#endif


    sprintf(C_H8,  "C_H8.%d.%s",msgout.mtype,Ftyp) ;
    sprintf(C_H8x, "C_H8x.%d.%s",msgout.mtype,Ftyp) ;

 
    // For performance evaluation:

    fp_FIX_1 = fopen(S_FIX_1, "w") ;
    fp_RLS_1 = fopen(S_RLS_1, "w") ;
    fp_LMS_1= fopen(S_LMS_1, "w") ;
    fp_WCE_1= fopen(S_WCE_1, "w") ;
    fp_KF_1 = fopen(S_KF_1, "w") ;
    fp_FQR_1= fopen(S_FQR_1, "w") ;
    fp_IQR_1 = fopen(S_IQR_1, "w") ;
    fp_QRL_1= fopen(S_QRL_1, "w") ;
    fp_KFA_1 = fopen(S_KFA_1, "w") ;

    fp_FIX_U1 = fopen(U_FIX_1, "w") ;
    fp_RLS_U1 = fopen(U_RLS_1, "w") ;
    fp_LMS_U1= fopen(U_LMS_1, "w") ;
    fp_WCE_U1= fopen(U_WCE_1, "w") ;
    fp_KF_U1 = fopen(U_KF_1, "w") ;
    fp_FQR_U1= fopen(U_FQR_1, "w") ;
    fp_IQR_U1 = fopen(U_IQR_1, "w") ;
    fp_QRL_U1= fopen(U_QRL_1, "w") ;
    fp_KFA_U1 = fopen(U_KFA_1, "w") ;

    
#if ALG2ND
    fp_FIX_2 = fopen(S_FIX_2, "w") ;
    fp_RLS_2 = fopen(S_RLS_2, "w") ;
    fp_LMS_2= fopen(S_LMS_2, "w") ;
    fp_WCE_2= fopen(S_WCE_2, "w") ;
    fp_KF_2 = fopen(S_KF_2, "w") ;
    fp_FQR_2= fopen(S_FQR_2, "w") ;
    fp_IQR_2= fopen(S_IQR_2, "w") ;
    fp_QRL_2= fopen(S_QRL_2, "w") ;
    fp_KFA_2 = fopen(S_KFA_2, "w") ; 
  
    fp_FIX_U2 = fopen(U_FIX_2, "w") ;
    fp_RLS_U2 = fopen(U_RLS_2, "w") ;
    fp_LMS_U2= fopen(U_LMS_2, "w") ;
    fp_WCE_U2= fopen(U_WCE_2, "w")   ;
    fp_KF_U2 = fopen(U_KF_2, "w") ;
    fp_FQR_U2= fopen(U_FQR_2, "w") ;
    fp_IQR_U2= fopen(U_IQR_2, "w") ;
    fp_QRL_U2= fopen(U_QRL_2, "w") ;
    fp_KFA_U2 = fopen(U_KFA_2, "w") ;
#endif


    fp_H8 = fopen(C_H8, "w") ;
    fp_H8x = fopen(C_H8x, "w") ;




    //cout << "***   MMAC  Starts **";

//==============================================================================
// System identification:    
//-----------------------------------------------------------------------------
#if not  ALG2ND
// 0th  algorithm:
 FIX_alg FIX[1]= { FIX_alg(MMAC_DC,MMAC_a,MMAC_b,MMAC_c)};
//-----------------------------------------------------------------------------
// 1st algorithm:
 RLS_alg RLS[1]= { RLS_alg((float)0.995, MMAC_DC,MMAC_a,MMAC_b,MMAC_c) }; //0.98
//-----------------------------------------------------------------------------
// 2nd algorithm:  
 LMS_alg LMS[1]= { LMS_alg((float)0.7,MMAC_DC,MMAC_a,MMAC_b,MMAC_c) }; //0.7
 //-----------------------------------------------------------------------------
 // 3th algorithm:  
 WCE_alg WCE[1]= { WCE_alg((float)1e+2,MMAC_DC,MMAC_a,MMAC_b,MMAC_c) }; // 800.0 pe
 //-----------------------------------------------------------------------------
 // 4th algorithm:  
 KF_alg KF[1]= { KF_alg((float)0.1,0.1,MMAC_DC,MMAC_a,MMAC_b,MMAC_c) }; //0.1,0.1
 //-----------------------------------------------------------------------------
 // 5th algorithm:  
 FQR_RLS_alg FQR_RLS[1]= { FQR_RLS_alg((float)0.995, MMAC_DC,MMAC_a,MMAC_b,MMAC_c) };//0.9991
 //-----------------------------------------------------------------------------
 // 6th algorithm:  
 IQR_RLS_alg IQR_RLS[1]= { IQR_RLS_alg((float)0.985,MMAC_DC,MMAC_a,MMAC_b,MMAC_c) }; //0.989
 //-----------------------------------------------------------------------------
 // 7th algorithm:  
 QR_LMS_alg QR_LMS[1]= { QR_LMS_alg(MMAC_DC,MMAC_a,MMAC_b,MMAC_c)};//
 //-----------------------------------------------------------------------------
 // 8th algorithm:                  R    Q
 KFA_alg KFA[1]= { KFA_alg((float) 0.1,0.1,MMAC_DC,MMAC_a,MMAC_b,MMAC_c) }; // 0.3,0.15 for pe
//-----------------------------------------------------------------------------
#endif

#if ALG2ND
// 0th  algorithm:
 FIX_alg FIX[2]= { FIX_alg(MMAC_DC,MMAC_a,MMAC_b,MMAC_c), 
		   FIX_alg(MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
//-----------------------------------------------------------------------------
// 1st algorithm:
 RLS_alg RLS[2]= { RLS_alg((float)0.98,MMAC_DC,MMAC_a,MMAC_b,MMAC_c), 
		   RLS_alg((float)0.98,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
//-----------------------------------------------------------------------------
// 2nd algorithm:  
 LMS_alg LMS[2]= { LMS_alg((float)0.7,MMAC_DC,MMAC_a,MMAC_b,MMAC_c), 
		   LMS_alg((float)0.7,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
 //-----------------------------------------------------------------------------
 // 3th algorithm:  
 WCE_alg WCE[2]= { WCE_alg((float)800.0,MMAC_DC,MMAC_a,MMAC_b,MMAC_c),   // d_omega
		   WCE_alg((float)45.1,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) }; // pe
 //-----------------------------------------------------------------------------
 // 4th algorithm:  
 KF_alg KF[2]= { KF_alg((float)0.1,0.1,MMAC_DC,MMAC_a,MMAC_b,MMAC_c), 
		 KF_alg((float)0.1,0.1,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
 //-----------------------------------------------------------------------------
 // 5th algorithm:  
 FQR_RLS_alg FQR_RLS[2]= { FQR_RLS_alg((float)0.9991,MMAC_DC,MMAC_a,MMAC_b,MMAC_c),
                       FQR_RLS_alg((float)0.988,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
 //-----------------------------------------------------------------------------
 // 6th algorithm:  
 IQR_RLS_alg IQR_RLS[2]= { IQR_RLS_alg((float)0.989,MMAC_DC,MMAC_a,MMAC_b,MMAC_c), 
			   IQR_RLS_alg((float)0.989,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
 //-----------------------------------------------------------------------------
 // 7th algorithm:  
 QR_LMS_alg QR_LMS[2]= { QR_LMS_alg(MMAC_DC,MMAC_a,MMAC_b,MMAC_c), 
			 QR_LMS_alg(MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) };
 //-----------------------------------------------------------------------------
 // 8th algorithm:                  R    Q
 KFA_alg KFA[2]= { KFA_alg((float) 0.31295,0.01595,MMAC_DC,MMAC_a,MMAC_b,MMAC_c),  // for d_omega
		   KFA_alg((float)0.1,0.1,MMAC_11,MMAC_aa,MMAC_bb,MMAC_cc) }; // for pe
//-----------------------------------------------------------------------------
#endif



 
//-----------------------------------------------------------------------------
// Control Algorithm:
//-----------------------------------------------------------------------------
 

 H8MPC_alg H8MPC[1] = {H8MPC_alg( MMAC_DC,MMAC_a,MMAC_b,MMAC_c,H8_l,H8_r,H8_p,H8_q) };
                     
                                                   
//-----------------------------------------------------------------------------

	matrix_alloc(&Mtemp, 35, 35);	// Temporary matrix:
	matrix_alloc(&Mtemp2, 35, 35);	// Temporary matrix:
	matrix_alloc(&Mtemp3, 35, 35);	// Temporary matrix:


	H8MPC[0].weightSet();

        sel_1 = FIX_1;   // <---initialization  for ploting only

#if ALG2ND
	sel_2 = FIX_2;
#endif


//-----------------------------------------------------------------------------
// End of initialisadion, The recursion is starts:
//-----------------------------------------------------------------------------
 

while ( 1 ) 

 {
     time_s = T_sample * sample_no; 
     
     sampling(&yt,&ut) ;

     // yt_filt = yt;                  
     yt_filt = FIX[0].DCfilter(yt) ;   //  <<<-----------!!!
    
  

     // if(time_s < 300.0) {  /// <-- time limit 

   
     //-----------------------------------------------------------------------------
     // Parameter estimation:
     //-----------------------------------------------------------------------------
     // 1st  algorithm:
     FIX[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 2nd algorithm:
     RLS[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 3th algorithm:  
     LMS[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 4th algorithm:  
     WCE[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 5th algorithm:  
     KF[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 6th algorithm:  
     FQR_RLS[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 7th algorithm:  
     IQR_RLS[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 8th algorithm:
     QR_LMS[0].update(yt_filt, ut);
     //------------------------------------------------------------------------
     // 9th algorithm:  
     KFA[0].update(yt_filt, ut);
     //-----------------------------------------------------------------------------


#if ALG2ND
     //-----------------------------------------------------------------------------
     // Parameter estimation:
     //-----------------------------------------------------------------------------
     // 1st  algorithm:
     FIX[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 2nd algorithm:
     RLS[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 3th algorithm:  
     LMS[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 4th algorithm:  
     WCE[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 5th algorithm:  
      KF[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 6th algorithm:  
      FQR_RLS[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 7th algorithm:  
     IQR_RLS[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
     // 8th algorithm:  
     QR_LMS[1].update(yt_filt, ut);
     //------------------------------------------------------------------------
     // 9th algorithm:  
     KFA[1].update(yt_filt, ut);
     //-----------------------------------------------------------------------------
#endif





     //-----------------------------------------------------------------------------
     // Parameter evaluation 1:
     //-----------------------------------------------------------------------------
     
     if(selectionflag)
     {
	 performanceindex = FIX[0].get_performance_index(); sel_1 = FIX_1;
	 
	
	 if(performanceindex > RLS[0].get_performance_index())   
            {performanceindex = RLS[0].get_performance_index(); sel_1 = RLS_1;}
	 
	
	  if(performanceindex > LMS[0].get_performance_index()) 
            {performanceindex = LMS[0].get_performance_index(); sel_1 = LMS_1;}

	  
	  if(performanceindex > WCE[0].get_performance_index())  
            {performanceindex = WCE[0].get_performance_index(); sel_1 = WCE_1;}

	 
	 if(performanceindex > KF[0].get_performance_index()) 
            {performanceindex = KF[0].get_performance_index(); sel_1 = KF_1;}

	 
	 if(performanceindex > FQR_RLS[0].get_performance_index())  
            {performanceindex = FQR_RLS[0].get_performance_index(); sel_1 = FQR_1;}

	 
	  if(performanceindex > IQR_RLS[0].get_performance_index())
            {performanceindex = IQR_RLS[0].get_performance_index(); sel_1 = IQR_1;}

	
	 if(performanceindex > QR_LMS[0].get_performance_index())
            {performanceindex = QR_LMS[0].get_performance_index(); sel_1 = QRL_1;}

	 
	 if(performanceindex > KFA[0].get_performance_index())
            {performanceindex = KFA[0].get_performance_index(); sel_1 = KFA_1;}

	
        
	 switch (sel_1)
	 {
	     case  FIX_1:  //FIX (privious values)
		 //printf("\n FIX"); 
		 break;
	
	     case  RLS_1: 
		 RLS[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n RLS"); 
		 break;

	     case  LMS_1:  
		 LMS[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n LMS"); 
		 break; 

	     case WCE_1:
		 WCE[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n WCE"); 
		 break;
 
	     case KF_1: 
		 KF[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n KF"); 
		 break; 
	     case FQR_1:
		 FQR_RLS[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n FQR_RLS"); 
		 break; 

	     case IQR_1:  
		 IQR_RLS[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n IQR_RLS"); 
		 break;

	     case QRL_1: 
		 QR_LMS[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n QR_LMS"); 
		 break;

	     case  KFA_1: 
		 KFA[0].get_theta(&Mtemp);
		 FIX[0].set_theta(&Mtemp);
		 //printf("\n KFA"); 
		 break; 
    
	     default:  ;//FIX (privious values)
	        
	 }  

	 FIX[0].get_theta(&Mtemp); //For the defoult and zero case !!

         // Real Time update the control paramethers in N*T period once:
	 // H_infinity MPC  control
	 H8MPC[0].update(&Mtemp, gama1);

	 //  printf("\n ||| msgout.mtype = %li,  idnum  = %li  ||| \n",msgout.mtype, idnum);  // PRINT for test
     }


     //-----------------------------------------------------------------------------
     // Parameter evaluation 2:
     //-----------------------------------------------------------------------------
#if ALG2ND 
     if(selectionflag)
     {
	 performanceindex2 = FIX[1].get_performance_index(); sel_2 = FIX_2;
	 
	
	 if((performanceindex2 > RLS[1].get_performance_index()) & RLS[1].stable())  
            {performanceindex2 = RLS[1].get_performance_index(); sel_2 = RLS_2;}
	 
	
	  if((performanceindex2 > LMS[1].get_performance_index()) &  LMS[1].stable())
            {performanceindex2 = LMS[1].get_performance_index(); sel_2 = LMS_2;}

	  
	  if((performanceindex2 > WCE[1].get_performance_index()) & WCE[1].stable())  
            {performanceindex2 = WCE[1].get_performance_index(); sel_2 = WCE_2;}

	 
	 if((performanceindex2 > KF[1].get_performance_index()) & KF[1].stable())
            {performanceindex2 = KF[1].get_performance_index(); sel_2 = KF_2;}

	 
	 if((performanceindex2 > FQR_RLS[1].get_performance_index()) & FQR_RLS[1].stable()) 
            {performanceindex2 = FQR_RLS[1].get_performance_index(); sel_2 = FQR_2;}

	 
	  if((performanceindex2 > IQR_RLS[1].get_performance_index()) & IQR_RLS[1].stable())
            {performanceindex2 = IQR_RLS[1].get_performance_index(); sel_2 = IQR_2;}

	
	 if((performanceindex2 > QR_LMS[1].get_performance_index()) & QR_LMS[1].stable())
            {performanceindex2 = QR_LMS[1].get_performance_index(); sel_2 = QRL_2;}

	 
	 if((performanceindex2 > KFA[1].get_performance_index()) &  KFA[1].stable())
            {performanceindex2 = KFA[1].get_performance_index(); sel_2 = KFA_2;}


        
	 switch (sel_2)
	 {
	     case  FIX_2:  //FIX (privious values)
		 //printf("\n FIX"); 
		 break;
	
	     case  RLS_2: 
		 RLS[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n RLS"); 
		 break;

	     case  LMS_2:  
		 LMS[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n LMS"); 
		 break; 

	     case WCE_2:
		 WCE[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n WCE"); 
		 break;
 
	     case KF_2: 
		 KF[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n KF"); 
		 break; 
	     case FQR_2:
		 FQR_RLS[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n FQR_RLS"); 
		 break; 

	     case IQR_2:  
		 IQR_RLS[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n IQR_RLS"); 
		 break;

	     case QRL_2: 
		 QR_LMS[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n QR_LMS"); 
		 break;

	     case  KFA_2: 
		 KFA[1].get_theta(&Mtemp);
		 FIX[1].set_theta(&Mtemp);
		 //printf("\n KFA"); 
		 break; 
    
	     default:  ;//FIX (privious values)
	        
	 }  

	 FIX[1].get_theta(&Mtemp); //For the defoult and zero case !!

        
     }


#endif

     // } // <--- if time limit


     //-----------------------------------------------------------------------------
     // Parameter :
     //-----------------------------------------------------------------------------
     // Control Signal Calculations:
     //=========================================================================
     
     // Signal preparation for control:
     

   
     // H_infinity control:

    
     ftemp = H8MPC[0].rtm_control(yt_filt,ut);

     

     //ftemp = 0;
      //---------------------------------------------------------------------------------  
   
     // if (ftemp >0.1) ftemp=0.1;
     // else if (ftemp < -0.1) ftemp=-0.1;
     
     //printf("\n yt: %f ut: %f",yt,ut);

     control(ftemp) ; 

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@


      //-------------------------------------------------------------------------
     // RESULS FOR EVALUATION:
     //-------------------------------------------------------------------------
     // 0th algorithm:
     FIX[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_FIX_1,"# FIX_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_FIX_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_FIX_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_FIX_1," \n" );
     //-------------------------------------------------------------------------
     // 1st algorithm:
     RLS[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_RLS_1,"# RLS_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_RLS_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_RLS_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_RLS_1," \n" );
     //-------------------------------------------------------------------------
     // 2nd algorithm:
     LMS[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_LMS_1,"# LMS_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_LMS_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_LMS_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_LMS_1," \n" );
      //-------------------------------------------------------------------------
     // 3th algorithm:
     WCE[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_WCE_1,"# WCE_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_WCE_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_WCE_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_WCE_1," \n" );
      //-------------------------------------------------------------------------
     // 4th algorithm:
     KF[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_KF_1,"# KF_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_KF_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_KF_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_KF_1," \n" );
      //-------------------------------------------------------------------------
     // 5th algorithm:
     FQR_RLS[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_FQR_1,"# FQR_RLS_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_FQR_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_FQR_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_FQR_1," \n" );
      //-------------------------------------------------------------------------
     // 6th algorithm:
     IQR_RLS[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_IQR_1,"# IQR_RLS_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_IQR_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_IQR_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_IQR_1," \n" );
     //-------------------------------------------------------------------------
     // 7th algorithm:
     QR_LMS[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_QRL_1,"# QR_LMS_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_QRL_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_QRL_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_QRL_1," \n" );
     //-------------------------------------------------------------------------
     // 8th algorithm:
     KFA[0].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_KFA_1,"# KF_1  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_KFA_1," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_KFA_1," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_KFA_1," \n" );
      //-------------------------------------------------------------------------

     //-------------------------------------------------------------------------
     //-------------------------------------------------------------------------
 #if ALG2ND
  //-------------------------------------------------------------------------
     // 1st algorithm:
     RLS[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_RLS_2,"# RLS_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_RLS_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_RLS_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_RLS_2," \n" );
     //-------------------------------------------------------------------------
     // 2nd algorithm:
     LMS[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_LMS_2,"# LMS_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_LMS_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_LMS_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_LMS_2," \n" );
      //-------------------------------------------------------------------------
     // 3th algorithm:
     WCE[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_WCE_2,"# WCE_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_WCE_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_WCE_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_WCE_2," \n" );
      //-------------------------------------------------------------------------
     // 4th algorithm:
     KF[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_KF_2,"# KF_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_KF_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_KF_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_KF_2," \n" );
      //-------------------------------------------------------------------------
     // 5th algorithm:
     FQR_RLS[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_FQR_2,"# FQR_RLS_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_FQR_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_FQR_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_FQR_2," \n" );
      //-------------------------------------------------------------------------
     // 6th algorithm:
     IQR_RLS[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_IQR_2,"# IQR_RLS_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_IQR_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_IQR_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_IQR_2," \n" );
     //-------------------------------------------------------------------------
     // 7th algorithm:
     QR_LMS[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_QRL_2,"# QR_LMS_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_QRL_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_QRL_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_QRL_2," \n" );
     //-------------------------------------------------------------------------
     // 8th algorithm:
     KFA[1].get_theta(&Mtemp);
     if(icomments == 0 ) fprintf(fp_KFA_2,"# KFA_2  theta[1] ... theta[%i] \n",Mtemp.row);
     fprintf(fp_KFA_2," %f ", time_s );
     for(i=1; Mtemp.row >= i;i++) fprintf(fp_KFA_2," %f ", Mtemp.mtx[i][1] );
     fprintf(fp_KFA_2," \n" );
      //-------------------------------------------------------------------------
     //-------------------------------------------------------------------------

#endif

     //------------------------------------------------------------------------- 
     // Parameter EVALUATION
     //-------------------------------------------------------------------------
     // 0th algorithm:
     if(icomments == 0 ) fprintf(fp_FIX_U1,"#time, v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_FIX_U1," %f ", time_s );
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_n());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_moav());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_qume());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_vari());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_maxd());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_mind());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_v_mami());
     fprintf(fp_FIX_U1," %g ", FIX[0].get_theta_frob());
      fprintf(fp_FIX_U1," %g ", FIX[0].get_performance_index());
      fprintf(fp_FIX_U1," %d ",sel_1); // <- The selected algorithma <<<
     fprintf(fp_FIX_U1," \n" );
    //-------------------------------------------------------------------------
     // 1st algorithm:
     if(icomments == 0 ) fprintf(fp_RLS_U1,"#time, v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_RLS_U1," %f ", time_s );
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_n());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_moav());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_qume());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_vari());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_maxd());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_mind());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_v_mami());
      fprintf(fp_RLS_U1," %g ", RLS[0].get_theta_frob());
     fprintf(fp_RLS_U1," %g ", RLS[0].get_performance_index());
     fprintf(fp_RLS_U1," \n" );
    //-------------------------------------------------------------------------
#if ALG2ND
     // 1st algorithm:
     if(icomments == 0 ) fprintf(fp_RLS_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_RLS_U2," %f ", time_s );
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_n());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_moav());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_qume());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_vari());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_maxd());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_mind());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_v_mami());
      fprintf(fp_RLS_U2," %g ", RLS[1].get_theta_frob());
     fprintf(fp_RLS_U2," %g ", RLS[1].get_performance_index());
     fprintf(fp_RLS_U2," \n" );
#endif 
    //-------------------------------------------------------------------------
     // 2nd algorithm:
     if(icomments == 0 ) fprintf(fp_LMS_U1,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_LMS_U1," %f ", time_s );
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_n());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_moav());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_qume());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_vari());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_maxd());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_mind());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_v_mami());
      fprintf(fp_LMS_U1," %g ", LMS[0].get_theta_frob());
     fprintf(fp_LMS_U1," %g ", LMS[0].get_performance_index());
     fprintf(fp_LMS_U1," \n" );
     //-------------------------------------------------------------------------
#if ALG2ND
     // 2nd algorithm:
     if(icomments == 0 ) fprintf(fp_LMS_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_LMS_U2," %f ", time_s );
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_n());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_moav());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_qume());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_vari());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_maxd());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_mind());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_v_mami());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_theta_frob());
     fprintf(fp_LMS_U2," %g ", LMS[1].get_performance_index());
     fprintf(fp_LMS_U2," \n" );
#endif 
    //-------------------------------------------------------------------------
    // 3th algorithm:  
     if(icomments == 0 ) fprintf(fp_WCE_U1,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
   
     fprintf(fp_WCE_U1," %f ", time_s );
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_n());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_moav());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_qume());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_vari());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_maxd());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_mind());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_v_mami());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_theta_frob());
     fprintf(fp_WCE_U1," %g ", WCE[0].get_performance_index());
     fprintf(fp_WCE_U1," \n" );
     //-------------------------------------------------------------------------  
#if ALG2ND
 // 3th algorithm:  
     if(icomments == 0 ) fprintf(fp_WCE_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
   
     fprintf(fp_WCE_U2," %f ", time_s );
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_n());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_moav());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_qume());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_vari());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_maxd());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_mind());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_v_mami());
      fprintf(fp_WCE_U2," %g ", WCE[1].get_theta_frob());
     fprintf(fp_WCE_U2," %g ", WCE[1].get_performance_index());
     fprintf(fp_WCE_U2," \n" );
#endif
     //-------------------------------------------------------------------------
     // 4th algorithm:
     if(icomments == 0 ) fprintf(fp_KF_U1,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");

     fprintf(fp_KF_U1," %f ", time_s );
     fprintf(fp_KF_U1," %g ", KF[0].get_v_n());
     fprintf(fp_KF_U1," %g ", KF[0].get_v_moav());
     fprintf(fp_KF_U1," %g ", KF[0].get_v_qume());
     fprintf(fp_KF_U1," %g ", KF[0].get_v_vari());
     fprintf(fp_KF_U1," %g ", KF[0].get_v_maxd());
     fprintf(fp_KF_U1," %g ", KF[0].get_v_mind());
     fprintf(fp_KF_U1," %g ", KF[0].get_v_mami());
     fprintf(fp_KF_U1," %g ", KF[0].get_theta_frob());
     fprintf(fp_KF_U1," %g ", KF[0].get_performance_index());
     fprintf(fp_KF_U1," \n" );
     //-------------------------------------------------------------------------     
     // 4th algorithm:
#if ALG2ND
     if(icomments == 0 ) fprintf(fp_KF_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");

     fprintf(fp_KF_U2," %f ", time_s );
     fprintf(fp_KF_U2," %g ", KF[1].get_v_n());
     fprintf(fp_KF_U2," %g ", KF[1].get_v_moav());
     fprintf(fp_KF_U2," %g ", KF[1].get_v_qume());
     fprintf(fp_KF_U2," %g ", KF[1].get_v_vari());
     fprintf(fp_KF_U2," %g ", KF[1].get_v_maxd());
     fprintf(fp_KF_U2," %g ", KF[1].get_v_mind());
     fprintf(fp_KF_U2," %g ", KF[1].get_v_mami());
     fprintf(fp_KF_U2," %g ", KF[1].get_theta_frob());
     fprintf(fp_KF_U2," %g ", KF[1].get_performance_index());
     fprintf(fp_KF_U2," \n" );
#endif
     //-------------------------------------------------------------------------
     // 5th algorithm:
     if(icomments == 0 ) fprintf(fp_FQR_U1,"#v_n,v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_FQR_U1," %f ", time_s );
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_n());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_moav());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_qume());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_vari());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_maxd());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_mind());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_v_mami());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_theta_frob());
     fprintf(fp_FQR_U1," %g ", FQR_RLS[0].get_performance_index());
     fprintf(fp_FQR_U1," \n" );
     //-------------------------------------------------------------------------
#if ALG2ND
     // 5th algorithm:
     if(icomments == 0 ) fprintf(fp_FQR_U2,"#v_n,v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_FQR_U2," %f ", time_s );
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_n());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_moav());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_qume());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_vari());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_maxd());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_mind());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_v_mami());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_theta_frob());
     fprintf(fp_FQR_U2," %g ", FQR_RLS[1].get_performance_index());
     fprintf(fp_FQR_U2," \n" );
#endif 
    //-------------------------------------------------------------------------
     // 6th algorithm:
     if(icomments == 0 ) fprintf(fp_IQR_U1,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_IQR_U1," %f ", time_s );
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_n());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_moav());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_qume());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_vari());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_maxd());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_mind());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_v_mami());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_theta_frob());
     fprintf(fp_IQR_U1," %g ", IQR_RLS[0].get_performance_index());
     fprintf(fp_IQR_U1," \n" );
     //-------------------------------------------------------------------------
#if ALG2ND
     // 6th algorithm:
     if(icomments == 0 ) fprintf(fp_IQR_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_IQR_U2," %f ", time_s );
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_n());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_moav());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_qume());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_vari());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_maxd());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_mind());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_v_mami());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_theta_frob());
     fprintf(fp_IQR_U2," %g ", IQR_RLS[1].get_performance_index());
     fprintf(fp_IQR_U2," \n" );
#endif  
   //-------------------------------------------------------------------------
     // 7th algorithm:
     if(icomments == 0 ) fprintf(fp_QRL_U1,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_QRL_U1," %f ", time_s );
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_n());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_moav());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_qume());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_vari());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_maxd());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_mind());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_v_mami());
      fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_theta_frob());
     fprintf(fp_QRL_U1," %g ", QR_LMS[0].get_performance_index());
     fprintf(fp_QRL_U1," \n" );
     //-------------------------------------------------------------------------
#if ALG2ND
     // 7th algorithm:
     if(icomments == 0 ) fprintf(fp_QRL_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");
     fprintf(fp_QRL_U2," %f ", time_s );
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_n());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_moav());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_qume());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_vari());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_maxd());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_mind());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_v_mami());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_theta_frob());
     fprintf(fp_QRL_U2," %g ", QR_LMS[1].get_performance_index());
     fprintf(fp_QRL_U2," \n" );
#endif
    //-------------------------------------------------------------------------
     // 8th algorithm:
     if(icomments == 0 ) fprintf(fp_KFA_U1,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");

     fprintf(fp_KFA_U1," %f ", time_s );
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_n());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_moav());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_qume());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_vari());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_maxd());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_mind());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_v_mami());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_theta_frob());
     fprintf(fp_KFA_U1," %g ", KFA[0].get_performance_index());
     fprintf(fp_KFA_U1," \n" );
     //-------------------------------------------------------------------------     
#if ALG2ND
     // 8th algorithm:
     if(icomments == 0 ) fprintf(fp_KF_U2,"#v_n, v_moav, v_qume, v_vari, v_maxd, v_mind, v_mami, thta_fob, performance_index \n");

     fprintf(fp_KFA_U2," %f ", time_s );
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_n());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_moav());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_qume());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_vari());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_maxd());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_mind());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_v_mami());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_theta_frob());
     fprintf(fp_KFA_U2," %g ", KFA[1].get_performance_index());
     fprintf(fp_KFA_U2," \n" );
     //-------------------------------------------------------------------------
#endif

     //-------------------------------------------------------------------------
     // END of RESULS FOR EVALUATION:
     //-------------------------------------------------------------------------
#if 0	
     if ( sample_no >  1)  
		  { 
		    ttemp = sample_no;
		    ttemp = ( ttemp - 800 ) ;
		    fprintf(ff,"%f  %f  %f  %f\n", 
			    ttemp, 
			    msgin.datain.Omega, 
			    msgin.datain.dOmega,
			    msgout.dataout.Upss);
		   }  
#endif
     
     if (icomments > 30 ) icomments = 0;
     else icomments++; 

 } // end of while(1) loop



 return 0;
}
//=============================================================================




//------ Start Ganping code ------  ********************************************

void	on_signal( int)
{
	exit(10);
}

//------  End Gamping code ------   ********************************************













