/*
 * MakeCG.c
 *
 *  Created on: 2009/02/11
 *      Author: H.Shouno
 *  This is a wrapper program for make_cg() in Neocog.c by Prof.Fukushima.
 *
 */

#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include "mex.h"
#include "neocog_proto.h"

void mexFunction( int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[] );

/*
 * Input & Output Arguments
 */

#define CG_OUT		lhs[0] // output Gaussian filter matrix
#define CG_SIZE_IN	rhs[0] // Size of output Matrix if you set.(optional)
#define AG_IN		rhs[1] // filter params if you set. (optional)
#define AGO_IN		rhs[2] // filter params if you set. (optional)
#define GAMCG_IN	rhs[3] // filter params if you set. (optional)


//
// available expressions...
// CG = MakeCG();
// CG = MakeCG( 7, 3.3, 0.05 );
//


#define CG_DEFAULT_SIZE ((int)(2*(AG_DEFAULT)+1))
#define AG_DEFAULT 3.3 // from Neocog.c
#define AGO_DEFAULT 1.5
#define GamCG_DEFAULT 0.05


//
// Gateway function
//
void mexFunction( int nlhs, mxArray *lhs[], int nrhs, const mxArray *rhs[] )
{
	int  i;
	double *coefp;
	double **cg;
	int ng = CG_DEFAULT_SIZE;
	double ag = AG_DEFAULT;
	double ago = AGO_DEFAULT;
	double gamcg = GamCG_DEFAULT;

	if( !( nrhs == 0 || nrhs == 4 ) ){
		mexErrMsgTxt( "MakeCG() requires input parameter(s): MakeCG() / MakeCG( SIZE, Ag, Ago, GamCG )" );
	}
	if( nlhs < 1 )		mexErrMsgTxt( "MakeCG() requires ouput parameter(s): CG = MakeCG(...)" );

	// right side
	if( nrhs == 4 ){
		ng = (int)mxGetScalar( CG_SIZE_IN );
		if( ng <= 0 )	mexErrMsgTxt( "coefficeints size is wrong." );
		ag = mxGetScalar( AG_IN );
		ago = mxGetScalar( AGO_IN );
		gamcg = mxGetScalar( GAMCG_IN );
	}

	CG_OUT = mxCreateDoubleMatrix( ng, ng, mxREAL );
	coefp = (double*)mxGetPr( CG_OUT );

	cg = (double**)mxCalloc( sizeof(double*), ng );
	cg[0] = coefp;
	for( i = 1 ; i < ng ; i++ )		cg[i] = cg[i-1] + ng;

	make_cg( cg, ng, ag, ago, gamcg );

	return;
}
