void RunIIRPolyForm1( float *Signal, float *FilteredSignal, int NumSigPts);
void RunIIRPolyForm1Init(void);





#define NCoef 2
float BCoef[NCoef+1] = {
		 0.060328887082885377, 0, -0.060328887082885377
};

float ACoef[NCoef+1] = {
                1,    -1.8034543081097714  ,    0.87934222583422916
};





#define NCoef2 2
float32_t ACoef2[NCoef2+1] = {
		0.060328887082885377, 0, -0.060328887082885377
};





float32_t BCoef2[NCoef2+1] = {
		   1,    -1.8034543081097714  ,    0.87934222583422916

};






#define REG_SIZE 100
//double ACoef[REG_SIZE], Bcoef[REG_SIZE];

float RegX[REG_SIZE], RegY[REG_SIZE], CenterTap;


void RunIIRPolyForm1Init(void)
{
	int i;

	  for(i=0; i<REG_SIZE; i++){
		  RegX[i] = RegY[i]= 0.0;  // Init the delay registers.
	  	  }
}

void RunIIRPolyForm1( float *Signal, float *FilteredSignal, int NumSigPts)
{
  int j, k;

  //float RegX[REG_SIZE], RegY[REG_SIZE], CenterTap;
/*
  for(j=0; j<REG_SIZE; j++){
	  RegX[j] = RegY[j]= 0.0;  // Init the delay registers.
  }
*/

  for(j=0; j<NumSigPts; j++)
   {
    // Shift the register values.
    for(k=NCoef; k>0; k--)RegX[k] = RegX[k-1];
    for(k=NCoef; k>0; k--)RegY[k] = RegY[k-1];

    // The numerator
    CenterTap = 0.0;
    RegX[0] = Signal[j];
    for(k=0; k<=NCoef; k++)
     {
      CenterTap += BCoef[k] * RegX[k];
     }

    // The denominator
    RegY[0] = CenterTap * ACoef[0];
    for(k=1; k<=NCoef; k++)
     {
      RegY[0] -= ACoef[k] * RegY[k];
     }
    FilteredSignal[j] = RegY[0];
   }

}









// pro 2000 Hz:

/*
// podvzorkovan filtry

// PP1: 25 - 40 Hz, fs = 2000Hz
/////////////////////////////////////////////////
#define NL1U 7
const float32_t NUM1U[NL1U] = {
-0.00010444073351465902000000000000,-0.00060335611080689522000000000000,-0.00122601105683233200000000000000,-0.00079533834286253072000000000000,0.00002877833446119065100000000000,0.00000536129423794971110000000000,-0.00000007158467154488217300000000,
};
#define DL1U 6
const float32_t DEN1U[DL1U] = {
0.82324443427756944000000000000000,-0.98804602494505023000000000000000,0.99568838107038471000000000000000,-0.99539871289052928000000000000000,0.99883444197328597000000000000000,-0.99782365901777348000000000000000,
};

#define ORDER_1U 				3
#define NUMBER_OF_TAPS_1U		6




// PP2: 50 - 80 Hz, fs = 2000Hz
/////////////////////////////////////////////////
#define NL2U 7
const float32_t NUM2U[NL2U] = {
-0.00032695951429664638000000000000,-0.00183069030853183710000000000000,-0.00363864118007548640000000000000,-0.00214966628171888930000000000000,0.00036720702283551238000000000000,0.00006862258452371302300000000000,-0.00000551316764452062030000000000,
};
#define DL2U 6
const float32_t DEN2U[DL2U] = {
0.74908065008383895000000000000000,-0.96514094287767593000000000000000,0.98892906816874016000000000000000,-0.98191427481718652000000000000000,0.99639139098063523000000000000000,-0.98793037691341867000000000000000,
};

#define ORDER_2U 				3
#define NUMBER_OF_TAPS_2U		6




// PP3: 100 - 160 Hz, fs = 2000Hz
/////////////////////////////////////////////////
#define NL3U 7
const float32_t NUM3U[NL3U] = {
-0.00223061187384273640000000000000,-0.01116709203830152700000000000000,-0.01961359776763569500000000000000,-0.00641815381058779420000000000000,0.00811259148593222560000000000000,0.00094883396382964159000000000000,-0.00049143571930958252000000000000,
};
#define DL3U 6
const float32_t DEN3U[DL3U] = {
0.56378159468746003000000000000000,-0.86752442710662769000000000000000,0.95594474089272718000000000000000,-0.92721245586291745000000000000000,0.98523258292791582000000000000000,-0.95102581657666074000000000000000,
};

#define ORDER_3U 				3
#define NUMBER_OF_TAPS_3U		6




// PP4: 200 - 315 Hz, fs = 2000Hz
/////////////////////////////////////////////////
#define NL4U 7
const float32_t NUM4U[NL4U] = {
-0.01214628039086610200000000000000,-0.04206040418413760400000000000000,-0.02840404615233191300000000000000,0.06248842182553260300000000000000,0.06125785946993651800000000000000,-0.02334203426587052800000000000000,-0.01550351936923193000000000000000,
};
#define DL4U 6
const float32_t DEN4U[DL4U] = {
0.33623763859680322000000000000000,-0.56791380770931910000000000000000,0.83687718368500075000000000000000,-0.70952376190074495000000000000000,0.93752355940613685000000000000000,-0.79359766666931097000000000000000,
};

#define ORDER_4U				3
#define NUMBER_OF_TAPS_4U		6


*/








