/*****************************************************************************/
/*                                                                           */
/* FILENAME                                                                  */
/*   combfilters.c                                                           */
/*                                                                           */
/* DESCRIPTION                                                               */
/*   Notch and bandpass comb filters for the TMS320VC5505 USB Stick.         */
/*                                                                           */
/* REVISION                                                                  */
/*   Revision: 1.00                                                          */
/*   Author  : Richard Sikora                                                */
/*---------------------------------------------------------------------------*/
/*                                                                           */
/* HISTORY                                                                   */
/*                                                                           */
/*   Revision 1.00                                                           */
/*   16th June 2010. Created by Richard Sikora.                              */
/*                                                                           */
/*****************************************************************************/
/*
 *
 * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com/ 
 * 
 * 
 *  Redistribution and use in source and binary forms, with or without 
 *  modification, are permitted provided that the following conditions 
 *  are met:
 *
 *    Redistributions of source code must retain the above copyright 
 *    notice, this list of conditions and the following disclaimer.
 *
 *    Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the   
 *    distribution.
 *
 *    Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
*/

#define LENGTH 128
//#define GAIN  26214L	// 0.8
//#define SCALE 29490		// 0.9

int buffer[LENGTH];
int buffer2[LENGTH];

int buffer3[LENGTH];

int buffer_X[LENGTH];
int buffer_Y[LENGTH];

int buffer_X_LP[LENGTH];
int buffer_Y_LP[LENGTH];


int grzebienFIR(int input, unsigned int length)
{
 static unsigned int index = 0;
 long output;

 /* Check that parameter length is not off the end of the buffer */ 
 if ( length > LENGTH) length = LENGTH;
 
 /* y(n) =  x(n)/2 + x(n-N)/2   Zespolony */
 //output = (input + buffer[index]) >> 1;
 
  /* y(n) =  x(n)/2 - x(n-N)/2  Rzeczywisty*/
 output = (input - buffer[index]) >> 1;

 /* Save new input */

 buffer[index] = input;  
 
// index = ++index % length;
 if(++index >= length) index = 0;

 return ((int) output);
}


int rezonatorIIR(int input, unsigned int length, float ALFA)
{
 static unsigned int index = 0;
 long output;
 long alfa = (long)(ALFA * 32768L);

 /* Check that parameter length is not off the end of the buffer */ 
 if ( length > LENGTH) length = LENGTH;

 /* y(n) =  (x(n)+ G. y(n-N) ) * (1+G) Rzeczywisty*/
 output = (alfa * buffer3[index]) >> 15;  
 output += ((long)input * (long)(32768L - alfa))>>15;

/* y(n) =  (x(n)+ G. y(n-N) ) * (1+G)  Zespolony */
// output = -(alfa * buffer3[index]) >> 15;  
// output += ((long)input * (32768L - alfa))>>15;


 /* Save latest output y(n) */
 buffer3[index] = output; 
 
 //index = ++index % length;
  if(++index >= length) index = 0;
 
 return ((int) output);
}

int grzebienIIR(int input, unsigned int length, float ALFA)
{
 static unsigned int index = 0;
 long output;
 long alfa = (long)(ALFA * 32768L);
 long wsp = (alfa >> 1) + (1 << 14);	// wsp = (1 + alfa) / 2

 /* Check that parameter length is not off the end of the buffer */ 
 if ( length > LENGTH) length = LENGTH;
  
 /* y(n) =  (x(n)+ G. y(n-N) ) * (1+G) Rzeczywisty */
 output = (alfa * buffer_Y[index]) >> 15;  
 //output += (((long)input -  buffer_X[index]) * (32768L + alfa)) >> 16;
 output += (wsp * ((long)input -  buffer_X[index])) >> 15;
 
  /* y(n) =  (x(n)+ G. y(n-N) ) * (1+G) Zespolony */
 //output = -(alfa * buffer_Y[index]) >> 15;  
 //output += (((long)input +  buffer_X[index]) * (32768L + alfa)) >> 16;

 /* Save new input */
 buffer_X[index] = input; 
 buffer_Y[index] = output; 
 
 //index = ++index % length;
 if(++index >= length) index = 0;
 
 return ((int) output);
}

int LP(int input, unsigned int length, float ALFA)
{
 static unsigned int index = 0;
 long output;
 long alfa = (long)(ALFA * 32768L);
 long wsp = (1 << 14) - (alfa >> 1);	// wsp = (1 - alfa) / 2

 /* Check that parameter length is not off the end of the buffer */ 
 if ( length > LENGTH) length = LENGTH;
  
 /* y(n) =  (x(n)+ G. y(n-N) ) * (1+G) Rzeczywisty */
 output = (alfa * buffer_Y_LP[index]) >> 15;  
 //output += (((long)input -  buffer_X[index]) * (32768L + alfa)) >> 16;
 output += (wsp * ((long)input +  buffer_X_LP[index])) >> 15;
 
  /* y(n) =  (x(n)+ G. y(n-N) ) * (1+G) Zespolony */
 //output = -(alfa * buffer_Y[index]) >> 15;  
 //output += (((long)input +  buffer_X[index]) * (32768L + alfa)) >> 16;

 /* Save new input */
 buffer_X_LP[index] = input; 
 buffer_Y_LP[index] = output; 
 
 //index = ++index % length;
 if(++index >= length) index = 0;
 
 return ((int) output);
}

int LP_I(int input, float ALFA)
{ 
 static int buffer_X;
 static int buffer_Y;
 long output;
 long alfa = (long)(ALFA * 32768L);
 long wsp = (1 << 14) - (alfa >> 1);	// wsp = (1 - alfa) / 2
 
 output = (alfa * buffer_Y) >> 15;  
 output += (wsp * ((long)input +  buffer_X)) >> 15;

 buffer_X = input; 
 buffer_Y = output; 

 return ((int) output);
}
