示例#1
0
/** computes the inverse discrete Radon transform of Rf
 *  on the grid given by gridfcn() with T angles and R offsets
 *  by a NFFT-based CG-type algorithm
 */
int Inverse_Radon_trafo(int (*gridfcn)(), int T, int S, double *Rf, int NN, double *f, int max_i)
{
  int j,k;                              /**< index for nodes and freqencies   */
  nfft_plan my_nfft_plan;               /**< plan for the nfft-2D             */
  solver_plan_complex my_infft_plan;             /**< plan for the inverse nfft        */

  fftw_complex *fft;                    /**< variable for the fftw-1Ds        */
  fftw_plan my_fftw_plan;               /**< plan for the fftw-1Ds            */

  int t,r;                              /**< index for directions and offsets */
  double *x, *w;                        /**< knots and associated weights     */
  int l;                                /**< index for iterations             */

  int N[2],n[2];
  int M=T*S;

  N[0]=NN; n[0]=2*N[0];
  N[1]=NN; n[1]=2*N[1];

  fft = (fftw_complex *)nfft_malloc(S*sizeof(fftw_complex));
  my_fftw_plan = fftw_plan_dft_1d(S,fft,fft,FFTW_FORWARD,FFTW_MEASURE);

  x = (double *)nfft_malloc(2*T*S*(sizeof(double)));
  if (x==NULL)
    return -1;

  w = (double *)nfft_malloc(T*S*(sizeof(double)));
  if (w==NULL)
    return -1;

  /** init two dimensional NFFT plan */
  nfft_init_guru(&my_nfft_plan, 2, N, M, n, 4,
                  PRE_PHI_HUT| PRE_PSI| MALLOC_X | MALLOC_F_HAT| MALLOC_F| FFTW_INIT | FFT_OUT_OF_PLACE,
                  FFTW_MEASURE| FFTW_DESTROY_INPUT);

  /** init two dimensional infft plan */
  solver_init_advanced_complex(&my_infft_plan,(nfft_mv_plan_complex*)(&my_nfft_plan), CGNR | PRECOMPUTE_WEIGHT);

  /** init nodes and weights of grid*/
  gridfcn(T,S,x,w);
  for(j=0;j<my_nfft_plan.M_total;j++)
  {
    my_nfft_plan.x[2*j+0] = x[2*j+0];
    my_nfft_plan.x[2*j+1] = x[2*j+1];
    if (j%S)
      my_infft_plan.w[j]    = w[j];
    else
      my_infft_plan.w[j]    = 0.0;
  }

  /** precompute psi, the entries of the matrix B */
  if(my_nfft_plan.nfft_flags & PRE_LIN_PSI)
    nfft_precompute_lin_psi(&my_nfft_plan);

  if(my_nfft_plan.nfft_flags & PRE_PSI)
    nfft_precompute_psi(&my_nfft_plan);

  if(my_nfft_plan.nfft_flags & PRE_FULL_PSI)
    nfft_precompute_full_psi(&my_nfft_plan);

  /** compute 1D-ffts and init given samples and weights */
  for(t=0; t<T; t++)
  {
/*    for(r=0; r<R/2; r++)
       fft[r] = cexp(I*KPI*r)*Rf[t*R+(r+R/2)];
      for(r=0; r<R/2; r++)
       fft[r+R/2] = cexp(I*KPI*r)*Rf[t*R+r];
 */

    for(r=0; r<S; r++)
      fft[r] = Rf[t*S+r] + _Complex_I*0.0;

    nfft_fftshift_complex(fft, 1, &S);
    fftw_execute(my_fftw_plan);
    nfft_fftshift_complex(fft, 1, &S);

    my_infft_plan.y[t*S] = 0.0;
    for(r=-S/2+1; r<S/2; r++)
      my_infft_plan.y[t*S+(r+S/2)] = fft[r+S/2]/KERNEL(r);
  }

  /** initialise some guess f_hat_0 */
  for(k=0;k<my_nfft_plan.N_total;k++)
    my_infft_plan.f_hat_iter[k] = 0.0 + _Complex_I*0.0;

  /** solve the system */
  solver_before_loop_complex(&my_infft_plan);

  if (max_i<1)
  {
    l=1;
    for(k=0;k<my_nfft_plan.N_total;k++)
      my_infft_plan.f_hat_iter[k] = my_infft_plan.p_hat_iter[k];
  }
  else
  {
    for(l=1;l<=max_i;l++)
    {
      solver_loop_one_step_complex(&my_infft_plan);
      /*if (sqrt(my_infft_plan.dot_r_iter)<=1e-12) break;*/
    }
  }
  /*printf("after %d iteration(s): weighted 2-norm of original residual vector = %g\n",l-1,sqrt(my_infft_plan.dot_r_iter));*/

  /** copy result */
  for(k=0;k<my_nfft_plan.N_total;k++)
    f[k] = creal(my_infft_plan.f_hat_iter[k]);

  /** finalise the plans and free the variables */
  fftw_destroy_plan(my_fftw_plan);
  nfft_free(fft);
  solver_finalize_complex(&my_infft_plan);
  nfft_finalize(&my_nfft_plan);
  nfft_free(x);
  nfft_free(w);
  return 0;
}
示例#2
0
 FftAdapter::FftAdapter(unsigned int fs){
   frameSize = fs;
   input  = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*frameSize);
   output = (fftw_complex*)fftw_malloc(sizeof(fftw_complex)*frameSize);
   plan = fftw_plan_dft_1d(frameSize, input, output, FFTW_FORWARD, FFTW_ESTIMATE);
 }
示例#3
0
void zfft1(dcomplex *data,  /* size n_in */
	   int n_in, 
	   dcomplex *dataout,  /* output */
	   int isign0)
{
  fftw_complex *in, *out;
  fftw_plan p;

  int i;
  double scale;
  int isign;

  if (isign0>0) { isign=-1; }
  else { isign=1; }

#ifdef fftw2
  in  = (fftw_complex*)malloc(sizeof(fftw_complex)*n_in); 
  out = (fftw_complex*)malloc(sizeof(fftw_complex)*n_in); 
#else
  in  = fftw_malloc(sizeof(fftw_complex)*n_in);
  out = fftw_malloc(sizeof(fftw_complex)*n_in);
#endif

  for (i=0;i<n_in;i++) {

#ifdef fftw2
    c_re(in[i]) = data[i].r;
    c_im(in[i]) = data[i].i;
#else
    in[i][0]=data[i].r;
    in[i][1]=data[i].i;
#endif

  }

#ifdef fftw2
  p = fftw_create_plan(n_in, isign, FFTW_ESTIMATE);
  fftw_one(p,in,out); 
#else
  p = fftw_plan_dft_1d(n_in,in,out,isign,FFTW_ESTIMATE);
  fftw_execute(p);
#endif

  if (isign==-1) {
    scale = 1.0/( (double)n_in);

    for (i=0;i<n_in;i++) {

#ifdef fftw2
      dataout[i].r=c_re(out[i])*scale;
      dataout[i].i=c_im(out[i])*scale;
#else
      dataout[i].r=out[i][0]*scale;
      dataout[i].i=out[i][1]*scale;
#endif

    }
  }
  else {
    for (i=0;i<n_in;i++) {

#ifdef fftw2
      dataout[i].r=c_re(out[i]);
      dataout[i].i=c_im(out[i]);
#else
      dataout[i].r=out[i][0];
      dataout[i].i=out[i][1];
#endif

    }
  }


  fftw_destroy_plan(p);  

#ifdef fftw2
  free(out);
  free(in);
#else
  fftw_free(out);
  fftw_free(in);
#endif

}
示例#4
0
void FFT_FFTW(fftw_complex *inputSignal, fftw_complex * outputSignal, int n)
{
	fftw_plan p1 = fftw_plan_dft_1d(n, inputSignal, outputSignal, FFTW_FORWARD, FFTW_ESTIMATE);
	fftw_execute(p1);
	fftw_destroy_plan(p1);
}
示例#5
0
void TRAN_Set_Electrode_Grid(MPI_Comm comm1,
                             int *TRAN_Poisson_flag2,
                             double *Grid_Origin,   /* origin of the grid */
                             double tv[4][4],       /* unit vector of the cell*/
                             double Left_tv[4][4],  /* unit vector  left */
                             double Right_tv[4][4], /* unit vector right */
                             double gtv[4][4],      /* unit vector of the grid point, which is gtv*integer */
                             int Ngrid1,
                             int Ngrid2,
                             int Ngrid3             /* # of c grid points */
                            )
{
    int l1[2];
    int i,j,k,k2,k3;
    int tnoA,tnoB,wanB,wanA;
    int GA_AN,MA_AN,Nc,GNc,LB_AN_e;
    int GB_AN;
    int Rn_e,GA_AN_e,GB_AN_e,GRc;
    int side,direction;
    int spin,p2,p3;
    int n1,n2,n3;
    int id,gidx;
    double rcutA,rcutB,r,r1,r2;
    double dx,dy,dz,xx;
    double dx1,dy1,dz1;
    double dx2,dy2,dz2;
    double x1,y1,z1;
    double x2,y2,z2;
    double sum,tmp;
    double offset[4];
    double R[4];
    double xyz[4];
    double *Chi0,*Chi1,*Chi2;
    int idim=1;
    int myid;
    fftw_complex *in, *out;
    fftw_plan p;

    MPI_Comm_rank(comm1, &myid);

    /* for passing TRAN_Poisson_flag to "DFT" */
    *TRAN_Poisson_flag2 = TRAN_Poisson_flag;

    if (myid==Host_ID) {
        printf("<TRAN_Set_Electrode_Grid>\n");
    }

    /* allocation of array */

    Chi0 = (double*)malloc(sizeof(double)*List_YOUSO[7]);
    Chi1 = (double*)malloc(sizeof(double)*List_YOUSO[7]);
    Chi2 = (double*)malloc(sizeof(double)*List_YOUSO[7]);

    in  = fftw_malloc(sizeof(fftw_complex)*List_YOUSO[17]);
    out = fftw_malloc(sizeof(fftw_complex)*List_YOUSO[17]);

    /* allocation of arrays */
    if (print_stdout) {
        printf("%d %d %d %d %d\n",Ngrid1,Ngrid2,Ngrid3,TRAN_grid_bound[0], TRAN_grid_bound[1]);
    }

    for (side=0; side<2; side++) {
        ElectrodeDensity_Grid[side] = (double**)malloc(sizeof(double*)*(SpinP_switch_e[side]+1));
    }

    /* left lead */

    side  = 0;
    l1[0] = 0;
    l1[1] = TRAN_grid_bound[0];
    for (spin=0; spin<=SpinP_switch_e[side]; spin++) {
        ElectrodeDensity_Grid[side][spin] = (double*)malloc(sizeof(double)*Ngrid3*Ngrid2*(l1[1]-l1[0]+1));
    }

    ElectrodeADensity_Grid[side] = (double*)malloc(sizeof(double)*Ngrid3*Ngrid2*(l1[1]-l1[0]+1));
    ElectrodedVHart_Grid[side] = (double*)malloc(sizeof(double)*Ngrid3*Ngrid2*(l1[1]-l1[0]+1));

    VHart_Boundary[side] = (dcomplex***)malloc(sizeof(dcomplex**)*Ngrid1_e[side]);
    for (n1=0; n1<Ngrid1_e[side]; n1++) {
        VHart_Boundary[side][n1] = (dcomplex**)malloc(sizeof(dcomplex*)*Ngrid2);
        for (n2=0; n2<Ngrid2; n2++) {
            VHart_Boundary[side][n1][n2] = (dcomplex*)malloc(sizeof(dcomplex)*Ngrid3);
        }
    }

    /* right lead */

    side  = 1;
    l1[0] = TRAN_grid_bound[1];
    l1[1] = Ngrid1 - 1;
    for (spin=0; spin<=SpinP_switch_e[side]; spin++) {
        ElectrodeDensity_Grid[side][spin] = (double*)malloc(sizeof(double)*Ngrid3*Ngrid2*(l1[1]-l1[0]+1));
    }

    ElectrodeADensity_Grid[side] = (double*)malloc(sizeof(double)*Ngrid3*Ngrid2*(l1[1]-l1[0]+1));
    ElectrodedVHart_Grid[side] = (double*)malloc(sizeof(double)*Ngrid3*Ngrid2*(l1[1]-l1[0]+1));

    VHart_Boundary[side] = (dcomplex***)malloc(sizeof(dcomplex**)*Ngrid1_e[side]);
    for (n1=0; n1<Ngrid1_e[side]; n1++) {
        VHart_Boundary[side][n1] = (dcomplex**)malloc(sizeof(dcomplex*)*Ngrid2);
        for (n2=0; n2<Ngrid2; n2++) {
            VHart_Boundary[side][n1][n2] = (dcomplex*)malloc(sizeof(dcomplex)*Ngrid3);
        }
    }

    /*******************************************************
     charge density contributed by the left and right sides
    *******************************************************/

    for (side=0; side<=1; side++) {

        if (side==0) {
            direction = -1;
            l1[0] = 0;
            l1[1] = TRAN_grid_bound[0];
        }
        else {
            direction = 1;
            l1[0] = TRAN_grid_bound[1];
            l1[1] = Ngrid1-1;
        }

        /* initialize ElectrodeDensity_Grid and ElectrodeADensity_Grid */

        for (spin=0; spin<=SpinP_switch_e[side]; spin++) {
            for (i=0; i<Ngrid3*Ngrid2*(l1[1]-l1[0]+1); i++) {
                ElectrodeDensity_Grid[side][spin][i] = 0.0;
            }
        }
        for (i=0; i<Ngrid3*Ngrid2*(l1[1]-l1[0]+1); i++) {
            ElectrodeADensity_Grid[side][i] = 0.0;
        }

        /* calculate charge density */

        for (n1=l1[0]; n1<=l1[1]; n1++) {
            for (n2=0; n2<Ngrid2; n2++) {
                for (n3=0; n3<Ngrid3; n3++) {

                    GNc = n1*Ngrid2*Ngrid3 + n2*Ngrid3 + n3;
                    Get_Grid_XYZ(GNc, xyz);

                    for (p2=-1; p2<=1; p2++) {
                        for (p3=-1; p3<=1; p3++) {
                            for (GA_AN_e=1; GA_AN_e<=atomnum_e[side]; GA_AN_e++) {

                                if (side==0) GA_AN = GA_AN_e;
                                else         GA_AN = GA_AN_e + Latomnum + Catomnum;

                                wanA = WhatSpecies[GA_AN];
                                tnoA = Spe_Total_CNO[wanA];
                                rcutA = Spe_Atom_Cut1[wanA];

                                x1 = Gxyz[GA_AN][1]
                                     + (double)direction*tv_e[side][1][1]
                                     +        (double)p2*tv_e[side][2][1]
                                     +        (double)p3*tv_e[side][3][1];

                                y1 = Gxyz[GA_AN][2]
                                     + (double)direction*tv_e[side][1][2]
                                     +        (double)p2*tv_e[side][2][2]
                                     +        (double)p3*tv_e[side][3][2];

                                z1 = Gxyz[GA_AN][3]
                                     + (double)direction*tv_e[side][1][3]
                                     +        (double)p2*tv_e[side][2][3]
                                     +        (double)p3*tv_e[side][3][3];

                                dx1 = xyz[1] - x1;
                                dy1 = xyz[2] - y1;
                                dz1 = xyz[3] - z1;
                                r1 = sqrt(dx1*dx1 + dy1*dy1 + dz1*dz1);

                                if (r1<=rcutA) {

                                    /******************************
                                         ElectrodeDensity_Grid
                                    ******************************/

                                    Get_Orbitals(wanA,dx1,dy1,dz1,Chi1);

                                    for (LB_AN_e=0; LB_AN_e<=FNAN_e[side][GA_AN_e]; LB_AN_e++) {

                                        GB_AN_e = natn_e[side][GA_AN_e][LB_AN_e];
                                        Rn_e    =  ncn_e[side][GA_AN_e][LB_AN_e];

                                        if (side==0) GB_AN = GB_AN_e;
                                        else         GB_AN = GB_AN_e + Latomnum + Catomnum;

                                        wanB = WhatSpecies[GB_AN];
                                        tnoB = Spe_Total_CNO[wanB];
                                        rcutB = Spe_Atom_Cut1[wanB];

                                        x2 = Gxyz[GB_AN][1]
                                             + (double)(direction+atv_ijk_e[side][Rn_e][1])*tv_e[side][1][1]
                                             + (double)(p2       +atv_ijk_e[side][Rn_e][2])*tv_e[side][2][1]
                                             + (double)(p3       +atv_ijk_e[side][Rn_e][3])*tv_e[side][3][1];

                                        y2 = Gxyz[GB_AN][2]
                                             + (double)(direction+atv_ijk_e[side][Rn_e][1])*tv_e[side][1][2]
                                             + (double)(p2       +atv_ijk_e[side][Rn_e][2])*tv_e[side][2][2]
                                             + (double)(p3       +atv_ijk_e[side][Rn_e][3])*tv_e[side][3][2];

                                        z2 = Gxyz[GB_AN][3]
                                             + (double)(direction+atv_ijk_e[side][Rn_e][1])*tv_e[side][1][3]
                                             + (double)(p2       +atv_ijk_e[side][Rn_e][2])*tv_e[side][2][3]
                                             + (double)(p3       +atv_ijk_e[side][Rn_e][3])*tv_e[side][3][3];

                                        dx2 = xyz[1] - x2;
                                        dy2 = xyz[2] - y2;
                                        dz2 = xyz[3] - z2;
                                        r2 = sqrt(dx2*dx2 + dy2*dy2 + dz2*dz2);

                                        if (r2<=rcutB) {

                                            Get_Orbitals(wanB,dx2,dy2,dz2,Chi2);

                                            for (spin=0; spin<=SpinP_switch; spin++) {

                                                if (atv_ijk_e[side][Rn_e][1]==(-direction)) {

                                                    sum = 0.0;
                                                    for (i=0; i<tnoA; i++) {
                                                        for (j=0; j<tnoB; j++) {
                                                            /* revised by Y. Xiao for Noncollinear NEGF calculations */
                                                            /*  sum += 2.0*DM_e[side][0][spin][GA_AN_e][LB_AN_e][i][j]*Chi1[i]*Chi2[j]; */
                                                            if(spin==3) {
                                                                sum -= 2.0*DM_e[side][0][spin][GA_AN_e][LB_AN_e][i][j]*Chi1[i]*Chi2[j];
                                                            } else {
                                                                sum += 2.0*DM_e[side][0][spin][GA_AN_e][LB_AN_e][i][j]*Chi1[i]*Chi2[j];
                                                            }
                                                            /* until here by Y. Xiao for Noncollinear NEGF calculations */
                                                        }
                                                    }
                                                }

                                                else if (atv_ijk_e[side][Rn_e][1]==0) {

                                                    sum = 0.0;
                                                    for (i=0; i<tnoA; i++) {
                                                        for (j=0; j<tnoB; j++) {
                                                            /* revised by Y. Xiao for Noncollinear NEGF calculations */
                                                            /*  sum += DM_e[side][0][spin][GA_AN_e][LB_AN_e][i][j]*Chi1[i]*Chi2[j]; */
                                                            if(spin==3) {
                                                                sum -= DM_e[side][0][spin][GA_AN_e][LB_AN_e][i][j]*Chi1[i]*Chi2[j];
                                                            } else {
                                                                sum += DM_e[side][0][spin][GA_AN_e][LB_AN_e][i][j]*Chi1[i]*Chi2[j];
                                                            }
                                                            /* until here by Y. Xiao for Noncollinear NEGF calculations */
                                                        }
                                                    }
                                                }

                                                gidx = (n1-l1[0])*Ngrid2*Ngrid3 + n2*Ngrid3 + n3;
                                                ElectrodeDensity_Grid[side][spin][gidx] += sum;

                                            }
                                        }
                                    }

                                    /******************************
                                         ElectrodeADensity_Grid
                                    ******************************/

                                    xx = 0.5*log(dx1*dx1 + dy1*dy1 + dz1*dz1);
                                    gidx = (n1-l1[0])*Ngrid2*Ngrid3 + n2*Ngrid3 + n3;
                                    ElectrodeADensity_Grid[side][gidx] += 0.5*KumoF( Spe_Num_Mesh_PAO[wanA], xx,
                                                                          Spe_PAO_XV[wanA], Spe_PAO_RV[wanA],
                                                                          Spe_Atomic_Den[wanA]);

                                } /* if (r1<=rcutA) */
                            }
                        }
                    }
                }
            }
        }

    } /* side */

    /*******************************************************
     2D FFT of dDen_Grid_e, which is difference between
     charge density calculated by KS wave functions and
     the superposition of atomic charge density, of the
     left and right leads on the bc plane for TRAN_Poisson
    *******************************************************/

    for (side=0; side<=1; side++) {

        /* set VHart_Boundary in real space */

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (n2=0; n2<Ngrid2; n2++) {
                for (n3=0; n3<Ngrid3; n3++) {

                    /* borrow the array "VHart_Boundary" */
                    VHart_Boundary[side][n1][n2][n3].r = dDen_Grid_e[side][n1*Ngrid2*Ngrid3+n2*Ngrid3+n3];
                    VHart_Boundary[side][n1][n2][n3].i = 0.0;
                }
            }
        }

        /* FFT of VHart_Boundary for c-axis */

        p = fftw_plan_dft_1d(Ngrid3,in,out,-1,FFTW_ESTIMATE);

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (n2=0; n2<Ngrid2; n2++) {

                for (n3=0; n3<Ngrid3; n3++) {

                    in[n3][0] = VHart_Boundary[side][n1][n2][n3].r;
                    in[n3][1] = VHart_Boundary[side][n1][n2][n3].i;
                }

                fftw_execute(p);

                for (k3=0; k3<Ngrid3; k3++) {

                    VHart_Boundary[side][n1][n2][k3].r = out[k3][0];
                    VHart_Boundary[side][n1][n2][k3].i = out[k3][1];
                }
            }
        }

        fftw_destroy_plan(p);

        /* FFT of VHart_Boundary for b-axis */

        p = fftw_plan_dft_1d(Ngrid2,in,out,-1,FFTW_ESTIMATE);

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (k3=0; k3<Ngrid3; k3++) {

                for (n2=0; n2<Ngrid2; n2++) {

                    in[n2][0] = VHart_Boundary[side][n1][n2][k3].r;
                    in[n2][1] = VHart_Boundary[side][n1][n2][k3].i;
                }

                fftw_execute(p);

                for (k2=0; k2<Ngrid2; k2++) {

                    VHart_Boundary[side][n1][k2][k3].r = out[k2][0];
                    VHart_Boundary[side][n1][k2][k3].i = out[k2][1];
                }
            }
        }

        fftw_destroy_plan(p);

        tmp = 1.0/(double)(Ngrid2*Ngrid3);

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (k2=0; k2<Ngrid2; k2++) {
                for (k3=0; k3<Ngrid3; k3++) {
                    VHart_Boundary[side][n1][k2][k3].r *= tmp;
                    VHart_Boundary[side][n1][k2][k3].i *= tmp;
                }
            }
        }

    } /* side */

    /*******************************************************
     interpolation of 2D Fourier transformed difference
     charge of the left and right leads on the bc plane for
     TRAN_Poisson_FFT_Extended
    *******************************************************/

    {
        int ip,Num;
        double x;
        double *vr,*vi,*xg;

        if      (1.0<fabs(tv[1][1])) ip = 1;
        else if (1.0<fabs(tv[1][2])) ip = 2;
        else if (1.0<fabs(tv[1][3])) ip = 3;

        side = 0;
        IntNgrid1_e[side] = (int)(fabs((double)TRAN_FFTE_CpyNum*tv_e[side][1][ip]+1.0e-8)/length_gtv[1]);

        dDen_IntBoundary[side] = (dcomplex***)malloc(sizeof(dcomplex**)*IntNgrid1_e[side]);
        for (n1=0; n1<IntNgrid1_e[side]; n1++) {
            dDen_IntBoundary[side][n1] = (dcomplex**)malloc(sizeof(dcomplex*)*Ngrid2);
            for (n2=0; n2<Ngrid2; n2++) {
                dDen_IntBoundary[side][n1][n2] = (dcomplex*)malloc(sizeof(dcomplex)*Ngrid3);
            }
        }

        side = 1;
        IntNgrid1_e[side] = (int)(fabs((double)TRAN_FFTE_CpyNum*tv_e[side][1][ip]+1.0e-8)/length_gtv[1]);

        dDen_IntBoundary[side] = (dcomplex***)malloc(sizeof(dcomplex**)*IntNgrid1_e[side]);
        for (n1=0; n1<IntNgrid1_e[side]; n1++) {
            dDen_IntBoundary[side][n1] = (dcomplex**)malloc(sizeof(dcomplex*)*Ngrid2);
            for (n2=0; n2<Ngrid2; n2++) {
                dDen_IntBoundary[side][n1][n2] = (dcomplex*)malloc(sizeof(dcomplex)*Ngrid3);
            }
        }

        for (side=0; side<=1; side++) {

            vr = (double*)malloc(sizeof(double)*(TRAN_FFTE_CpyNum+2)*Ngrid1_e[side]);
            vi = (double*)malloc(sizeof(double)*(TRAN_FFTE_CpyNum+2)*Ngrid1_e[side]);
            xg = (double*)malloc(sizeof(double)*(TRAN_FFTE_CpyNum+2)*Ngrid1_e[side]);

            for (k2=0; k2<Ngrid2; k2++) {
                for (k3=0; k3<Ngrid3; k3++) {

                    /* set up the 1D data */

                    for (i=0; i<(TRAN_FFTE_CpyNum+2); i++) {

                        for (n1=0; n1<Ngrid1_e[side]; n1++) {
                            vr[n1+i*Ngrid1_e[side]] = VHart_Boundary[side][n1][k2][k3].r;
                            vi[n1+i*Ngrid1_e[side]] = VHart_Boundary[side][n1][k2][k3].i;
                        }

                        for (n1=0; n1<Ngrid1_e[side]; n1++) {
                            xg[n1+i*Ngrid1_e[side]] =
                                (double)(n1+i*Ngrid1_e[side])*fabs(tv_e[side][1][ip]/(double)Ngrid1_e[side])
                                - (double)(Ngrid1_e[side]*(TRAN_FFTE_CpyNum+1))*fabs(tv_e[side][1][ip]/(double)Ngrid1_e[side])
                                + Grid_Origin[ip];
                        }
                    }

                    /*
                    if (myid==0 && side==0 && k2==1 && k3==0){
                      for (n1=0; n1<Ngrid1_e[side]*(TRAN_FFTE_CpyNum+2); n1++){
                        printf("A %15.12f %15.12f %15.12f\n",xg[n1],vr[n1],vi[n1]);
                      }
                        }
                    */

                    /* interpolation */

                    for (n1=0; n1<IntNgrid1_e[side]; n1++) {

                        x = (double)n1*length_gtv[1] - (double)IntNgrid1_e[side]*length_gtv[1] + Grid_Origin[ip];

                        dDen_IntBoundary[side][n1][k2][k3].r = Interpolated_Func(x, xg, vr, (TRAN_FFTE_CpyNum+2)*Ngrid1_e[side]);
                        dDen_IntBoundary[side][n1][k2][k3].i = Interpolated_Func(x, xg, vi, (TRAN_FFTE_CpyNum+2)*Ngrid1_e[side]);


                        /*
                            if (myid==0 && side==0 && k2==1 && k3==0){
                            printf("B %15.12f %15.12f %15.12f\n",
                                   x,dDen_IntBoundary[side][n1][k2][k3].r,
                                     dDen_IntBoundary[side][n1][k2][k3].i);
                        }
                        */

                    }
                }
            }

            free(xg);
            free(vi);
            free(vr);

        }
    }

    /****************************************************
      2D FFT of dVHartree of the left and right leads
      on the bc plane for TRAN_Poisson
    ****************************************************/

    for (side=0; side<=1; side++) {

        /* set VHart_Boundary in real space */

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (n2=0; n2<Ngrid2; n2++) {
                for (n3=0; n3<Ngrid3; n3++) {

                    VHart_Boundary[side][n1][n2][n3].r = dVHart_Grid_e[side][n1*Ngrid2*Ngrid3+n2*Ngrid3+n3];
                    VHart_Boundary[side][n1][n2][n3].i = 0.0;
                }
            }
        }

        /* FFT of VHart_Boundary for c-axis */

        p = fftw_plan_dft_1d(Ngrid3,in,out,-1,FFTW_ESTIMATE);

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (n2=0; n2<Ngrid2; n2++) {

                for (n3=0; n3<Ngrid3; n3++) {

                    in[n3][0] = VHart_Boundary[side][n1][n2][n3].r;
                    in[n3][1] = VHart_Boundary[side][n1][n2][n3].i;
                }

                fftw_execute(p);

                for (k3=0; k3<Ngrid3; k3++) {

                    VHart_Boundary[side][n1][n2][k3].r = out[k3][0];
                    VHart_Boundary[side][n1][n2][k3].i = out[k3][1];
                }
            }
        }

        fftw_destroy_plan(p);

        /* FFT of VHart_Boundary for b-axis */

        p = fftw_plan_dft_1d(Ngrid2,in,out,-1,FFTW_ESTIMATE);

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (k3=0; k3<Ngrid3; k3++) {

                for (n2=0; n2<Ngrid2; n2++) {

                    in[n2][0] = VHart_Boundary[side][n1][n2][k3].r;
                    in[n2][1] = VHart_Boundary[side][n1][n2][k3].i;
                }

                fftw_execute(p);

                for (k2=0; k2<Ngrid2; k2++) {

                    VHart_Boundary[side][n1][k2][k3].r = out[k2][0];
                    VHart_Boundary[side][n1][k2][k3].i = out[k2][1];
                }
            }
        }

        fftw_destroy_plan(p);

        tmp = 1.0/(double)(Ngrid2*Ngrid3);

        for (n1=0; n1<Ngrid1_e[side]; n1++) {
            for (k2=0; k2<Ngrid2; k2++) {
                for (k3=0; k3<Ngrid3; k3++) {
                    VHart_Boundary[side][n1][k2][k3].r *= tmp;
                    VHart_Boundary[side][n1][k2][k3].i *= tmp;
                }
            }
        }

    } /* side */

    /* freeing of arrays */
    free(Chi0);
    free(Chi1);
    free(Chi2);

    fftw_free(in);
    fftw_free(out);
}
示例#6
0
static void setup_fftw() {
    fft_in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SAMPLES_SIZE);
    fft_out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * SAMPLES_SIZE);
    fft_history = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * FFT_SIZE * FFT_HISTORY_SIZE);
    fft_plan = fftw_plan_dft_1d(FFT_SIZE, fft_in, fft_out, FFTW_FORWARD, FFTW_ESTIMATE);
}
bool c_FourierTransfrom::fftw_complex_3d(const Mat_<Vec3d> &_input,
                                         Mat_<Vec6d> &_output)
{
    size_t height = _input.rows;
    size_t width = _input.cols;
    size_t n_channels = _input.channels();
    size_t n_pixels = height * width;
    size_t n_data = n_pixels * n_channels;

    fftw_complex *in, *out;
    fftw_plan p;

    in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * n_data);
    out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * n_data);

