示例#1
0
void des(unsigned char *data_p,char type1,unsigned char* key_p,char type2,int type)
{

	unsigned char tempbuf[12];
	unsigned char key[7];
	unsigned char i;
	unsigned char j;
	unsigned char count;
	void (*f)(unsigned char* data_p,char type);

	selectbits(data_p,type1,(unsigned  char *)DesIp,PGROM,tempbuf,SRAM,64);/*????*/

	movram(tempbuf,SRAM,data_p,type1,8);
	selectbits((unsigned  char *)key_p,type2,(unsigned  char *)DesPc_1,PGROM,(unsigned  char *)key,SRAM,56);/*KEY?????*/
	
	for(i = 0;i < 16;i ++)
	{
		selectbits((unsigned  char *)data_p + 4,type1,(unsigned  char *)DesE,PGROM,tempbuf,SRAM,48);/*????*/
		
		if(type ==1)		//jia mi
		{
			f = shlc;
			count = i;
		}
		else
		{
			count = 16 - i;	
			f = shrc;
		}
		
		for(j = 0;j < pgm_read_byte(&DesRots[count]);j++)/*KEY ???*/
		{
			f(key,SRAM);
		}
		
		selectbits(key,SRAM,(unsigned char *)DesPc_2,PGROM,tempbuf + 6,SRAM,48);/*KEY ????*/
		doxor(tempbuf,SRAM,tempbuf + 6,SRAM,6);
		strans(tempbuf,SRAM,tempbuf + 6,SRAM);
		selectbits(tempbuf + 6,SRAM,(unsigned char *)DesP,PGROM,tempbuf,SRAM,32);
		doxor(tempbuf,SRAM,data_p,type1,4);
		
		if(i < 15)
		{
			movram(data_p + 4,type1,data_p,type1,4);
			movram(tempbuf,SRAM,data_p + 4,type1,4);
		}
	}
	
	movram(tempbuf,SRAM,data_p,type1,4);
	selectbits(data_p,type1,(unsigned char *)DesIp_1,PGROM,tempbuf,SRAM,64);
	movram(tempbuf,SRAM,data_p,type1,8);

}
示例#2
0
// Reset the Kalman Filter for a CARMA(p,q) process
void KalmanFilterp::Reset() {
    
    // Initialize the matrix of Eigenvectors. We will work with the state vector
	// in the space spanned by the Eigenvectors because in this space the state
	// transition matrix is diagonal, so the calculation of the matrix exponential
	// is fast.
    arma::cx_mat EigenMat(p_,p_);
	EigenMat.row(0) = arma::ones<arma::cx_rowvec>(p_);
	EigenMat.row(1) = omega_.st();
	for (int i=2; i<p_; i++) {
		EigenMat.row(i) = strans(arma::pow(omega_, i));
	}
    
	// Input vector under original state space representation
	arma::cx_vec Rvector = arma::zeros<arma::cx_vec>(p_);
	Rvector(p_-1) = 1.0;
    
	// Transform the input vector to the rotated state space representation.
	// The notation R and J comes from Belcher et al. (1994).
	arma::cx_vec Jvector(p_);
	Jvector = arma::solve(EigenMat, Rvector);
	
	// Transform the moving average coefficients to the space spanned by EigenMat.

    rotated_ma_coefs_ = ma_coefs_ * EigenMat;
	
	// Calculate the stationary covariance matrix of the state vector.
	for (int i=0; i<p_; i++) {
		for (int j=i; j<p_; j++) {
			// Only fill in upper triangle of StateVar because of symmetry
			StateVar_(i,j) = -sigsqr_ * Jvector(i) * std::conj(Jvector(j)) /
            (omega_(i) + std::conj(omega_(j)));
		}
	}
	StateVar_ = arma::symmatu(StateVar_); // StateVar is symmetric
	PredictionVar_ = StateVar_; // One-step state prediction error
	
	state_vector_.zeros(); // Initial state is set to zero
	
	// Initialize the Kalman mean and variance. These are the forecasted value
	// for the measured time series values and its variance, conditional on the
	// previous measurements
	mean(0) = 0.0;
    var(0) = std::real( arma::as_scalar(rotated_ma_coefs_ * StateVar_ * rotated_ma_coefs_.t()) );
    var(0) += yerr_(0) * yerr_(0); // Add in measurement error contribution

	innovation_ = y_(0); // The innovation
    current_index_ = 1;
}
示例#3
0
inline
typename
enable_if2
  <
  (is_arma_type<T1>::value && is_complex_strict<typename T1::elem_type>::value),
  Mat< std::complex<typename T1::pod_type> >
  >::result
