//-----------------------------------------------------------------------------------
void fft2d(COMPLEX **inimage,COMPLEX **timage,int size2,int x,int y,int inc,double dx,double dy)
{
    COMPLEX *ptr;
    COMPLEX  buf[2*size];
    double pix,piy,theta;
    int cx,cy;
    int i,j,k;
    cx=-size/2;
    cy=-size/2;
    pix=2*M_PI*dx/size2;
    piy=2*M_PI*dy/size2;

    for(i=0; i<size2; i++) {
        for(j=0; j<size2; j++) {
            theta=(i+cy)*piy+(j+cx)*pix;
            image[i][j].r= inimage[i][j].r*cos(theta);
            image[i][j].i= inimage[i][j].r*sin(theta);
        }
        four1(image[i],size2,-1);
    }
    for(i=size/2-1; i<size2; i++) {
        for(j=0; j<size2; j++) {
            buf[j]=image[j][i];
        }
        four1(buf,size2,-1);
        ptr=buf;
        k=i*inc+y;
        for(j=x; j<size2*inc; j+=inc) {
            timage[k][j]=*ptr;
            ptr++;
        }
    }
}
Ejemplo n.º 2
0
int main(void)
{
	unsigned long i;
	int isign;
	float *data1,*data2,*fft1,*fft2;

	data1=vector(1,N);
	data2=vector(1,N);
	fft1=vector(1,N2);
	fft2=vector(1,N2);
	for (i=1;i<=N;i++) {
		data1[i]=floor(0.5+cos(i*2.0*PI/PER));
		data2[i]=floor(0.5+sin(i*2.0*PI/PER));
	}
	twofft(data1,data2,fft1,fft2,N);
	printf("Fourier transform of first function:\n");
	prntft(fft1,N);
	printf("Fourier transform of second function:\n");
	prntft(fft2,N);
	/* Invert transform */
	isign = -1;
	four1(fft1,N,isign);
	printf("inverted transform  =  first function:\n");
	prntft(fft1,N);
	four1(fft2,N,isign);
	printf("inverted transform  =  second function:\n");
	prntft(fft2,N);
	free_vector(fft2,1,N2);
	free_vector(fft1,1,N2);
	free_vector(data2,1,N);
	free_vector(data1,1,N);
	return 0;
}
Ejemplo n.º 3
0
static void realft(real data[], int n, int isign)
{
    int    i, i1, i2, i3, i4, n2p3;
    real   c1 = 0.5, c2, h1r, h1i, h2r, h2i;
    double wr, wi, wpr, wpi, wtemp, theta;

    theta = 3.141592653589793/(double) n;
    if (isign == 1)
    {
        c2 = -0.5;
        four1(data, n, 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;
    n2p3  = 2*n+3;
    for (i = 2; i <= n/2; i++)
    {
        i4       = 1+(i3 = n2p3-(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);
    }
}
Ejemplo n.º 4
0
Archivo: twofft.c Proyecto: gnovak/bin
void twofft(float data1[], float data2[], float fft1[], float fft2[],
	unsigned long n)
{
	void four1(float data[], unsigned long nn, int isign);
	unsigned long nn3,nn2,jj,j;
	float rep,rem,aip,aim;

	nn3=1+(nn2=2+n+n);
	for (j=1,jj=2;j<=n;j++,jj+=2) {
		fft1[jj-1]=data1[j];
		fft1[jj]=data2[j];
	}
	four1(fft1,n,1);
	fft2[1]=fft1[2];
	fft1[2]=fft2[2]=0.0;
	for (j=3;j<=n+1;j+=2) {
		rep=0.5*(fft1[j]+fft1[nn2-j]);
		rem=0.5*(fft1[j]-fft1[nn2-j]);
		aip=0.5*(fft1[j+1]+fft1[nn3-j]);
		aim=0.5*(fft1[j+1]-fft1[nn3-j]);
		fft1[j]=rep;
		fft1[j+1]=aim;
		fft1[nn2-j]=rep;
		fft1[nn3-j] = -aim;
		fft2[j]=aip;
		fft2[j+1] = -rem;
		fft2[nn2-j]=aip;
		fft2[nn3-j]=rem;
	}
}
Ejemplo n.º 5
0
static void twofft(real data1[],real data2[],real fft1[],real fft2[],int n)
{
  int nn3,nn2,jj,j;
  real rep,rem,aip,aim;

  nn3=1+(nn2=2+n+n);
  for (j=1,jj=2;j<=n;j++,jj+=2) {
    fft1[jj-1]=data1[j];
    fft1[jj]=data2[j];
  }
  four1(fft1,n,1);
  fft2[1]=fft1[2];
  fft1[2]=fft2[2]=0.0;
  for (j=3;j<=n+1;j+=2) {
    rep=0.5*(fft1[j]+fft1[nn2-j]);
    rem=0.5*(fft1[j]-fft1[nn2-j]);
    aip=0.5*(fft1[j+1]+fft1[nn3-j]);
    aim=0.5*(fft1[j+1]-fft1[nn3-j]);
    fft1[j]=rep;
    fft1[j+1]=aim;
    fft1[nn2-j]=rep;
    fft1[nn3-j] = -aim;
    fft2[j]=aip;
    fft2[j+1] = -rem;
    fft2[nn2-j]=aip;
    fft2[nn3-j]=rem;
  }
}
Ejemplo n.º 6
0
Archivo: fft.c Proyecto: cran/Rwave
void double_fft(double *Or,double *Oi,double *Ir,double *Ii,
  int isize,int isign)
{
  double *tmp;
  int nt, find2power(), newsize, i;

  nt = find2power(isize);
  newsize = 1 << nt;

  if(!(tmp = (double *)R_alloc(newsize * 2, sizeof(double))))
     Rf_error("Memory allocation failed for tmp in cwt_morlet.c \n");

  for(i = 0; i < isize; i++) {
    tmp[2 * i] = Ir[i];
    tmp[2 * i + 1] = Ii[i];
  }
  four1(tmp-1,newsize,isign);  

  
  for(i = 0; i < isize; i++) {
    if(isign == -1) {
      Or[i] = tmp[2 * i]/newsize;
      Oi[i] = tmp[2 * i + 1]/newsize;
    } 
    else {
      Or[i] = tmp[2 * i];
      Oi[i] = tmp[2 * i + 1];
    }
  }
}
Ejemplo n.º 7
0
void realfft(float *table, int tablesize)
{
    int i, i1, i2, i3, i4, n2p3, n;
    float c1 = 0.5, c2 = -0.5, hir, h1i, h2r, h2i, nyquist, *data;
    double wr, wi, wpr, wpi, wtemp, theta;

    /*  INITIALIZE CONSTANTS  */
    data = table - 1;
    n = tablesize / 2;
    theta = M_PI / (double)n;
    
    /*  DO FOURIER TRANSFORM ON TABLE (WITH INDEXING STARTING AT 0)  */
    four1(table, n, 1);
    
    /*  REPACK THE TRANSFORMED DATA INTO STANDARD ORDER  */
    wtemp = sin(0.5 * theta);
    wpr = -2.0 * wtemp * wtemp;
    wpi = sin(theta);
    wr = 1.0 + wpr;
    wi = wpi;
    n2p3 = 2 * n + 3;
    for (i = 2; i <= n/2; i++) {
        i4 = 1 + (i3 = n2p3 - (i2 = 1 + (i1 = i + i - 1)));
        hir = 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] = hir + wr * h2r - wi * h2i;
        data[i2] = h1i + wr * h2i + wi * h2r;
        data[i3] = hir - wr * h2r + wi * h2i;
        data[i4] = -h1i + wr * h2i + wi * h2r;
        wr = (wtemp = wr) * wpr - wi * wpi + wr;
        wi = wi * wpr + wtemp * wpi + wi;
    }
    
    /*  CALCULATE THE DC COMPONENT  */
    data[1] = (hir = data[1]) + data[2];
    
    /*  CALCULATE THE NYQUIST COMPONENT  */
    nyquist = data[2] = hir - data[2];
    
    /*  CALCULATE THE MAGNITUDE OF EACH COMPONENT BY TAKING THE GEOMETRIC
     MEAN OF THE REAL AND IMAGINARY PARTS, AND SCALING BY n.  ALSO,
     RE-ARRANGE THESE MAGNITUDES SO THAT THEY RANGE FROM HARMONICS 1 TO
     NYQUIST IN THE ORIGINAL DATA ARRAY, LEAVING OUT THE DC COMPONENT
     (SINCE IT IS NOT USUALLY NEEDED).  NOTE THAT THE UPPER HALF OF THE
     ORIGINAL DATA ARRAY WILL CONTAIN GARBAGE, AND SHOULD BE IGNORED  */
    for (i = 1; i < n; i++)
        data[i] = sqrt(SQR(data[(2*i)+1]) + SQR(data[(2*i)+2])) / (float)n;
    
    data[n] = fabs(nyquist) / (float)n;
}
Ejemplo n.º 8
0
void spctrm(FILE *fp, float p[], int m, int k, int ovrlap)
{
	void four1(float data[], unsigned long nn, int isign);
	int mm,m44,m43,m4,kk,joffn,joff,j2,j;
	float w,facp,facm,*w1,*w2,sumw=0.0,den=0.0;

	mm=m+m;
	m43=(m4=mm+mm)+3;
	m44=m43+1;
	w1=vector(1,m4);
	w2=vector(1,m);
	facm=m;
	facp=1.0/m;
	for (;j<=mm;j++) sumw += SQR(WINDOW(j,facm,facp));
	for (;j<=m;j++) p[j]=0.0;
	if (ovrlap)
		for (;j<=m;j++) fscanf(fp,"%f",&w2[j]);
	for (;kk<=k;kk++) {
		for (;joff<=0;joff++) {
			if (ovrlap) {
				for (;j<=m;j++) w1[joff+j+j]=w2[j];
				for (;j<=m;j++) fscanf(fp,"%f",&w2[j]);
				joffn=joff+mm;
				for (;j<=m;j++) w1[joffn+j+j]=w2[j];
			} else {
				for (;j<=m4;j+=2)
					fscanf(fp,"%f",&w1[j]);
			}
		}
		for (;j<=mm;j++) {
			j2=j+j;
			w=WINDOW(j,facm,facp);
			w1[j2] *= w;
			w1[j2-1] *= w;
		}
		four1(w1,mm,1);
		p[1] += (SQR(w1[1])+SQR(w1[2]));
		for (;j<=m;j++) {
			j2=j+j;
			p[j] += (SQR(w1[j2])+SQR(w1[j2-1])
				+SQR(w1[m44-j2])+SQR(w1[m43-j2]));
		}
		den += sumw;
	}
	den *= m4;
	for (;j<=m;j++) p[j] /= den;
	free_vector(w2,1,m);
	free_vector(w1,1,m4);
}
Ejemplo n.º 9
0
double * AudioCore::getAudioSampleFourier()
{
    int nn = AudioCore::FFTN * 2;
    if (primaryAudioData && latestAllocatedSize > nn) {
        if (fourierAudioData) delete[] fourierAudioData;
        fourierAudioData = new double[nn];


        qDebug() << "Fourier length is: " << nn;

        memcpy(fourierAudioData, primaryAudioData, nn * sizeof(double));
        qDebug() << "Processing FFT";
        four1(fourierAudioData, nn / 2, 1);

        qDebug() << "Returning FFT result";
        return fourierAudioData;
    }

    return NULL;
}
Ejemplo n.º 10
0
void realft(float *data, int n, int isign) {
/*-------------------------------------------------------------------------------------------
 USES four1
 Calculates the Fourier transform of a set of n real-valued data points. Replaces this data 
 (which is stored in array data(1:n)) by the positive frequency half of its complex Fourier 
 transform. The real-valued first and last components of the complex transform are returned 
 as elements data(1) and data(2), respectively. n must be a power of 2. This routine 
 also calculates the inverse transform of a complex data array if it is the transform of real 
 data. (Result in this case must be multiplied by 2/n.) 
-------------------------------------------------------------------------------------------*/
  int    i,i1,i2,i3,i4,n2p3; 
  float  c1,c2,h1i,h1r,h2i,h2r,wis,wrs;
  double theta,wi,wpi,wpr,wr,wtemp;    //Double precision for the trigonometric recurrences
  double PI=4.0*atan(1.0);   
  
  theta=PI/(n/2);                      //Initialize the recurrence
  c1=0.5;
  if (isign == 1) {
    c2=-0.5;
    four1(data,n/2,1);                 //The forward transform is here
  }   
  else {
    c2=0.5;                            //Otherwise set up for an inverse transform
    theta=-theta;
  }
  wpr=-2.0*sin(0.5*theta)*sin(0.5*theta);
  wpi=sin(theta);
  wr=1.0+wpr;
  wi=wpi;
  n2p3=n+3;
  for (i=2; i<= n/4; i++) {            //Case i=1 done separately below
    i1=2*i-1;
    i2=i1+1;
    i3=n2p3-i2;
    i4=i3+1;
    wrs=wr;
    wis=wi;
    h1r=c1*(data[i1]+data[i3]);    //The two separate transforms are separated out of data
    h1i=c1*(data[i2]-data[i4]);
    h2r=-c2*(data[i2]+data[i4]);
    h2i=c2*(data[i1]-data[i3]);
    data[i1]=h1r+wrs*h2r-wis*h2i;    //Here they are recombined to form the true transform
                                      //of the original real data
    data[i2]=h1i+wrs*h2i+wis*h2r;
    data[i3]=h1r-wrs*h2r+wis*h2i;
    data[i4]=-h1i+wrs*h2i+wis*h2r;
    wtemp=wr;                         //The recurrence
    wr=wr*wpr-wi*wpi+wr;
    wi=wi*wpr+wtemp*wpi+wi;
  }

  if (isign == 1) {
    h1r=data[1];
    data[1]=h1r+data[2];
    data[2]=h1r-data[2];         //Squeeze the first and last data together to get
  }                              //them all within the original array
  else {
    h1r=data[1];
    data[1]=c1*(h1r+data[2]);
    data[2]=c1*(h1r-data[2]);
    four1(data,n/2,-1);          //This is the inverse transform for the case isign-1
  } 
} 
Ejemplo n.º 11
0
void b2a(double *b, int n, double *a)
{
  double bmx, bm, p;
  int i, j, nn, nnc;
  char s[80];

  /* next bigger power of 2 */
  for (nn=1; nn<=n; nn*=2);
  nn *=2;
  /*nn = (int) (exp(log(2)*ceil(log((double)n)/log(2))))+1;*/

  /* size of arrays used for computation */
  nnc = nn*ZEROPAD;

  for (i=0; i<n; i++) {bf[i*2] = b[i*2]; bf[i*2+1] = b[i*2+1];}
  for (i=n; i<nnc; i++) {bf[i*2] = 0; bf[i*2+1] = 0.0;}
  four1(bf-1, nnc, 1);

  /* check to see (| Fourier (beta(x)) |) < 1 */

  for (i=0, bmx=0; i<nnc; i++)
  {
     bm = magsqr(bf,i);
     if (bm > bmx) 
	bmx = bm;
  }
  if (bmx >= 1.0)
     for (i=0; i<nnc; i++) 
     {
	bf[i*2] /= (sqrt(bmx)+MP);
	bf[i*2+1] /= (sqrt(bmx)+MP);
     }

  /* compute |alpha(x)| */
  for (i=0; i<nnc; i++) {
    am[i*2] = sqrt(1.0 - magsqr(bf,i));
  }

  /* compute the phase of alpha, mag and phase are HT pair */
  /* ABK - God, it took me forever to figure out what John did here...
     he wants the hilbert transform of the log magnitude, so what
     he does is to generate the analytic version of the signal knowing
     that the imaginary part will be the negative of the hilbert transform
     of the log magnitude....  this is why I took 261...
     */
  for (i=0; i<nnc; i++) {
    af[i*2] = log(am[i*2]); 
    af[i*2+1] = 0;
  }
  four1(af-1,nnc,1);
  for (i=1; i<(nnc/2)-1; i++) {	/* leave DC and halfway point untouched!! */
    af[i*2] *= 2.0;
    af[i*2+1] *= 2.0;
  }
  for (i=(nnc/2)+1; i<nnc; i++) {
    af[i*2] = 0.0;
    af[i*2+1] = 0.0;
  }
  four1(af-1,nnc,-1);
  for (i=0; i<nnc; i++) {
    af[i*2] /= nnc;
    af[i*2+1] /= nnc;
  }

  /* compute the minimum phase alpha */
  for (i=0; i<nnc; i++) {
    p = af[i*2+1];
    af[i*2] = am[i*2] * cos(-p);
    af[i*2+1] = am[i*2] * sin(-p);
  }

  /* compute the minimum phase alpha coefficients */
  four1(af-1, nnc, -1);
  for (i=0; i<n; i++)
  {
    j = n-i-1;
    a[j*2] = af[i*2]/nnc;
    a[j*2+1] = af[i*2+1]/nnc;
  }
}
Ejemplo n.º 12
0
Archivo: fft.c Proyecto: jys673/Synergy
void FftSolver()
{
    double dta[1024];
    int i,j, isign, width;
    struct _complex temp;

    isign = 1;
/* */
/* Fill in the complex array  */
/* */
    printf("Filling array Nres = %d \n", XRES);
    for  (i=0; i<YRES; i++) {
        for (j=0; j<XRES; j++) {
            cmp[i][j].r = 0.0;
            cmp[i][j].i = 0.0;
        }
    }
    for  (i=YRES/2-2; i<YRES/2+2; i++) {
        for (j=XRES/2-2; j<XRES/2+2; j++) {
            cmp[i][j].r = 10000.0;
            cmp[i][j].i = 0.0;
        }
    }
/* */
/* Fill in phiold and display it */
/* */
    for  (i=0; i<YRES; i++) {
        for  (j=0; j<XRES; j++) {
            phiold[i][j] = sqrt(pow(cmp[i][j].r,2.0) + pow(cmp[i][j].i,2.0));
        }
    }
    /***ShowDbl( XRES, YRES, XDIS, XDIS, phiold );***/

/* */
/* Transform the rows   */
/* */
    printf("Transforming rows \n");
    for  (i=0; i<YRES; i++) {
        four1( cmp[i], XRES, isign);
    }
/* */
/* Transpose and FTT again the new rows */
/* */
    printf("Transposing \n");
     for  ( i=0; i<XRES; i++ ) {
         for  ( j=i+1; j<YRES; j++ ) {
              temp = cmp[i][j];
              cmp[i][j] = cmp[j][i];
              cmp[j][i] = temp;
         }
     }

/*    Transpose(); */
    printf("Transforming columns \n");
    for  (i=0; i<YRES; i++) {
        four1( cmp[i], XRES, isign);
    }
    width = 512;
/*--------------------------------------------------------------------*/
/*    Now put the values in their proper places                       */
/*--------------------------------------------------------------------*/
    for  ( i=0; i<width; i++ ) {
        for  (j=0; j<width/2; j++ ) {
            temp.r = cmp[i][j].r;
            temp.i = cmp[i][j].i;
            cmp[i][j].r = cmp[i][j+width/2].r;
            cmp[i][j].i = cmp[i][j+width/2].i;
            cmp[i][j+width/2].r = temp.r;
            cmp[i][j+width/2].i = temp.i;
        }
    }
    for  ( i=0; i<width; i++ ) {
        for  (j=0; j<width/2; j++ ) {
            temp.r = cmp[j][i].r;
            temp.i = cmp[j][i].i;
            cmp[j][i].r = cmp[j+width/2][i].r;
            cmp[j][i].i = cmp[j+width/2][i].i;
            cmp[j+width/2][i].r = temp.r;
            cmp[j+width/2][i].i = temp.i;
        }
    }
/* */
/* Fill in phiold and sisplay it */
/* */
    printf("Displaying Results \n");
    for  (i=0; i<YRES; i++) {
        for  (j=0; j<XRES; j++) {
            phiold[i][j] = sqrt(pow(cmp[i][j].r,2.0) + pow(cmp[i][j].i,2.0));
            phiold[i][j] = log(1.0 + phiold[i][j]);
        }
    }
}
Ejemplo n.º 13
0
void main()
{
	int i;
	float dat1[SAMP_SIZE], fr1, j;
	char fname1[] = "fourtest1.txt";
	char fname2[] = "fourtest2.txt";
	char fname3[] = "fourtest3.txt";

	FILE *file= NULL;
	printf("Frequency : ");
	scanf("%f", &fr1);
	file = fopen("fourtest1.txt","w"); 

	if(file == NULL)
	{
		printf("Could not open the file");
//		exit(0);
	}
	
	for(i=0;i<SAMP_SIZE;i++)
	{
		j=i;
		dat1[i] = sin(2*M_PI *i*fr1/SAMP_F);
		//printf("\n%d\t%f",i,dat1[i]);
		fprintf(file,"%f\t%f\n", (j/SAMP_F),dat1[i]);  
	}

	
	plotdata(fname1);	

	fclose(file); 

	
	file = fopen("fourtest2.txt","w");
	if(file == NULL)
	{
		printf("Could not open the file");
//		exit(0);
	}

// Fourier Transform of dat1	
	four1(dat1, SAMP_SIZE /2, 1);

	for(i=2;i<SAMP_SIZE/2;i++)
	{	j=i;
		//printf("\n%d\t%f",i,dat1[i]);
		fprintf(file,"%f\t%f\n",(j-2)/4,dat1[i]); 
	}
	plotdata(fname2);	
	
	fclose(file); 	


	file = fopen("fourtest3.txt","w");
	if(file == NULL)
	{
		printf("Could not open the file");
//		exit(0);
	}
	

// Inverse fourier transform of the dat1
	four1(dat1, SAMP_SIZE /2, -1);

	for(i=0;i<SAMP_SIZE;i++)
	{	j=i;
		//printf("\n%d\t%f",i,dat1[i]);
		fprintf(file,"%f\t%f\n",j/SAMP_F,dat1[i]/(SAMP_F)); 
	}
	plotdata(fname3);	
	
	fclose(file); 	
}
Ejemplo n.º 14
0
void do_four(const char *fn, const char *cn, int nx, real x[], real dy[],
             real eps0, real epsRF, const output_env_t oenv)
{
    FILE      *fp, *cp;
    t_complex *tmp, gw, hw, kw;
    int        i, nnx, nxsav;
    real       fac, nu, dt, *ptr, maxeps, numax;

    nxsav = nx;
    /*while ((dy[nx-1] == 0.0) && (nx > 0))
       nx--;*/
    if (nx == 0)
    {
        gmx_fatal(FARGS, "Empty dataset in %s, line %d!", __FILE__, __LINE__);
    }

    nnx = 1;
    while (nnx < 2*nx)
    {
        nnx *= 2;
    }
    snew(tmp, 2*nnx);
    printf("Doing FFT of %d points\n", nnx);
    for (i = 0; (i < nx); i++)
    {
        tmp[i].re = dy[i];
    }
    ptr = &tmp[0].re;
    four1(ptr-1, nnx, -1);

    dt = x[1]-x[0];
    if (epsRF == 0)
    {
        fac = (eps0-1)/tmp[0].re;
    }
    else
    {
        fac = ((eps0-1)/(2*epsRF+eps0))/tmp[0].re;
    }
    fp     = xvgropen(fn, "Epsilon(\\8w\\4)", "Freq. (GHz)", "eps", oenv);
    cp     = xvgropen(cn, "Cole-Cole plot", "Eps'", "Eps''", oenv);
    maxeps = 0;
    numax  = 0;
    for (i = 0; (i < nxsav); i++)
    {
        if (epsRF == 0)
        {
            kw.re = 1+fac*tmp[i].re;
            kw.im = 1+fac*tmp[i].im;
        }
        else
        {
            gw     = rcmul(fac, tmp[i]);
            hw     = rcmul(2*epsRF, gw);
            hw.re += 1.0;
            gw.re  = 1.0 - gw.re;
            gw.im  = -gw.im;
            kw     = cdiv(hw, gw);
        }
        kw.im *= -1;

        nu     = (i+1)*1000.0/(nnx*dt);
        if (kw.im > maxeps)
        {
            maxeps = kw.im;
            numax  = nu;
        }

        fprintf(fp, "%10.5e  %10.5e  %10.5e\n", nu, kw.re, kw.im);
        fprintf(cp, "%10.5e  %10.5e\n", kw.re, kw.im);
    }
    printf("MAXEPS = %10.5e at frequency %10.5e GHz (tauD = %8.1f ps)\n",
           maxeps, numax, 1000/(2*M_PI*numax));
    ffclose(fp);
    ffclose(cp);
    sfree(tmp);
}
Ejemplo n.º 15
0
Archivo: fft.c Proyecto: BlinkD/vicare
static FLOAT test ()
{
  four1 (data, N);
  return data[0];
}
Ejemplo n.º 16
0
void NBImpedance::InducedField(Grid1D& efield, Grid1D& line_current_density)
{
	int n=efield.get_size();
	int N_t=time_window.size()/2;
	
	if ( N_t == 0) {
		for(int j=0;j<n;j++) efield[j]=0.0;
		return;
	}
	
	static int t=0;
	double bl_amp, bl_real, bl_imag, bl_phase; 
	double dz=efield.get_dz();
	
	double circum=dz*n;
	double bucket_h = circum/(double)harmonic;
	vektor current(n);
	for(int j=0; j<n; j++) current[j]=line_current_density[j]; 

	realft(current,1);

	if(t==0)
	{
		for(int j=0;j<2*N_t;j+=2)
		{
			time_window[j]=current[2*(int)harmonic];
			time_window[j+1]=current[2*(int)harmonic+1];
		}
		t=1;
	}

	time_window.pop_front();
	time_window.pop_front();
	time_window.push_back(current[2*(int)harmonic]);
	time_window.push_back(current[2*(int)harmonic+1]);

	vektor tem(2*N_t);
	for(int j=0;j<2*N_t;j++)
		tem[j]=time_window[j]; 
	four1(tem,1);

	for(int j=0;j<=N_t/2;j++)
	{ 
		komplex temc(tem[2*j],tem[2*j+1]);
		komplex temc2(0.0,0.0);
		temc2=-temc*Z[N_t/2-j];
		tem[2*j]=temc2.real(); 
		tem[2*j+1]=temc2.imag(); 
	}

	for(int j=1;j<N_t/2;j++)
	{ 
		komplex temc(tem[2*N_t-2*j-3],tem[2*N_t-2*j-2]);
		komplex temc2(0.0,0.0);
		temc2=-temc*Z[j+N_t/2];
		tem[2*N_t-2*j-3]=temc2.real(); 
		tem[2*N_t-2*j-2]=temc2.imag(); 
	}

	four1(tem,-1);

	bl_real=tem[2*N_t-2]/circum;
	bl_imag=tem[2*N_t-1]/circum;
	bl_amp   = sqrt(bl_real*bl_real+bl_imag*bl_imag) ;
	bl_phase = atan2(bl_imag,bl_real) ;

	for(int j=0;j<n;j++)
		efield[j]=bl_amp*cos(2.0*PI/bucket_h*(j+0.5)*dz-bl_phase);

	return;

}
Ejemplo n.º 17
0
int main(void)
{
	long i;
	int isign;
	float *data,*dcmp;

	data=vector(1,NN2);
	dcmp=vector(1,NN2);
	printf("h(t)=real-valued even-function\n");
	printf("h(n)=h(N-n) and real?\n");
	for (i=1;i<NN2;i+=2) {
		data[i]=1.0/(SQR((float) (i-NN-1)/NN)+1.0);
		data[i+1]=0.0;
	}
	isign=1;
	four1(data,NN,isign);
	prntft(data,NN);
	printf("h(t)=imaginary-valued even-function\n");
	printf("h(n)=h(N-n) and imaginary?\n");
	for (i=1;i<NN2;i+=2) {
		data[i+1]=1.0/(SQR((float) (i-NN-1)/NN)+1.0);
		data[i]=0.0;
	}
	isign=1;
	four1(data,NN,isign);
	prntft(data,NN);
	printf("h(t)=real-valued odd-function\n");
	printf("h(n) = -h(N-n) and imaginary?\n");
	for (i=1;i<NN2;i+=2) {
		data[i]=((float) (i-NN-1)/NN)/(SQR((float) (i-NN-1)/NN)+1.0);
		data[i+1]=0.0;
	}
	data[1]=0.0;
	isign=1;
	four1(data,NN,isign);
	prntft(data,NN);
	printf("h(t)=imaginary-valued odd-function\n");
	printf("h(n) = -h(N-n) and real?\n");
	for (i=1;i<NN2;i+=2) {
		data[i+1]=((float) (i-NN-1)/NN)/(SQR((float) (i-NN-1)/NN)+1.0);
		data[i]=0.0;
	}
	data[2]=0.0;
	isign=1;
	four1(data,NN,isign);
	prntft(data,NN);
	/* transform, inverse-transform test */
	for (i=1;i<NN2;i+=2) {
		data[i]=1.0/(SQR(0.5*(i-NN-1.0)/NN)+1.0);
		dcmp[i]=data[i];
		data[i+1]=(0.25*(i-NN-1.0)/NN)*exp(-SQR(0.5*(i-NN-1)/NN));
		dcmp[i+1]=data[i+1];
	}
	isign=1;
	four1(data,NN,isign);
	isign = -1;
	four1(data,NN,isign);
	printf("%23s %33s \n","original data:","double fourier transform:");
	printf("\n %3s %15s %12s %12s %12s \n",
		"k","real h(k)","imag h(k)","real h(k)","imag h(k)");
	for (i=1;i<NN;i+=2)
		printf("%4lu %14.6f %12.6f %12.6f %12.6f\n",
			(i+1)/2,dcmp[i],dcmp[i+1],data[i]/NN,data[i+1]/NN);
	free_vector(dcmp,1,NN2);
	free_vector(data,1,NN2);
	return 0;
}
Ejemplo n.º 18
0
Fluxrec *cross_corr_fft(float *flux1, float *flux2, int size, float dx)
{
  int i;                  /* Looping variable */
  int no_error=1;         /* Flag set to 0 on error */
  float re1,im1;          /* Real and imag. parts of FFT of flux1 */
  float re2,im2;          /* Real and imag. parts of FFT of flux2 */
  float nfact;            /* Normalization factor for FFTs */
  float *y1=NULL;         /* Fluxes from first array -- complex format */
  float *y2=NULL;         /* Fluxes from second array -- complex format */
  float *yptr1,*yptr2;    /* Pointers for y arrays */
  float *fptr1,*fptr2;    /* Pointers to navigate flux1 and flux2 */
  Fluxrec *correl=NULL;   /* Cross-correlation of flux1 and flux2 */
  Fluxrec *cptr;          /* Pointer to navigate correl */

  /*
   * Allocate memory for float arrays
   */

  if(!(y1 = new_array(2*size,1))) {
    return NULL;
  }

  if(!(y2 = new_array(2*size,1)))
    no_error = 0;

  /*
   * Allocate memory for the cross-correlation array
   */

  if(no_error) {
    if(!(correl = new_fluxrec(size))) {
      no_error = 0;
    }
  }

  /*
   * Put information from Fluxrec arrays into float arrays, which
   *  are in the Numerical Recipes "complex" format which consists.
   *  of alternating real and complex values.  Since our arrays
   *  are real, set all the imaginary members to zero.  Note that
   *  new_array automatically sets all of its members to zero, so
   *  all we have to do is fill in the real members of the arrays with
   *  the members of flux1 and flux2.
   */

  yptr1 = y1;
  yptr2 = y2;

  if(no_error) {
#if 0
    for(i=0,fptr1=flux1,fptr2=flux2; i<size; i++,fptr1++,fptr2++) {
      *yptr1 = *fptr1;
      *yptr2 = *fptr2;
      yptr1 += 2;
      yptr2 += 2;
    }
#endif
    for(i=0; i<size; i++) {
      y1[2*i] = flux1[i];
      y2[2*i] = flux2[i];
    }
  }

  /*
   * Do the cross-correlation
   */

  if(no_error) {

    /*
     * Fourier transform the two arrays
     */

    four1(y1-1,size,1);
    four1(y2-1,size,1);

    /*
     * Now we have the transform -> compute the correlation.
     */

    nfact = 1.0;

    for(i=0; i<size; i++) {
      re1 = y1[2*i];
      im1 = y1[2*i+1];
      re2 = y2[2*i];
      im2 = y2[2*i+1];

      /*
       * Store the correlation in the first complex array.
       */

      y1[2*i] = (re1*re2 + im1*im2)/nfact;
      y1[2*i+1] = (im1*re2 - im2*re1)/nfact;
    }

    /*
     * Transform back.
     */

    four1(y1-1,size,-1);
  }

  /*
   * Put the results into the cross correlation array
   */

  if(no_error) {
#if 0
    yptr1 = y1;
    for(i=0,cptr=correl; i<size/2; i++,cptr++) {
      yptr2 = yptr1 + size;
      cptr->day = (i-size/2)*dx;
      (cptr+size/2)->day = i*dx;
      cptr->flux = *yptr2;
      (cptr+size/2)->flux = *yptr1;
      yptr1 += 2;
    }
#endif
    for(i=0; i<size/2; i++) {
      correl[i].day = (i-size/2)*dx;
      correl[i+size/2].day = i*dx;
      correl[i].flux = y1[size+2*i];
      correl[i+size/2].flux = y1[2*i];
    }
  }

  /*
   * Clean up
   */

  y1 = del_array(y1);
  y2 = del_array(y2);

  if(no_error)
    return correl;
  else {
    fprintf(stderr,"ERROR: cross_corr_fft\n");
    return NULL;
  }
}
Ejemplo n.º 19
0
/**
 * This method is given a pointer to nFft short input samples and calculates
 * a power spectral estimate of the provided samples.
 */
int SiEstimator::Pse( short *input, int input_samples )
{
    int    n;
    short *src;
    int    nSamples;
    int    fftCount;

    if( mLog & M2_LOG_INPUT ){
        for(n=0;n<mNfftPoints;n++){
            printf("t=%d %d\n",n,input[n]);
        }
    }

    fftCount = 0;

    // FFTW Case
    if( mUseFftw ){
        src      = input;
        nSamples = input_samples;
        while( nSamples>=mNfftPoints ){
           for(n=0;n<mNfftPoints;n++){
              mInOut[n] = src[n] * mWin[n];
           }
           src      += (mNfftPoints/2);
           nSamples -= (mNfftPoints/2);
           fftCount++;

           fftw_execute( mFftwPlan );
           if( 1==fftCount ){
               for(n=0;n<mNfftPoints/2;n++){
                   mTmp[2*n]   = mFftwOutput[n][0];
                   mTmp[2*n+1] = mFftwOutput[n][1];
               }
           }else{
               for(n=0;n<mNfftPoints/2;n++){
                   mTmp[2*n]   += mFftwOutput[n][0];
                   mTmp[2*n+1] += mFftwOutput[n][1];
               }
           }
        }
    }

    // NR Case
    else{
        src      = input;
        nSamples = input_samples;
        while( nSamples>=mNfftPoints ){
           for(n=0;n<mNfftPoints;n++){
               mNrInOut[ 2*n     ] = src[n] * mWin[n];
               mNrInOut[ 2*n + 1 ] = 0.0;
           }
           src      += (mNfftPoints/2);
           nSamples -= (mNfftPoints/2);
           fftCount++;

           four1(mNrInOut - 2,mNfftPoints,1);
           if( 1==fftCount ){
               for(n=0;n<mNfftPoints/2;n++){
                   mTmp[2*n]   = mNrInOut[2*n];
                   mTmp[2*n+1] = mNrInOut[2*n+1];
               }
           }else{
               for(n=0;n<mNfftPoints/2;n++){
                   mTmp[2*n]   += mNrInOut[2*n];
                   mTmp[2*n+1] += mNrInOut[2*n+1];
               }
           }
        }
    }

    // Create final mag squared
    for(n=0;n<mNfftPoints/2;n++){
       mInOut[n] = mTmp[2*n  ]*mTmp[2*n  ] + 
                   mTmp[2*n+1]*mTmp[2*n+1];
    }

    if( mLog & M2_LOG_OUTPUT ){
        for(n=0;n<mNfftPoints/2;n++){
            printf("f=%d %f\n",n,mInOut[n]);
        }
    }
    return( fftCount );
}