#if 0
    p = fftw_plan_dft_3d(n_channels, width, height, in, out, FFTW_FORWARD,
                         FFTW_ESTIMATE);
#else
#if 1
    p = fftw_plan_dft_2d(n_channels, width * height, in, out, FFTW_FORWARD,
                         FFTW_ESTIMATE);
#else
    p = fftw_plan_dft_1d(n_channels * width * height, in, out, FFTW_FORWARD,
                         FFTW_ESTIMATE);
#endif
#endif

    /*!< prepare the data */
    for (size_t i_row = 0; i_row < height; ++i_row)
    {
        const Vec3d *p = _input.ptr<Vec3d>(i_row);
        for (size_t i_col = 0; i_col < width; ++i_col)
        {
            size_t index = i_row * width + i_col;
            for (size_t k = 0; k < n_channels; ++k)
            {
                in[n_pixels * k + index][0] = p[i_col][k];
            }
#if 0
            in[index][0] = p[i_col][2];
            in[n_pixels + index][0] = p[i_col][1];
            in[n_pixels * 2 + index][0] = p[i_col][0];
#endif
        }
    }
    for (size_t i = 0; i < n_data; ++i)
    {
        in[i][1] = 0;
    }

    fftw_execute(p);

    /*!< write back data */
    _output = Mat_<Vec6d>::zeros(_input.size());
    for (size_t i_row = 0; i_row < height; ++i_row)
    {
        Vec6d *p = _output.ptr<Vec6d>(i_row);
        for (size_t i_col = 0; i_col < width; ++i_col)
        {
            size_t index = i_row * width + i_col;
            for (size_t k = 0; k < n_channels; ++k)
            {
                p[i_col][k] = out[n_pixels * k + index][0];
                p[i_col][k + n_channels] = out[n_pixels * k + index][1];
            }
#if 0
            p[i_col][4] = out[index][0];
            p[i_col][5] = out[index][1];
            p[i_col][2] = out[n_pixels + index][0];
            p[i_col][3] = out[n_pixels + index][1];
            p[i_col][0] = out[n_pixels * 2 + index][0];
            p[i_col][1] = out[n_pixels * 2 + index][1];
#endif
        }
    }

    fftw_destroy_plan(p);
    fftw_free(in);
    fftw_free(out);

    return true;
}
Datum transform_eeg(PG_FUNCTION_ARGS)
{
    //the size of the array we are trying to transform
    int arraySize;
    // a Datum for the constructred and deconstructed input arrays
    Datum *intermResult;
    Datum *input_data;
    // the final result to return and the input to the function
    ArrayType *result, *input;
    Oid element_type, return_type, input_type;
    //next 6 variables are inputted into get_typlenbyvalalign() and are used for construct and deconstruct array
    //small int in postgreSQL is int16
    int16 return_typelen, input_typelen;
    bool return_typbyval, input_typbyval;
    char return_typalign, input_typalign;

    //the arrays we are transforming. Transforming array in[] into array out[]
    //fftw_complex, a data structure with real (in[i].re) and imaginary (in[i].im) floating-point components. 
    fftw_complex *in, *out;
    fftw_plan my_plan;


    // get input row
    input = PG_GETARG_ARRAYTYPE_P(0);
    // get input array element type
    input_type = ARR_ELEMTYPE(input);
    //get needed variabels
    get_typlenbyvalalign(input_type, &input_typelen, &input_typbyval, &input_typalign);
    // deconstruct inupt array and save the array as Datum input_data
    deconstruct_array(input, input_type, input_typelen, input_typbyval, input_typalign, &input_data, NULL, &arraySize);

    // get element type of return vale (Complex[])
    element_type = get_fn_expr_rettype(fcinfo->flinfo);
    // get element type of an element in the return value (Complex)
    return_type = get_element_type(element_type);
    //get needed variabels
    get_typlenbyvalalign(return_type, &return_typelen, &return_typbyval, &return_typalign);

    // in and out array and plan declarations
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*arraySize);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex)*arraySize);
    my_plan = fftw_plan_dft_1d(arraySize, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    
    // set the in variable to the array we got as input.
    // the real parts are from the input_data array
    // the imaginary part is set to 0
    for (int i = 0; i < arraySize; i++) {
        in[i][0] = (double) DatumGetInt16(input_data[i]);
        in[i][1] = 0;
    }

    // run the plan
    fftw_execute(my_plan);

    // array to store the out array (the transformed in array)
    intermResult = palloc(sizeof(Datum)*arraySize);
    
    // for each variable in the out array. 
    // Create a complex variable then set its real and imaginary party from the processed array
    // get the datum from the pointer and save it in the result array
    for(int32 i = 0; i < arraySize; i++)
    {
        Complex *temp = palloc(sizeof(Complex));
        temp->x = out[i][0];
        temp->y = out[i][1];
        intermResult[i] = PointerGetDatum(temp);
    }

    // construct a result array
    result = construct_array(intermResult, arraySize, return_type, return_typelen, return_typbyval, return_typalign);
    
    // free memory
    fftw_destroy_plan(my_plan);
    fftw_free(in);
    fftw_free(out);
    pfree(input_data);
    pfree(intermResult);

    // return result
    PG_RETURN_POINTER(result);

}
示例#9
0
/**
   This code computes the integtated autocorrelation time :

   It expects a file of length N double precision measurements

   Defining c(t) = ( Y(t) - \bar{Y} )

   C(T) = \sum_{t=0}^{N} ( c(t) c(t+T) )

   R(T) = C(T) / C(0)

   Setting a cutoff point "n"

   Tau(n) = 0.5 + Nsep * \sum_{T}^{n} R(T)

   We estimate the error on Tau(T) by

   S_E(n) = n * \sqrt( ( 0.5 + \sum_{t}^{n} R(T) ) / N ) 

   The computation of C(T) is performed by convolution with FFTs

   The autocorrelation time is written to the file specified
 */