ifft2(const T1& A)
  {
  arma_extra_debug_sigprint();
  
  // not exactly efficient, but "better-than-nothing" implementation
  
  typedef typename T1::pod_type T;
  
  Mat< std::complex<T> > B = ifft(A);
  
  // for square matrices, strans() will work out that an inplace transpose can be done,
  // hence we can potentially avoid creating a temporary matrix
  
  B = strans(B);
  
  return strans( ifft(B) );
  }
示例#4
0
/**
  * Participation coefficient is a measure of diversity of intermodular
  * connections of individual nodes.
  *  Inputs:     W,      binary/weighted, directed/undirected
  *                      connection matrix
  *              Ci,     community affiliation vector
  *  Output:     P,      participation coefficient

  * Note: The output for directed graphs is the "out-neighbor"
  *   participation coefficient.
  * Reference: Guimera R, Amaral L. Nature (2005) 433:895-900.
  */
rowvec Connectome::participationCoefficient(const mat &W, const urowvec &Ci)
{
    uint n = W.n_rows;
    mat binaryW = zeros(n,n);
    binaryW(find(W!=0)).fill(1);
    rowvec Ko = sum(binaryW,0);
    // A^T = (B C)^T = C^T B^T = C B^T since C = C^T
    mat Gc = diagmat(conv_to<rowvec>::from(Ci))*strans(abs(sign(W)));
    rowvec Kc2 = zeros(1,n);

    for (uint i=0;i<=Ci.max();++i) {
        mat c = zeros(n,n);
        c(find(Gc==i)).fill(1);
        Kc2 += arma::pow(sum((W%c),0),2);
    }

    rowvec P = ones(1,n) - Kc2/arma::pow(Ko,2);
    P(find(Ko == 0)).fill(0);
    return P;
}
示例#5
0
int main(int argc, char *argv[]){
    enum{
	P_EXE,
	P_FRAC,
	P_NSTEP,
	P_TOT,
    };
    if(argc!=P_TOT){
	info2("Usage: \n\tenv MVM_CLIENT=hostname MVM_PORT=port MVM_SASTEP=sastep ./mvm_cpu fraction nstep\n");
	_Exit(0);
    }
    int fraction=strtol(argv[P_FRAC], NULL, 10);
    int nstep=strtol(argv[P_NSTEP], NULL, 10);
    int nstep0=nstep>1?20:0;//warm up
    dmat *d_saind=dread("NFIRAOS_saind");
    const int nsa=(d_saind->nx-1)/fraction;
    int *saind=mymalloc((1+nsa),int);
    for(int i=0; i<nsa+1; i++){
	saind[i]=(int)d_saind->p[i];
    }
    dfree(d_saind);
    const int totpix=saind[nsa];
    const int nact=6981;//active subapertures.
    int ng=nsa*2;
    float FSMdelta=-0.2;
    smat *dm=snew(nact,1);
    smat *mvm=snew(nact, ng);
    smat *mtch=snew(totpix*2,1);
    smat *grad=snew(ng,1);
    smat *im0=snew(totpix,3);
    short *pix=mymalloc(totpix,short);
    short *pixbias=mymalloc(totpix,short);
    {
	rand_t rseed;
	seed_rand(&rseed, 1);
	srandu(mvm, 1e-7, &rseed);
	srandu(mtch, 1, &rseed);
	for(int i=0; i<totpix; i++){
	    pix[i]=(short)(randu(&rseed)*25565);
	    pixbias[i]=(short)(randu(&rseed)*1000);
	}
    }
    smat *mvmt=strans(mvm);
    int sastep=200;//how many subapertures each time
    int nrep=1;
    if(getenv("MVM_NREP")){
	nrep=strtol(getenv("MVM_NREP"), NULL, 10);
    }
    if(getenv("MVM_SECT")){
	sastep=nsa/strtol(getenv("MVM_SECT"), NULL, 10);
    }
    if(getenv("MVM_TRANS")){
	use_trans=strtol(getenv("MVM_TRANS"), NULL, 10);
    }
    if(getenv("MVM_SASTEP")){
	sastep=strtol(getenv("MVM_SASTEP"), NULL, 10);
    }
    info2("use_trans=%d, nrep=%d, sastep=%d\n", use_trans, nrep, sastep);
    int sock=-1;
    char* MVM_CLIENT=getenv("MVM_CLIENT");
    if(MVM_CLIENT){
	short port=(short)strtol(getenv("MVM_PORT"), NULL, 10);
	sock=connect_port(MVM_CLIENT, port, 0 ,1);
	if(sock!=-1) {
	    info2("Connected\n");
	    int cmd[7];
	    cmd[0]=nact;
	    cmd[1]=nsa;
	    cmd[2]=sastep;
	    cmd[3]=totpix;
	    cmd[4]=nstep;
	    cmd[5]=nstep0;
	    cmd[6]=2;
	    if(stwriteintarr(sock, cmd, 7) 
	       || stwriteintarr(sock, saind, nsa+1)
	       || stwrite(sock, pix, sizeof(short)*totpix)){
		close(sock); sock=-1;
		warning("Failed: %s\n", strerror(errno));
	    }
	}
    }
    int ready=0;
    if(sock!=-1 && stwriteint(sock, ready)){
	warning("error send ready signal: %s\n", strerror(errno));
	close(sock); sock=-1;
    }
    smat *timing=snew(nstep, 1);
    TIC;
    float timtot=0, timmax=0, timmin=INFINITY;
    set_realtime(-1, -20);
    for(int jstep=-nstep0; jstep<nstep; jstep++){
	int istep=jstep<0?0:jstep;
	tic;
	double theta=M_PI*0.5*istep+FSMdelta;
	float cd=cos(theta);
	float sd=cos(theta);
	szero(dm);
	for(int isa=0; isa<nsa; isa+=sastep){
	    int npixleft;
	    int nsaleft;
	    if(nsa<isa+sastep){//terminate
		npixleft=totpix-saind[isa];
		nsaleft=nsa-isa;
	    }else{
		npixleft=saind[isa+sastep]-saind[isa];
		nsaleft=sastep;
	    }

	    short *pcur=pix+saind[isa];
	    if(sock!=-1){
		if(stread(sock, pcur, sizeof(short)*npixleft)){
		    warning("failed: %s\n", strerror(errno));
		    close(sock); sock=-1;
		    _Exit(1);
		}
		if(isa==0) tic;
	    }
	    //Matched filter
	    mtch_do(mtch->p, pix, pixbias, 
		    grad->p+isa*2, im0->p, im0->p+totpix, im0->p+totpix*2,
		    saind+isa, nsaleft, cd, sd);
	    //MVM
	    for(int irep=0; irep<nrep; irep++){
		if(use_trans){
		    mvmt_do(mvmt->p+isa*2, grad->p+isa*2,dm->p, nact, nsaleft*2, ng);
		}else{
		    mvm_do(mvm->p+isa*2*nact, grad->p+isa*2, dm->p, nact, nsaleft*2);
		}
	    }
	}//for isa
	if(sock!=-1){
	    if(stwrite(sock, dm->p, sizeof(float)*nact)){
		warning("error write dmres: %s\n", strerror(errno));
		close(sock); sock=-1;
		_Exit(1);
	    }
	    if(streadint(sock, &ready)){//acknowledgement.
		warning("error read ack failed: %s\n", strerror(errno));
		close(sock), sock=-1;
		_Exit(1);
	    }
	    timing->p[istep]=ready*1.e-6;
	}else{
	    timing->p[istep]=toc3;//do not tic.
	}
	if(jstep==istep){
	    timtot+=timing->p[istep];
	    if(timmax<timing->p[istep]){
		timmax=timing->p[istep];
	    }
	    if(timmin>timing->p[istep]){
		timmin=timing->p[istep];
	    }
	}
    }//for istep
    float timmean=timtot/nstep;
    info2("Timing is mean %.3f, max %.3f min %.3f. BW is %.1f of 51.2GB/s\n",
	  timmean*1e3, timmax*1e3, timmin*1e3, nrep*(nact*ng+nact+ng)*sizeof(float)/timmean/(1024*1024*1024));
    writebin(timing, "cpu_timing_%s", HOST);
    if(nstep==1){
	writearr("cpu_pix", 1, sizeof(short), M_INT16, NULL, pix, totpix, 1);
	writearr("cpu_pixbias", 1, sizeof(short), M_INT16, NULL, pixbias, totpix, 1);
	writebin(dm, "cpu_dm");
	writebin(grad, "cpu_grad");
	writebin(mvm, "cpu_mvm");
	writebin(mtch, "cpu_mtch");
    }
   
}
inline
void
running_stat_vec_aux::update_stats(running_stat_vec< std::complex<T> >& x, const Mat< std::complex<T> >& sample)
  {
  arma_extra_debug_sigprint();
  
  typedef typename std::complex<T> eT;
  
  const T N = x.counter.value();
  
  if(N > T(0))
    {
    arma_debug_assert_same_size(x.r_mean, sample, "running_stat_vec(): dimensionality mismatch");
    
    const uword n_elem           = sample.n_elem;
    const eT* sample_mem       = sample.memptr();
          eT* r_mean_mem       = x.r_mean.memptr();
           T* r_var_mem        = x.r_var.memptr();
          eT* min_val_mem      = x.min_val.memptr();
          eT* max_val_mem      = x.max_val.memptr();
           T* min_val_norm_mem = x.min_val_norm.memptr();
           T* max_val_norm_mem = x.max_val_norm.memptr();
    
    const T  N_plus_1   = x.counter.value_plus_1();
    const T  N_minus_1  = x.counter.value_minus_1();
    
    if(x.calc_cov == true)
      {
      Mat<eT>& tmp1 = x.tmp1;
      Mat<eT>& tmp2 = x.tmp2;
      
      tmp1 = sample - x.r_mean;
      
      if(sample.n_cols == 1)
        {
        tmp2 = arma::conj(tmp1)*strans(tmp1);
        }
      else
        {
        tmp2 = trans(tmp1)*tmp1;  //tmp2 = strans(conj(tmp1))*tmp1;
        }
      
      x.r_cov *= (N_minus_1/N);
      x.r_cov += tmp2 / N_plus_1;
      }
    
    
    for(uword i=0; i<n_elem; ++i)
      {
      const eT& val      = sample_mem[i];
      const  T  val_norm = std::norm(val);
      
      if(val_norm < min_val_norm_mem[i])
        {
        min_val_norm_mem[i] = val_norm;
        min_val_mem[i]      = val;
        }
      
      if(val_norm > max_val_norm_mem[i])
        {
        max_val_norm_mem[i] = val_norm;
        max_val_mem[i]      = val;
        }
      
      const eT& r_mean_val = r_mean_mem[i];
      
      r_var_mem[i] = N_minus_1/N * r_var_mem[i] + std::norm(val - r_mean_val)/N_plus_1;
      
      r_mean_mem[i] = r_mean_val + (val - r_mean_val)/N_plus_1;
      }
    
    }
  else
    {
    arma_debug_check( (sample.is_vec() == false), "running_stat_vec(): given sample is not a vector");
    
    x.r_mean.set_size(sample.n_rows, sample.n_cols);
    
    x.r_var.zeros(sample.n_rows, sample.n_cols);
    
    if(x.calc_cov == true)
      {
      x.r_cov.zeros(sample.n_elem, sample.n_elem);
      }
    
    x.min_val.set_size(sample.n_rows, sample.n_cols);
    x.max_val.set_size(sample.n_rows, sample.n_cols);
    
    x.min_val_norm.set_size(sample.n_rows, sample.n_cols);
    x.max_val_norm.set_size(sample.n_rows, sample.n_cols);
    
    
    const uword n_elem           = sample.n_elem;
    const eT* sample_mem       = sample.memptr();
          eT* r_mean_mem       = x.r_mean.memptr();
          eT* min_val_mem      = x.min_val.memptr();
          eT* max_val_mem      = x.max_val.memptr();
           T* min_val_norm_mem = x.min_val_norm.memptr();
           T* max_val_norm_mem = x.max_val_norm.memptr();
    
    for(uword i=0; i<n_elem; ++i)
      {
      const eT& val      = sample_mem[i];
      const  T  val_norm = std::norm(val);
      
      r_mean_mem[i]  = val;
      min_val_mem[i] = val;
      max_val_mem[i] = val;
      
      min_val_norm_mem[i] = val_norm;
      max_val_norm_mem[i] = val_norm;
      }
    }
  
  x.counter++;
  }