Exemple #1
0
int VU_vfrdB_f(vsip_vview_f *a,vsip_scalar_f range)
{ int ret = 0;
  vsip_length N_len=vsip_vgetlength_f(a);
  vsip_cvview_f *ca=vsip_cvcreate_f(N_len,VSIP_MEM_NONE);
  vsip_fft_f *fft =   vsip_ccfftip_create_f(
       N_len,1,VSIP_FFT_FWD,0,0);
  vsip_vview_f *ra = vsip_vrealview_f(ca),
               *ia = vsip_vimagview_f(ca),
               *ta = vsip_vcloneview_f(a);
  vsip_offset s = (vsip_offset)vsip_vgetstride_f(ta);
  if((ca == NULL) || (fft == NULL) || (ra == NULL) ||
     (ia == NULL) || (ta == NULL)){ret =  1;
  }else{
     vsip_vfill_f(0,ia); vsip_vcopy_f_f(a,ra);
     vsip_ccfftip_f(fft,ca);
     vsip_vcmagsq_f(ca,ra);
     {  vsip_index ind;/* scale by "range" min to max*/
        vsip_scalar_f max = vsip_vmaxval_f(ra,&ind);
        vsip_scalar_f min = max * range;
        vsip_vclip_f(ra,min,max,min,max,ra);
     }
     if(N_len%2){vsip_length Nlen = N_len/2;
         vsip_vputlength_f(ta,Nlen+1);
         vsip_vputlength_f(ra,Nlen+1);
         vsip_vputoffset_f(ta,Nlen * s);
         vsip_vcopy_f_f(ra,ta);
         vsip_vputlength_f(ra,Nlen);
         vsip_vputlength_f(ta,Nlen);
         vsip_vputoffset_f(ta,vsip_vgetoffset_f(a));
         vsip_vputoffset_f(ra,Nlen+1);
         vsip_vcopy_f_f(ra,ta);
     }else{vsip_length Nlen = N_len/2;
         vsip_vcopy_f_f(ra,ta);
         vsip_vputlength_f(ta,Nlen);
         vsip_vputlength_f(a,Nlen);
         vsip_vputoffset_f(ta,(vsip_offset)(Nlen) * s);
         vsip_vswap_f(ta,a);
         vsip_vputlength_f(a,N_len);
      }vsip_vlog10_f(a,a);vsip_svmul_f(10,a,a);
   }vsip_fft_destroy_f(fft);
     vsip_vdestroy_f(ra); vsip_vdestroy_f(ia);
     vsip_cvalldestroy_f(ca);vsip_vdestroy_f(ta);
     return ret;
}
int main(){vsip_init((void*)0);
{
    int i,j, solretval=0;
    vsip_cmview_f *A  = vsip_cmcreate_f(M,N,VSIP_COL,0);
    vsip_cmview_f *X  = vsip_cmcreate_f(M,NB,VSIP_ROW,0);

    /* Nullify the data-space */
    for (i=0; i <  vsip_cmgetcollength_f(A); i++)
      for(j=0; j < vsip_cmgetrowlength_f(A); j++)
        vsip_cmput_f(A,i,j,vsip_cmplx_f(0,0));

    for (i=0; i <  vsip_cmgetcollength_f(X); i++)
      for(j=0; j <  vsip_cmgetrowlength_f(X); j++)
        vsip_cmput_f(X,i,j,vsip_cmplx_f(0,0));

    /* Initialise matrix A */
    for (i=0; i<M; i++)
      for (j = 0; j < N; j++)
	if(i == j) 
          vsip_cmput_f(A,i,j,vsip_cmplx_f(M+1, 0));
        else if(i > j)
          vsip_cmput_f(A,i,j, vsip_cmplx_f(1,1));
        else if(i < j)
          vsip_cmput_f(A,i,j,vsip_cmplx_f(1,-1));


    {int i,j; 
    printf("A matrix\nA = [\n");
    for(i=0; i<M; i++)
      {
	for(j=0; j< N; j++) 
	  printf("%6.2f+%6.2fi%s",
                     vsip_real_f(vsip_cmget_f(A,i,j)),
                     vsip_imag_f(vsip_cmget_f(A,i,j)),(j == N-1) ? "":",");
                   (i == M - 1) ? printf("]\n") : printf(";\n");
      }
    }
    { int j, k; 
    vsip_cvview_f *y = NULL;
    vsip_cvview_f *x;
    vsip_vview_f *yr = NULL, *yi = NULL;

    vsip_length L    = NB;
    vsip_length p    = M;
    for(k=0; k<L; k++)
      {
        x  = vsip_cmcolview_f(X,k);
	for (j=0; j<p; j++)
	  {
	    y  = vsip_cmrowview_f(A,j);
            yr = vsip_vrealview_f(y);
            yi = vsip_vimagview_f(y);
	    vsip_cvput_f(x,j, vsip_cmplx_f((double)(k+1)*(vsip_vsumval_f(yr)),
                             (double) (k+1)*(vsip_vsumval_f(yi))));
	    /* vsip_vput_f(x,j,(vsip_vsumval_f(y)));*/
	   vsip_cvdestroy_f(y);
           vsip_vdestroy_f(yr);
           vsip_vdestroy_f(yi);
	  }
        vsip_cvdestroy_f(x);
      }
    }
    {int i,j; 
    printf("rhs matrix\nB = [\n");
    for(i=0; i<NN; i++)
      {
	for(j=0; j<NB; j++) 
	  printf("%7.2f+%7.2fi%s",
                     vsip_real_f(vsip_cmget_f(X,i,j)),
                     vsip_imag_f(vsip_cmget_f(X,i,j)),(j == NB-1) ? "":",");
                   (i == NN - 1) ? printf("]\n") : printf(";\n");
      }
    }
    {vsip_cqr_f* qrAop = vsip_cqrd_create_f(M,N, QOPT);
    if(qrAop == NULL) exit(1);

    {int i,j;
    printf("matrix A after factorisation\n R/Q -- \n");
    if(QOPT == VSIP_QRD_SAVEQ1)
    {
      printf("qrd } returns %i\n",vsip_cqrd_f(qrAop,A));      
      printf("matrix A after factorisation: skinny Q explicitly\n Q1 = [\n");
      for(i= 0; i< M ; i++)
        {
          for(j=0; j< N; j++)
            printf("%8.4f+%8.4fi%s",
                     vsip_real_f(vsip_cmget_f(A,i,j)),
                     vsip_imag_f(vsip_cmget_f(A,i,j)),(j == N-1) ? "":",");
                   (i == M - 1) ? printf("]\n") : printf(";\n");
        }

    } else if(QOPT == VSIP_QRD_SAVEQ || QOPT == VSIP_QRD_NOSAVEQ)
    {
      printf("qrd returns %i\n",vsip_cqrd_f(qrAop,A));
      printf("matrix A after fact.: R and ");
	(QOPT == VSIP_QRD_SAVEQ) ?  printf("full Q implicitly\n Q/R = [\n") :
			printf("Q not saved -- ignore LT portion. \n R = [\n");
      for(i= 0; i<M ; i++)
	{
	  for(j=0; j< N; j++)
	    printf("%9.5f+%9.5fi%s",
                     vsip_real_f(vsip_cmget_f(A,i,j)),
                     vsip_imag_f(vsip_cmget_f(A,i,j)),(j == N-1) ? "":",");
                   (i == M - 1) ? printf("]\n") : printf(";\n"); 
	}
    }
    }
    if( QPROB == VSIP_LLS)
    {
       if (QOPT == VSIP_QRD_SAVEQ1 || QOPT == VSIP_QRD_SAVEQ)
       {
	  if((solretval=vsip_cqrsol_f(qrAop, QPROB, X)))
	  {
	    printf("WARNING -- Least Squares soln returns %i-- CHECK\n", 
		   solretval);	    
	    printf("Upper triang. mat. R, possibly singular\n");
	  }
	  else
	    printf("Least Squares soln returns %i\n", solretval);
       }
       else
	 {
	 printf("Least Squares systems cannot be solved by the NOSAVEQ option -- exiting\n");
	 exit(1);
	 }
       }
    else
      {
      if((solretval=vsip_cqrsol_f(qrAop,QPROB, X)))
      {
	printf("Covariance soln returns %i\n",solretval);
	printf("Upper triang. mat. R, possibly singular\n");
      }
      else
      printf("Covariance soln returns %i\n",solretval);
    }
    vsip_cqrd_destroy_f(qrAop);
    }

    {int i,j;
    printf("Soln Matrix\nX = [\n");
      for(i=0; i<N; i++)
	{
	  for(j=0; j<NB; j++) 
	    printf("%9.5f+%9.5fi%s",
                     vsip_real_f(vsip_cmget_f(X,i,j)),
                     vsip_imag_f(vsip_cmget_f(X,i,j)),(j == NB-1) ? "":",");
                   (i == N - 1) ? printf("]\n") : printf(";\n");
	}
    }

    vsip_cmalldestroy_f(X);
    vsip_cmalldestroy_f(A);
    } vsip_finalize((void*)0); return 1;
}
int main(){vsip_init((void*)0);
{  int i,j; /* counters */
   vsip_vview_f *windowt = vsip_vcreate_hanning_f(Ns,0);
   vsip_vview_f *windowp = vsip_vcreate_hanning_f(Mp,0);
   vsip_vview_f *kernel = 
         vsip_vcreate_kaiser_f(Nfilter,kaiser,0);
   vsip_fir_f *fir = vsip_fir_create_f(kernel,
                         VSIP_NONSYM,2 * Nn,2,VSIP_STATE_SAVE,0,0);
   vsip_vview_f *t =vsip_vcreate_f(Ns,0); /*time vector*/
   vsip_vview_f *noise[Nnoise];
   vsip_vview_f *nv = vsip_vcreate_f(2 * Nn,0);
   vsip_vview_f *tt = vsip_vcreate_f(Ns,0); 
   vsip_mview_f *data = vsip_mcreate_f(Mp,Ns,VSIP_ROW,0),
                *rmview;
   vsip_vview_f *data_v, *gram_v;
   vsip_cvview_f *gram_data_v;
   vsip_cmview_f *gram_data = 
        vsip_cmcreate_f(Mp,Ns/2 + 1,VSIP_COL,0);
   vsip_mview_f  *gram = 
         vsip_mcreate_f(Mp,Ns/2 + 1,VSIP_ROW,0);
   vsip_mview_f  *Xim = 
         vsip_mcreate_f(Mp,Mp+1,VSIP_ROW,0);
   vsip_scalar_f alpha = (D * Fs) / c;
vsip_vview_f *m = vsip_vcreate_f(Mp,0);
   vsip_vview_f *Xi = vsip_vcreate_f(Mp + 1,0);
   vsip_randstate *state =
          vsip_randcreate(15,1,1,VSIP_PRNG);
   vsip_scalar_f w0 = 2 * M_PI * F0/Fs;
   vsip_scalar_f w1 = 2 * M_PI * F1/Fs;
   vsip_scalar_f w2 = 2 * M_PI * F2/Fs;
   vsip_scalar_f w3 = 2 * M_PI * F3/Fs;
   vsip_scalar_f cnst1 = M_PI/Nnoise;  
   vsip_offset offset0 = (vsip_offset)(alpha * Mp + 1);
   vsip_fftm_f *rcfftmop_obj = /* time fft */
   vsip_rcfftmop_create_f(Mp,Ns,1,VSIP_ROW,0,0);
   vsip_fftm_f *ccfftmip_obj =
         vsip_ccfftmip_create_f(Mp,Ns/2 +
             1,VSIP_FFT_FWD,1,VSIP_COL,0,0);
   vsip_vramp_f(0,1,m);
   vsip_vramp_f(0,M_PI/Mp,Xi); 
   vsip_vcos_f(Xi,Xi);
   vsip_vouter_f(alpha,m,Xi,Xim);
   { vsip_vview_f *gram_v = vsip_mrowview_f(gram,0);
     vsip_vputlength_f(gram_v,Mp*(Ns/2 + 1));
     vsip_vfill_f(0,gram_v);
     vsip_vdestroy_f(gram_v);
   }
   for(j=0; j<Nnoise; j++){
      noise[j] = vsip_vcreate_f(Nn,0);
      vsip_vrandn_f(state,nv);
      vsip_firflt_f(fir,nv,noise[j]);
      vsip_svmul_f(12.0/(Nnoise),noise[j],noise[j]);
      vsip_vputlength_f(noise[j],Ns);
   }
   vsip_vramp_f(0,1.0,t); /* time vector */
for(i=0; i<Mp; i++){
      vsip_scalar_f Xim_val = vsip_mget_f(Xim,i,Theta_o);
      data_v = vsip_mrowview_f(data,i);
      vsip_vsmsa_f(t,w0,-w0 * Xim_val,tt);
      vsip_vcos_f(tt,data_v); /*F0 time series */
      vsip_vsmsa_f(t,w1,-w1 * Xim_val,tt);
      vsip_vcos_f(tt,tt); /*F1 time series */
      vsip_vadd_f(tt,data_v,data_v);
      vsip_vsmsa_f(t,w2,-w2 * Xim_val,tt);
      vsip_vcos_f(tt,tt); /*F2 time series */
      vsip_vadd_f(tt,data_v,data_v);
      vsip_vsmsa_f(t,w3,-w3 * Xim_val,tt);
      vsip_vcos_f(tt,tt); /*F3 time series */
      vsip_svmul_f(3.0,tt,tt); /* scale by 3.0 */
      vsip_vadd_f(tt,data_v,data_v);
      vsip_svmul_f(3,data_v,data_v);
      for(j=0; j<Nnoise; j++){ 
          /* simple time delay beam forming for noise */
         vsip_vputoffset_f(noise[j],offset0 + 
           (int)( i * alpha * cos(j * cnst1)));
         vsip_vadd_f(noise[j],data_v,data_v); 
      } 
      /* need to destroy before going on to next phone */
      vsip_vdestroy_f(data_v);
   }
   /* window the data and the array to reduce sidelobes */
   vsip_vmmul_f(windowt,data,VSIP_ROW,data);
   vsip_vmmul_f(windowp,data,VSIP_COL,data);

   /* do ffts */
   vsip_rcfftmop_f(rcfftmop_obj,data,gram_data);
   vsip_ccfftmip_f(ccfftmip_obj,gram_data);

   { /* scale gram to db, min 0 max 255 */
      vsip_index ind;
      gram_v = vsip_mrowview_f(gram,0);
      gram_data_v = vsip_cmcolview_f(gram_data,0);
      rmview = vsip_mrealview_f(gram_data);
      vsip_vputlength_f(gram_v,Mp*(Ns/2 + 1)); 
      vsip_cvputlength_f(gram_data_v,Mp*(Ns/2 + 1));
data_v = vsip_vrealview_f(gram_data_v);
      vsip_vcmagsq_f(gram_data_v,data_v);
      vsip_mcopy_f_f(rmview,gram);
      vsip_vdestroy_f(data_v);
      vsip_svadd_f(1.0 -
      vsip_vminval_f(gram_v,&ind),gram_v,gram_v);
      vsip_vlog10_f(gram_v,gram_v); 
      vsip_svmul_f(256.0 / vsip_vmaxval_f(gram_v,&ind),
                    gram_v,gram_v);/* scale */
      /* reorganize the data to place zero in the 
          center for direction space */
      data_v = vsip_vcloneview_f(gram_v);
      vsip_vputlength_f(data_v,(Mp/2) * (Ns/2 + 1));
      vsip_vputoffset_f(data_v,(Mp/2) * (Ns/2 + 1));
      vsip_vputlength_f(gram_v,(Mp/2) * (Ns/2 + 1));
      vsip_vswap_f(data_v,gram_v);
      vsip_vdestroy_f(gram_v);
      vsip_vdestroy_f(data_v);
      vsip_cvdestroy_f(gram_data_v);
      vsip_mdestroy_f(rmview);
   }
   VU_mprintgram_f(gram,"gram_output");
   } vsip_finalize((void*)0); return 0;        
}
int main(){
   int init = vsip_init((void*)0);
   int i,j, cholsol_retval,chold_retval;
   double t0 = VU_ansi_c_clock(); /* for doing some timeing */
   vsip_cscalar_f czero = vsip_cmplx_f((vsip_scalar_f)0.0,(vsip_scalar_f)0.0);
   vsip_cmview_f *A  = vsip_cmcreate_f(N,N,VSIP_COL,0);
   vsip_cmview_f *RU  = vsip_cmcreate_f(N,N,VSIP_COL,0);
   vsip_cmview_f *RL  = vsip_cmcreate_f(N,N,VSIP_COL,0);
   vsip_cmview_f *XB  = vsip_cmcreate_f(N,M,VSIP_ROW,0);
   vsip_cchol_f* chol = vsip_cchold_create_f(UPORLO,N);   /* NOTE: UPORLO macro above main() */

   /* to make sure we have a valid Positive Symetric define */
   /* an upper triangular (RU) with positive pivots and     */
   /* zero below the main diagonal.                         */
   /* Then initialize RL with hermitian of RU               */
   /* finally create A as the matrix product of RL and RU   */

   /* Initialise matrix RU  */
   /* time this             */
   t0 = VU_ansi_c_clock();
   for (i=0; i<N; i++){
      for(j = i; j < N; j++){
         #ifdef OBNOXIOUS
         /* make up some reasonably obnoxious data                */
         vsip_scalar_f a = cos(1.5/((j+1)*(i+1)))+sqrt(i*j);
         vsip_scalar_f b = (i + j + 1) * cos(M_PI * a);
         #else
         /* the above was to obnoxious for bigger than about N = 10 */
         /* the following works for N > 100 */
         vsip_scalar_f a = 1; vsip_scalar_f b = 1; 
         #endif
         if(i == j) /* fill diagonal */
             vsip_cmput_f(RU,i,j, vsip_cmplx_f(sqrt(N) + sqrt(i),0));
         else { /* fill off diagonal */
                vsip_cmput_f(RU,i,j,vsip_cmplx_f(b,a)); 
                vsip_cmput_f(RU,j,i,czero); 
         }
      }
   }
   /* initialize RL */
   vsip_cmherm_f(RU,RL);
   #ifdef PRINT
      VU_cmprintm_f("7.4",RU);
      VU_cmprintm_f("7.4",RL);
   #endif
   printf("Matrix initialize for RU and RL = %f seconds\n",VU_ansi_c_clock() - t0);

   /* initialize A */
   /* this step will take a long time so time it */
   t0 = VU_ansi_c_clock();
   vsip_cmprod_f(RL,RU,A);
   #ifdef OBNOXIOUS
      for(i=0; i<N; i++){
         vsip_cvview_f *aview = vsip_cmrowview_f(A,i);
         vsip_cvrsdiv_f(aview,vsip_cmag_f(vsip_cvmeanval_f(aview)),aview);
         vsip_cvdestroy_f(aview);
      }
   #endif
   printf("Matrix multiply for initialization of A = %f seconds\n",VU_ansi_c_clock() - t0);

   /* print  A                                                      */
   /* we only want to do this if A is something reasonable to print */
   /* selected as an option in the make file                        */
   #ifdef PRINT
      printf("Matrix A =\n");
      VU_cmprintm_f("4.2",A);
      fflush(stdout);
   #endif

   /* initialise rhs                      */
   /* start out with XB = {1,2,3,...,M}   */
   /* calculate what B must be using A    */
   /* then solve to see if we get XB back */
   {  vsip_index i;
      vsip_vview_f *y = vsip_vcreate_f(vsip_cmgetcollength_f(A),VSIP_MEM_NONE);
      vsip_vview_f *x_r,*x_i;
      vsip_cvview_f *x;
      vsip_mview_f *A_r = vsip_mrealview_f(A),
                   *A_i = vsip_mimagview_f(A);
      /* time this */
      t0 = VU_ansi_c_clock();
      for(i=0; i<M; i++){
         vsip_vfill_f((vsip_scalar_f)i+1.0,y);
         x = vsip_cmcolview_f(XB,i);
         x_r = vsip_vrealview_f(x);
         x_i = vsip_vimagview_f(x);
         vsip_mvprod_f(A_r,y,x_r);
         vsip_mvprod_f(A_i,y,x_i);
         vsip_cvdestroy_f(x);
         vsip_vdestroy_f(x_r);
         vsip_vdestroy_f(x_i);
      }
      vsip_mdestroy_f(A_r);
      vsip_mdestroy_f(A_i);
      printf("Matrix init for B = %f seconds\n",VU_ansi_c_clock() - t0);
   }

   /* print  XB                                                      */
   /* we only want to do this if XB is something reasonable to print */
   /* selected as an option in the make file                         */
   #ifdef PRINT
      printf("Matrix B = \n");
      VU_cmprintm_f("7.4",XB);
      fflush(stdout);
   #endif

   if(chol != NULL){
      t0 = VU_ansi_c_clock(); /* we want to time the decomposition */
      chold_retval = vsip_cchold_f(chol,A);
      printf("time decomp %f\n",VU_ansi_c_clock() - t0);
      printf("decompostion returns %d\n",chold_retval);
   
      /* now do the solution */
      t0 = VU_ansi_c_clock(); /* we want to time the solution */
      cholsol_retval=vsip_ccholsol_f(chol,XB);
      printf("time solution %f\n",VU_ansi_c_clock() - t0);
      printf("cholsol returns %d\n",cholsol_retval);

      /* print  XB                                                      */
      /* we only want to do this if XB is something reasonable to print */
      /* selected as an option in the make file; otherwise              */
      /* we print a single row of XB if the matrix is to large since    */
      /* M is usally reasonable. Printed as a column vector             */
      #ifdef PRINT
         printf("Matrix X = \n");
         VU_cmprintm_f("7.4",XB);
         fflush(stdout);
      #else
         {  /* pick a row in the middle */
            vsip_cvview_f *x = vsip_cmrowview_f(XB,N/2);
            printf("This output sould be 1,2,...,M\n");
            VU_cvprintm_f("7.4",x);
            fflush(stdout);
            vsip_cvdestroy_f(x);
         }
      #endif
   } else {
         printf("failed to create cholesky object \n");
   }
   vsip_cmalldestroy_f(XB);
   vsip_cmalldestroy_f(A);
   vsip_cmalldestroy_f(RL);
   vsip_cmalldestroy_f(RU);
   vsip_cchold_destroy_f(chol);
   vsip_finalize((void*)0);
   return 1;
}