int
autocorrelation( const struct resampled RAW ,
		 const int NSEP ,
		 const char *output )
{
  // openmp'd fftws
  parallel_ffts( ) ;

  if( RAW.restype != RAWDATA ) {
    printf( "Resampled data is not RAW ... Cannot compute autocorrelation\n" ) ;
    return FAILURE ;
  }

  // some constants
  const int N = RAW.NSAMPLES ;
  const int N2 = 2 * N ;

  printf( "RAWDATA has %d samples\n" , N ) ;

  printf( "Measurement separation %d\n\n" , NSEP ) ;

  // allocate memory
  double complex *in  = calloc( N2 , sizeof( double complex ) ) ;
  double complex *out = calloc( N2 , sizeof( double complex ) ) ;

  // subtract the average from each data point
  int i ;
#pragma omp parallel for private(i)
  for( i = 0 ; i < N ; i++ ) {
    in[ i ] = ( RAW.resampled[ i ] - RAW.avg ) ;
  }

  message( "FFT planning" ) ;

  // are we doing this using openmp ffts?
#if ( defined OMP_FFTW ) && ( defined HAVE_OMP_H )
  if( parallel_ffts( ) == FAILURE ) {
    printf( "Parallel FFT setting failed \n" ) ;
    return FAILURE ;
  }
#endif
  
  // create the plans
  const fftw_plan forward = fftw_plan_dft_1d( N2 , in , out , 
					      FFTW_FORWARD , FFTW_ESTIMATE ) ; 

  const fftw_plan backward = fftw_plan_dft_1d( N2 , out , in , 
					       FFTW_BACKWARD , FFTW_ESTIMATE ) ;
  
  fftw_execute( forward ) ;

  // convolve
#pragma omp parallel for private(i)
  for( i = 0 ; i < N2 ; i++ ) {
    out[i] = creal( out[i] ) * creal( out[i] ) + 
             cimag( out[i] ) * cimag( out[i] ) ;
  }

  fftw_execute( backward ) ;

  // normalise
  const double zeropoint = 1.0 / in[ 0 ] ;
#pragma omp parallel for private(i)
  for( i = 0 ; i < N2 ; i++ ) {
    in[ i ] *= zeropoint ;
  }

  // summy the lags
  message( "Computing tau(n)" ) ;

  FILE *output_file = fopen( output , "w" ) ;

  printf( "Writing tau(n) to file %s \n" , output ) ;

  int n ;
  for( n = 0 ; n < 30 ; n++ ) {
    register double sum = 0.5 ;
    int j ;
    for( j = 0 ; j < n ; j++ ) {
      sum += NSEP * in[j] ;
    }
    // simple error estimate
    const double err = n * sqrt( ( sum ) / N ) ;
    fprintf( output_file , "%d %e %e \n" , n * NSEP , sum , err ) ;
  }

  fclose( output_file ) ;

  // memory free
  free( in ) ;
  free( out ) ;
  fftw_destroy_plan(forward ) ;
  fftw_destroy_plan( backward ) ;
#ifdef OMP_FFTW
  // parallel
  fftw_cleanup_threads( ) ;
#endif  
  fftw_cleanup( ) ;

  return SUCCESS ;
}
示例#10
0
M_vocoder::M_vocoder(QWidget* parent)
  : Module(M_type_vocoder, 5, parent, tr("FFT Vocoder"))
{
  QString  qs;
  int l1;
  unsigned int l2;

  setGeometry(MODULE_NEW_X, MODULE_NEW_Y, MODULE_VOCODER_WIDTH, MODULE_VOCODER_HEIGHT);
  port_M_modulator = new Port(tr("Modulator"), PORT_IN, 0, this);
  port_M_pitchshift = new Port(tr("Pitch Shift"), PORT_IN, 1, this);
  port_M_freqshift = new Port(tr("Freq Shift"), PORT_IN, 2, this);
  port_M_channels = new Port(tr("Channels"), PORT_IN, 3, this);
  port_M_carrier = new Port(tr("Carrier"), PORT_IN, 4, this);
  cv.out_off = 65;
  port_altmodulator_out = new Port (tr("Altered Mod"), PORT_OUT, 0, this);
  port_vocoder_out = new Port(tr("Vocoder Out"), PORT_OUT, 1, this);
  port_modfft_out = new Port (tr("Modulator FFT"), PORT_OUT, 2, this);
  port_firstharmonic_out = new Port (tr("Mod 1st H"), PORT_OUT, 3, this);

  fftsize = synthdata->cyclesize * 4;
  //  fprintf (stderr, "FFTSize = %d\n", fftsize);

  channels = 16;
  configDialog->addSlider(tr("&Bins/Channel"), channels, 1,
			  (999 < synthdata->cyclesize
			   ? 999 : synthdata->cyclesize));
  vcchannels = 0;
  configDialog->addSlider(tr("&VC Bins/Channels"), vcchannels, -100, 100);
  attack = 0;
  configDialog->addSlider(tr("&Attack time"), attack, 0,1);
  release = 0;
  configDialog->addSlider(tr("&Release time"), release, 0, 1);
  pitchshift = 0;
  configDialog->addSlider(tr("&Pitch shift (octaves)"), pitchshift, -3, 3);
  vcpitch = 0;
  configDialog->addSlider(tr("V&C Pitch shift"), vcpitch, -5, 5);
  freqshift = 0;
  configDialog->addSlider(tr("&Frequency (Bode) shift"), freqshift, -999, 999);
  vcfreqshift = 0;
  configDialog->addSlider(tr("VC Fre&q shift"), vcfreqshift, -500, 500);
  phaseshift = 0;
  configDialog->addSlider(tr("P&hase shift"), phaseshift, -6.283, 6.283);
  myFFTWindowFunc = 0;
  QStringList windowFormats;
  windowFormats << tr("Rectangular");
  windowFormats << tr("Trapezoidal");
  windowFormats << tr("Hann (Cosine)");
  windowFormats << tr("Hamming (Cosine)");
  windowFormats << tr("Tukey (flattop cosine)");
  windowFormats << tr("Blackman-Nutall (minimum spill)");
  configDialog->addComboBox (tr("FFT &Window function"),
			     myFFTWindowFunc, windowFormats);
  dynsplice = 0;
  configDialog->addCheckBox(tr("Dynamic &splicing"), dynsplice);
  rtheta = 0;
  configDialog->addCheckBox(tr("R-&Theta modulator"), rtheta);

  modbuf = (float **)malloc(synthdata->poly * sizeof(float *));
  for (l1 = 0; l1 < synthdata->poly; l1++) {
    modbuf[l1] = (float *)malloc( fftsize * sizeof(float));
    memset( modbuf[l1], 0, fftsize * sizeof(float));
  }
  carrbuf = (float **)malloc(synthdata->poly * sizeof(float *));
  for (l1 = 0; l1 < synthdata->poly; l1++) {
    carrbuf[l1] = (float *)malloc( fftsize * sizeof(float));
    memset( carrbuf[l1], 0, fftsize * sizeof(float));
  }

  modmap = (float *) malloc (sizeof (float) * fftsize);
  armodmap = (float *) malloc (sizeof (float) * fftsize);

  whichwin = 0;
  window = (float *) malloc (sizeof (float) * fftsize);
  for (l2 = 0; l2 < (unsigned int) fftsize; l2++)
    window[l2] = windowcurve (whichwin, fftsize, l2, 0.25);

  //  FFTW setup stuff
  carrinforward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					    * fftsize);
  carrinbackward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					     * fftsize);
  carroutforward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					     * fftsize);
  carroutbackward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					      * fftsize);
  modinforward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					    * fftsize);
  modinbackward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					     * fftsize);
  modoutforward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					     * fftsize);
  modoutbackward = (fftw_complex *) fftw_malloc (sizeof (fftw_complex)
					      * fftsize);
  fftw_set_timelimit (0.1);
  planmodforward = fftw_plan_dft_1d (fftsize, modinforward,
				  modoutforward, FFTW_FORWARD, FFTW_MEASURE);
  planmodbackward = fftw_plan_dft_1d (fftsize, modinbackward,
 				  modoutbackward, FFTW_BACKWARD, FFTW_MEASURE);
  plancarrforward = fftw_plan_dft_1d (fftsize, carrinforward,
				  carroutforward, FFTW_FORWARD, FFTW_MEASURE);
  plancarrbackward = fftw_plan_dft_1d (fftsize, carrinbackward,
 				  carroutbackward, FFTW_BACKWARD, FFTW_MEASURE);
}
示例#11
0
void CFFT::initFFT(int size)
{
    // Describe plan
    ch1 = fftw_plan_dft_1d( size, in1, out1, FFTW_FORWARD, FFTW_MEASURE );
    ch2 = fftw_plan_dft_1d( size, in2, out2, FFTW_FORWARD, FFTW_MEASURE );
}
示例#12
0
文件: scft.cpp 项目: ren5885a/SCFT
int fftw3_test(GPU_INFO *gpu_info,CUFFT_INFO *cufft_info){

	
	int i,j,k;
	double *array;
	int Nx=cufft_info->Nx;
	int Ny=cufft_info->Ny;
	int Nz=cufft_info->Nz;
	array=(double *)malloc(sizeof(double)*cufft_info->NxNyNz);
	
	double *in;
	fftw_complex *in_one;
	fftw_complex *out,*out_one;

	in=(double *)malloc(sizeof(double)*cufft_info->NxNyNz);
	out=(fftw_complex *)malloc(sizeof(fftw_complex)*cufft_info->NxNyNz);

	in_one=(fftw_complex *)malloc(sizeof(fftw_complex)*cufft_info->NxNyNz);
	out_one=(fftw_complex *)malloc(sizeof(fftw_complex)*cufft_info->NxNyNz);

	fftw_plan plan_forward;
	int temp;

	int rank=3;int n[3];
	n[0]=cufft_info->Nx;n[1]=cufft_info->Ny;n[2]=cufft_info->Nz;
	unsigned flags;
	
	for(long ijk=0;ijk<cufft_info->NxNyNz;ijk++) {
		
				
		array[ijk]=ijk;
		
		in[ijk]=ijk;
		
	}
	int Nxh1=Nx/2+1;
	cufft_info->Nxh1NyNz=Nxh1*Ny*Nz;
	long ijk,ijkr;
	/*fftw_plan fftw_plan_dft_2d(int n0, int n1,
                                fftw_complex *in, fftw_complex *out,
                                int sign, unsigned flags);*/
	
	
	for(i=0;i<cufft_info->Nz;i++){
		plan_forward=fftw_plan_dft_r2c_3d(1, Ny, Nx, in+Nx*Ny*i, out+Nx*Ny*i, FFTW_ESTIMATE);
		
		fftw_execute ( plan_forward );
	}
	
	
	for(i=0;i<Nxh1;i++){
		for(j=0;j<cufft_info->Ny;j++){
			
			for(k=0;k<Nz;k++) {in_one[k][0]=out[i+j*Nxh1+k*Nx*Ny][0];in_one[k][1]=out[i+j*Nxh1+k*Nx*Ny][1];}
			//if(i==0&&j==0) for(k=0;k<Nz;k++)  printf("%g %g\n",in_one[k][0],in_one[k][1]);
			plan_forward=fftw_plan_dft_1d(Nz, in_one, out_one,FFTW_FORWARD, FFTW_ESTIMATE);
			fftw_execute ( plan_forward );
			for(k=0;k<Nz;k++) {out[i+j*Nxh1+k*Nx*Ny][0]=out_one[k][0];out[i+j*Nxh1+k*Nx*Ny][1]=out_one[k][1];}
		}
	}
	
	FILE *dp;
	dp=fopen("fftw3_compare.dat","w");

	for(k=0;k<Nz;k++)
	for(j=0;j<Ny;j++)
	for(i=0;i<Nx;i++){
		ijkr=(long)((k*Ny+j)*Nx+i);
		ijk=(long)(i+Nx*j+Nxh1*Ny*k);
		if(abs(out[ijkr][0])>0.0000001||abs(out[ijkr][1])>0.0000001)
		fprintf(dp,"%d %g %g\n",ijk,out[ijkr][0],out[ijkr][1]);
	}
	fclose(dp);
	
	for(i=0;i<Nxh1;i++){
		for(j=0;j<cufft_info->Ny;j++){
			for(k=0;k<Nz;k++) {in_one[k][0]=out[i+j*Nxh1+k*Nx*Ny][0];in_one[k][1]=out[i+j*Nxh1+k*Nx*Ny][1];}
			// for(k=0;k<Nz;k++)  printf("%g %g\n",in_one[k][0],in_one[k][1]);
			plan_forward=fftw_plan_dft_1d(Nz, in_one, out_one,FFTW_BACKWARD, FFTW_ESTIMATE);
			fftw_execute ( plan_forward );
			//for(k=0;k<Nz;k++)  printf("%g %g\n",out_one[k][0],out_one[k][1]);
			for(k=0;k<Nz;k++) {out[i+j*Nxh1+k*Nx*Ny][0]=out_one[k][0];out[i+j*Nxh1+k*Nx*Ny][1]=out_one[k][1];}
			//for(k=0;k<Nz;k++)  printf("%g %g\n",out_one[k][0],out_one[k][1]);
		}
	}
	for(i=0;i<16;i++) {out[i][0]=i;out[i][1]=0;}
	plan_forward=fftw_plan_dft_c2r_3d(2, 8, 1,  out,in, FFTW_ESTIMATE);
	fftw_execute ( plan_forward );
	/*
	for(i=0;i<cufft_info->Nz;i++){
		//for(k=0;k<8;k++)  printf("%g %g\n",out[k+8*i][0],out[k+8*i][1]);
		plan_forward=fftw_plan_dft_c2r_3d(1, 8, 1,  out+Nx*Ny*i,in+Nx*Ny*i, FFTW_ESTIMATE);
		
		fftw_execute ( plan_forward );
	}
	*/
	//for(i=0;i<16;i++) {printf("%g\n",in[i]);}
	/*	
	for(k=0;k<Nz;k++)
	for(j=0;j<Ny;j++)
	for(i=0;i<Nxh1;i++){
		
		//ijk=(long)((k*Ny+j)*Nxh1+i);
		ijkr=(long)((k*Ny+j)*Nx+i);
		ijk=(long)(k*Nx*Ny+j*Nxh1+i);
		if(abs(out[ijk][0])>=0.00000000001||abs(out[ijk][1])>=0.00000000001){
			
			fprintf(dp,"%d %g %g %g\n",ijkr,out[ijk][0],out[ijk][1],in[ijkr]/512);
		}
		
	}
	*/
	
	printf("finish writiing\n");
	
	free(array);
	free(in);
	free(out);
	return 1;
}
示例#13
0
void fourier(const int N, dcomplex* fw, dcomplex* ft){
    fftw_plan p;
    p = fftw_plan_dft_1d(N, reinterpret_cast<fftw_complex*>(fw), reinterpret_cast<fftw_complex*>(ft), FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
}
示例#14
0
void test01 ( void )

/******************************************************************************/
/*
  Purpose:

    TEST01: apply FFT to complex 1D data.

  Discussion:

    In this example, we generate N=100 random complex values stored as
    a vector of type FFTW_COMPLEX named "IN".

    We have FFTW3 compute the Fourier transform of this data named "OUT".

    We have FFTW3 compute the inverse Fourier transform of "OUT" to get
    "IN2", which should be the original input data, scaled by N.

  Modified:

    04 November 2007

  Author:

    John Burkardt
*/
{
  int i;
  fftw_complex *in;
  fftw_complex *in2;
  int n = 100;
  fftw_complex *out;
  fftw_plan plan_backward;
  fftw_plan plan_forward;
  unsigned int seed = 123456789;

  printf ( "\n" );
  printf ( "TEST01\n" );
  printf ( "  Demonstrate FFTW3 on a single vector of complex data.\n" );
  printf ( "\n" );
  printf ( "  Transform data to FFT coefficients.\n" );
  printf ( "  Backtransform FFT coefficients to recover data.\n" );
  printf ( "  Compare recovered data to original data.\n" );
/*
  Create the input array.
*/
  in = fftw_malloc ( sizeof ( fftw_complex ) * n );

  srand ( seed );

  for ( i = 0; i < n; i++ )
  {
    in[i][0] = frand ( );
    in[i][1] = frand ( );
  }

  printf ( "\n" );
  printf ( "  Input Data:\n" );
  printf ( "\n" );

  for ( i = 0; i < n; i++ )
  {
    printf ( "  %3d  %12f  %12f\n", i, in[i][0], in[i][1] );
  }
/*
  Create the output array.
*/
  out = fftw_malloc ( sizeof ( fftw_complex ) * n );

  plan_forward = fftw_plan_dft_1d ( n, in, out, FFTW_FORWARD, FFTW_ESTIMATE );

  fftw_execute ( plan_forward );

  printf ( "\n" );
  printf ( "  Output FFT Coefficients:\n" );
  printf ( "\n" );

  for ( i = 0; i < n; i++ )
  {
    printf ( "  %3d  %12f  %12f\n", i, out[i][0], out[i][1] );
  }
/*
  Recreate the input array.
*/
  in2 = fftw_malloc ( sizeof ( fftw_complex ) * n );

  plan_backward = fftw_plan_dft_1d ( n, out, in2, FFTW_BACKWARD, FFTW_ESTIMATE );

  fftw_execute ( plan_backward );

  printf ( "\n" );
  printf ( "  Recovered input data:\n" );
  printf ( "\n" );

  for ( i = 0; i < n; i++ )
  {
    printf ( "  %3d  %12f  %12f\n", i, in2[i][0], in2[i][1] );
  }

  printf ( "\n" );
  printf ( "  Recovered input data divided by N:\n" );
  printf ( "\n" );

  for ( i = 0; i < n; i++ )
  {
    printf ( "  %3d  %12f  %12f\n", i, 
      in2[i][0] / ( double ) ( n ), in2[i][1] / ( double ) ( n ) );
  }
/*
  Free up the allocated memory.
*/
  fftw_destroy_plan ( plan_forward );
  fftw_destroy_plan ( plan_backward );

  fftw_free ( in );
  fftw_free ( in2 );
  fftw_free ( out );

  return;
}
示例#15
0
void dgt_fac(ltfat_complex *f, ltfat_complex *gf, const int L, const int W,
	     const int R, const int a, const int M, ltfat_complex *cout, int dotime)
{

   /*  --------- initial declarations -------------- */

   int b, N, c, d, p, q, h_a, h_m;
   
   double *gbase, *fbase, *cbase;

   int l, k, r, s, u, w, rw, nm, mm, km;
   int ld2a, ld1b, ld3b;
   int ld4c, ld5c;
   int rem;

   fftw_plan p_before, p_after, p_veryend;
   double *ff, *cf, *ffp, *sbuf, *fp, *cfp;

   double scalconst;
   
   double st0, st1, st6, st7;

   /*  ----------- calculation of parameters and plans -------- */
   
   if (dotime)
   {
      st0=ltfat_time();
   }

   b=L/M;
   N=L/a;
   
   c=gcd(a, M,&h_a, &h_m);
   p=a/c;
   q=M/c;
   d=b/p;

   h_a=-h_a;

   /* Scaling constant needed because of FFTWs normalization. */
   scalconst=1.0/((double)d*sqrt((double)M));

   ff = (double*)ltfat_malloc(2*d*p*q*W*sizeof(double));
   cf = (double*)ltfat_malloc(2*d*q*q*W*R*sizeof(double));
   sbuf = (double*)ltfat_malloc(2*d*sizeof(double));

   /* Create plans. In-place. */

   p_before = fftw_plan_dft_1d(d, (ltfat_complex*)sbuf, (ltfat_complex*)sbuf,
			       FFTW_FORWARD, FFTW_MEASURE);

   p_after  = fftw_plan_dft_1d(d, (ltfat_complex*)sbuf, (ltfat_complex*)sbuf,
			       FFTW_BACKWARD, FFTW_MEASURE);
   
  /* Create plan. In-place. */
   p_veryend = fftw_plan_many_dft(1, &M, N*R*W,
				  cout, NULL,
				  1, M,
				  cout, NULL,
				  1, M,
				  FFTW_FORWARD, FFTW_OPTITYPE);
      

   if (dotime)
   {
      st1=ltfat_time();
      printf("DGT_FAC_7: Planning phase %f\n",st1-st0);
   }


   /* Leading dimensions of the 4dim array. */
   ld2a=2*p*q*W;

   /* Leading dimensions of cf */
   ld1b=q*R;
   ld3b=2*q*R*q*W;
   
   /* --------- main loop begins here ------------------- */
   for (r=0;r<c;r++)
   {
      
      
      /*  ---------- compute signal factorization ----------- */
      ffp=ff;
      fp=(double*)f+2*r;
      if (p==1)

	 /* Integer oversampling case */
      {
	 
	 for (w=0;w<W;w++)
	 {
	    for (l=0;l<q;l++)
	    {
	       for (s=0;s<d;s++)
	       {		  
		  rem = 2*((s*M+l*a)%L);
		  sbuf[2*s]   = fp[rem];
		  sbuf[2*s+1] = fp[rem+1];
	       }
	       
	       fftw_execute(p_before);
	       
	       for (s=0;s<d;s++)
	       {		  
		 ffp[s*ld2a]   = sbuf[2*s]*scalconst;
		 ffp[s*ld2a+1] = sbuf[2*s+1]*scalconst;
	       }
	       ffp+=2;
	    }
	    fp+=2*L;
	 }
	 fp-=2*L*W;
	 
      }
      else
      {      
	 /* rational sampling case */

	 for (w=0;w<W;w++)
	 {
	    for (l=0;l<q;l++)
	    {
	       for (k=0;k<p;k++)
	       {
		  for (s=0;s<d;s++)
		  {		  
		     rem = 2*positiverem(k*M+s*p*M-l*h_a*a, L);
		     sbuf[2*s]   = fp[rem];
		     sbuf[2*s+1] = fp[rem+1];
		  }
		  
		  fftw_execute(p_before);
		  
		  for (s=0;s<d;s++)
		  {		  
		     ffp[s*ld2a]   = sbuf[2*s]*scalconst;
		     ffp[s*ld2a+1] = sbuf[2*s+1]*scalconst;
		  }
		  ffp+=2;
	       }
	    }
	    fp+=2*L;
	 }
	 fp-=2*L*W;
      }

      /* ----------- compute matrix multiplication ----------- */

      /* Do the matmul  */
      if (p==1)
      {
	 /* Integer oversampling case */
	 

	 /* Rational oversampling case */
	 for (s=0;s<d;s++)
	 {	
	    gbase=(double*)gf+2*(r+s*c)*q*R;
	    fbase=ff+2*s*q*W;
	    cbase=cf+2*s*q*q*W*R;
	    
	    for (nm=0;nm<q*W;nm++)
	    {
	       for (mm=0;mm<q*R;mm++)
	       {
		  cbase[0]=gbase[0]*fbase[0]+gbase[1]*fbase[1];
		  cbase[1]=gbase[0]*fbase[1]-gbase[1]*fbase[0];
		  gbase+=2;
		  cbase+=2;
	       }			       
	       gbase-=2*q*R;
	       fbase+=2;
	    }
	    cbase-=2*q*R*q*W;
	 }




      }
      else
      {

	 /* Rational oversampling case */
	 for (s=0;s<d;s++)
	 {	
	    gbase=(double*)gf+2*(r+s*c)*p*q*R;
	    fbase=ff+2*s*p*q*W;
	    cbase=cf+2*s*q*q*W*R;
	    
	    for (nm=0;nm<q*W;nm++)
	    {
	       for (mm=0;mm<q*R;mm++)
	       {
		  cbase[0]=0.0;
		  cbase[1]=0.0;
		  for (km=0;km<p;km++)
		  {
		     cbase[0]+=gbase[0]*fbase[0]+gbase[1]*fbase[1];
		     cbase[1]+=gbase[0]*fbase[1]-gbase[1]*fbase[0];
		     gbase+=2;
		     fbase+=2;
		  }
		  fbase-=2*p;
		  cbase+=2;
	       }			       
	       gbase-=2*q*R*p;
	       fbase+=2*p;
	    }
	    cbase-=2*q*R*q*W;
	    fbase-=2*p*q*W;
	 }
      }



      /*  -------  compute inverse coefficient factorization ------- */
      cfp=cf;
      ld4c=M*N;
      ld5c=M*N*R;

      /* Cover both integer and rational sampling case */
      for (w=0;w<W;w++)
      {
	 /* Complete inverse fac of coefficients */
	 for (l=0;l<q;l++)
	 {
	    for (rw=0;rw<R;rw++)
	    {
	       for (u=0;u<q;u++)
	       {	       	       
		  for (s=0;s<d;s++)	       
		  {	
		     sbuf[2*s]   = cfp[s*ld3b];
		     sbuf[2*s+1] = cfp[s*ld3b+1];
		  }
		  cfp+=2;
		  
		  /* Do inverse fft of length d */
		  fftw_execute(p_after);
		  
		  for (s=0;s<d;s++)	       
		  {	
		     rem= r+l*c+positiverem(u+s*q-l*h_a,N)*M+rw*ld4c+w*ld5c;
		     cout[rem][0]=sbuf[2*s];
		     cout[rem][1]=sbuf[2*s+1];
		  }		    
	       }
	    }
	 }
      }            

      
      /* ----------- Main loop ends here ------------------------ */
   }     

   if (dotime)
   {
      st6=ltfat_time();
      printf("DGT_FAC_7: Main loop done %f\n",st6-st1);
   }

   /* FFT to modulate the coefficients. */
   fftw_execute(p_veryend);   

   if (dotime)
   {
      st7=ltfat_time();
      printf("DGT_FAC_7: Final FFT %f\n",st7-st6);
      printf("DGT_FAC_7: Total time %f\n",st7-st0);
   }

    /* -----------  Clean up ----------------- */   
   fftw_destroy_plan(p_before);
   fftw_destroy_plan(p_after);
   fftw_destroy_plan(p_veryend);

   ltfat_free(sbuf);
   ltfat_free(ff);
   ltfat_free(cf);
   
}
示例#16
0
static void generate_ad_edd(void)
{
    float f;
    float offset;
    float amp;
    float phase;
    float delay;
    float pw;
#if defined(HAVE_FFTW3_H)
    double in[FFT_SIZE][2];
    double out[FFT_SIZE][2];
#else
    fftw_complex in[FFT_SIZE];
    fftw_complex out[FFT_SIZE];
#endif
    fftw_plan p;
    int i;
    int j;
    int k;
    int l;

#if defined(HAVE_FFTW3_H)
    p = fftw_plan_dft_1d(FFT_SIZE, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
#else
    p = fftw_create_plan(FFT_SIZE, FFTW_BACKWARD, FFTW_ESTIMATE);
#endif
    for (j = 0;  j < 6;  j++)
    {
        for (k = 0;  k < 3;  k++)
        {
            for (i = 0;  i < FFT_SIZE;  i++)
            {
#if defined(HAVE_FFTW3_H)
                in[i][0] =
                in[i][1] = 0.0f;
#else
                in[i].re =
                in[i].im = 0.0f;
#endif
            }
            for (i = 1;  i < FFT_SIZE/2;  i++)
            {
                f = (float) i*SAMPLE_RATE/FFT_SIZE;
                amp = 0.0f;
                for (l = 0;  l < (int) (sizeof(ad)/sizeof(ad[0]));  l++)
                {
                    if (f < ad[l].freq)
                        break;
                }
                if (l < (int) (sizeof(ad)/sizeof(ad[0])))
                {
                    offset = (f - ad[l - 1].freq)/(ad[l].freq - ad[l - 1].freq);
                    amp = (1.0 - offset)*ad[l - 1].ad[j] + offset*ad[l].ad[j];
                    amp = pow(10.0, -amp/20.0);
                }
                delay = 0.0f;
                for (l = 0;  l < (int) (sizeof(edd)/sizeof(edd[0]));  l++)
                {
                    if (f < edd[l].freq)
                        break;
                }
                if (l < (int) (sizeof(edd)/sizeof(edd[0])))
                {
                    offset = (f - edd[l - 1].freq)/(edd[l].freq - edd[l - 1].freq);
                    delay = (1.0f - offset)*edd[l - 1].edd[k] + offset*edd[l].edd[k];
                }
                phase = 2.0f*M_PI*f*delay*0.001f;
#if defined(HAVE_FFTW3_H)    
                in[i][0] = amp*cosf(phase);
                in[i][1] = amp*sinf(phase);
                in[FFT_SIZE - i][0] = in[i][0];
                in[FFT_SIZE - i][1] = -in[i][1];
#else
                in[i].re = amp*cosf(phase);
                in[i].im = amp*sinf(phase);
                in[FFT_SIZE - i].re = in[i].re;
                in[FFT_SIZE - i].im = -in[i].im;
#endif
            }
#if 0
            for (i = 0;  i < FFT_SIZE;  i++)
                fprintf(outfile, "%5d %15.5f,%15.5f\n", i, in[i].re, in[i].im);
#endif
#if defined(HAVE_FFTW3_H)    
            fftw_execute(p);
#else
            fftw_one(p, in, out);
#endif

            fprintf(outfile, "/* V.56bis AD-%d, EDD%d */\n", (j == 0)  ?  1  :  j + 4, k + 1);

            fprintf(outfile, "float ad_%d_edd_%d_model[] =\n", (j == 0)  ?  1  :  j + 4, k + 1);
            fprintf(outfile, "{\n");
            /* Normalise the filter's gain */
            pw = 0.0f;
            l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
            for (i = 0;  i < LINE_FILTER_SIZE;  i++)
            {
#if defined(HAVE_FFTW3_H)
                pw += out[l][0]*out[l][0];
#else
                pw += out[l].re*out[l].re;
#endif
                if (++l == FFT_SIZE)
                    l = 0;
            }
            pw = sqrt(pw);
            l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
            for (i = 0;  i < LINE_FILTER_SIZE;  i++)
            {
                
#if defined(HAVE_FFTW3_H)
                impulse_responses[filter_sets][i] = out[l][0]/pw;
#else
                impulse_responses[filter_sets][i] = out[l].re/pw;
#endif
                fprintf(outfile, "%15.5f,\n", impulse_responses[filter_sets][i]);
                if (++l == FFT_SIZE)
                    l = 0;
            }
            fprintf(outfile, "};\n\n");
            filter_sets++;
        }
    }
}
示例#17
0
MFCC* generateMFCC(
    Waveform inputAudio, 
    const MFCCParams params,
    bool debugOutput,
    std::string debugPrefix) {
    
    // Yay compiler optimizations
    const double& frameLengthMilliseconds = params.frameLengthMilliseconds;
    const double& frameStepMilliseconds = params.frameStepMilliseconds;
    const double& filterMinFreq = params.filterMinFreq;
    const double& filterMaxFreq = params.filterMaxFreq;
    const int32_t& numFilterbanks = params.numFilterbanks;
    const int32_t& numMfccs = params.numMfccs;
    
    // (Rounded toward zero)
    int32_t windowLength = (frameLengthMilliseconds * inputAudio.mSampleRate) / 1000;
    int32_t spectrumLength = windowLength / 2;
    int32_t windowStep = (frameStepMilliseconds * inputAudio.mSampleRate) / 1000;
    
    std::cout << "\tWindow function: Hanning" << std::endl;
    std::cout << "\tWindow length: " << windowLength << " samples / " << frameLengthMilliseconds << "ms" << std::endl;
    if(windowLength < 0) {
        std::cerr << "Fatal error! Length cannot be negative!" << std::endl;
        return NULL;
    }
    
    std::cout << "\tWindow step: " << windowStep << " samples / " << frameStepMilliseconds << "ms" << std::endl;
    if(windowStep < 1) {
        std::cerr << "Fatal error! Step must be greater than 0!" << std::endl;
        return NULL;
    }
    
    int64_t numWindows = 0;
    // TODO: use constant time calculation
    for(int64_t windowIndex = 0; (windowIndex * windowStep) < inputAudio.mNumSamples; ++ windowIndex) {
        numWindows ++;
    }
    std::cout << "\tWindow count: " << numWindows << std::endl;
    
    std::cout << "\tFilterbank energy count: " << numFilterbanks << std::endl;
    std::cout << "\tMel frequency count:" << numMfccs << std::endl;
    if(numMfccs > numFilterbanks) {
        std::cerr << "Fatal error! Mel frequency count is greater than the number of filterbank energies!" << std::endl;
        return NULL;
    }
    
    std::cout << "\tLower filterbank range: " << filterMinFreq << "hz" << std::endl;
    std::cout << "\tUpper filterbank range: " << filterMaxFreq << "hz" << std::endl;
    if(filterMinFreq > filterMaxFreq) {
        std::cerr << "Fatal error! Invalid range!" << std::endl;
        return NULL;
    }
    
    // The frequency represented by fftwCompleteOutput[i][n] in hertz is equal to:
    // freq = n * inputAudio.mSampleRate / windowLength
    // Inverse is approximated by:
    // bin = floor((freq * (windowLength + 1)) / inputAudio.mSampleRate)

    // FFTW transform
    ComplexNumber** fftwCompleteOutput = new ComplexNumber*[numWindows];
    for(int64_t i = 0; i < numWindows; ++ i) {
        fftwCompleteOutput[i] = new ComplexNumber[spectrumLength];
    }
    {
        fftw_complex* fftwInput = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * windowLength);
        fftw_complex* fftwOutput = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * windowLength);
        
        std::cout << "\tOptimizing FFTW... ";
        fftw_plan fftwPlan = fftw_plan_dft_1d(windowLength, fftwInput, fftwOutput, FFTW_FORWARD, FFTW_MEASURE);
        std::cout << "\tdone" << std::endl;
        
        // Set imaginary components to be zero
        for(int64_t windowSample = 0; windowSample < windowLength; ++ windowSample) {
            fftwInput[windowSample][1] = 0;
        }
        
        std::cout << "\tPerforming DFT... ";
        for(int64_t windowIndex = 0; windowIndex < numWindows; ++ windowIndex) {
            
            int64_t beginningSample = windowIndex * windowStep;
            
            for(int64_t windowSample = 0; windowSample < windowLength; ++ windowSample) {
                int64_t currentSampleIndex = beginningSample + windowSample;
                
                double sample;
                if(currentSampleIndex >= inputAudio.mNumSamples) {
                    sample = 0;
                } else {
                    sample = inputAudio.mFloatSamples[currentSampleIndex];
                    
                    // Apply hanning window
                    
                    // Hooray for compiler optimizations
                    double tau = 6.28318530717958647692528677;
                    double numerator = tau * windowSample;
                    double denominator = windowLength - 1;
                    double hanning = 0.5 * (1.0 - std::cos(numerator / denominator));
                    
                    sample *= hanning;
                }
                
                fftwInput[windowSample][0] = sample;
            }
            
            fftw_execute(fftwPlan);
            
            for(int64_t windowSample = 0; windowSample < spectrumLength; ++ windowSample) {
                
                fftwCompleteOutput[windowIndex][windowSample].real = fftwOutput[windowSample][0];
                fftwCompleteOutput[windowIndex][windowSample].imag = fftwOutput[windowSample][1];
            }
        }
        if(debugOutput) writeFFTWOutputDebug(debugPrefix + "_fftw.png", fftwCompleteOutput, numWindows, spectrumLength);
        std::cout << "\tdone" << std::endl;
        
        fftw_destroy_plan(fftwPlan);
        fftw_free(fftwOutput);
        fftw_free(fftwInput);
    }
    
    // Power estmates
    double** powerEstimates = new double*[numWindows];
    for(int64_t i = 0; i < numWindows; ++ i) {
        powerEstimates[i] = new double[spectrumLength];
    }
    {
        std::cout << "\tComputing power estimates... ";
        for(int64_t windowIndex = 0; windowIndex < numWindows; ++ windowIndex) {
            for(int64_t windowSample = 0; windowSample < spectrumLength; ++ windowSample) {
                
                double real = fftwCompleteOutput[windowIndex][windowSample].real;
                double imaginary = fftwCompleteOutput[windowIndex][windowSample].imag;
                double absValSq = real * real + imaginary * imaginary;
                double denom = windowLength; // NOT spectrumLength!
                
                
                powerEstimates[windowIndex][windowSample] = absValSq / denom;
            }
        }
        if(debugOutput) writeGenericHeatOutput(debugPrefix + "_power.png", powerEstimates, numWindows, spectrumLength);
        std::cout << "\tdone" << std::endl;
    }
    
    for(int64_t i = 0; i < numWindows; ++ i) {
        delete[] fftwCompleteOutput[i];
    }
    delete[] fftwCompleteOutput;
    
    // Mel filterbank
    double* filterbank = new double[numFilterbanks + 2];
    {
        double filterMaxFreqMels = melScale(filterMaxFreq);
        double filterMinFreqMels = melScale(filterMinFreq);
        
        std::cout << "\tMel filterbank (hz): ";
        double melsStep = (filterMaxFreqMels - filterMinFreqMels) / (numFilterbanks + 1);
        for(int32_t i = 0; i < numFilterbanks + 2; ++ i) {
            double mels = melsStep * i + filterMinFreqMels;
            double hertz = invMelScale(mels);
            
            filterbank[i] = hertz;
            
            std::cout << hertz;
            if(i != numFilterbanks + 1) {
                std::cout << ", ";
            }
        }
        std::cout << std::endl;
    }
    
    //
    double** loggedFilterbankEnergies = new double*[numWindows];
    for(int64_t i = 0; i < numWindows; ++ i) {
        loggedFilterbankEnergies[i] = new double[numFilterbanks];
    }
    {
        #ifndef NDEBUG
        double** filterbankEnergies = new double*[numWindows];
        for(int64_t i = 0; i < numWindows; ++ i) {
            filterbankEnergies[i] = new double[numFilterbanks];
        }
        #endif // !NDEBUG
        std::cout << "\tComputing filterbank energies... ";
        for(int64_t windowIndex = 0; windowIndex < numWindows; ++ windowIndex) {
            for(int32_t filterbankIndex = 0; filterbankIndex < numFilterbanks; ++ filterbankIndex) {
                double filterBegin = filterbank[filterbankIndex];
                double filterMiddle = filterbank[filterbankIndex + 1];
                double filterEnd = filterbank[filterbankIndex + 2];
                
                
                double totalEnergy = 0;
                for(int64_t windowSample = 0; windowSample < spectrumLength; ++ windowSample) {
                    
                    double sampleFrequency = ((double) (inputAudio.mSampleRate * windowSample)) / ((double) windowLength);
                    
                    if(sampleFrequency < filterBegin) continue;
                    if(sampleFrequency > filterEnd) break;
                    
                    double filterY = filterMiddle - sampleFrequency;
                    if(filterY < 0) {
                        filterY /= filterMiddle - filterEnd;
                    } else {
                        filterY /= filterMiddle - filterBegin;
                    }
                    
                    totalEnergy += powerEstimates[windowIndex][windowSample] * filterY;
                }
                #ifndef NDEBUG
                filterbankEnergies[windowIndex][filterbankIndex] = totalEnergy;
                #endif // !NDEBUG
                loggedFilterbankEnergies[windowIndex][filterbankIndex] = std::log(totalEnergy); // Natural log, please
            }
        }
        if(debugOutput) writeGenericHeatOutput(debugPrefix + "_energies_log.png", loggedFilterbankEnergies, numWindows, numFilterbanks, -10, 1);
        #ifndef NDEBUG
        if(debugOutput) writeGenericHeatOutput(debugPrefix + "_energies.png", filterbankEnergies, numWindows, numFilterbanks);
        for(int64_t i = 0; i < numWindows; ++ i) {
            delete[] filterbankEnergies[i];
        }
        delete[] filterbankEnergies;
        #endif // !NDEBUG
        
        std::cout << "\tdone" << std::endl;
        
    }
    for(int64_t i = 0; i < numWindows; ++ i) {
        delete[] powerEstimates[i];
    }
    delete[] powerEstimates;
    delete[] filterbank;
    
    // MFCC (Discrete cosine transform and tossing out high-frequency data)
    double** mfccs = new double*[numWindows];
    for(int64_t i = 0; i < numWindows; ++ i) {
        mfccs[i] = new double[numMfccs];
    }
    {
        
        std::cout << "\tComputing mel frequency cepstral coefficients... ";
        for(int64_t windowIndex = 0; windowIndex < numWindows; ++ windowIndex) {
            for(int32_t mfccIndex = 0; mfccIndex < numMfccs; ++ mfccIndex) {
                
                double total = 0;
                for(int32_t filterbankIndex = 0; filterbankIndex < numFilterbanks; ++ filterbankIndex) {
                    total += loggedFilterbankEnergies[windowIndex][filterbankIndex] * std::cos((3.141592653589793 / numFilterbanks) * (0.5 + filterbankIndex) * mfccIndex);
                }
                
                // May or may not be needed here
                // Makes the transformation orthoganal
                if(mfccIndex == 0) total *= std::sqrt(0.5);
                total *= std::sqrt(2.0 / numFilterbanks);
                
                mfccs[windowIndex][mfccIndex] = total;
            }
            
        }
        if(debugOutput) writeGenericHeatOutput(debugPrefix + "_mfcc.png", mfccs, numWindows, numMfccs, -4, 18);
        std::cout << "\tdone" << std::endl;
    }
    for(int64_t i = 0; i < numWindows; ++ i) {
        delete[] loggedFilterbankEnergies[i];
    }
    delete[] loggedFilterbankEnergies;
    
    return new MFCC(mfccs, numWindows, numMfccs);
}
示例#18
0
static void generate_proakis(void)
{
    float f;
    float f1;
    float offset;
    float amp;
    float phase;
    float delay;
    float pw;
    int index;
    int i;
    int l;
#if defined(HAVE_FFTW3_H)
    double in[FFT_SIZE][2];
    double out[FFT_SIZE][2];
#else
    fftw_complex in[FFT_SIZE];
    fftw_complex out[FFT_SIZE];
#endif
    fftw_plan p;

#if defined(HAVE_FFTW3_H)
    p = fftw_plan_dft_1d(FFT_SIZE, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
#else
    p = fftw_create_plan(FFT_SIZE, FFTW_BACKWARD, FFTW_ESTIMATE);
#endif
    for (i = 0;  i < FFT_SIZE;  i++)
    {
#if defined(HAVE_FFTW3_H)
        in[i][0] =
        in[i][1] = 0.0f;
#else
        in[i].re =
        in[i].im = 0.0f;
#endif
    }
    for (i = 1;  i < FFT_SIZE/2;  i++)
    {
        f = (float) i*SAMPLE_RATE/FFT_SIZE;
        f1 = f/200.0f;
        offset = f1 - floor(f1);
        index = (int) floor(f1);

        /* Linear interpolation */
        amp = ((1.0f - offset)*proakis[index].amp + offset*proakis[index + 1].amp)/2.3f;
        delay = (1.0f - offset)*proakis[index].delay + offset*proakis[index + 1].delay;
        phase = 2.0f*M_PI*f*delay*0.001f;
#if defined(HAVE_FFTW3_H)
        in[i][0] = amp*cosf(phase);
        in[i][1] = amp*sinf(phase);
        in[FFT_SIZE - i][0] = in[i][0];
        in[FFT_SIZE - i][1] = -in[i][1];
#else
        in[i].re = amp*cosf(phase);
        in[i].im = amp*sinf(phase);
        in[FFT_SIZE - i].re = in[i].re;
        in[FFT_SIZE - i].im = -in[i].im;
#endif
    }

#if defined(HAVE_FFTW3_H)
    fftw_execute(p);
#else
    fftw_one(p, in, out);
#endif

    fprintf(outfile, "/* Medium range telephone line response\n");
    fprintf(outfile, "   (from p 537, Digital Communication, John G. Proakis */\n");

    fprintf(outfile, "float proakis_line_model[] =\n");
    fprintf(outfile, "{\n");
    /* Normalise the filter's gain */
    pw = 0.0f;
    l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
    for (i = 0;  i < LINE_FILTER_SIZE;  i++)
    {
#if defined(HAVE_FFTW3_H)
        pw += out[l][0]*out[l][0];
#else
        pw += out[l].re*out[l].re;
#endif
        if (++l == FFT_SIZE)
            l = 0;
    }
    pw = sqrt(pw);
    l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
    for (i = 0;  i < LINE_FILTER_SIZE;  i++)
    {
#if defined(HAVE_FFTW3_H)
        impulse_responses[filter_sets][i] = out[l][0]/pw;
#else
        impulse_responses[filter_sets][i] = out[l].re/pw;
#endif
        fprintf(outfile, "%15.5f,\n", impulse_responses[filter_sets][i]);
        if (++l == FFT_SIZE)
            l = 0;
    }
    fprintf(outfile, "};\n\n");
    filter_sets++;
}
示例#19
0
void main(int argc, char *argv[], char *envp[])
{
	fftw_complex *in, *out;
	fftw_plan p;
	size_t N = 1024;
	double FS = 1e6;	// 1MHz sample rate
	double WIN = 0.0;	// 0% window overlap
	double nadd,nmul,nfma,ntotal,factor;
	char *units, *fs_units;
	bool EST = false;
	char *endp = NULL;
    cpuid_info_t cpu;

	if(argc>1 && ( *argv[1]=='?' || *argv[1]=='-' ) ) usage(argv[0]);

	errno = 0;
	if(argc>1) N   = strtoul(argv[1],&endp,0); if(errno) perror("N"); errno = 0;
	     if(endp && tolower(*endp)=='k') N *= 1024;
	else if(endp && tolower(*endp)=='m') N *= 1024*1024;
	if(argc>2) FS  = strtod(argv[2],&endp); if(errno) perror("FS(Hz)"); errno = 0;
	     if(endp && *endp=='k') FS *= 1000.0;	// common use would be to qualify with a 2nd character
	else if(endp && *endp=='M') FS *= 1000.0*1000.0;
	else if(endp && *endp=='G') FS *= 1000.0*1000.0*1000.0;
	else if(endp && *endp=='T') FS *= 1000.0*1000.0*1000.0*1000.0;  // I'm dreaming of the day...
	else if(endp && *endp=='m') FS /= 1000.0;	// ok, kind of silly
	if(argc>3) WIN = strtod(argv[3],NULL); if(errno) perror("WINDOW(%)"); errno = 0;
	if(argc>4) EST = atoi(argv[4])?true:false;

    cpuid_get_info( &cpu );

	printf("FFT(%u,%s):\n",N,EST?"estimated":"measured");
	in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
	out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
	p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, EST?FFTW_ESTIMATE:FFTW_MEASURE);
	fftw_flops(p,&nadd,&nmul,&nfma);
	fftw_print_plan(p);
	ntotal = nadd+nmul+((cpu.exts.XOP||cpu.exts.FMA3||cpu.exts.FMA4)?nfma:2*nfma);
	
	printf("\nFLOPS: add=%.0f mul=%.0f fma=%.0f total=%.0f Flops/frame\n",
		nadd, nmul, nfma, ntotal);
	factor  = (1.0 + ( 1.0 / ( 1.0 - WIN ))) / 2.0 ;	// additional FFTs required due to overlap
	ntotal *= (FS * factor) / N;						// FFTs = FS/N times the factor due to overlap
	     if( ntotal > 5e17) { ntotal /= 1e18;units = "ExaFlops"; }
	else if( ntotal > 5e14) { ntotal /= 1e15;units = "PFlops"; }
	else if( ntotal > 5e11) { ntotal /= 1e12;units = "TFlops"; }
	else if( ntotal > 5e8 ) { ntotal /= 1e9; units = "GFlops"; }
	else if( ntotal > 5e5 ) { ntotal /= 1e6; units = "MFlops"; }
	else if( ntotal > 5e2 ) { ntotal /= 1e3; units = "KFlops"; }
    else                    {                units = "Flops";  }
	     if( FS > 5e11) { FS /= 1e12;fs_units = "THz"; }
	else if( FS > 5e8 ) { FS /= 1e9; fs_units = "GHz"; }
	else if( FS > 5e5 ) { FS /= 1e6; fs_units = "MHz"; }
	else if( FS > 5e2 ) { FS /= 1e3; fs_units = "KHz"; }
	else                {            fs_units = "Hz";  }
	printf("FS=%.2f%s, %.2f%% overlap, %.2f %s (%s method)\n",
		FS, fs_units, WIN*100.0, ntotal, units, EST?"by estimate":"by measure");

    printf("Current CPU = %s\n", cpu.name.str);
    printf("CPU Threads = %d\n", cpu.threads);

	// TODO: actually compute some representative FFTs, timing them and
	//       extrapolate the performance on *THIS* machine as configured
	//fftw_execute(p); /* repeat as needed */

	fftw_destroy_plan(p);
	fftw_free(in); fftw_free(out);
}
int effect_update(struct razer_fx_render_node *render)
{
	float magnitude = daemon_get_parameter_float(daemon_effect_get_parameter_by_index(render->effect,1));
	int x,y;
	struct razer_rgb col;
	#ifdef USE_DEBUGGING
		printf(" (Fft.%d ## %%:%f)",render->id);
	#endif

	unsigned long samples_left=0;
	while((samples_left=wav_samples_left(effect_input_file)))
	{
		unsigned int sample = read_wav_stereo_sample(effect_input_file);
		short high = sample >> 16;
		//short low = sample & 0xFFFF;
		//add sample to fft buffer
		effect_fft_in[effect_fft_samples_used][0] = (double)high * effect_fft_hamming_buffer[effect_fft_samples_used];///(double)32768;//* windowHanning(step++, N);
  		effect_fft_in[effect_fft_samples_used++][1] = 0.0f;
		//enough samples gathered?
		if(effect_fft_samples_used==effect_fft_samples)
  		{
  			printf("Computing fft, still %d samples left\n",samples_left);
			//compute fft
    		effect_fft_plan = fftw_plan_dft_1d(effect_fft_samples, effect_fft_in, effect_fft_out, FFTW_FORWARD, FFTW_ESTIMATE);
    		fftw_execute(effect_fft_plan);
    		fftw_destroy_plan(effect_fft_plan);
    		effect_fft_samples_used = 0;
 		    double tmp_magnitude = sqrt(effect_fft_out[0][0]*effect_fft_out[0][0] + effect_fft_out[0][1]*effect_fft_out[0][1]);
		    tmp_magnitude = 10./log(10.) * log(tmp_magnitude + 1e-6);
    		printf("new fft mag db:%f\n",tmp_magnitude);
    		double sum = 0.0f;
    		for(unsigned int i=0;i<effect_fft_samples/2;i++)
    		{
	 		    double tmp_bin_magnitude = sqrt(effect_fft_out[i][0]*effect_fft_out[i][0] + effect_fft_out[i][1]*effect_fft_out[i][1]);
			    tmp_bin_magnitude = 10./log(10.) * log(tmp_bin_magnitude + 1e-6);
			    sum += tmp_bin_magnitude;
    		}
    		printf("sum:%f\n",sum/(effect_fft_samples/2));
    		magnitude = (float)tmp_magnitude - 50.0f;
    		break;
  		}
	}
	if(!samples_left)
	{
		#ifdef USE_DEBUGGING
			printf("no samples left to analyze, closing input file\n");
		#endif
		close_wav(effect_input_file);
		effect_input_file = NULL;
		return(0);
	}


	//set color to avg magnitude ,transformed to 0.0-1.0 space	

	//calculate hue from magnitude
	rgb_from_hue(magnitude/96,0.3f,0.0f,&col);

	for(x=0;x<render->device->columns_num;x++)
		for(y=0;y<render->device->rows_num;y++)
		{
			rgb_mix_into(&render->output_frame->rows[y]->column[x],&render->input_frame->rows[y]->column[x],&col,render->opacity);//*render->opacity  //&render->second_input_frame->rows[y]->column[x]
			render->output_frame->update_mask |= 1<<y;
		}
	daemon_set_parameter_float(daemon_effect_get_parameter_by_index(render->effect,1),magnitude);	
	return(1);
}
示例#21
0
Baseline *newBaseline(const char *confFile)
{
	Baseline *B;
	FILE *in;
	const int NItem = 3;
	char buffer[NItem][MaxLineLen+1];
	int i;
	char *v;

	in = fopen(confFile, "r");
	if(!in)
	{
		fprintf(stderr, "Cannot open conf file %s\n", confFile);
	}

	B = (Baseline *)calloc(1, sizeof(Baseline));
	B->confFile = strdup(confFile);
	B->ds1 = newDataStream(in);
	B->ds2 = newDataStream(in);

	if(!B->ds1 || !B->ds2)
	{
		deleteBaseline(B);

		fclose(in);

		return 0;
	}

	if(B->ds1->ms->sec != B->ds2->ms->sec ||
	   B->ds1->ms->ns  != B->ds2->ms->ns)
	{
		printf("\n\n*** WARNING *** Data stream times do not match ***\n\n");
	}
	
	for(i = 0; i < NItem; i++)
	{
		v = fgets(buffer[i], MaxLineLen, in);
		if(!v)
		{
			deleteBaseline(B);

			fclose(in);

			return 0;
		}
		stripEOL(buffer[i]);
	}

	fclose(in);

	if(abs(B->ds1->nChan) != abs(B->ds2->nChan))
	{
		fprintf(stderr, "Number of channels per datastream must match (%d %d)\n",
			B->ds1->nChan, B->ds2->nChan);

		deleteBaseline(B);

		return 0;
	}
	
	B->nChan = abs(B->ds1->nChan);
	B->visFile = strdup(buffer[0]);
	B->lagFile = strdup(buffer[1]);
	B->nFFT = atoi(buffer[2]);
	if(B->nFFT <= 0)
	{
		B->nFFT = 0x7FFFFFFF;	/* effectively no limit */
	}
	B->visibility = (fftw_complex *)calloc(B->nChan, sizeof(fftw_complex));
	B->lags = (fftw_complex *)calloc(B->nChan, sizeof(fftw_complex));
	B->ac1 = (double *)calloc(B->nChan, sizeof(double));
	B->ac2 = (double *)calloc(B->nChan, sizeof(double));

	B->outVis = fopen(B->visFile, "w");
	if(!B->outVis)
	{
		fprintf(stderr, "Cannot open %s for output\n", B->visFile);

		deleteBaseline(B);

		return 0;
	}

	B->outLag = fopen(B->lagFile, "w");
	if(!B->outLag)
	{
		fprintf(stderr, "Cannot open %s for output\n", B->lagFile);

		deleteBaseline(B);

		return 0;
	}

	B->plan = fftw_plan_dft_1d(B->nChan, B->visibility, B->lags, FFTW_BACKWARD, FFTW_ESTIMATE);

	/* FIXME: check that ds1 and ds2 have same */
	B->deltaF = B->ds1->deltaF;
	B->deltaT = 1.0/(B->nChan*B->ds1->deltaF);

	return B;
}
示例#22
0
int main() {
  int rc;
  int size;
  snd_pcm_t *handle;
  snd_pcm_hw_params_t *params;
  unsigned int val;
  int dir;
  snd_pcm_uframes_t frames;
  char *buffer;

  screen = reinterpret_cast<msgBlit *>(malloc(sizeof(msgBlit) + WIDTH * HEIGHT));
  screen->cmd = CMD_BLIT;
  screen->x = 0;
  screen->y = 0;
  screen->w = WIDTH;
  screen->h = HEIGHT;

  pthread_t blitter;
  pthread_create(&blitter, nullptr, blitScreen, nullptr);

  fftw_complex *in, *out;
  fftw_plan plan;

  in = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * FFTWSIZE);
  out = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * FFTWSIZE);
  plan = fftw_plan_dft_1d(FFTWSIZE, in, out, FFTW_FORWARD, FFTW_ESTIMATE);

  /* Open PCM device for recording (capture). */
  rc = snd_pcm_open(&handle, "hw:1,0",
                    SND_PCM_STREAM_CAPTURE, 0);
  if (rc < 0) {
    fprintf(stderr,
            "unable to open pcm device: %s\n",
            snd_strerror(rc));
    exit(1);
  }

  /* Allocate a hardware parameters object. */
  snd_pcm_hw_params_alloca(&params);

  /* Fill it in with default values. */
  snd_pcm_hw_params_any(handle, params);

  /* Set the desired hardware parameters. */

  /* Interleaved mode */
  snd_pcm_hw_params_set_access(handle, params,
                      SND_PCM_ACCESS_RW_INTERLEAVED);

  /* Signed 16-bit little-endian format */
  snd_pcm_hw_params_set_format(handle, params,
                              SND_PCM_FORMAT_S16_LE);

  /* Two channels (stereo) */
  snd_pcm_hw_params_set_channels(handle, params, 2);

  /* 44100 bits/second sampling rate (CD quality) */
  val = 44100;
  snd_pcm_hw_params_set_rate_near(handle, params,
                                  &val, &dir);

  /* Set period size to FFTWSIZE frames. */
  frames = FFTWSIZE;
  snd_pcm_hw_params_set_period_size_near(handle,
                              params, &frames, &dir);

  /* Write the parameters to the driver */
  rc = snd_pcm_hw_params(handle, params);
  if (rc < 0) {
    fprintf(stderr,
            "unable to set hw parameters: %s\n",
            snd_strerror(rc));
    exit(1);
  }

  /* Use a buffer large enough to hold one period */
  snd_pcm_hw_params_get_period_size(params,
                                      &frames, &dir);
  size = frames * 4; /* 2 bytes/sample, 2 channels */
  buffer = (char *) malloc(size);

