www.pudn.com > MutualInformationICA.zip > milca.C


//####################################################################
//MILCA
//####################################################################
//2 May 2004
//Contact: kraskov@its.caltech.edu
//####################################################################
//uses mi2c mi2r
#include 
#include 
#include 
#include 
#include 

#include "miutils.h"

#define M 7
#define NSTACK 50
#define SWAP(a,b) tempr=(a); (a)=(b); (b)=tempr

void four1(double *data, unsigned long nn, int isign);
void realft(double *data, unsigned long n, int isign);


int main(int argc, char **argv) {

  FILE *fin;
  int i,d,d1,d2, d_,d2_;
  int N=4096;;
  int dim=3;
  int K=1;

  double **x;
  double **xx;
  double *psi,*min,*max,*scalxx;
  double t_d,t_d1;
  int BOX1;

  double s,me;


  int k;
  double *rmi;
  double angle;
  double st_d, ct_d;


  double **Rall,**R,**Rt;
  int p, pMax;
  int count;
  int ns=1;

  double mimin;
  int angI;

  int method=1;
  int nr=128;
  double addnoise=-1;
  
  if (argc<5) {
    fprintf(stderr,"\nMutual Information Least dependent Component Analysis (MILCA)\n\n");
    fprintf(stderr,"Usage:\n%s   <# points> <# neighbours> [method] [nrot] [nharm] [addnoise]\n\n",argv[0]);
    fprintf(stderr,"Input:\n\t\ttext file with  columns and <# points> rows\n");
    fprintf(stderr,"\t\t\tnumber of columns in file\n");
    fprintf(stderr,"\t<# points>\tnumber of rows (length of characteristic vector)\n");
    fprintf(stderr,"\t<# neighbours>\tnumber of the nearest neighbours for MI estimator\n");
    fprintf(stderr,"\t[method]\teither cubic (0), or rectangular (1); default 1\n");
    fprintf(stderr,"\t[nrot]\t\tnumber of angles to minimize over; default 128\n");
    fprintf(stderr,"\t[nharm]\t\tnumber of harmonics to approximate MI(angle) dependence; default 1\n");
    fprintf(stderr,"\t[addnoise]\tnoise amplitude; default 1e-8\n");
    fprintf(stderr,"\nOutput:\n");
    fprintf(stderr,"\nde-mixing matrix\n");
    fprintf(stderr,"\nContact: kraskov@its.caltech.edu\n");
    exit(-1);
  }

  dim=atoi(argv[2]);
  N=atoi(argv[3]);
  K=atoi(argv[4]);
  if (argc>=6) method=atoi(argv[5]);
  if (argc>=7) nr=atoi(argv[6]);
  if (argc>=8) ns=atoi(argv[7]);
  if (argc>=9) addnoise=atof(argv[8]);
  if (argc>=10) {fprintf(stderr,"Too many input arguments\n");exit(-1);}


  pMax=dim-1;

  rmi=(double *)calloc(nr+1,sizeof(double));

  xx=(double **)calloc(2,sizeof(double*));
  xx[0]=(double *)calloc(N,sizeof(double));
  xx[1]=(double *)calloc(N,sizeof(double));

  x=(double **)calloc(dim,sizeof(double*));
  for (d=0;dmax[d]) max[d]=x[d][i];
    }
    for (i=0;i2) && (d!=d2)) ) {
	  for (k=0;kmax[d1]) max[d1]=xx[d1][i];
	      }
	      for (i=0;i=1) && (angI<=nr-1) ) {//minumin angle rotation are too small, and not considered
	    for (d_=0;d_ i) {
      SWAP(data[j],data[i]);
      SWAP(data[j+1],data[i+1]);
    }
    m=n >> 1;
    while (m >= 2 && j > m) {
      j -= m;
      m >>= 1;
    }
    j += m;
  }
  mmax=2;
  while (n > mmax) {
    istep=mmax << 1;
    theta=isign*(6.28318530717959/mmax);
    wtemp=sin(0.5*theta);
    wpr = -2.0*wtemp*wtemp;
    wpi=sin(theta);
    wr=1.0;
    wi=0.0;
    for (m=1;m>1);
  if (isign == 1) {
    c2 = -0.5;
    four1(data,n>>1,1);
  } else {
    c2=0.5;
    theta = -theta;
  }
  wtemp=sin(0.5*theta);
  wpr = -2.0*wtemp*wtemp;
  wpi=sin(theta);
  wr=1.0+wpr;
  wi=wpi;
  np3=n+3;
  for (i=2;i<=(n>>2);i++) {
    i4=1+(i3=np3-(i2=1+(i1=i+i-1)));
    h1r=c1*(data[i1]+data[i3]);
    h1i=c1*(data[i2]-data[i4]);
    h2r = -c2*(data[i2]+data[i4]);
    h2i=c2*(data[i1]-data[i3]);
    data[i1]=h1r+wr*h2r-wi*h2i;
    data[i2]=h1i+wr*h2i+wi*h2r;
    data[i3]=h1r-wr*h2r+wi*h2i;
    data[i4] = -h1i+wr*h2i+wi*h2r;
    wr=(wtemp=wr)*wpr-wi*wpi+wr;
    wi=wi*wpr+wtemp*wpi+wi;
  }
  if (isign == 1) {
    data[1] = (h1r=data[1])+data[2];
    data[2] = h1r-data[2];
  } else {
    data[1]=c1*((h1r=data[1])+data[2]);
    data[2]=c1*(h1r-data[2]);
    four1(data,n>>1,-1);
  }
}
/* (C) Copr. 1986-92 Numerical Recipes Software -0)0+3$j3D.. */