Пример #1
0
void BBImpedance::InducedField(Grid1D& efield, Grid1D& line_current_density)
{
	double bl_real, bl_imag; 
	double dz=efield.get_dz();
	int n=efield.get_size();
	double circum=dz*n;
	vektor current(n);
	for(int j=0; j<n; j++) current[j]=line_current_density[j]; 

	realft(current,1);

	for(int h=0; h < n/2-1; h++)
	{

		komplex temc(current[2*h],current[2*h+1]);
		komplex temc2=-temc*Z[h];

		bl_real = temc2.real()/circum; 
		bl_imag = temc2.imag()/circum;         

		current[2*h]=bl_real;
		current[2*h+1]=bl_imag;
	}

	realft(current,-1);

	for(int j=0; j<n; j++)
		efield[j]=current[j];    

	return;
}
Пример #2
0
void BBImpedance::InducedPotential(Grid1D& potential, Grid1D& line_current_density)
{
	komplex i(0.0,1.0);
	double bl_real=0.0, bl_imag=0.0; 
	double dz=potential.get_dz();
	int n=potential.get_size();
	double circum=dz*n;
	double R=circum/(2.0*PI);
	vektor current(n);
	for(int j=0; j<n; j++) current[j]=line_current_density[j]; 

	realft(current,1);

	for(int h=1; h < n/2-1; h++)
	{

		komplex temc(current[2*h],current[2*h+1]);
		komplex temc2=-i*temc*Z[h]*R/((double)h);

		bl_real = temc2.real(); 
		bl_imag = temc2.imag();         

		current[2*h]=bl_real;
		current[2*h+1]=bl_imag;
	}

	realft(current,-1);

	for(int j=0; j<n; j++)
		potential[j]=current[j];    

	return;
}
Пример #3
0
void fasper(float x[], float y[], unsigned long n, float ofac, float hifac,
	float wk1[], float wk2[], unsigned long nwk, unsigned long *nout,
	unsigned long *jmax, float *prob)
{
	void avevar(float data[], unsigned long n, float *ave, float *var);
	void realft(float data[], unsigned long n, int isign);
	void spread(float y, float yy[], unsigned long n, float x, int m);
	unsigned long j,k,ndim,nfreq,nfreqt;
	float ave,ck,ckk,cterm,cwt,den,df,effm,expy,fac,fndim,hc2wt;
	float hs2wt,hypo,pmax,sterm,swt,var,xdif,xmax,xmin;

	*nout=0.5*ofac*hifac*n;
	nfreqt=ofac*hifac*n*MACC;
	nfreq=64;
	while (nfreq < nfreqt) nfreq <<= 1;
	ndim=nfreq << 1;
	if (ndim > nwk) nrerror("workspaces too small in fasper");
	avevar(y,n,&ave,&var);
	if (var == 0.0) nrerror("zero variance in fasper");
	xmin=x[1];
	xmax=xmin;
	for (j=2;j<=n;j++) {
		if (x[j] < xmin) xmin=x[j];
		if (x[j] > xmax) xmax=x[j];
	}
	xdif=xmax-xmin;
	for (j=1;j<=ndim;j++) wk1[j]=wk2[j]=0.0;
	fac=ndim/(xdif*ofac);
	fndim=ndim;
	for (j=1;j<=n;j++) {
		ck=(x[j]-xmin)*fac;
		MOD(ck,fndim)
		ckk=2.0*(ck++);
		MOD(ckk,fndim)
		++ckk;
		spread(y[j]-ave,wk1,ndim,ck,MACC);
		spread(1.0,wk2,ndim,ckk,MACC);
	}
	realft(wk1,ndim,1);
	realft(wk2,ndim,1);
	df=1.0/(xdif*ofac);
	pmax = -1.0;
	for (k=3,j=1;j<=(*nout);j++,k+=2) {
		hypo=sqrt(wk2[k]*wk2[k]+wk2[k+1]*wk2[k+1]);
		hc2wt=0.5*wk2[k]/hypo;
		hs2wt=0.5*wk2[k+1]/hypo;
		cwt=sqrt(0.5+hc2wt);
		swt=SIGN(sqrt(0.5-hc2wt),hs2wt);
		den=0.5*n+hc2wt*wk2[k]+hs2wt*wk2[k+1];
		cterm=SQR(cwt*wk1[k]+swt*wk1[k+1])/den;
		sterm=SQR(cwt*wk1[k+1]-swt*wk1[k])/(n-den);
		wk1[j]=j*df;
		wk2[j]=(cterm+sterm)/(2.0*var);
		if (wk2[j] > pmax) pmax=wk2[(*jmax=j)];
	}
	expy=exp(-pmax);
	effm=2.0*(*nout)/ofac;
	*prob=effm*expy;
	if (*prob > 0.01) *prob=1.0-pow(1.0-expy,effm);
}
Пример #4
0
void SMOOFT(float *Y, int N, int PTS) {
/*--------------------------------------------------------------
  Smooths an array Y of length N, with a window whose full width
  is of order PTS neighboring points, a user supplied value. 
  Array Y is modified.
--------------------------------------------------------------*/
//Label: e1
  int   J,K,M,MO2,NMIN;
  float CNST,FAC,RN1,Y1,YN;
  float ONE=1.0;
  float QUART=0.25;
  float ZERO=0.0;
  
  M=2;
  NMIN=N + 2*PTS;
e1:if (M < NMIN) {
    M=2*M;
    goto e1;
   }
  if (M > NMAX) {
    printf(" Sample too big.\n");
    return;
  }
  CNST=(ONE*PTS/M)*(ONE*PTS/M);
  Y1=Y[1];
  YN=Y[N];
  RN1=ONE/(N-1);
  for (J=1; J<=N; J++)      //Remove linear trend
    Y[J]=Y[J]-RN1*(Y1*(N-J)+YN*(J-1));
  if (N+1 <= M)
    for (J=N+1; J<=M; J++)  Y[J]=0.0;
  MO2=M/2;
  realft(Y,MO2,1);      //Fourier transform
  Y[1]=Y[1]/MO2;
  FAC=1.0;
  for (J=1; J<MO2; J++) {
    K=2*J+1;
    if (FAC != 0.0) {
      FAC=MAX(ZERO,(ONE-CNST*J*J)/MO2);
      Y[K]=FAC*Y[K];
      Y[K+1]=FAC*Y[K+1];
    }
    else {
      Y[K]=0.0;
      Y[K+1]=0.0;
    }
  }
  FAC=MAX(ZERO,(ONE-QUART*PTS*PTS)/MO2);  //Last point
  Y[2]=FAC*Y[2];
  realft(Y,MO2,-1);       //Inverse Fourier transform
  for (J=1; J<=N; J++)    //Restore linear trend
    Y[J]=RN1*(Y1*(N-J)+YN*(J-1))+Y[J];
}
Пример #5
0
void convlv(float data[], unsigned long n, float respns[], unsigned long m,
	int isign, float ans[])
{
	void realft(float data[], unsigned long n, int isign);
	void twofft(float data1[], float data2[], float fft1[], float fft2[],
		unsigned long n);
	unsigned long i,no2;
	float dum,mag2,*fft;

	fft=vector(1,n<<1);
	for (i=1;i<=(m-1)/2;i++)
		respns[n+1-i]=respns[m+1-i];
	for (i=(m+3)/2;i<=n-(m-1)/2;i++)
		respns[i]=0.0;
	twofft(data,respns,fft,ans,n);
	no2=n>>1;
	for (i=2;i<=n+2;i+=2) {
		if (isign == 1) {
			ans[i-1]=(fft[i-1]*(dum=ans[i-1])-fft[i]*ans[i])/no2;
			ans[i]=(fft[i]*dum+fft[i-1]*ans[i])/no2;
		} else if (isign == -1) {
			if ((mag2=SQR(ans[i-1])+SQR(ans[i])) == 0.0)
				nrerror("Deconvolving at response zero in convlv");
			ans[i-1]=(fft[i-1]*(dum=ans[i-1])+fft[i]*ans[i])/mag2/no2;
			ans[i]=(fft[i]*dum-fft[i-1]*ans[i])/mag2/no2;
		} else nrerror("No meaning for isign in convlv");
	}
	ans[2]=ans[n+1];
	realft(ans,n,-1);
	free_vector(fft,1,n<<1);
}
Пример #6
0
void sinft(float y[], int n)
{
	void realft(float data[], unsigned long n, int isign);
	int j,n2=n+2;
	float sum,y1,y2;
	double theta,wi=0.0,wr=1.0,wpi,wpr,wtemp;

	theta=3.14159265358979/(double) n;
	wtemp=sin(0.5*theta);
	wpr = -2.0*wtemp*wtemp;
	wpi=sin(theta);
	y[1]=0.0;
	for (j=2;j<=(n>>1)+1;j++) {
		wr=(wtemp=wr)*wpr-wi*wpi+wr;
		wi=wi*wpr+wtemp*wpi+wi;
		y1=wi*(y[j]+y[n2-j]);
		y2=0.5*(y[j]-y[n2-j]);
		y[j]=y1+y2;
		y[n2-j]=y1-y2;
	}
	realft(y,n,1);
	y[1]*=0.5;
	sum=y[2]=0.0;
	for (j=1;j<=n-1;j+=2) {
		sum += y[j];
		y[j]=y[j+1];
		y[j+1]=sum;
	}
}
Пример #7
0
void cosft1(float y[], int n)
{
	void realft(float data[], unsigned long n, int isign);
	int j,n2;
	float sum,y1,y2;
	double theta,wi=0.0,wpi,wpr,wr=1.0,wtemp;

	theta=PI/n;
	wtemp=sin(0.5*theta);
	wpr = -2.0*wtemp*wtemp;
	wpi=sin(theta);
	sum=0.5*(y[1]-y[n+1]);
	y[1]=0.5*(y[1]+y[n+1]);
	n2=n+2;
	for (j=2;j<=(n>>1);j++) {
		wr=(wtemp=wr)*wpr-wi*wpi+wr;
		wi=wi*wpr+wtemp*wpi+wi;
		y1=0.5*(y[j]+y[n2-j]);
		y2=(y[j]-y[n2-j]);
		y[j]=y1-wi*y2;
		y[n2-j]=y1+wi*y2;
		sum += wr*y2;
	}
	realft(y,n,1);
	y[n+1]=y[2];
	y[2]=sum;
	for (j=4;j<=n;j+=2) {
		sum += y[j];
		y[j]=sum;
	}
}
void FourierCallback(void *arg){
	unsigned char channel;
	
	//send the control byte to indicate the start of the ffts
	sendByte(DATA_MAXIMUM);
	
	//iterate over each channel, only doing stuff if we're using that channel
	for(channel = 0;channel < NUM_CHANNELS;channel++)
	{
		if(CHANNEL_IS_ENABLED(status, channel))
		{			
			copyDataToTempArray(channel);
			
			/*note: realft() is written for a array with starting index of 1 (array[1] is the first element)
			obviously c doesn't work like that, so we just subtract one from the address to make it work.
			this is specifically mentioned in the first complete paragraph on page 507 of "NUMERICAL RECIPES in C"
			http://www.mathcs.org/java/programs/FFT/FFTInfo/c12-2.pdf
			*/
			#pragma GCC diagnostic ignored "-Warray-bounds"
			realft(transformData-1, DATA_LENGTH);
			#pragma GCC diagnostic pop
	
			//bin the data to be sent
			transformToBins(transformData, DATA_LENGTH, transformBins, &transformScalingValue);
			
			
			//send away the data
			sendTransformData(transformScalingValue, transformBins);
		}
	}
}
Пример #9
0
void NR::convlv(Vec_I_DP &data, Vec_I_DP &respns, const int isign,
	Vec_O_DP &ans)
{
	int i,no2;
	DP mag2,tmp;

	int n=data.size();
	int m=respns.size();
	Vec_DP temp(n);
	temp[0]=respns[0];
	for (i=1;i<(m+1)/2;i++) {
		temp[i]=respns[i];
		temp[n-i]=respns[m-i];
	}
	for (i=(m+1)/2;i<n-(m-1)/2;i++)
		temp[i]=0.0;
	for (i=0;i<n;i++)
		ans[i]=data[i];
	realft(ans,1);
	realft(temp,1);
	no2=n>>1;
	if (isign == 1) {
		for (i=2;i<n;i+=2) {
			tmp=ans[i];
			ans[i]=(ans[i]*temp[i]-ans[i+1]*temp[i+1])/no2;
			ans[i+1]=(ans[i+1]*temp[i]+tmp*temp[i+1])/no2;
		}
		ans[0]=ans[0]*temp[0]/no2;
		ans[1]=ans[1]*temp[1]/no2;
	} else if (isign == -1) {
		for (i=2;i<n;i+=2) {
			if ((mag2=SQR(temp[i])+SQR(temp[i+1])) == 0.0)
				nrerror("Deconvolving at response zero in convlv");
			tmp=ans[i];
			ans[i]=(ans[i]*temp[i]+ans[i+1]*temp[i+1])/mag2/no2;
			ans[i+1]=(ans[i+1]*temp[i]-tmp*temp[i+1])/mag2/no2;
		}
		if (temp[0] == 0.0 || temp[1] == 0.0)
			nrerror("Deconvolving at response zero in convlv");
		ans[0]=ans[0]/temp[0]/no2;
		ans[1]=ans[1]/temp[1]/no2;
	} else nrerror("No meaning for isign in convlv");
	realft(ans,-1);
}
Пример #10
0
void dftint(float (*func)(float), float a, float b, float w, float *cosint,
	float *sinint)
{
	void dftcor(float w, float delta, float a, float b, float endpts[],
		float *corre, float *corim, float *corfac);
	void polint(float xa[], float ya[], int n, float x, float *y, float *dy);
	void realft(float data[], unsigned long n, int isign);
	static int init=0;
	int j,nn;
	static float aold = -1.e30,bold = -1.e30,delta,(*funcold)(float);
	static float data[NDFT+1],endpts[9];
	float c,cdft,cerr,corfac,corim,corre,en,s;
	float sdft,serr,*cpol,*spol,*xpol;

	cpol=vector(1,MPOL);
	spol=vector(1,MPOL);
	xpol=vector(1,MPOL);
	if (init != 1 || a != aold || b != bold || func != funcold) {
		init=1;
		aold=a;
		bold=b;
		funcold=func;
		delta=(b-a)/M;
		for (j=1;j<=M+1;j++)
			data[j]=(*func)(a+(j-1)*delta);
		for (j=M+2;j<=NDFT;j++)
			data[j]=0.0;
		for (j=1;j<=4;j++) {
			endpts[j]=data[j];
			endpts[j+4]=data[M-3+j];
		}
		realft(data,NDFT,1);
		data[2]=0.0;
	}
	en=w*delta*NDFT/TWOPI+1.0;
	nn=IMIN(IMAX((int)(en-0.5*MPOL+1.0),1),NDFT/2-MPOL+1);
	for (j=1;j<=MPOL;j++,nn++) {
		cpol[j]=data[2*nn-1];
		spol[j]=data[2*nn];
		xpol[j]=nn;
	}
	polint(xpol,cpol,MPOL,en,&cdft,&cerr);
	polint(xpol,spol,MPOL,en,&sdft,&serr);
	dftcor(w,delta,a,b,endpts,&corre,&corim,&corfac);
	cdft *= corfac;
	sdft *= corfac;
	cdft += corre;
	sdft += corim;
	c=delta*cos(w*a);
	s=delta*sin(w*a);
	*cosint=c*cdft-s*sdft;
	*sinint=s*cdft+c*sdft;
	free_vector(cpol,1,MPOL);
	free_vector(spol,1,MPOL);
	free_vector(xpol,1,MPOL);
}
Пример #11
0
void NR::mpmul(Vec_O_UCHR &w, Vec_I_UCHR &u, Vec_I_UCHR &v)
{
	const DP RX=256.0;
	int j,n_max,nn=1;
	DP cy,t;

	int n=u.size();
	int m=v.size();
	int p=w.size();
	n_max=MAX(m,n);
	while (nn < n_max) nn <<= 1;
	nn <<= 1;
	Vec_DP a(0.0,nn),b(0.0,nn);
	for (j=0;j<n;j++) a[j]=u[j];
	for (j=0;j<m;j++) b[j]=v[j];
	realft(a,1);
	realft(b,1);
	b[0] *= a[0];
	b[1] *= a[1];
	for (j=2;j<nn;j+=2) {
		b[j]=(t=b[j])*a[j]-b[j+1]*a[j+1];
		b[j+1]=t*a[j+1]+b[j+1]*a[j];
	}
	realft(b,-1);
	cy=0.0;
	for (j=nn-1;j>=0;j--) {
		t=b[j]/(nn >> 1)+cy+0.5;
		cy=(unsigned long) (t/RX);
		b[j]=t-cy*RX;
	}
	if (cy >= RX) nrerror("cannot happen in mpmul");
	for (j=0;j<p;j++) w[j]=0;
	w[0]=(unsigned char) cy;
	for (j=1;j<MIN(n+m,p);j++)
		w[j]=(unsigned char) b[j-1];
}
Пример #12
0
main(int argc, char *argv[])
{
  FILE *fileptr;
  int i,numsamps,nfft,nspc,headersize,binmin;
  float *data, *amplitude, ar, ai, arl, ail, a1, a2;
  double frequency, pmax=9.99999;

  fileptr=open_file(argv[1],"rb");

  if (!(headersize=read_header(fileptr))) {
    error_message("could not read header parameters!");
    exit(1);
  }

  numsamps=nsamples(argv[1],headersize,nbits,nifs,nchans);
  nfft=(int)pow(2.0,(double)np2(numsamps));
  nspc=nfft/2;
  data=(float *) malloc(sizeof(float)*numsamps);
  
  printf("#Reading in %d samples from file: %s...\n",numsamps,argv[1]);
  if ((read_block(fileptr,nbits,data,numsamps)) != numsamps) {
    error_message("error reading data");
  }

  printf("#Performing 2^%d point FFT...\n",np2(numsamps));
  realft(data-1,(unsigned long) nfft,1);

  printf("#Forming amplitude spectrum...\n");
  amplitude=(float *) malloc(nspc*sizeof(float));
  binmin=fbin(tsamp,nspc,1,1.0/pmax);
  ar=ai=arl=ail=a1=a2=0.0;
  for (i=0;i<nspc;i++) {
    if (i<binmin) 
      amplitude[i]=0.0;
    else {
      ar=data[2*i];
      ai=data[2*i+1];
      a1=ar*ar+ai*ai;
      a2=0.5*((ar-arl)*(ar-arl)+(ai-ail)*(ai-ail));
      amplitude[i]=(a1>a2)?a1:a2;
      arl=ar;
      ail=ai;
    }
    /*printf("%f %f\n",ffreq(tsamp,nspc,1,i+1),amplitude[i]);*/
  }
  free(data);

}
Пример #13
0
void correl(real data1[],real data2[],int n,real ans[])
{
  int     no2,i;
  real dum,*fft;

  snew(fft,2*n+1);
  twofft(data1,data2,fft,ans,n);
  no2=n/2;
  for (i=2;i<=n+2;i+=2) {
    dum      = ans[i-1];
    ans[i-1] = (fft[i-1]*dum+fft[i]*ans[i])/no2;
    ans[i]   = (fft[i]*dum-fft[i-1]*ans[i])/no2;
  }
  ans[2]=ans[n+1];
  realft(ans,no2,-1);
  sfree(fft);
}
Пример #14
0
void correl(float data1[], float data2[], unsigned long n, float ans[])
{
	void realft(float data[], unsigned long n, int isign);
	void twofft(float data1[], float data2[], float fft1[], float fft2[],
		unsigned long n);
	unsigned long no2,i;
	float dum,*fft;

	fft=vector(1,n<<1);
	twofft(data1,data2,fft,ans,n);
	no2=n>>1;
	for (i=2;i<=n+2;i+=2) {
		ans[i-1]=(fft[i-1]*(dum=ans[i-1])+fft[i]*ans[i])/no2;
		ans[i]=(fft[i]*dum-fft[i-1]*ans[i])/no2;
	}
	ans[2]=ans[n+1];
	realft(ans,n,-1);
	free_vector(fft,1,n<<1);
}
Пример #15
0
void cosft2(float y[], int n, int isign)
{
	void realft(float data[], unsigned long n, int isign);
	int i
	float sum,sum1,y1,y2,ytemp;
	doub;e theta,wi=0.0,wi1,wpi,wpr,wr=1.0,wr1,wtemp;
	
	theta=0.5*PI/n;
	wr1=cos(theta);
	wi1=sin(theta);
	wpr = -2.0*wi1*wi1;
	wpi=sin(2.0*theta);
	if (isign ==1) {
		for (i=1;i<n/2,i++) {
			y1=0.5*(y[i]+y[n-i+1]);
			y2=wi1*(y[i]-y[n-i+1]);
			y[i]=y1+y2;
			y[n-i+1]=y1-y2;
			wr1=(wtemp=wr1)*wpr-wi1*wpi+wr1;
			wi1=wi1*wpr+wtemp*wpi+wi1;
		}
		realft(y,n,1);
		for (i=2;i<=n; i+=2) {
			wr=(wtemp=wr)*wpr-wi*wpi+wr;
			wi=wi*wpr+wtemp*wpi+wr;
			wi = wi*wpr+wtemp*wpi+wi;
			y1=y[i]*wr-y[i+1]*wi;
			y2=y[i+1]*wr+y[i]*wi;
			y[i]=y1;
			y[i+1]=y2;
		}
		sum=0.5*y2[2];
		for (i=2;i>2;i-=2) {
			sum1=sum;
			sum += y[i];
			y[i]=sum1;
		}
	} else if (isign == -1( {
Пример #16
0
void poisson_rz(Grid2D& rho)
{
 int j,m;
 int NR=rho.get_NX();
 int NZ=rho.get_NY();
 double dr=rho.get_dx();
 double dz=rho.get_dy();

 double *q1=new double[NR];
 double *q2=new double[NR];
 double *tem=new double[NZ];

 for(j=0; j<NR; j++)
   {
    for(m=0;m<NZ;m++)
     tem[m]=rho[m+NZ*j];
    realft(tem-1,NZ,1);
    //cosft1(tem-1,NZ);
    //sinft(tem-1,NZ);
    for(m=0;m<NZ;m++)
     rho[m+NZ*j]=tem[m];
   }

 for(m=2;m<NZ;m+=2)
    {
     for(j=0;j<NR;j++)
      {
       q1[j]=rho[m+NZ*j];
       q2[j]=rho[m+1+NZ*j];
      }       
     poisson_r(q1,(int)(0.5*m),NR,NZ,dr,dz);
     poisson_r(q2,(int)(0.5*m),NR,NZ,dr,dz);
     for(j=0;j<NR;j++)
      {
       rho[m+NZ*j]=q1[j];
       rho[m+1+NZ*j]=q2[j];
      }    
    }

  for(j=0;j<NR;j++)
       q1[j]=rho[1+NZ*j];
  poisson_r(q1,(int)(0.5*NZ),NR,NZ,dr,dz);
  for(j=0;j<NR;j++)
     rho[1+NZ*j]=q1[j];


  for(j=0;j<NR;j++)               
    q1[j]=rho[j*NZ];          
  poisson_r(q1,0,NR,NZ,dr,dz);      


  for(j=0;j<NR;j++)
    rho[0+NZ*j]=q1[j]; 


  for(j=0; j<NR; j++)
    { 
     for(m=0;m<NZ;m++)
      tem[m]=rho[m+NZ*j];
     realft(tem-1,NZ,-1);
     //cosft1(tem-1,NZ);
     //sinft(tem-1,NZ);
     for(m=0;m<NZ;m++)
      rho[m+NZ*j]=2.0*tem[m]/NZ;     
    }

  delete q1;
  delete q2;
  delete tem;
}
Пример #17
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;

}
Пример #18
0
/* 
 * Get the MFCC feature for a raw voice data. You can get the sample from the
 * wav filer header.
 * 
 * @param data the raw data the wav file
 * @param len the length of the wav raw data
 * @param sample_rate sample frequency in Hz (8000 or 16000 or 22050 or 44000)
 * @param mfcc mfcc (39 dimension, in the program)
 * @param frame_time the time(in ms) you take it as a frame
 * @param step_time the time(in ms) between two neighbor frame
 * 
 */
int array_to_mfcc(const double* data, const int len, int sample_rate,
                  double*** mfcc_data, double frame_time, double step_time) {
    int i, j;
    /* (frame size / sameple rate) * 1000 = frame time */
    // int frame_size = sample_rate * frame_time / 1000;
    // int move_size = sample_rate * step_time / 1000;
    int frame_size = 240;
    int move_size = 80;
    int n = (len - frame_size) / move_size + 1; /* total number of frames */
    int tmp = log(frame_size * 1.0) / log(2.0);
    double** mfcc; /* temp data for storing mfcc */
    double* tmp_data; /* temp data for storing each frame */
    double* lo_chan;
    double* lo_wt;
    double* cep_win; /* weighted window for MFCC */
    double* fbank; /* triangular bandpass filter */
    int fft_n; /*  */

    if ((int)pow(2.0, tmp * 1.0) + 0.5 >= frame_size) {
        fft_n = (int)pow(2.0, tmp) + 0.5;
    } else {
        fft_n = (int)pow(2.0, tmp + 1) + 0.5;
    }

    mfcc = (double**) calloc(n, sizeof(double*));
    for (i = 0; i < n; ++i) {
        mfcc[i] = (double*) calloc(MFCC_DIMENSION, sizeof(double));
    }

    tmp_data = (double*) calloc(fft_n, sizeof(double));
    lo_chan = (double*) calloc(fft_n, sizeof(double));
    lo_wt = (double*) calloc(fft_n, sizeof(double));
    cep_win = (double*) calloc(12, sizeof(double));
    fbank = (double*) calloc(27, sizeof(double));

    gen_cep_win(cep_win, 22, 12);
    for (i = 0; i < n; ++i) {
        for (j = 0; j < frame_size; ++j) {
            tmp_data[j] = data[i * move_size + j];
        }
        for (j = frame_size; j < fft_n; ++j) {
            tmp_data[j] = 0.0;
        }

        mfcc[i][12] = cpt_energy(tmp_data, frame_size);
        pre_emphasise(tmp_data, frame_size);
        hamming(tmp_data, frame_size);
        realft(tmp_data, fft_n);
        init_mel(lo_chan, lo_wt, sample_rate, fft_n);
        wave_to_fbank(tmp_data, fbank, lo_chan, lo_wt, fft_n);
        fbank_to_mfcc(mfcc[i], fbank, 12);
        weight_cep(mfcc[i], cep_win, 12);
    }
    get_delta_cep(mfcc, n, 13, 2);
    zero_mean(mfcc, n, 12);

    *mfcc_data = mfcc; /*  */

    free(tmp_data);
    free(lo_chan);
    free(lo_wt);
    free(cep_win);
    free(fbank);

    return n;
}