//   /* We want to loop for 5 seconds */
//   snd_pcm_hw_params_get_period_time(params,
//                                          &val, &dir);
//   const long one_second = 1000000 / val;
//   const long refresh = one_second / 30;

  double minVol[MAXPOS];
  double maxVol[MAXPOS];
  for(int i = 0; i < MAXPOS; ++i) {
    minVol[i] = maxVol[i] = 0;
  }

  while (1) {
    rc = snd_pcm_readi(handle, buffer, frames);
    if (rc == -EPIPE) {
      /* EPIPE means overrun */
      fprintf(stderr, "overrun occurred\n");
      snd_pcm_prepare(handle);
      continue;
    } else if (rc < 0) {
      fprintf(stderr,
              "error from read: %s\n",
              snd_strerror(rc));
    } else if (rc != (int)frames) {
      fprintf(stderr, "short read, read %d frames\n", rc);
    } else {
      for(int pos = 0; pos < FFTWSIZE; ++pos) {
        // printf("%d, %d, %d, %d\n", buffer[pos * 4], buffer[pos * 4 + 1], buffer[pos * 4 + 2], buffer[pos * 4 + 3]);
        in[pos][0] = buffer[pos * 4 + 1];
        in[pos][1] = 0;
      }

      fftw_execute(plan);

      for(int pos = 0; pos < MAXPOS; ++pos) {
        double vol = 0;
        for(int i = 0; i < 1; i++) {
          vol += fabs(out[pos+i][0]) + fabs(out[pos+i][1]);
        }

        if(vol < minVol[pos]) minVol[pos] = vol;
        if(vol > maxVol[pos]) maxVol[pos] = vol;

        int level = log(1000 * (vol - minVol[pos]) / maxVol[pos]) * HEIGHT / 6.9;

        int xStart = pos * WIDTH / MAXPOS;
        int xEnd = (pos + 1) * WIDTH / MAXPOS;
        for(int x = xStart; x < xEnd; ++x) {
          int yCross = std::min(HEIGHT, std::max(0, HEIGHT - level));

          for(int y = 0; y < yCross; ++y) {
            screen->data[y * WIDTH + x] = 0;
          }
          for(int y = yCross; y < HEIGHT; ++y) {
            screen->data[y * WIDTH + x] = 1;
          }
        }
      }
    }
  }

  snd_pcm_drain(handle);
  snd_pcm_close(handle);
  free(buffer);

  return 0;
}
示例#23
0
REAL8 MatchSI(COMPLEX16FrequencySeries **htilde1, COMPLEX16FrequencySeries **htilde2, REAL8 fMin, REAL8 fMax, REAL8 df) {
  // Frequencies are in Hz!

  // Assume that waveforms use same frequency points
  int len1 = (*htilde1)->data->length;
  int len2 = (*htilde1)->data->length;
  if (len1 != len2) {
    XLALPrintError("Length of waveforms differs!\n");
    XLAL_ERROR(XLAL_EDOM); // FIXME
  }
  int n = len1;

  fftw_complex *integrand;
  integrand = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * n);
  REAL8 PSDfact = XLALSimNoisePSDaLIGOZeroDetHighPower((fMax-fMin)/4.);
  REAL8 tableS;
  COMPLEX16 h1,h2;

  REAL8 norm1 = 0.0;
  REAL8 norm2 = 0.0;
  int iStart = (int)(fMin / df);
  int iStop = (int)(fMax / df);
  iStart = (iStart < 1) ? 1 : iStart;
  iStop = (iStop > n) ? n : iStop;
  for (int i=iStart; i<iStop; i++) {
    REAL8 f = i*df;
    tableS = PSDfact / XLALSimNoisePSDaLIGOZeroDetHighPower(f);
    h1 = ((*htilde1)->data->data)[i];
    h2 = ((*htilde2)->data->data)[i];
    integrand[i] = h1 * conj(h2) * tableS;
    norm1 += sqr(cabs(h1)) * tableS;
    norm2 += sqr(cabs(h2)) * tableS;
    // printf("f = %g\tnoise(f) = %g\ttableS[i] = %g\tintegrand[i] = %g\n", f, XLALSimNoisePSDaLIGOZeroDetHighPower(f), tableS, creal(integrand[i]));
    // printf("{norm1, norm2} = {%g,%g}\t{%g,%g}\n", norm1,norm2, cabs(h1)*tableS, cabs(h2)*tableS);
  }

  n = iStop-iStart;
  int zpf = 10;
  int m = n + 2*zpf*n; // zero-pad on both sides
  //printf("Total length %d\n", m);
  fftw_complex *array;
  fftw_plan p;
  array = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * m);
  p = fftw_plan_dft_1d(m, array, array, FFTW_FORWARD, FFTW_ESTIMATE); // prepare in-place FFT

  // fill input array
  for (int i=0; i<zpf*n; i++)
    array[i] = 0.0;
  for (int i=zpf*n; i<(zpf*n + n); i++)
    array[i] = integrand[iStart + i-zpf*n];
  for (int i=zpf*n + n; i<m; i++)
    array[i] = 0.0;

  fftw_execute(p);

  REAL8 match=0; REAL8 val;
  for (int i=0; i<m; i++) {
    val = cabs(array[i]);
    if (val > match)
      match = val;
  }

  fftw_destroy_plan(p);
  fftw_free(array);
  fftw_free(integrand);

  return match / sqrt(norm1*norm2);
}
示例#24
0
//***************************************************************************
int main(int argc, char *argv[])
{
  extern char *optarg;
  extern int optind;
  int i,j,k;
  unsigned char *symbols, *decdata;
  signed char message[]={-9,13,-35,123,57,-39,64,0,0,0,0};
  char *callsign,*grid,*grid6, *call_loc_pow, *cdbm;
  char *ptr_to_infile,*ptr_to_infile_suffix;
  char uttime[5],date[7];
  char xuttime[6],xdate[11];
  int c,delta,nfft2=65536,verbose=0,quickmode=0,writenoise=0,usehashtable=1;
  int shift1, lagmin, lagmax, lagstep, worth_a_try, not_decoded, nadd, ndbm;
  int32_t n1, n2, n3;
  unsigned int nbits;
  unsigned int npoints, metric, maxcycles, cycles, maxnp;
  float df=375.0/256.0/2;
  float freq0[200],snr0[200],drift0[200],sync0[200];
  int shift0[200];
  float dt=1.0/375.0;
  double dialfreq_cmdline=0.0, dialfreq;
  float dialfreq_error=0.0;
  float fmin=-110, fmax=110;
  float f1, fstep, sync1, drift1, tblank=0, fblank=0;
  double *idat, *qdat;
  clock_t t0,t00;
  double tfano=0.0,treadwav=0.0,tcandidates=0.0,tsync0=0.0;
  double tsync1=0.0,tsync2=0.0,ttotal=0.0;

// Parameters used for performance-tuning:
  maxcycles=10000;                         //Fano timeout limit
  double minsync1=0.10;                    //First sync limit
  double minsync2=0.12;                    //Second sync limit
  int iifac=3;                             //Step size in final DT peakup
  int symfac=45;                           //Soft-symbol normalizing factor
  int maxdrift=4;                          //Maximum (+/-) drift
  double minrms=52.0 * (symfac/64.0);      //Final test for palusible decoding
  delta=60;                                //Fano threshold step

  t00=clock();
  fftw_complex *fftin, *fftout;
#include "./mettab.c"

// Check for an optional FFTW wisdom file
  FILE *fp_fftw_wisdom_file;
  if ((fp_fftw_wisdom_file = fopen("fftw_wisdom_wsprd", "r"))) {
    fftw_import_wisdom_from_file(fp_fftw_wisdom_file);
    fclose(fp_fftw_wisdom_file);
  }

  idat=malloc(sizeof(double)*nfft2);
  qdat=malloc(sizeof(double)*nfft2);

  while ( (c = getopt(argc, argv, "b:e:f:Hnqt:wv")) !=-1 ) {
    switch (c) {
    case 'b':
      fblank = strtof(optarg,NULL);
      break;
    case 'e':
      dialfreq_error = strtof(optarg,NULL);   // units of Hz
      // dialfreq_error = dial reading - actual, correct frequency
      break;
    case 'f':
      dialfreq_cmdline = strtod(optarg,NULL); // units of MHz
      break;
    case 'H':
      usehashtable = 0;
      break;
    case 'n':
      writenoise = 1;
      break;
    case 'q':
      quickmode = 1;
      break;
    case 't':
      tblank = strtof(optarg,NULL);
      break;
    case 'v':
      verbose = 1;
      break;
    case 'w':
      fmin=-150.0;
      fmax=150.0;
      break;
    case '?':
      usage();
      return 1;
    }
  }

  if( optind+1 > argc) {
    usage();
    return 1;
  } else {
    ptr_to_infile=argv[optind];
  }

  FILE *fall_wspr, *fwsprd, *fhash, *ftimer, *fweb;
  FILE *fdiag;
  fall_wspr=fopen("ALL_WSPR.TXT","a");
  fwsprd=fopen("wsprd.out","w");
  fdiag=fopen("wsprd_diag","a");
  fweb=fopen("wspr-now.txt","a");

  if((ftimer=fopen("wsprd_timer","r"))) {
    //Accumulate timing data
    nr=fscanf(ftimer,"%lf %lf %lf %lf %lf %lf %lf",
	   &treadwav,&tcandidates,&tsync0,&tsync1,&tsync2,&tfano,&ttotal);
    fclose(ftimer);
  }
  ftimer=fopen("wsprd_timer","w");

  if( strstr(ptr_to_infile,".wav") ) {
    ptr_to_infile_suffix=strstr(ptr_to_infile,".wav");

    t0 = clock();
    npoints=readwavfile(ptr_to_infile, idat, qdat);
    treadwav += (double)(clock()-t0)/CLOCKS_PER_SEC;

    if( npoints == 1 ) {
      return 1;
    }
    dialfreq=dialfreq_cmdline - (dialfreq_error*1.0e-06);
  } else if ( strstr(ptr_to_infile,".c2") !=0 )  {
    ptr_to_infile_suffix=strstr(ptr_to_infile,".c2");
    npoints=readc2file(ptr_to_infile, idat, qdat, &dialfreq);
    if( npoints == 1 ) {
      return 1;
    }
    dialfreq -= (dialfreq_error*1.0e-06);
  } else {
    printf("Error: Failed to open %s\n",ptr_to_infile);
    printf("WSPR file must have suffix .wav or .c2\n");
    return 1;
  }

// Parse date and time from given filename
  strncpy(date,ptr_to_infile_suffix-11,6);
  strncpy(uttime,ptr_to_infile_suffix-4,4);
  date[6]='\0';
  uttime[4]='\0';
//added riyas
  sprintf(xdate, "20%.2s-%.2s-%.2s", date, date+2, date+4);
  xdate[10]='\0';
  sprintf(xuttime, "%.2s:%.2s", uttime, uttime+2);
  xuttime[5]='\0';

// Do windowed ffts over 2 symbols, stepped by half symbols
  int nffts=4*floor(npoints/512)-1;
  fftin=(fftw_complex*) fftw_malloc(sizeof(fftw_complex)*512);
  fftout=(fftw_complex*) fftw_malloc(sizeof(fftw_complex)*512);
  PLAN3 = fftw_plan_dft_1d(512, fftin, fftout, FFTW_FORWARD, PATIENCE);
    
  float ps[512][nffts];
  float w[512];
  for(i=0; i<512; i++) {
    w[i]=sin(0.006135923*i);
  }

  memset(ps,0.0, sizeof(float)*512*nffts);
  for (i=0; i<nffts; i++) {
    for(j=0; j<512; j++ ) {
      k=i*128+j;
      fftin[j][0]=idat[k] * w[j];
      fftin[j][1]=qdat[k] * w[j];
    }
    fftw_execute(PLAN3);
    for (j=0; j<512; j++ ) {
      k=j+256;
      if( k>511 )
	k=k-512;
      ps[j][i]=fftout[k][0]*fftout[k][0]+fftout[k][1]*fftout[k][1];
    }
  }

  fftw_free(fftin);
  fftw_free(fftout);

// Compute average spectrum
  float psavg[512];
  memset(psavg,0.0, sizeof(float)*512);
  for (i=0; i<nffts; i++) {
    for (j=0; j<512; j++) {
      psavg[j]=psavg[j]+ps[j][i];
    }
  }

// Smooth with 7-point window and limit spectrum to +/-150 Hz
  int window[7]={1,1,1,1,1,1,1};
  float smspec[411];
  for (i=0; i<411; i++) {
    smspec[i]=0.0;
    for(j=-3; j<=3; j++) {
      k=256-205+i+j;
      smspec[i]=smspec[i]+window[j+3]*psavg[k];
    }
  }

// Sort spectrum values, then pick off noise level as a percentile
  float tmpsort[411];
  for (j=0; j<411; j++) {
    tmpsort[j]=smspec[j];
  }
  qsort(tmpsort, 411, sizeof(float), floatcomp);

// Noise level of spectrum is estimated as 123/411= 30'th percentile
  float noise_level = tmpsort[122];

// Renormalize spectrum so that (large) peaks represent an estimate of snr
  float min_snr_neg33db = pow(10.0,(-33+26.5)/10.0);
  for (j=0; j<411; j++) {
    smspec[j]=smspec[j]/noise_level - 1.0;
    if( smspec[j] < min_snr_neg33db) smspec[j]=0.1;
    continue;
  }

// Find all local maxima in smoothed spectrum.
  for (i=0; i<200; i++) {
    freq0[i]=0.0;
    snr0[i]=0.0;
    drift0[i]=0.0;
    shift0[i]=0;
    sync0[i]=0.0;
  }

  int npk=0;
  for(j=1; j<410; j++) {
    if((smspec[j]>smspec[j-1]) && (smspec[j]>smspec[j+1]) && (npk<200)) {
      freq0[npk]=(j-205)*df;
      snr0[npk]=10*log10(smspec[j])-26.5;
      npk++;
    }
  }

// Compute corrected fmin, fmax, accounting for dial frequency error
  fmin += dialfreq_error;    // dialfreq_error is in units of Hz
  fmax += dialfreq_error;

// Don't waste time on signals outside of the range [fmin,fmax].
  i=0;
  for( j=0; j<npk; j++) {
    if( freq0[j] >= fmin && freq0[j] <= fmax ) {
      freq0[i]=freq0[j];
      snr0[i]=snr0[j];
      i++;
    }
  }
  npk=i;

  t0=clock();
/* Make coarse estimates of shift (DT), freq, and drift

  * Look for time offsets up to +/- 8 symbols (about +/- 5.4 s) relative 
    to nominal start time, which is 2 seconds into the file

  * Calculates shift relative to the beginning of the file

  * Negative shifts mean that signal started before start of file

  * The program prints DT = shift-2 s

  * Shifts that cause sync vector to fall off of either end of the data 
    vector are accommodated by "partial decoding", such that missing 
    symbols produce a soft-decision symbol value of 128 

  * The frequency drift model is linear, deviation of +/- drift/2 over the
    span of 162 symbols, with deviation equal to 0 at the center of the 
    signal vector. 
*/

  int idrift,ifr,if0,ifd,k0;
  int kindex;
  float smax,ss,pow,p0,p1,p2,p3;
  for(j=0; j<npk; j++) {                              //For each candidate...
    smax=-1e30;
    if0=freq0[j]/df+256;
    for (ifr=if0-1; ifr<=if0+1; ifr++) {                      //Freq search
      for( k0=-10; k0<22; k0++) {                             //Time search
	for (idrift=-maxdrift; idrift<=maxdrift; idrift++) {  //Drift search
	  ss=0.0;
	  pow=0.0;
	  for (k=0; k<162; k++) {                             //Sum over symbols
	    ifd=ifr+((float)k-81.0)/81.0*( (float)idrift )/(2.0*df);
	    kindex=k0+2*k;
	    if( kindex < nffts ) {
	      p0=ps[ifd-3][kindex];
	      p1=ps[ifd-1][kindex];
	      p2=ps[ifd+1][kindex];
	      p3=ps[ifd+3][kindex];

	      p0=sqrt(p0);
	      p1=sqrt(p1);
	      p2=sqrt(p2);
	      p3=sqrt(p3);

	      ss=ss+(2*pr3[k]-1)*((p1+p3)-(p0+p2));
	      pow=pow+p0+p1+p2+p3;
	      sync1=ss/pow;
	    }
	  }
	  if( sync1 > smax ) {                  //Save coarse parameters
	    smax=sync1;
	    shift0[j]=128*(k0+1);
	    drift0[j]=idrift;
	    freq0[j]=(ifr-256)*df;
	    sync0[j]=sync1;
	  }
	}
      }
    }
  }
  tcandidates += (double)(clock()-t0)/CLOCKS_PER_SEC;

  nbits=81;
  symbols=malloc(sizeof(char)*nbits*2);
  memset(symbols,0,sizeof(char)*nbits*2);
  decdata=malloc((nbits+7)/8);
  grid=malloc(sizeof(char)*5);
  grid6=malloc(sizeof(char)*7);
  callsign=malloc(sizeof(char)*13);
  call_loc_pow=malloc(sizeof(char)*23);
  cdbm=malloc(sizeof(char)*3);
  float allfreqs[npk];
  memset(allfreqs,0,sizeof(float)*npk);
  char allcalls[npk][13];
  memset(allcalls,0,sizeof(char)*npk*13);
  memset(grid,0,sizeof(char)*5);
  memset(grid6,0,sizeof(char)*7);
  memset(callsign,0,sizeof(char)*13);
  memset(call_loc_pow,0,sizeof(char)*23);
  memset(cdbm,0,sizeof(char)*3);
  char hashtab[32768][13];
  memset(hashtab,0,sizeof(char)*32768*13);
  uint32_t nhash( const void *, size_t, uint32_t);
  int nh;
    
  if( usehashtable ) {
    char line[80], hcall[12];
    if( (fhash=fopen("hashtable.txt","r+")) ) {
      while (fgets(line, sizeof(line), fhash) != NULL) {
	sscanf(line,"%d %s",&nh,hcall);
	strcpy(*hashtab+nh*13,hcall);
      }
    } else {
      fhash=fopen("hashtable.txt","w+");
    }
    fclose(fhash);
  }
    
  int uniques=0, noprint=0;
/*    
 Refine the estimates of freq, shift using sync as a metric.
 Sync is calculated such that it is a float taking values in the range
 [0.0,1.0].
        
 Function sync_and_demodulate has three modes of operation
 mode is the last argument:

      0 = no frequency or drift search. find best time lag.
      1 = no time lag or drift search. find best frequency.
      2 = no frequency or time lag search. Calculate soft-decision 
          symbols using passed frequency and shift.

NB: best possibility for OpenMP may be here: several worker threads
could each work on one candidate at a time.
*/

  for (j=0; j<npk; j++) {
    f1=freq0[j];
    drift1=drift0[j];
    shift1=shift0[j];
    sync1=sync0[j];

// Fine search for best sync lag (mode 0)
    fstep=0.0;
    lagmin=shift1-144;
    lagmax=shift1+144;
    lagstep=8;
    if(quickmode) lagstep=16;
    t0 = clock();
    sync_and_demodulate(idat, qdat, npoints, symbols, &f1, fstep, &shift1, 
		    lagmin, lagmax, lagstep, &drift1, symfac, &sync1, 0);
    tsync0 += (double)(clock()-t0)/CLOCKS_PER_SEC;

// Fine search for frequency peak (mode 1)
    fstep=0.1;
    t0 = clock();
    sync_and_demodulate(idat, qdat, npoints, symbols, &f1, fstep, &shift1, 
		     lagmin, lagmax, lagstep, &drift1, symfac, &sync1, 1);
    tsync1 += (double)(clock()-t0)/CLOCKS_PER_SEC;

    if( sync1 > minsync1 ) {
      worth_a_try = 1;
    } else {
      worth_a_try = 0;
    }

    int idt=0, ii=0, jiggered_shift;
    uint32_t ihash;
    double y,sq,rms;
    not_decoded=1;

    while ( worth_a_try && not_decoded && idt<=(128/iifac)) {
      ii=(idt+1)/2;
      if( idt%2 == 1 ) ii=-ii;
      ii=iifac*ii;
      jiggered_shift=shift1+ii;

// Use mode 2 to get soft-decision symbols
      t0 = clock();
      sync_and_demodulate(idat, qdat, npoints, symbols, &f1, fstep, 
	 &jiggered_shift, lagmin, lagmax, lagstep, &drift1, symfac, 
			  &sync1, 2);
      tsync2 += (double)(clock()-t0)/CLOCKS_PER_SEC;

      sq=0.0;
      for(i=0; i<162; i++) {
	y=(double)symbols[i] - 128.0;
	sq += y*y;
      }
      rms=sqrt(sq/162.0);

      if((sync1 > minsync2) && (rms > minrms)) {
	deinterleave(symbols);
	t0 = clock();
	  not_decoded = fano(&metric,&cycles,&maxnp,decdata,symbols,nbits,
			     mettab,delta,maxcycles);
	tfano += (double)(clock()-t0)/CLOCKS_PER_SEC;

	/* ### Used for timing tests:
	if(not_decoded) fprintf(fdiag,
	    "%6s %4s %4.1f %3.0f %4.1f %10.7f  %-18s %2d %5u %4d %6.1f %2d\n",
	    date,uttime,sync1*10,snr0[j], shift1*dt-2.0, dialfreq+(1500+f1)/1e6,
	    "@                 ", (int)drift1, cycles/81, ii, rms, maxnp);
	*/
      }
      idt++;
      if( quickmode ) break;  
    }

    if( worth_a_try && !not_decoded ) {
      for(i=0; i<11; i++) {
	if( decdata[i]>127 ) {
	  message[i]=decdata[i]-256;
	} else {
	  message[i]=decdata[i];
	}
      }

      unpack50(message,&n1,&n2);
      unpackcall(n1,callsign);
      unpackgrid(n2, grid);
      int ntype = (n2&127) - 64;

/*
 Based on the value of ntype, decide whether this is a Type 1, 2, or 
 3 message.

 * Type 1: 6 digit call, grid, power - ntype is positive and is a member 
         of the set {0,3,7,10,13,17,20...60}

 * Type 2: extended callsign, power - ntype is positive but not
         a member of the set of allowed powers

 * Type 3: hash, 6 digit grid, power - ntype is negative.
*/

      if( (ntype >= 0) && (ntype <= 62) ) {
	int nu=ntype%10;
	if( nu == 0 || nu == 3 || nu == 7 ) {
	  ndbm=ntype;
	  memset(call_loc_pow,0,sizeof(char)*23);
	  sprintf(cdbm,"%2d",ndbm);
	  strncat(call_loc_pow,callsign,strlen(callsign));
	  strncat(call_loc_pow," ",1);
	  strncat(call_loc_pow,grid,4);
	  strncat(call_loc_pow," ",1);
	  strncat(call_loc_pow,cdbm,2);
	  strncat(call_loc_pow,"\0",1);
                    
	  ihash=nhash(callsign,strlen(callsign),(uint32_t)146);
	  strcpy(*hashtab+ihash*13,callsign);

	  noprint=0;
	} else {
	  nadd=nu;
	  if( nu > 3 ) nadd=nu-3;
	  if( nu > 7 ) nadd=nu-7;
	  n3=n2/128+32768*(nadd-1);
	  unpackpfx(n3,callsign);
	  ndbm=ntype-nadd;

	  memset(call_loc_pow,0,sizeof(char)*23);
	  sprintf(cdbm,"%2d",ndbm);
	  strncat(call_loc_pow,callsign,strlen(callsign));
	  strncat(call_loc_pow," ",1);
	  strncat(call_loc_pow,cdbm,2);
	  strncat(call_loc_pow,"\0",1);
                    
	  ihash=nhash(callsign,strlen(callsign),(uint32_t)146);
	  strcpy(*hashtab+ihash*13,callsign);

	  noprint=0;
	}
      } else if ( ntype < 0 ) {
	ndbm=-(ntype+1);
	memset(grid6,0,sizeof(char)*7);
	strncat(grid6,callsign+5,1);
	strncat(grid6,callsign,5);
	ihash=(n2-ntype-64)/128;
	if( strncmp(hashtab[ihash],"\0",1) != 0 ) {
	  sprintf(callsign,"<%s>",hashtab[ihash]);
	} else {
	  sprintf(callsign,"%5s","<...>");
	}

	memset(call_loc_pow,0,sizeof(char)*23);
	sprintf(cdbm,"%2d",ndbm);
	strncat(call_loc_pow,callsign,strlen(callsign));
	strncat(call_loc_pow," ",1);
	strncat(call_loc_pow,grid6,strlen(grid6));
	strncat(call_loc_pow," ",1);
	strncat(call_loc_pow,cdbm,2);
	strncat(call_loc_pow,"\0",1);
                
	noprint=0;
                
// I don't know what to do with these... They show up as "A000AA" grids.
	if( ntype == -64 ) noprint=1;
	
      }
            
// Remove dupes (same callsign and freq within 1 Hz)
      int dupe=0;
      for (i=0; i<npk; i++) {
	if(!strcmp(callsign,allcalls[i]) && 
	   (fabs(f1-allfreqs[i]) <1.0)) dupe=1;
      }
      if( (verbose || !dupe) && !noprint) {
	uniques++;
	strcpy(allcalls[uniques],callsign);
	allfreqs[uniques]=f1;
// Add an extra space at the end of each line so that wspr-x doesn't 
// truncate the power (TNX to DL8FCL!)
    char mygrid[]="NK03";
	char mycall[]="SIARS";
    //printf("%4s=================%d\n",grid,distance(grid, mygrid));
	
    
	printf("%4s %3.0f %4.1f %10.6f %2d  %-s \n",
	       uttime, snr0[j],(shift1*dt-2.0), dialfreq+(1500+f1)/1e6,
	       (int)drift1, call_loc_pow);

	fprintf(fall_wspr,
		"%6s %4s %3.0f %3.0f %4.1f %10.7f  %-22s %2d %5u %4d\n",
		date,uttime,sync1*10,snr0[j],
		shift1*dt-2.0, dialfreq+(1500+f1)/1e6,
		call_loc_pow, (int)drift1, cycles/81, ii);
		
	fprintf(fwsprd,
		"%6s %4s %3.0f %3.0f %4.1f %10.7f  %-22s %2d %5u %4d\n",
		date,uttime,sync1*10,snr0[j],
		shift1*dt-2.0, dialfreq+(1500+f1)/1e6,
		call_loc_pow, (int)drift1, cycles/81, ii);		

	fprintf(fweb,"&nbsp;%10s %5s&nbsp;&nbsp;%s&nbsp;&nbsp;%10.7f&nbsp;&nbsp;%3.0f&nbsp;&nbsp;%2d&nbsp;&nbsp;%4s&nbsp;&nbsp;%2d&nbsp;&nbsp;%2d&nbsp;&nbsp;%5s&nbsp;&nbsp;%4s&nbsp;&nbsp;%d&nbsp;&nbsp;%d&nbsp;\n",
		xdate,xuttime,callsign,dialfreq+(1500+f1)/1e6,snr0[j],(int)drift1,grid,ndbm,ndbm,mycall,mygrid,distance(grid, mygrid),distance(grid, mygrid));

/* For timing tests

	fprintf(fdiag,
	  "%6s %4s %4.1f %3.0f %4.1f %10.7f  %-18s %2d %5u %4d %6.1f\n",
	  date,uttime,sync1*10,snr0[j],
	  shift1*dt-2.0, dialfreq+(1500+f1)/1e6,
	  call_loc_pow, (int)drift1, cycles/81, ii, rms);
*/
      }
    }
  }
  printf("<DecodeFinished>\n");

  if ((fp_fftw_wisdom_file = fopen("fftw_wisdom_wsprd", "w"))) {
    fftw_export_wisdom_to_file(fp_fftw_wisdom_file);
    fclose(fp_fftw_wisdom_file);
  }

  ttotal += (double)(clock()-t00)/CLOCKS_PER_SEC;

  fprintf(ftimer,"%7.2f %7.2f %7.2f %7.2f %7.2f %7.2f %7.2f\n\n",
	  treadwav,tcandidates,tsync0,tsync1,tsync2,tfano,ttotal);

  fprintf(ftimer,"Code segment        Seconds   Frac\n");
  fprintf(ftimer,"-----------------------------------\n");
  fprintf(ftimer,"readwavfile        %7.2f %7.2f\n",treadwav,treadwav/ttotal);
  fprintf(ftimer,"Coarse DT f0 f1    %7.2f %7.2f\n",tcandidates,
	                                            tcandidates/ttotal);
  fprintf(ftimer,"sync_and_demod(0)  %7.2f %7.2f\n",tsync0,tsync0/ttotal);
  fprintf(ftimer,"sync_and_demod(1)  %7.2f %7.2f\n",tsync1,tsync1/ttotal);
  fprintf(ftimer,"sync_and_demod(2)  %7.2f %7.2f\n",tsync2,tsync2/ttotal);
  fprintf(ftimer,"Fano decoder       %7.2f %7.2f\n",tfano,tfano/ttotal);
  fprintf(ftimer,"-----------------------------------\n");
  fprintf(ftimer,"Total              %7.2f %7.2f\n",ttotal,1.0);

  fclose(fall_wspr);
  fclose(fwsprd);
  fclose(fdiag);
  fclose(ftimer);
  fftw_destroy_plan(PLAN1);
  fftw_destroy_plan(PLAN2);
  fftw_destroy_plan(PLAN3);

  if( usehashtable ) {
    fhash=fopen("hashtable.txt","w");
    for (i=0; i<32768; i++) {
      if( strncmp(hashtab[i],"\0",1) != 0 ) {
	fprintf(fhash,"%5d %s\n",i,*hashtab+i*13);
      }
    }
    fclose(fhash);
  }
  if(fblank+tblank+writenoise == 999) return -1;  //Silence compiler warning
  return 0;
}
示例#25
0
/* residual_analysis
 * =================
 * performs the critical-band analysis of the residual file
 * file: name of the sound file containing the residual 
 * sound: sound to store the residual data 
 */
