#ifndef _MATHLIB
#define _MATHLIB

#include "IQmathLib.h"


/*

real pi_regul(real Kp_regul, real Tintegral_regul, real Tperiod_regul,
              real Minimum, real Maximum, real InpVar, real *VarIntegral);

real exp_regul(real Tperiod_regul, real Texp_regul, real InpVarCurr, real InpVarInstant);



real pid_regul2(real Kp_regul, real Tperiod_regul, real Minimum, real Maximum,
               real yk, real *uk1, real *yk1, real *yk2, real *yzad, 
               real d0, real d1, real d2);
               
real pid_regul(real Kp_regul, real Tperiod_regul, real Minimum, real Maximum,
               real yk, real *uk1, real *yk1, real *yzad, real *ek, real *ek1, real *ek2,
               real d0, real d1, real d2);

real pi_regul3(real Kp_regul, real ki_regul, real Tperiod_regul, real Minimum, real Maximum,
               real InpVar, real *InpVarPrev,  real *OutVarPrev);

real pi_regul4(real Kp_regul, real ki_regul, real Tperiod_regul, real Minimum, real Maximum,
               real InpVar, real *InpVarPrev,  real *OutVarPrev);

*/
float zad_intensiv(float StepP, float StepN, float InpVarCurr, float InpVarInstant);

_iq zad_intensiv_q(_iq StepP, _iq StepN, _iq InpVarCurr, _iq InpVarInstant);
_iq im_calc( _iq ia, _iq ib, _iq ic);

float exp_regul(float Tperiod_regul, float Texp_regul, float InpVarCurr, float InpVarInstant);
                 


float my_satur_float(float Input, float Positive, float Negative, float DeadZone);

int my_satur_int(int Input, int Positive, int Negative, int DeadZone);
long my_satur_long(long Input, long Positive, long Negative, long DeadZone);




#define exp_regul_fast(Tperiod_regul,Texp_regul,InpVarCurr,InpVarInstant)	(InpVarCurr + Tperiod_regul*(InpVarInstant-InpVarCurr)/Texp_regul)
				
typedef struct {
    _iq *data_array;
    int size_array;
    _iq (*calc)();
} RMS_CALC_ARRAY;

#define RMS_CALC_DEFAULTS { 0,0, calc_rms_array_simple}

_iq calc_rms_array_simple(RMS_CALC_ARRAY *v);

typedef struct {
    _iq16 *data_array;
    int size_array;
    int last_elem_position;
    int signal_period;
    _iq Out_rms;
    _iq (*calc)();
} RMS_CALC_ARRAY_THINNING;

#define RMS_CALC_THINNING_DEFAULTS { 0,0,0,0,0, calc_rms_array_var_period_IQ15}

_iq calc_rms_array_var_period(RMS_CALC_ARRAY_THINNING *v);
_iq calc_rms_array_var_period_IQ15(RMS_CALC_ARRAY_THINNING *v);
float fast_round(float x);
float fast_round_with_limiter(float x, float lim);
float fast_round_with_delta(float prev, float x, float delta);

_iq calc_rms(_iq input, _iq input_prev, _iq freq_signal);

#endif