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

#include <string.h>
#include <math.h>
#include <limits.h>
#include <math.h>
#include "image.h"
#include "windows.h"


extern VENSTER* vp[MAXSIG];
extern unsigned short MAX_LENGTH;
extern unsigned short SAMPLE_RATE;


int Window_functions(ASM_SAMPLE *real_data_in,ASM_SAMPLE *imag_data_in,ASM_SAMPLE *real_data_out,ASM_SAMPLE *imag_data_out,
int lengte,int windowing)
{

	int i;
	double hanning,hamming,gauss,blackman,kaiser,triangle;

	switch (windowing)
	{
	case 0:  /* Geen window */

		for (i=0;i < lengte;i++)
		{
			real_data_out[i] = real_data_in[i];
			imag_data_out[i] = imag_data_in[i];
		}
		break;
	case 1:  /* Hanning (cosinus) window */

		for (i=0;i < lengte;i++)
		{
			hanning = (1-cos((2*M_PI*i)/(lengte-1)))/2;
			real_data_out[i] = real_data_in[i] * hanning;
			imag_data_out[i] = imag_data_in[i] * hanning;

		}
		break;
	case 2:  /* Hamming window */

		for (i=0;i < lengte;i++)
		{
			hamming = (0.538 - 0.462*cos(2*M_PI*i/(lengte-1)));
			real_data_out[i] = real_data_in[i] * hamming;
			imag_data_out[i] = imag_data_in[i] * hamming;
		}
		break;
	case 3:   /* Gaus window */

		for (i=0;i < lengte;i++)
		{
			gauss = exp(-0.5* pow( (3.0*(i-(lengte-1)/2.0)*2/(lengte-1)),2));
			real_data_out[i] = real_data_in[i] * gauss;
			imag_data_out[i] = imag_data_in[i] * gauss;
		}
		break;
	case 4:  /* Blackman window */

		for (i=0;i < lengte;i++)
		{
			blackman = (0.42 - 0.50*cos((2*M_PI*i)/(lengte-1)) + 0.08*cos((4*M_PI*i)/(lengte-1)) );
			real_data_out[i] = real_data_in[i] * blackman;
			imag_data_out[i] = imag_data_in[i] * blackman;
		}
		break;
	case 5:  /* Kaiser window */

		for (i=0;i < lengte;i++)
		{
			kaiser = (1 - 1.24*cos(2*M_PI*i/(lengte-1)) + 0.244*cos(4*M_PI*i/(lengte-1)) - 0.00305*cos(6*M_PI*i/(lengte-1)))/2.48;
			real_data_out[i] = real_data_in[i] * kaiser;
			imag_data_out[i] = imag_data_in[i] * kaiser;
		}
		break;
	case 6:  /* Driehoek window */

		for (i=0;i < lengte;i++)
		{
			triangle =1 - fabs(i-((lengte-1)/2)) / ((lengte-1)/2);
			real_data_out[i] = real_data_in[i] * triangle;
			imag_data_out[i] = imag_data_in[i] * triangle;
		}
		break;
	default: 
		break;
	}

	return 0;
}