void residual_analysis(ANARGS *anargs, ATS_SOUND *sound)
{
  int fil, file_sampling_rate, sflen, hop, M, N, frames, *band_limits;
  int smp=0, M_2, st_pt, filptr, i, frame_n, k;
  double norm=1.0, threshold, fft_mag, **band_arr=NULL, *band_energy;
  double time_domain_energy=0.0, freq_domain_energy=0.0, sum=0.0;
  double edges[ATSA_CRITICAL_BANDS+1] = ATSA_CRITICAL_BAND_EDGES;
  ATS_FFT fft;
//  mus_sample_t **bufs;
#ifdef FFTW
  fftw_plan plan;
  //  FILE *fftw_wisdom_file;
#endif
//  if ((fil = mus_sound_open_input(file))== -1) {
//    fprintf(stderr, "\n%s: %s\n", file, strerror(errno));
//    exit(1);
//  }
//  file_sampling_rate = mus_sound_srate(file);
//  sflen = mus_sound_frames(file);
  hop = sound->frame_size;
  M = sound->window_size;
  N = residual_get_N(M, ATSA_RES_MIN_FFT_SIZE, ATSA_RES_PAD_FACTOR);
//  bufs = (mus_sample_t **)malloc(2*sizeof(mus_sample_t*));
//  bufs[0] = (mus_sample_t *)malloc(sflen * sizeof(mus_sample_t));
//  bufs[1] = (mus_sample_t *)malloc(sflen * sizeof(mus_sample_t));
  fft.size = N;
  fft.rate = file_sampling_rate;
#ifdef FFTW
  fft.data = fftw_malloc(sizeof(fftw_complex) * fft.size);
  //fftw_wisdom_file = fopen("fftw-wisdom", "r");
  //  fftw_import_wisdom_from_file(fftw_wisdom_file);
  plan = fftw_plan_dft_1d(fft.size, fft.data, fft.data, FFTW_FORWARD, FFTW_PATIENT);
  //  fclose(fftw_wisdom_file);
#else
  fft.fdr = (double *)malloc(N * sizeof(double));
  fft.fdi = (double *)malloc(N * sizeof(double));
#endif
  threshold = AMP_DB(ATSA_NOISE_THRESHOLD);
  frames = sound->frames;
  fft_mag = (double)file_sampling_rate / (double)N;
  band_limits = (int *)malloc(sizeof(int)*(ATSA_CRITICAL_BANDS+1));
  residual_get_bands(fft_mag, edges, band_limits, ATSA_CRITICAL_BANDS+1);
  band_arr = sound->band_energy;
  band_energy = (double *)malloc(ATSA_CRITICAL_BANDS*sizeof(double));

  M_2 = floor( ((double)M - 1) * 0.5 );
  st_pt = N - M_2;
  filptr = M_2 * -1;
  /* read sound into memory */
//  mus_sound_read(fil, 0, sflen-1, 2, bufs);     

  for(frame_n = 0 ; frame_n < frames ; frame_n++){ 
    for(i=0; i<N; i++) {
#ifdef FFTW
      fft.data[i][0] = fft.data[i][1] = 0.0;
#else
      fft.fdr[i] = fft.fdi[i] = 0.0;
#endif
    }
    for(k=0; k<M; k++) {
      if (filptr >= 0 && filptr < sflen)
#ifdef FFTW
//        fft.data[(k+st_pt)%N][0] = MUS_SAMPLE_TO_FLOAT(bufs[0][filptr]);
        fft.data[(k+st_pt)%N][0] = anargs->residual[filptr];
#else
//	fft.fdr[(k+st_pt)%N] = MUS_SAMPLE_TO_FLOAT(bufs[0][filptr]);
	fft.fdr[(k+st_pt)%N] = anargs->residual[filptr];
#endif
      filptr++;
    }
    smp = filptr - M_2 - 1;
    time_domain_energy = residual_compute_time_domain_energy(&fft);
    /* take the fft */
#ifdef FFTW
    fftw_execute(plan);
#else
    fft_slow(fft.fdr, fft.fdi, fft.size, 1);
#endif
    residual_compute_band_energy(&fft, band_limits, ATSA_CRITICAL_BANDS+1, band_energy, norm);
    sum = 0.0;
    for(k = 0;  k < ATSA_CRITICAL_BANDS; k++){
      sum += band_energy[k];
    }
    freq_domain_energy = 2.0 * sum;
    for(k = 0; k < ATSA_CRITICAL_BANDS; k++){
      if( band_energy[k] < threshold) {
	band_arr[k][frame_n] = 0.0;
      } else {
	band_arr[k][frame_n] = band_energy[k];
      }
    }
    filptr = filptr - M + hop;
  }
  /* save data in sound */
  sound->band_energy = band_arr;
#ifdef FFTW
  fftw_destroy_plan(plan);
  fftw_free(fft.data);
#else
  free(fft.fdr);
  free(fft.fdi);
#endif
  free(band_energy);
  free(band_limits);
//  free(bufs[0]);
//  free(bufs[1]);
//  free(bufs);
}
示例#26
0
//***************************************************************************
unsigned long readwavfile(char *ptr_to_infile, double *idat, double *qdat )
{
  unsigned long i, j;
  int nfft1=1474560;;
  int nfft2=nfft1/32;                  //nfft2=46080
  int nh2=nfft2/2;
  double df=12000.0/nfft1;
  int i0=1500.0/df+0.5;
  double *realin;
  fftw_complex *fftin, *fftout;

  FILE *fp;
  unsigned long npoints=114*12000;
  short int *buf2;
  buf2 = malloc(npoints*sizeof(short int));

  fp = fopen(ptr_to_infile,"rb");
  if (fp == NULL) {
    fprintf(stderr, "Cannot open data file '%s'\n", ptr_to_infile);
    return 1;
  }
  nr=fread(buf2,2,22,fp);            //Read and ignore header
  nr=fread(buf2,2,npoints,fp);       //Read raw data
  fclose(fp);

  realin=(double*) fftw_malloc(sizeof(double)*nfft1);
  fftout=(fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nfft1);
  PLAN1 = fftw_plan_dft_r2c_1d(nfft1, realin, fftout, PATIENCE);
  
  for (i=0; i<npoints; i++) {
    realin[i]=buf2[i]/32768.0;
  }

  for (i=npoints; i<nfft1; i++) {
    realin[i]=0.0;
  }

  free(buf2);
  fftw_execute(PLAN1);  
  fftw_free(realin);
 
  fftin=(fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nfft2);

  for (i=0; i<nfft2; i++) { 
   j=i0+i;
    if( i>nh2 ) j=j-nfft2;
    fftin[i][0]=fftout[j][0];
    fftin[i][1]=fftout[j][1];
  }

  fftw_free(fftout);
  fftout=(fftw_complex*) fftw_malloc(sizeof(fftw_complex)*nfft2);
  PLAN2 = fftw_plan_dft_1d(nfft2, fftin, fftout, FFTW_BACKWARD, PATIENCE);
  fftw_execute(PLAN2);
    
  for (i=0; i<nfft2; i++) {
    idat[i]=fftout[i][0]/1000.0;
    qdat[i]=fftout[i][1]/1000.0;
  }

  fftw_free(fftin);
  fftw_free(fftout);
  return nfft2;
}
示例#27
0
int main(int argc, char* argv[]) {
	cvNamedWindow( "capture", 1 );
	IplImage* img_8uc1 = cvLoadImage( "/home/eugene/hand_gest/TestingData/10-2.png", CV_LOAD_IMAGE_GRAYSCALE );
	IplImage* img_edge = cvCreateImage( cvGetSize(img_8uc1), 8, 1 );
	IplImage* img_8uc3 = cvCreateImage( cvGetSize(img_8uc1), 8, 3 );
	cvThreshold( img_8uc1, img_edge, 128, 255, CV_THRESH_BINARY );
	CvMemStorage* storage = cvCreateMemStorage();
	CvSeq* first_contour = NULL;
	int Nc = cvFindContours(
				img_edge,
				storage,
				&first_contour,
				sizeof(CvContour),
				CV_RETR_EXTERNAL // Try all four values and see what happens
				);
	int i;
	int n=0;
	int best=0;
	int current=0;
	int N=8;
	int n2;
	double Scale;
	double Features[N];
	fftw_complex *contour;
	fftw_complex *FD;
	fftw_plan plan_forward;
	printf( "Total Contours Detected: %d\n", Nc );
	//Find max contour
	for( CvSeq* c=first_contour; c!=NULL; c=c->h_next ) {
		if(c->total>current);
			best=n;
		n++;
	}
	fprintf(stderr,"best is %d",best);
	n=0;
	for( CvSeq* c=first_contour; c!=NULL; c=c->h_next ) {
		if(n==best){
			cvCvtColor( img_8uc1, img_8uc3, CV_GRAY2BGR );
			cvDrawContours(
				img_8uc3,
				c,
				CVX_RED,
				CVX_BLUE,
				0, // Try different values of max_level, and see what happens
				2,
				8
				);
			printf("Contour #%d\n", n );
			cvShowImage("capture", img_8uc3 );
			printf("%d elements:\n", c->total );
			contour= fftw_malloc(sizeof(fftw_complex)*c->total);

			for( int i=0; i<c->total; ++i ) {
				CvPoint* p = CV_GET_SEQ_ELEM( CvPoint, c, i );
			//	printf("(%d,%d)\n", p->x, p->y );
				//assemble complex representation here
				contour[i][0]=p->x;
				contour[i][1]=p->y;
			}
			//do fft
			//cvCvtSeqToArray(c
			cvWaitKey(0);
		}
		n++;
	}
	//try downspampling later
	FD=fftw_malloc(sizeof(fftw_complex)*c->total);
	plan_forward=fftw_plan_dft_1d(c->total,contour,FD,FFTW_FORWARD,FFTW_ESTIMATE);
	fftw_execute(plan_forward);

	n2=c->total/2;
	Scale=(double)sqrt(pow(FD[1][0],2)+pow(FD[1][1],2));
	//reduce to 10 coefficients
	//normalize
	if(N+2>=c->total)
	{
		fprintf(stderr,"Contour Is too small");
		exit(1);
	}
	//positive frequency components
	for(i=0;i<N/2;i++)
	{
		//fftshift stuff
		Features[i]=(double)sqrt(pow(FD[i+2][0],2)+pow(FD[i+2][1],2))/Scale;
	}
	for(i=0;i<N/2;i++)
	{
		Features[i+N/2]=(double)sqrt(pow(FD[N-1-i][0],2)+pow(FD[N-1-i][1],2))/Scale;
	}

	printf("Finished all contours.\n");
	cvCvtColor( img_8uc1, img_8uc3, CV_GRAY2BGR );
	cvShowImage( "capture", img_8uc3 );
	cvWaitKey(0);
	cvDestroyWindow( "capture" );
	cvReleaseImage( &img_8uc1 );
	cvReleaseImage( &img_8uc3 );
	cvReleaseImage( &img_edge );
	return 0;
}
示例#28
0
/**
 * Function used only internally, to create an FFTW plan for a specified problem (thereby adding to wisdom)
 */
