gausswin source code

I want to create gaussian window program in C/C++. I have checked at Matlab and they give gausswin function to create gaussian window. I have checked gausswin Matlab source code and convert that code to C/C++.

The coefficients of a Gaussian window are computed from the following equation:

gaussian_window_equationwhere –(N – 1)/2 ≤ n ≤ (N – 1)/2 and α is inversely proportional to the standard deviation, σ, of a Gaussian random variable. The exact correspondence with the standard deviation of a Gaussian probability density function is σ = (N – 1)/(2α).

Below is gaussian window code converted from Matlab to C/C++:

#include <stdlib.h>
#include <math.h>
#include <stdio.h>

float *gausswin(int L, float a)
{
	//	GAUSSWIN Gaussian window.
	//	GAUSSWIN(N) returns an N-point Gaussian window.
	//
	//    GAUSSWIN(N, ALPHA) returns the ALPHA-valued N-point Gaussian
	//    window.  ALPHA is defined as the reciprocal of the standard
	//    deviation and is a measure of the width of its Fourier Transform.
	//    As ALPHA increases, the width of the window will decrease. If omitted,
	//    ALPHA is 2.5.
	// 
	//    Reference:
	//      [1] fredric j. harris [sic], On the Use of Windows for Harmonic
	//          Analysis with the Discrete Fourier Transform, Proceedings of
	//          the IEEE, Vol. 66, No. 1, January 1978
	//    Author(s): P. Costa
	//    Copyright 1988-2005 The MathWorks, Inc.
	//    $Revision: 1.14.4.4 $  $Date: 2007/12/14 15:04:55 $

	// Compute window according to [1]
	int i, N;
	float *G;
	float n;

	N = L-1;
	G = (float*) calloc(L,sizeof(float));

	n = -N/2.0;
	for (i=0; i<L; i++)
	{
		G[i] = expf(-(1.0/2.0) * powf((a*n / (N/2.0)),2.0) );
		n = n+1.0;
	}
	return(G);
}

int main(int argc, char **argv)
{
	int i;
	int L;
	float a;
	
	if(argc!=3)
	{
		fprintf(stderr, "gausswin  L   a \n");
		fprintf(stderr, "L (integer) = npoint data \n");
		fprintf(stderr, "a (float)   = inversely proportional to the standard deviation of a Gaussian random variable \n");
		return(1);
	}
	
	//get input parameter
	sscanf(argv[1], "%i", &L);
	sscanf(argv[2], "%f", &a);
	
	//process gaussian window
	float *G = gausswin(L, a);
	
	//print output
	fprintf(stderr, "Gaussian Window Data (L=%i a=%f) : \n", L, a);
	for(i=0; i<L; i++)
	{
		fprintf(stderr, "%f \n", G[i]); 
	}
	
	free(G);
	return(0);
}

Compile this gausswin.c (gaussian window source code) using command :

gcc gausswin.c -lm -o gausswin

Show gaussian window program manual using command :

toto@ubuntu:~/Documents$ ./gausswin 
gausswin  L   a 
L (integer) = npoint data 
a (float)   = inversely proportional to the standard deviation of a Gaussian random variable

Sample command how to run gaussian window program

toto@ubuntu:~/Documents$ ./gausswin 10 0.2
Gaussian Window Data (L=10 a=0.200000) : 
0.980199 
0.987974 
0.993846 
0.997780 
0.999753 
0.999753 
0.997780 
0.993846 
0.987974 
0.980199

I have compare the result from my gaussian window program and show same result compared with gausswin Matlab.

Add a Comment

Your email address will not be published. Required fields are marked *