Make your own free website on Tripod.com
These fuctions calculate the window coefficients using the various
window function specified by the user in the main program

To go to the main program click here
To go to the index click here
To go to my page click here

#include "../optimal/globDefs.h"

/* idealLowpass()  */

void idealLowpass(int numbTaps, real lambdaU, real hh[])
{
	int n,nMax;
	real mm;
	printf("in idealLowpass\n");
	for (n=0; n<numbTaps; n++)
	{
		mm=n-(real)(numbTaps-1)/2.0;
		if(mm==0)
		{
			hh[n]=lambdaU/PI; 
		}
		else {
			hh[n]=sin(mm*lambdaU)/(mm*PI);
		     }
	}
	return;
}

/* idealHighpass()   */

void idealHighpass( int numbTaps, real lambdaL, real hh[])
{
	int n,nMax;
	real mm;
	printf("in idealHighpass\n");
	for (n=0; n<numbTaps; n++)
	{
		mm=n-(real)(numbTaps-1)/2.0;
		if(mm==0)
		{
			hh[n]=1.0-lambdaL/PI;
		}
		else
			{hh[n]=-sin(mm*lambdaL)/(mm*PI);}
	}
	return;
}

/* idealBandpass() */

void idealBandpass( int numbTaps, real lambdaL, real lambdaU, real hh[])
{
	int n, nMax;
	real mm;
	printf("in idealBandpass\n");
	for(n=0; n<numbTaps; n++)
	{
		mm=n-(real)(numbTaps-1)/2.0;
		if(mm==0)
			{hh[n]=(lambdaU-lambdaL)/PI;}
		else
			{hh[n]=(sin(mm*lambdaU)-sin(mm*lambdaL))/(mm*PI);}
	}
	return;
}

/*   idealBandstop()   */

void idealBandstop( int numbTaps, real lambdaL, real lambdaU, real hh[])
{
	int n, nMax;
	real mm;
	printf("in idealBandstop\n");
	for(n=0; n<numbTaps; n++)
	{
		mm=n-(real)(numbTaps-1)/2.0;
		if(mm==0)
			{hh[n]=1.0+(lambdaU-lambdaL)/PI;}
		else
			{hh[n]=(sin(n*lambdaL)-sin(mm*lambdaU))/(mm*PI);}
	}
	return;
	
}

/* contRectangularResponse()    */

#define TINY 3.16e-5
real contrectangularResponse(real freq, real tau, logical dbScale)
{
	real x;
	x=sinc(PI*freq*tau);
	if(dbScale)
	{
		if(fabs(x) < TINY)
			{x=-90.0;}
		else
			{x=20.0*log10(fabs(x));}
	}
	return(x);
}

/* discRectangularResponse()    */

real discRectangularResponse( real freq, int M, logical normalizedAmplitude)
{
	real result;
	if(freq==0.0)
		{result=(real)(2*M+1);}
	else
		{result=fabs(sin(PI*freq*(2*M+1))/sin(PI*freq));}

	if(normalizedAmplitude)
		result = result/(real)(2*M+1);
	return(result);
}	

/* contTriangularResponse()  */

real contTriangularResponse( real freq, real tau, logical dbScale)
{
	real amp0, x;
	amp0=0.5*tau;
	x=PI*freq*tau/2.0;
	x=0.5*tau*sincSqrd(x);
	if(dbScale)
	{
		if(fabs(x/amp0) < TINY)
			{x=-90.0;}
		else
			{x=20.0*log10(fabs(x/amp0));}
	}
	return(x);
}

/* triangularWindow()   */

void triangularWindow( int N, real window[])
{
	real offset;
	int n; 
	offset=(real)(1-(n%2));
	for (n=0; n<(N/2.0); n++)
	{
		window[n]=1.0-(2.0*n+offset)/(N+1.0);
	}
	return;
}

/* makeLagWindow ()    */

void makeLagWindow(int N, real window[], int center, real outWindow[])
{
	int n,M,negative;
	if(N%2) {
		M=(N-1)/2;
		for(n=0; n<=M; n++) {
			outWindow[n]= window[n];
			outWindow[-n]=outWindow[n];
			}
		}
	else {
		M=(N-2)/2;
		if(center == negative) {
			for(n=0; n<=M; n++) {
				outWindow[n] = window[n];
				outWindow[-(1+n)] = window[n];
				}
			}
		else {
			for(n=0; n<=M; n++) {
				outWindow[n+1]=window[n];
				outWindow[-n]=window[n];
				}
			}
		}
		return;
		}

/* makeDataWindow()   */

void makedataWindow(int N, real window[], real outWindow[])
{
	int n,M;
	if (N%2) {
		M=(N-1)/2;
		for(n=0; n<=M; n++) {
			outWindow[n]=window[M-n];
			outWindow[M+n]=window[n];
			}
		}
		else {
			M=(N-2)/2;
			for(n=0;n<=M; n++) {
				outWindow[n]=window[M-n];
				outWindow[M+n+1]-window[n];
				}
			}
			return;
			}

/* hanningWindow()   */

void hanningWindow( int N, real window[])
{
	logical odd;
	int n;
	odd=N%2;
	for(n=0; n<(N/2.0); n++)
	{
		if(odd)
			{window[n]=0.5+0.5*cos(TWO_PI*n/(N-1));}
		else
			{window[n]=0.5+0.5*cos(TWO_PI*(2*n+1)/(2.0*(N-1)));}
	}
	return;
}


/* hammingWindow() */

void hammingWindow( int N, real window[])
{
	logical odd;
	int n;
	odd=N%2;
	for (n=0; n<(N/2.0); n++)
	{
		if(odd)
			{window[n]=.54+.46*cos(TWO_PI*n/(N-1));}
		else
			{window[n]=.54+.46* cos(TWO_PI*(2*n+1)/(2.0*(N-1)));}
	}
	return;
}

void blackmanWindow(int N, real window[])
{
	int n;
	logical odd;
	odd=N%2;
	if (odd){
		for (n=0; n<=((N-1)/2.0); n++)
			window[n]=0.42 + .5*cos(TWO_PI*n/(N-1))+.08*cos(2*TWO_PI*n/(N-1));
		}
	else	{
		for (n=0; n<((N/2)-1); n++)
			window[n]=0.42 + .5*cos(TWO_PI*n/(N-1))+.08*cos(2*TWO_PI*n/(N-1));
		}

}	
/* rectangular Window */ 

void rectWindow(int N, real window[])
{
	int n;
	logical odd;
	odd=N%2;
	if (odd){
		for (n=0; n<=((N-1)/2.0); n++)
			window[n]= 1;
		}
	else	{
		for (n=0; n<((N/2)-1); n++)
			window[n]= 1;
		}
}