int plan_problem(char type,            /**< 'r' for real or 'c' for complex transform */
		 char direc,           /**< 'f' for forward or 'b'/'r' for backward/reverse transform */
		 UINT4 transform_size, /**< Size of transform to plan */
		 int measurelvl)       /**< Level of patience in planning (0 least, 3 most) */
{
  fftw_plan genericPlan;
  void *indata, *outdata;
  int fwdflag, planning_flags;

  fwdflag = ( (direc=='f') || (direc=='F') );

  /* We call FFTW routines directly, rather than through LAL, so that if XLAL planning routines
     are changed to always read in wisdom, we can still toggle that behavior through the command line.
     In case we ever allow for aligned memory, we allocate everything with fftw_malloc().
  */

  /* If we ever allow for aligned memory, this will have to toggle depending on input: */
  planning_flags = FFTW_UNALIGNED;

  switch(measurelvl)
    {
    case 0:
      planning_flags |= FFTW_ESTIMATE;
      break;
    default:
    case 3:
      planning_flags |= FFTW_EXHAUSTIVE;
      /* Fall through: */
#if __GNUC__ >= 7
      __attribute__ ((fallthrough));
#endif
    case 2:
      planning_flags |= FFTW_PATIENT;
      /* Fall through */
#if __GNUC__ >= 7
      __attribute__ ((fallthrough));
#endif
    case 1:
      planning_flags |= FFTW_MEASURE;
      break;
    }

  /* Ugly, but makes us 'locale' independent */

  if ( (type=='r') || (type=='R') )
    {

      indata  = (double *) fftw_malloc(transform_size*sizeof(double));
      outdata = (double *) fftw_malloc(transform_size*sizeof(double));

      if ( (!indata) || (!outdata) )
	{
	  if (indata) fftw_free(indata);
	  if (outdata) fftw_free(outdata);
	  return 1;
	}

      genericPlan = fftw_plan_r2r_1d(transform_size,indata,outdata,
				      (fwdflag ? FFTW_R2HC : FFTW_HC2R),
				      planning_flags);
      if (!genericPlan)
	{
	  fftw_free(indata);
	  fftw_free(outdata);
	  return 1;
	}
      else
	{
	  fftw_free(indata);
	  fftw_free(outdata);
	  fftw_destroy_plan(genericPlan);
	  return 0;
	}
    }
  else
    {  /* type == 'c' */

      indata  = (fftw_complex *) fftw_malloc(transform_size*sizeof(fftw_complex));
      outdata = (fftw_complex *) fftw_malloc(transform_size*sizeof(fftw_complex));

      if ( (!indata) || (!outdata) )
	{
	  if (indata) fftw_free(indata);
	  if (outdata) fftw_free(outdata);
	  return 1;
	}

      genericPlan = fftw_plan_dft_1d(transform_size,indata,outdata,
				      (fwdflag ? FFTW_FORWARD : FFTW_BACKWARD),
				      planning_flags);

      if (!genericPlan)
	{
	  fftw_free(indata);
	  fftw_free(outdata);
	  return 1;
	}
      else
	{
	  fftw_free(indata);
	  fftw_free(outdata);
	  fftw_destroy_plan(genericPlan);
	  return 0;
	}
    }
}
示例#29
0
int sdr_demod(struct demapped_transmission_frame_t *tf, struct sdr_state_t *sdr){
  int i,j;

  tf->has_fic = 0;

  /* resetting coarse freqshift */
  sdr->coarse_freq_shift = 0;
  
  /* write input data into fifo */
  for (i=0;i<sdr->input_buffer_len;i++) {
    cbWrite(&(sdr->fifo),&sdr->input_buffer[i]);
  }

  /* Check for data in fifo */
  if (sdr->fifo.count < 196608*3) {
    return 0;
  }
  
  /* read fifo */
  sdr_read_fifo(&(sdr->fifo),196608*2,sdr->coarse_timeshift+sdr->fine_timeshift,sdr->buffer);


  
  /* give the AGC some time to settle */
  if (sdr->startup_delay<=GAIN_SETTLE_TIME) {
    sdr->startup_delay+=1;
    fprintf(stderr,"startup_delay=%i\n",sdr->startup_delay);
    return 0;
  }
  


  /* complex data conversion */
  for (j=0;j<196608*2;j+=2){
    sdr->real[j/2]=sdr->buffer[j]-127;
    sdr->imag[j/2]=sdr->buffer[j+1]-127;
  }

  /* resetting coarse timeshift */
  sdr->coarse_timeshift = 0;

  /* coarse time sync */
  /* performance bottleneck atm */
  sdr->coarse_timeshift = dab_coarse_time_sync(sdr->real,sdr->filt,sdr->force_timesync);
  // we are not in sync so -> next frame
  sdr->force_timesync=0;
  if (sdr->coarse_timeshift) {
    //printf("coarse time shift\n");
    return 0;
  }

  /* create complex frame */
  for (j=0;j<196608;j++){
    sdr->dab_frame[j][0] = sdr->real[j];
    sdr->dab_frame[j][1] = sdr->imag[j];
  }

  /* fine time sync */
  sdr->fine_timeshift = dab_fine_time_sync(sdr->dab_frame);
  if (sdr->coarse_freq_shift) {
    sdr->fine_timeshift = 0;
    }
  /* coarse_frequency shift */
  fftw_plan p;
  p = fftw_plan_dft_1d(2048, &sdr->dab_frame[2656+505+sdr->fine_timeshift], sdr->symbols[0], FFTW_FORWARD, FFTW_ESTIMATE);
  fftw_execute(p);
  fftw_destroy_plan(p);
  
  fftw_complex tmp;
    for (i = 0; i < 2048/2; i++)
    {
      tmp[0]     = sdr->symbols[0][i][0];
      tmp[1]     = sdr->symbols[0][i][1];
      sdr->symbols[0][i][0]    = sdr->symbols[0][i+2048/2][0];
      sdr->symbols[0][i][1]    = sdr->symbols[0][i+2048/2][1];
      sdr->symbols[0][i+2048/2][0] = tmp[0];
      sdr->symbols[0][i+2048/2][1] = tmp[1];
    }
  sdr->coarse_freq_shift = dab_coarse_freq_sync_2(sdr->symbols[0]);
  if (abs(sdr->coarse_freq_shift)>1) {
    sdr->force_timesync = 1;
    return 0;
  }

  /* fine freq correction */
  sdr->fine_freq_shift = dab_fine_freq_corr(sdr->dab_frame,sdr->fine_timeshift);

  /* d-qpsk */
  for (i=0;i<76;i++) {
    p = fftw_plan_dft_1d(2048, &sdr->dab_frame[2656+(2552*i)+504],
			 sdr->symbols[i], FFTW_FORWARD, FFTW_ESTIMATE);
    fftw_execute(p);
    fftw_destroy_plan(p);
    for (j = 0; j < 2048/2; j++)
      {
	tmp[0]     = sdr->symbols[i][j][0];
	tmp[1]     = sdr->symbols[i][j][1];
	sdr->symbols[i][j][0]    = sdr->symbols[i][j+2048/2][0];
	sdr->symbols[i][j][1]    = sdr->symbols[i][j+2048/2][1];
	sdr->symbols[i][j+2048/2][0] = tmp[0];
	sdr->symbols[i][j+2048/2][1] = tmp[1];
      }
    
  }
  //
  for (j=1;j<76;j++) {
    for (i=0;i<2048;i++)
      {
	sdr->symbols_d[j*2048+i][0] =
	  ((sdr->symbols[j][i][0]*sdr->symbols[j-1][i][0])
	   +(sdr->symbols[j][i][1]*sdr->symbols[j-1][i][1]))
	  /(sdr->symbols[j-1][i][0]*sdr->symbols[j-1][i][0]+sdr->symbols[j-1][i][1]*sdr->symbols[j-1][i][1]);
	sdr->symbols_d[j*2048+i][1] = 
	  ((sdr->symbols[j][i][0]*sdr->symbols[j-1][i][1])
	   -(sdr->symbols[j][i][1]*sdr->symbols[j-1][i][0]))
	  /(sdr->symbols[j-1][i][0]*sdr->symbols[j-1][i][0]+sdr->symbols[j-1][i][1]*sdr->symbols[j-1][i][1]);
      }
  }
  
  uint8_t* dst = tf->fic_symbols_demapped[0];
  tf->has_fic = 1;  /* Always true for SDR input */

  int k,kk;
  for (j=1;j<76;j++) {
    if (j == 4) { dst = tf->msc_symbols_demapped[0]; }
    k = 0;
    for (i=0;i<2048;i++){
      if ((i>255) && i!=1024 && i < 1793) {
        /* Frequency deinterleaving and QPSK demapping combined */  
        kk = rev_freq_deint_tab[k++];
        dst[kk] = (sdr->symbols_d[j*2048+i][0]>0)?0:1;
        dst[1536+kk] = (sdr->symbols_d[j*2048+i][1]>0)?1:0;
      }
    }
    dst += 3072;
  }
  
  return 1;
}
void calc_envelope(float ** datatrace, float ** envelope, int ns, int ntr){

	/* declaration of variables */
	int i,j, nfreq, npad, k;
	float xr, yr, x, y, dump, a, *h;
	double npadd;
		
	/* declaration of variables for FFTW3*/
	fftw_complex *in, *out;
	fftw_plan p1, p2;

	/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */
	/* calculation of the Hilbert transform */
	/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */


	/* calculation of the FFT */
	npad = (int)(pow(2.0, ceil(log((double)(ns))/log(2.0))+2.0) );  /* ns -> npad for usage in FFT*/
        npadd = (double)npad;
	in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * npad);
	out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * npad);
	
	/* calculation of the vector h */
	     h = vector(1,npad);
	     if ((2*(npad/2))==npad) {
		/* npad is even */
		h[1]=1.0;
		h[npad/2+1]=1.0;
		for (i=2;i<=(npad/2);i++) {
	   	     h[i] = 2.0; }
	     } else {
		/* npad is odd */
		h[1]=1.0;
		for (i=2;i<=((npad+1)/2);i++) {
	   	     h[i]=2.0;}
	     }
	     
	for(k=1;k<=ntr;k++){
						
	     for (j=0;j<ns;j++){
	     in[j][0]=datatrace[k][j+1];
	     in[j][1]=0.0;
		     }
	     for (j=ns;j<npad;j++){
	     in[j][0]=0.0;
	     in[j][1]=0.0;
		     }
	     /* FFT */
	     p1 = fftw_plan_dft_1d(npad, in, out, 1, FFTW_ESTIMATE);
	     fftw_execute(p1);
	     fftw_destroy_plan(p1);		    

	     /* elementwise multiplication of FFT and h */
	     for (i=0;i<npad;i++){
		out[i][0] *= h[i+1];
		out[i][1] *= h[i+1];
	     }


	     /* inverse FFT */
	     p2 = fftw_plan_dft_1d(npad, out, in, -1, FFTW_ESTIMATE);
	     fftw_execute(p2);
             fftw_destroy_plan(p2);

	     /* %%%%%%%%%%%%%%%%%%%%%%% */
	     /* calculation of envelope */
	     /* %%%%%%%%%%%%%%%%%%%%%%% */
	     for (j=0;j<ns;j++){
	     	envelope[k][j+1]=(1.0/npadd)*sqrt(in[j][0]*in[j][0]+in[j][1]*in[j][1]);
	     }
	     
	}

	fftw_free(in);
	fftw_free(out);
	free_vector(h,1,npad);
	
}