#include "mex.h"
#include <math.h>
#include "adapt.h"
static complex zero={0,0};
void
mexFunction(nlhs,plhs,nrhs,prhs)
int nlhs;
mxArray *plhs[];
int nrhs;
const mxArray *prhs[];
{
/* For T-spaced forward filters (spacing=1) */
/* Input args: spacing,a,r,y,x,ahat,F0,G0,muf,mug,offset,delay,pdelay,
* structure,constellation,M,sig,algorithm,regfilt
*/
double spacing; /* spacing, i.e. BSE=1, FSE=2 */
double *ar,*ai; /* T-spaced desired transmitted symbols */
double *rr,*ri; /* T-spaced received data */
double *yr,*yi; /* T-spaced equalizer output */
double *xr,*xi; /* T-spaced filtered output */
double *ahatr,*ahati; /* T-spaced symbol estimates */
double *F0r,*F0i; /* T-spaced forward filters */
double *G0r,*G0i; /* T-spaced feedback filter */
double muf; /* forward filter step-size muf */
double mug; /* forward filter step-size mug */
int offset; /* offset used to recover initial conditions */
int delay; /* transmission delay */
int pdelay; /* processing delay */
int structure; /* equalizer structure, i.e. LE=1, DFE=2 */
int constellation; /* constellation type */
int M; /* constellation size */
double sig; /* constellation power */
int algorithm; /* choice of algorithm,
* i.e. IIR-LMS=1, DD-LMS=2, IIR-CMA=3
*/
int regfilt; /* use regressor filtering or not */
/* For T/2-spaced forward filters (spacing=0.5) */
/* Input args: spacing,a,ra,rb,y,x,ahat,Fa0,Fb0,G0,muf,mug,offset,delay,
* pdelay,structure,constellation,M,sig,algorithm
*/
double *rar,*rai; /* T-spaced received even subchannel data */
double *rbr,*rbi; /* T-spaced received odd subchannel data */
double *Fa0r,*Fa0i; /* T-spaced forward even subfilter */
double *Fb0r,*Fb0i; /* T-spaced forward odd subfilter */
/* Intermediate variables */
int Nf,Nfa,Nfb,Ng; /* Length of filters */
int L; /* Lenght of simulation */
/* For T-spaced forward filters (spacing=1) */
/* Output args: outF,outG,outy,outx,outz,outahat,outef,outeg,outphi */
double *outFr,*outFi;
double *outGr,*outGi;
double *outyr,*outyi;
double *outxr,*outxi;
double *outzr,*outzi;
double *outahatr,*outahati;
double *outefr,*outefi;
double *outegr,*outegi;
double *outphi;
/* For T/2-spaced forward filters (spacing=1) */
/* Output args: outFa,outFb,outG,outy,oute,outahat */
double *outFar,*outFai;
double *outFbr,*outFbi;
/* Find out if BSE or FSE */
spacing=(double)mxGetScalar(prhs[0]);
/* Retrieve BSE variables */
/* Input args: spacing,a,r,y,x,ahat,F0,G0,muf,mug,offset,delay,pdelay,
* structure,constellation,M,sig,algorithm
*/
if (spacing==1.0) {
/* Retrieve scalars */
L=(int)mxGetM(prhs[1]);
Nf=(int)mxGetM(prhs[6]);
Ng=(int)mxGetM(prhs[7]);
muf=(double)mxGetScalar(prhs[8]);
mug=(double)mxGetScalar(prhs[9]);
offset=(int)mxGetScalar(prhs[10]);
delay=(int)mxGetScalar(prhs[11]);
pdelay=(int)mxGetScalar(prhs[12]);
structure=(int)mxGetScalar(prhs[13]);
constellation=(int)mxGetScalar(prhs[14]);
M=(int)mxGetScalar(prhs[15]);
sig=(double)mxGetScalar(prhs[16]);
algorithm=(int)mxGetScalar(prhs[17]);
regfilt=(int)mxGetScalar(prhs[18]);
/* Get input pointers */
ar=mxGetPr(prhs[1]);
if (mxIsComplex(prhs[1]))
ai=mxGetPi(prhs[1]);
else
ai=(double *)mxCalloc(L,sizeof(double));
rr=mxGetPr(prhs[2]);
if (mxIsComplex(prhs[2]))
ri=mxGetPi(prhs[2]);
else
ri=(double *)mxCalloc(L,sizeof(double));
yr=mxGetPr(prhs[3]);
if (mxIsComplex(prhs[3]))
yi=mxGetPi(prhs[3]);
else
yi=(double *)mxCalloc(L,sizeof(double));
xr=mxGetPr(prhs[4]);
if (mxIsComplex(prhs[4]))
xi=mxGetPi(prhs[4]);
else
xi=(double *)mxCalloc(L,sizeof(double));
ahatr=mxGetPr(prhs[5]);
if (mxIsComplex(prhs[5]))
ahati=mxGetPi(prhs[5]);
else
ahati=(double *)mxCalloc(L,sizeof(double));
F0r=mxGetPr(prhs[6]);
if (mxIsComplex(prhs[6]))
F0i=mxGetPi(prhs[6]);
else
F0i=(double *)mxCalloc(Nf,sizeof(double));
G0r=mxGetPr(prhs[7]);
if (mxIsComplex(prhs[7]))
G0i=mxGetPi(prhs[7]);
else
G0i=(double *)mxCalloc(Ng,sizeof(double));
/* Allocate memory for outputs */
plhs[0]=mxCreateDoubleMatrix(Nf,1,mxCOMPLEX); /* F */
plhs[1]=mxCreateDoubleMatrix(Ng,1,mxCOMPLEX); /* G */
plhs[2]=mxCreateDoubleMatrix(L,1,mxCOMPLEX); /* y */
plhs[3]=mxCreateDoubleMatrix(L,1,mxCOMPLEX); /* x */
plhs[4]=mxCreateDoubleMatrix(L,1,mxCOMPLEX); /* z */
plhs[5]=mxCreateDoubleMatrix(L,1,mxCOMPLEX); /* ahat */
plhs[6]=mxCreateDoubleMatrix(L,1,mxCOMPLEX); /* ef */
plhs[7]=mxCreateDoubleMatrix(L,1,mxCOMPLEX); /* eg */
plhs[8]=mxCreateDoubleMatrix(L,1,mxREAL); /* phi */
/* Get output pointers */
outFr=mxGetPr(plhs[0]);
outFi=mxGetPi(plhs[0]);
outGr=mxGetPr(plhs[1]);
outGi=mxGetPi(plhs[1]);
outyr=mxGetPr(plhs[2]);
outyi=mxGetPi(plhs[2]);
outxr=mxGetPr(plhs[3]);
outxi=mxGetPi(plhs[3]);
outzr=mxGetPr(plhs[4]);
outzi=mxGetPi(plhs[4]);
outahatr=mxGetPr(plhs[5]);
outahati=mxGetPi(plhs[5]);
outefr=mxGetPr(plhs[6]);
outefi=mxGetPi(plhs[6]);
outegr=mxGetPr(plhs[7]);
outegi=mxGetPi(plhs[7]);
outphi=mxGetPr(plhs[8]);
/* Adapt! */
adaptBSE(Nf,Ng,F0r,F0i,G0r,G0i,
L,ar,ai,rr,ri,yr,yi,xr,xi,ahatr,ahati,
muf,mug,offset,delay,pdelay,
constellation,M,sig,algorithm,regfilt,structure,
outFr,outFi,outGr,outGi,outyr,outyi,outxr,outxi,
outzr,outzi,outahatr,outahati,
outefr,outefi,outegr,outegi,outphi);
}
/* Retrieve FSE variables */
/* Input args: spacing,a,ra,rb,y,x,ahat,Fa0,Fb0,G0,muf,mug,offset,delay,
* pdelay,structure,constellation,M,sig,algorithm
*/
else {
/* Retrieve scalars */
L=(int)mxGetM(prhs[1]);
Nfa=(int)mxGetM(prhs[7]);
Nfb=(int)mxGetM(prhs[8]);
Ng=(int)mxGetM(prhs[9]);
muf=(double)mxGetScalar(prhs[10]);
mug=(double)mxGetScalar(prhs[11]);
offset=(int)mxGetScalar(prhs[12]);
delay=(int)mxGetScalar(prhs[13]);
pdelay=(int)mxGetScalar(prhs[14]);
structure=(int)mxGetScalar(prhs[15]);
constellation=(int)mxGetScalar(prhs[16]);
M=(int)mxGetScalar(prhs[17]);
sig=(double)mxGetScalar(prhs[18]);
algorithm=(int)mxGetScalar(prhs[19]);
regfilt=(int)mxGetScalar(prhs[20]);
/* Get input pointers */
ar=mxGetPr(prhs[1]);
if (mxIsComplex(prhs[1]))
ai=mxGetPi(prhs[1]);
else
ai=(double *)mxCalloc(L,sizeof(double));
rar=mxGetPr(prhs[2]);
if (mxIsComplex(prhs[2]))
rai=mxGetPi(prhs[2]);
else
rai=(double *)mxCalloc(L,sizeof(double));
rbr=mxGetPr(prhs[3]);
if (mxIsComplex(prhs[3]))
rbi=mxGetPi(prhs[3]);
else
rbi=(double *)mxCalloc(L,sizeof(double));
yr=mxGetPr(prhs[4]);
if (mxIsComplex(prhs[4]))
yi=mxGetPi(prhs[4]);
else
yi=(double *)mxCalloc(L,sizeof(double));
xr=mxGetPr(prhs[5]);
if (mxIsComplex(prhs[5]))
xi=mxGetPi(prhs[5]);
else
xi=(double *)mxCalloc(L,sizeof(double));
ahatr=mxGetPr(prhs[6]);
if (mxIsComplex(prhs[6]))
ahati=mxGetPi(prhs[6]);