// LP11_AA: 400 Hz
/////////////////////////////////////////////////
#define NL11 7
float32_t NUM11[NL11] = {
0.00000000029138773476076138000000,0.00000000343770551477751000000000,0.00000001743113008525699000000000,0.00000004651237330179918500000000,0.00000006974466010460063200000000,0.00000005574785170857377100000000,0.00000001855069568368254500000000,
};
#define DL11 6
float32_t DEN11[DL11] = {
0.81682341999258701000000000000000,-0.99304328457742541000000000000000,0.99849163602865842000000000000000,-0.99926496643055729000000000000000,0.99949823376291902000000000000000,-0.99949765293235626000000000000000,
};

#define ORDER_11 			3
#define NUMBER_OF_TAPS_11		6






/*
float32_t StateBuffer1U[BLOCK_SIZE_2KHZ	 + NUMBER_OF_TAPS_1U - 1];
float32_t StateBuffer2U[BLOCK_SIZE_2KHZ	 + NUMBER_OF_TAPS_2U - 1];
float32_t StateBuffer3U[BLOCK_SIZE_2KHZ	 + NUMBER_OF_TAPS_3U - 1];
float32_t StateBuffer4U[BLOCK_SIZE_2KHZ	 + NUMBER_OF_TAPS_4U - 1];
*/


float32_t StateBuffer11[BLOCK_SIZE + NUMBER_OF_TAPS_11 -1];

arm_iir_lattice_instance_f32 LP11_400Hz;


/*
	arm_iir_lattice_init_f32( &PP1U_25_40Hz, NUMBER_OF_TAPS_1U, &DEN1U[0], &NUM1U[0], &StateBuffer1U[0], BLOCK_SIZE_2KHZ);
	arm_iir_lattice_init_f32( &PP2U_50_80Hz, NUMBER_OF_TAPS_2U, &DEN2U[0], &NUM2U[0], &StateBuffer2U[0], BLOCK_SIZE_2KHZ);
	arm_iir_lattice_init_f32( &PP3U_100_160Hz, NUMBER_OF_TAPS_3U, &DEN3U[0], &NUM3U[0], &StateBuffer3U[0], BLOCK_SIZE_2KHZ);
	arm_iir_lattice_init_f32( &PP4U_200_315Hz, NUMBER_OF_TAPS_4U, &DEN4U[0], &NUM4U[0], &StateBuffer4U[0], BLOCK_SIZE_2KHZ);
*/




	arm_iir_lattice_init_f32( &LP11_400Hz, NUMBER_OF_TAPS_11, &DEN11[0], &NUM11[0], &StateBuffer11[0], BLOCK_SIZE);



		 case 2000:
		 {
				gF = gF_2;
				gS = gS_2;
				gIa = gIa_2;
				gIr = gIr_2;
		 } break;




	gF_2 = exp(-1/(SAMPLING_FREQUENCY_2KHZ*tauF));
	gS_2 = exp(-1/(SAMPLING_FREQUENCY_2KHZ*tauS));
	gIa_2 = exp(-1/(SAMPLING_FREQUENCY_2KHZ*tauIa));
	gIr_2 = exp(-1/(SAMPLING_FREQUENCY_2KHZ*tauIr));

static volatile float gF_2;
static volatile float gS_2;
static volatile float gIa_2;
static volatile float gIr_2;







int numberOfStages2KHz;

	if (filterSelect == 2) {
		  for(i=0; i < numberOfStages2KHz; i++)
		    {
		      arm_iir_lattice_f32( S, inputBuffer + (i * BLOCK_SIZE_2KHZ), outputBuffer + (i * BLOCK_SIZE_2KHZ), BLOCK_SIZE_2KHZ);
		    }
	}








numberOfStages2KHz = LENGTH_OF_SAMPLES_2KHZ/BLOCK_SIZE_2KHZ;



		 carry12KHz = var - LENGTH_OF_SAMPLES + 1;
		 //n = 251;








#define LENGTH_OF_SAMPLES_2KHZ 			41
#define BLOCK_SIZE_2KHZ					41
#define SAMPLING_FREQUENCY_2KHZ		2000

float32_t filterBuffer2[LENGTH_OF_SAMPLES];
float32_t filterBuffer2KHz[LENGTH_OF_SAMPLES_2KHZ ];

float32_t underSamplingBuffer2kHz[LENGTH_OF_SAMPLES_2KHZ];
float32_t underSamplingBuffer2kHzTemp[LENGTH_OF_SAMPLES_2KHZ];





		 DSPrunFilter(&PP1_25_40Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[0] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);


		 DSPrunFilter(&PP2_50_80Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[1] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);


		 DSPrunFilter(&PP3_100_160Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[2] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);


		 DSPrunFilter(&PP4_200_315Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[3] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);


		 DSPrunFilter(&PP5_400_630Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[4] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);


		 DSPrunFilter(&PP6_800_1250Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[5] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);

		 DSPrunFilter(&PP7_1600_2500Hz, bufferPointer, &filterBuffer[0]);
		 arm_abs_f32( &filterBuffer[0], &filterBufferTemp[0], LENGTH_OF_SAMPLES);
		 TimeWeighting(&filterBufferTemp[0], timeWeightingType, &previousWeightedValue[6] , LENGTH_OF_SAMPLES, SAMPLING_FREQUENCY_48KHZ);


