Exemplo n.º 1
0
int PokemonInfo::Stat(int poke, int stat, int level, quint8 dv, quint8 ev)
{
    quint8 basestat = PokemonInfo::BaseStats(poke).baseStat(stat);
    if (stat == Hp) {
        /* Shedinja */
        if (poke == Pokemon::Shedinja)
            return 1;
        else
            return calc_stat(basestat, level, dv, ev) + level + 5;
    }
    else
	return calc_stat(basestat, level, dv, ev);
}
Exemplo n.º 2
0
void normalize (enum WINDOWTYPE wt, float *gclookup, float *gclookup_x, float *max, float *max_x, float *min, float *min_x){

  int i;

  for (i=0; i<num_chrom; i++)
    norm_wins(i, wt, gclookup, gclookup_x);

  calc_stat(wt, gclookup, gclookup_x, 0, max, max_x, min, min_x);

}
Exemplo n.º 3
0
//type=0 regular=old, type=1 start isfirst pos,type=2 is from pos0.
kstring_t do_stat_main(perChr &pc,int step,int win,int nChr,int type){
  int pS,pE;//physical start,physical end
  int begI,endI;//position in array for pS, pE;
  
  kstring_t str;
  str.l=str.m=0;
  str.s=NULL;

  if(step==0&&win==0){
    //assuming whole chromosome as window
    begI=0;
    endI=pc.nSites-1;
    pS=0;
    pE=pc.posi[endI];
    ksprintf(&str,"(%d,%d)(%d,%d)(%d,%d)\t%s\t%d\t",begI,endI,pc.posi[begI],pc.posi[endI],pS,pE,pc.chr,pS+(pE-pS)/2);
    calc_stat(begI,endI,pc,str,nChr);
    ksprintf(&str,"\t%d\n",endI-begI);
    return str;

  }


  if(type==0)
    pS = (pc.posi[0]/step)*step +step;
  else if(type==1)
    pS = pc.posi[0];
  else if(type==2)
    pS = 1;
  pE = pS+win;
  begI=endI=0;
  
  
  if(pE>pc.posi[pc.nSites-1]){
    fprintf(stderr,"end of dataset is before end of window: end of window:%d last position in chr:%d\n",pE,pc.posi[pc.nSites-1]);
    return str;
  }

  while(pc.posi[begI]<pS) begI++;
  
  endI=begI;
  while(pc.posi[endI]<pE) endI++;

  while(1){
    //    fprintf(estpgF,"(%d,%d)(%d,%d)\t%s\t%d\t",begI,endI,the[begI].pos,the[endI].pos,chrCur,the[begI].pos+(the[endI].pos-the[begI].pos)/2);
    ksprintf(&str,"(%d,%d)(%d,%d)(%d,%d)\t%s\t%d\t",begI,endI,pc.posi[begI],pc.posi[endI],pS,pE,pc.chr,pS+(pE-pS)/2);
    calc_stat(begI,endI,pc,str,nChr);
    ksprintf(&str,"\t%d\n",endI-begI);

    //str.l=0;
    pS += step;
    pE =pS+win;
    if(pE>pc.posi[pc.nSites-1])
      break;

    while(pc.posi[begI]<pS) begI++;
    while(pc.posi[endI]<pE) endI++;
    
  }
  
  return str;

}
Exemplo n.º 4
0
void set_lpc_mode(float * signal_ptr,	/* I   Input signal */
		  float * a_fwd,	/* I   Forward LPC filter */
		  float * a_bwd,	/* I   Backward LPC filter */
		  int *mode,	/* O  Backward / forward Indication */
		  float * lsp_new,	/* I   LSP vector current frame */
		  float * lsp_old,	/* I   LSP vector previous frame */
		  int *bwd_dominant,	/* O   Bwd dominant mode indication */
		  int prev_mode,	/* I   previous frame Backward / forward Indication */
		  float * prev_filter,	/* I   previous frame filter */
		  float * C_int,	/*I/O filter interpolation parameter */
		  int16_t * glob_stat,	/* I/O Mre of global stationnarity */
		  int16_t * stat_bwd,	/* I/O Number of consecutive backward frames */
		  int16_t * val_stat_bwd	/* I/O Value associated with stat_bwd */
    )
{

	int i;
	float res[L_FRAME];
	float *pa_bwd;
	float gap;
	float gpred_f, gpred_b, gpred_bint;
	float tmp;
	float thresh_lpc;
	float dist_lsp, energy;

	pa_bwd = a_bwd + M_BWDP1;
	/* Backward filter prediction gain (no interpolation ) */
	/* --------------------------------------------------- */
	residue(M_BWD, pa_bwd, signal_ptr, res, L_FRAME);
	gpred_b = ener_dB(signal_ptr, L_FRAME) - ener_dB(res, L_FRAME);

	/* Interpolated LPC filter for transition forward -> backward */
	/* (used during 10 frames) ( for the second sub-frame )       */
	/* Interpolated backward filter for the first sub-frame       */
	/* ---------------------------------------------------------- */
	int_bwd(a_bwd, prev_filter, C_int);

	/* Interpolated backward filter prediction gain */
	/* -------------------------------------------- */
	residue(M_BWD, a_bwd, signal_ptr, res, L_SUBFR);
	residue(M_BWD, pa_bwd, &signal_ptr[L_SUBFR], &res[L_SUBFR], L_SUBFR);
	gpred_bint = ener_dB(signal_ptr, L_FRAME) - ener_dB(res, L_FRAME);

	/* Forward filter prediction gain */
	/* ------------------------------ */
	residue(M, a_fwd, signal_ptr, res, L_SUBFR);
	residue(M, &a_fwd[MP1], &signal_ptr[L_SUBFR], &res[L_SUBFR], L_SUBFR);
	gpred_f = ener_dB(signal_ptr, L_FRAME) - ener_dB(res, L_FRAME);

	/* -------------------------------------------------------------- */
	/*                  BACKWARD / FORWARD DECISION                   */
	/*                                                                */
	/* The Main criterion is based on the prediction gains :          */
	/* The global stationnarity index is used to adapt the threshold  */
	/* value " GAP ".                                                 */
	/* This adaptation is used to favour one mode according to the    */
	/* stationnarity of the input signal (backward for music and      */
	/* forward for speech) which avoids too many switches.            */
	/*                                                                */
	/* A second criterion based on the LSPs is used to avoid switches */
	/* if the successive LPC forward filters are very stationnary     */
	/* (which is measured a Euclidean distance on LSPs).              */
	/*                                                                */
	/* -------------------------------------------------------------- */

	/* 1st criterion with prediction gains */
	/* ----------------------------------- */

	/* Threshold adaptation according to the global stationnarity indicator */
	gap = (float) (*glob_stat) * GAP_FACT;
	gap += (float) 1.;

	if ((gpred_bint > gpred_f - gap) && (gpred_b > gpred_f - gap) &&
	    (gpred_b > (float) 0.) && (gpred_bint > (float) 0.))
		*mode = 1;
	else
		*mode = 0;

	if (*glob_stat < 13000)
		*mode = 0;	/* => Forward mode imposed */

	/* 2nd criterion with a distance between 2 successive LSP vectors */
	/* -------------------------------------------------------------- */
	/* Computation of the LPC distance */
	dist_lsp = 0;
	for (i = 0; i < M; i++) {
		tmp = lsp_old[i] - lsp_new[i];
		dist_lsp += tmp * tmp;
	}

	/* Adaptation of the LSPs thresholds */
	if (*glob_stat < 32000) {
		thresh_lpc = (float) 0.;
	} else {
		thresh_lpc = (float) 0.03;
	}

	/* Switching backward -> forward forbidden in case of a LPC stationnary */
	if ((dist_lsp < thresh_lpc) && (*mode == 0) && (prev_mode == 1)
	    && (gpred_b > (float) 0.) && (gpred_bint > (float) 0.)) {
		*mode = 1;
	}

	/* Low energy frame => Forward mode chosen */
	/* --------------------------------------- */
	energy = ener_dB(signal_ptr, L_FRAME);

	if (energy < THRES_ENERGY) {
		*mode = 0;
		if (*glob_stat > 13000)
			*glob_stat = 13000;
	} else
		tst_bwd_dominant(bwd_dominant, *mode);

	/* Adaptation of the global stationnarity indicator */
	/* ------------------------------------------------ */
	if (energy >= THRES_ENERGY)
		calc_stat(gpred_b, gpred_f, *mode,
			  prev_mode, glob_stat, stat_bwd, val_stat_bwd);
	if (*mode == 0)
		*C_int = (float) 1.1;
	return;
}
Exemplo n.º 5
0
static int64_t bitfury_scanHash(struct thr_info *thr)
{
	static struct bitfury_device *devices; // TODO Move somewhere to appropriate place
	int chip_n;
	int chip;
	uint64_t hashes = 0;
	struct timeval now;
	unsigned char line[2048];
	int short_stat = 10;
	static time_t short_out_t;
	int long_stat = 1800;
	static time_t long_out_t;
	int long_long_stat = 60 * 30;
	static time_t long_long_out_t;
	static first = 0; //TODO Move to detect()
	int i;

	devices = thr->cgpu->devices;
	chip_n = thr->cgpu->chip_n;

	if (!first) {
		for (i = 0; i < chip_n; i++) {
			devices[i].osc6_bits = 54;
		}
		for (i = 0; i < chip_n; i++) {
			send_reinit(devices[i].slot, devices[i].fasync, devices[i].osc6_bits);
		}
	}
	first = 1;

	for (chip = 0; chip < chip_n; chip++) {
		devices[chip].job_switched = 0;
		if(!devices[chip].work) {
			devices[chip].work = get_queued(thr->cgpu);
			if (devices[chip].work == NULL) {
				return 0;
			}
			work_to_payload(&(devices[chip].payload), devices[chip].work);
		}
	}

	libbitfury_sendHashData(devices, chip_n);
	nmsleep(5);

	cgtime(&now);
	chip = 0;
	for (;chip < chip_n; chip++) {
		if (devices[chip].job_switched) {
			int i,j;
			int *res = devices[chip].results;
			struct work *work = devices[chip].work;
			struct work *owork = devices[chip].owork;
			struct work *o2work = devices[chip].o2work;
			i = devices[chip].results_n;
			for (j = i - 1; j >= 0; j--) {
				if (owork) {
					submit_nonce(thr, owork, bswap_32(res[j]));
					devices[chip].stat_ts[devices[chip].stat_counter++] =
						now.tv_sec;
					if (devices[chip].stat_counter == BITFURY_STAT_N) {
						devices[chip].stat_counter = 0;
					}
				}
				if (o2work) {
					// TEST
					//submit_nonce(thr, owork, bswap_32(res[j]));
				}
			}
			devices[chip].results_n = 0;
			devices[chip].job_switched = 0;
			if (devices[chip].old_nonce && o2work) {
					submit_nonce(thr, o2work, bswap_32(devices[chip].old_nonce));
					i++;
			}
			if (devices[chip].future_nonce) {
					submit_nonce(thr, work, bswap_32(devices[chip].future_nonce));
					i++;
			}

			if (o2work)
				work_completed(thr->cgpu, o2work);

			devices[chip].o2work = devices[chip].owork;
			devices[chip].owork = devices[chip].work;
			devices[chip].work = NULL;
			hashes += 0xffffffffull * i;
		}
	}

	if (now.tv_sec - short_out_t > short_stat) {
		int shares_first = 0, shares_last = 0, shares_total = 0;
		char stat_lines[32][256] = {0};
		int len, k;
		double gh[32][8] = {0};
		double ghsum = 0, gh1h = 0, gh2h = 0;
		unsigned strange_counter = 0;

		for (chip = 0; chip < chip_n; chip++) {
			int shares_found = calc_stat(devices[chip].stat_ts, short_stat, now);
			double ghash;
			len = strlen(stat_lines[devices[chip].slot]);
			ghash = shares_to_ghashes(shares_found, short_stat);
			gh[devices[chip].slot][chip & 0x07] = ghash;
			snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);

			if(short_out_t && ghash < 0.5) {
				applog(LOG_WARNING, "Chip_id %d FREQ CHANGE\n", chip);
				send_freq(devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits - 1);
				nmsleep(1);
				send_freq(devices[chip].slot, devices[chip].fasync, devices[chip].osc6_bits);
			}
			shares_total += shares_found;
			shares_first += chip < 4 ? shares_found : 0;
			shares_last += chip > 3 ? shares_found : 0;
			strange_counter += devices[chip].strange_counter;
			devices[chip].strange_counter = 0;
		}
		sprintf(line, "vvvvwww SHORT stat %ds: wwwvvvv", short_stat);
		applog(LOG_WARNING, line);
		sprintf(line, "stranges: %u", strange_counter);
		applog(LOG_WARNING, line);
		for(i = 0; i < 32; i++)
			if(strlen(stat_lines[i])) {
				len = strlen(stat_lines[i]);
				ghsum = 0;
				gh1h = 0;
				gh2h = 0;
				for(k = 0; k < 4; k++) {
					gh1h += gh[i][k];
					gh2h += gh[i][k+4];
					ghsum += gh[i][k] + gh[i][k+4];
				}
				snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
				applog(LOG_WARNING, stat_lines[i]);
			}
		short_out_t = now.tv_sec;
	}

	if (now.tv_sec - long_out_t > long_stat) {
		int shares_first = 0, shares_last = 0, shares_total = 0;
		char stat_lines[32][256] = {0};
		int len, k;
		double gh[32][8] = {0};
		double ghsum = 0, gh1h = 0, gh2h = 0;

		for (chip = 0; chip < chip_n; chip++) {
			int shares_found = calc_stat(devices[chip].stat_ts, long_stat, now);
			double ghash;
			len = strlen(stat_lines[devices[chip].slot]);
			ghash = shares_to_ghashes(shares_found, long_stat);
			gh[devices[chip].slot][chip & 0x07] = ghash;
			snprintf(stat_lines[devices[chip].slot] + len, 256 - len, "%.1f-%3.0f ", ghash, devices[chip].mhz);

			shares_total += shares_found;
			shares_first += chip < 4 ? shares_found : 0;
			shares_last += chip > 3 ? shares_found : 0;
		}
		sprintf(line, "!!!_________ LONG stat %ds: ___________!!!", long_stat);
		applog(LOG_WARNING, line);
		for(i = 0; i < 32; i++)
			if(strlen(stat_lines[i])) {
				len = strlen(stat_lines[i]);
				ghsum = 0;
				gh1h = 0;
				gh2h = 0;
				for(k = 0; k < 4; k++) {
					gh1h += gh[i][k];
					gh2h += gh[i][k+4];
					ghsum += gh[i][k] + gh[i][k+4];
				}
				snprintf(stat_lines[i] + len, 256 - len, "- %2.1f + %2.1f = %2.1f slot %i ", gh1h, gh2h, ghsum, i);
				applog(LOG_WARNING, stat_lines[i]);
			}
		long_out_t = now.tv_sec;
	}

	return hashes;
}
Exemplo n.º 6
0
int norm_until_converges (enum WINDOWTYPE wt, float *gclookup, float *gclookup_x){
  float max;
  float max_x;
  float min;
  float min_x;

  float p_max;
  float p_max_x;
  float p_min;
  float p_min_x;

  int i, j;
  float mean    = 0.0;
  float mean_x  = 0.0;
  float stdev   = 0.0;
  float stdev_x = 0.0;
  int iter;

  float maxcut;
  float maxcut_x;
  float mincut;
  float mincut_x;

  iter = 1;
  calc_stat(wt, gclookup, gclookup_x, 0, &max, &max_x, &min, &min_x);

  switch(wt){
  case LW:
    mean    = LW_MEAN;
    mean_x  = LW_MEAN_X;
    break;
  case SW:
    mean    = SW_MEAN;
    mean_x  = SW_MEAN_X;
    break;
  case CW:
    mean    = CW_MEAN;
    mean_x  = CW_MEAN_X;
    break;
  }


  if (GENDER == AUTODETECT){
    if (VERBOSE)
      fprintf(stdout, "MEAN: %f\tMEAN_X: %f\n", mean, mean_x);


    if (mean_x / mean < 0.75){ // magical ratio
      GENDER = MALE;

      if (VERBOSE)
	fprintf(stdout, "Autodetect Gender: Male\n");

      i = findChrom("chrX", "", -1);
      
      if (i==-1)
        i = findChrom("X", "", -1);

      if (i != -1){
	switch(wt){
	  
	case CW:
	  for (j=0; j<chromosomes[i]->cw_cnt; j++)
	    if (chromosomes[i]->cw[j].depth > (float) CW_MEAN_X * 2 || chromosomes[i]->cw[j].depth < (float) CW_MEAN_X / 10.0)
	      chromosomes[i]->cw[j].isControl = 0;
	  break;
	  
	case LW:
	  for (j=0; j<chromosomes[i]->lw_cnt; j++)
	    if (chromosomes[i]->lw[j].depth > (float) LW_MEAN_X * 2 || chromosomes[i]->lw[j].depth < (float) LW_MEAN_X / 10.0)
	      chromosomes[i]->lw[j].isControl = 0;
	  break;
	  
	case SW:
	  for (j=0; j<chromosomes[i]->sw_cnt; j++)
	    if (chromosomes[i]->sw[j].depth > (float) SW_MEAN_X * 2 || chromosomes[i]->sw[j].depth < (float) SW_MEAN_X / 10.0)
	      chromosomes[i]->sw[j].isControl = 0;
	  break;
	  
	}
      }
    }

    else{
      GENDER = FEMALE;
      if (VERBOSE)
	fprintf(stdout, "Autodetect Gender: Female\n");
    }
  }

  if (!ISNORMALIZED)
    normalize(wt, gclookup, gclookup_x, &max, &max_x, &min, &min_x);


  do{

    if (VERBOSE){
      fprintf(stdout, "Control regions %s iteration %d ", (wt == LW ? "LW" : (wt == SW ? "SW" : "CW")), iter);
      fflush(stdout);
    }

    p_min   = min;
    p_max   = max;
    p_min_x = min_x;
    p_max_x = max_x;

    calc_stat(wt, gclookup, gclookup_x, 1, &max, &max_x, &min, &min_x);

    switch(wt){
    case LW:
      mean    = LW_MEAN;
      stdev   = LW_STD;
      mean_x  = LW_MEAN_X;
      stdev_x = LW_STD_X;
      break;
    case SW:
      mean    = SW_MEAN;
      stdev   = SW_STD;
      mean_x  = SW_MEAN_X;
      stdev_x = SW_STD_X;
      break;
    case CW:
      mean    = CW_MEAN;
      stdev   = CW_STD;
      mean_x  = CW_MEAN_X;
      stdev_x = CW_STD_X;
      break;
    }

    if (GENDER == FEMALE){
      max_x   = max;
      mean_x  = mean;
      stdev_x = stdev;
      min_x   = min;
    }

    iter++;

    maxcut   = mean + STDMULT * stdev;
    mincut   = mean - STDMULT * stdev;
    maxcut_x = mean_x + STDMULT * stdev_x;
    mincut_x = mean_x - STDMULT * stdev_x;

    /*
    maxcut   = mean * 2.5;// + STDMULT * stdev;
    mincut   = mean / 2.5; //- STDMULT * stdev;
    maxcut_x = mean_x * 1.5;//+ STDMULT * stdev_x;
    mincut_x = mean_x / 1.5; //- STDMULT * stdev_x;
    */

    /*
    if (GENDER != FEMALE){
      maxcut_x = mean_x + STDMULT * stdev_x;
      mincut_x = mean_x - STDMULT * stdev_x;
      }*/

    if (mincut < 0.0) mincut = mean / 10.0;
    if (mincut_x < 0.0) mincut_x = mean_x / 10.0;

    /*
    if (maxcut - mean > mean - mincut)
      maxcut = mean + (mean - mincut);

    if (GENDER != FEMALE){
      if (maxcut_x - mean_x > mean_x - mincut_x)
	maxcut_x = mean_x + (mean_x - mincut_x);
	}*/


    if (VERBOSE){
      fprintf(stdout, "mean: %f\tstdev: %f\tmax: %f (cut: %f)\tmin: %f (cut: %f) \n", mean, stdev, max, maxcut, min, mincut);
      if (GENDER != FEMALE)
	fprintf(stdout, "mean_x: %f\tstdev_x: %f\tmax_x: %f (cut: %f)\tmin_x: %f (cut: %f)\n", mean_x, stdev_x, max_x, maxcut_x, min_x, mincut_x);
    }

    if (p_min == min && p_max == max && p_min_x == min_x && p_max_x == max_x)
      break;

    //  } while (max >= maxcut || max_x >= maxcut_x || min <= mincut || min_x <= mincut_x);  
  } while (stdev > 0.25 * mean);


  fprintf(stdout, "%s Normalization completed.\n",  (wt == LW ? "LW" : (wt == SW ? "SW" : "CW")));

  return 1;
}
Exemplo n.º 7
0
void set_lpc_modeg ( Word16 *signal_ptr,  /* I   Input signal */
                    Word16 *a_fwd,       /* I   Forward LPC filter */
                    Word16 *a_bwd,       /* I   Backward LPC filter */
                    Word16 *mode,        /* O  Backward / forward Indication */
                    Word16 *lsp_new,     /* I   LSP vector current frame */
                    Word16 *lsp_old,     /* I   LSP vector previous frame */
                    Word16 *bwd_dominant,/* O   Bwd dominant mode indication */
                    Word16 prev_mode,     /* I   previous frame Backward / forward Indication */
                    Word16 *prev_filter,  /* I   previous frame filter */
                    Word16 *C_int,       /*I/O filter interpolation parameter */
                    Word16 *glob_stat,   /* I/O Mre of global stationnarity Q8 */
                    Word16 *stat_bwd,   /* I/O Number of consecutive backward frames */
                    Word16 *val_stat_bwd/* I/O Value associated with stat_bwd */
)
{

    Word16  i;
    Word16  res[L_FRAME];
    Word16  *pa_bwd;
    Word16  gap;
    Word16  gpred_f, gpred_b, gpred_bint;
    Word16  tmp;
    Word32  thresh_lpc_L;
    Word32  tmp_L;
    Word32  dist_lsp, energy;


    pa_bwd = a_bwd + M_BWDP1;
    /* Backward filter prediction gain (no interpolation ) */
    /* --------------------------------------------------- */
    Residue(M_BWD,pa_bwd, signal_ptr, res, L_FRAME);
    gpred_b = ener_dB(signal_ptr, L_FRAME) - ener_dB(res, L_FRAME);
    
    /* Interpolated LPC filter for transition forward -> backward */
    /* (used during 10 frames) ( for the second sub-frame )       */
    /* Interpolated backward filter for the first sub-frame       */
    /* ---------------------------------------------------------- */
    Int_bwd(a_bwd, prev_filter, C_int);
    
    /* Interpolated backward filter prediction gain */
    /* -------------------------------------------- */
    Residue(M_BWD,a_bwd, signal_ptr, res, L_SUBFR);
    Residue(M_BWD,pa_bwd, &signal_ptr[L_SUBFR], &res[L_SUBFR], L_SUBFR);
    gpred_bint = ener_dB(signal_ptr, L_FRAME) - ener_dB(res, L_FRAME);
    

    /* Forward filter prediction gain */
    /* ------------------------------ */
    Residue(M, a_fwd, signal_ptr, res, L_SUBFR);
    Residue(M, &a_fwd[MP1], &signal_ptr[L_SUBFR], &res[L_SUBFR], L_SUBFR);
    gpred_f = ener_dB(signal_ptr, L_FRAME) - ener_dB(res, L_FRAME);
    
    /* -------------------------------------------------------------- */
    /*                  BACKWARD / FORWARD DECISION                   */
    /*                                                                */
    /* The Main criterion is based on the prediction gains :          */
    /* The global stationnarity index is used to adapt the threshold  */
    /* value " GAP ".                                                 */
    /* This adaptation is used to favour one mode according to the    */
    /* stationnarity of the input signal (backward for music and      */
    /* forward for speech) which avoids too many switches.            */
    /*                                                                */
    /* A second criterion based on the LSPs is used to avoid switches */
    /* if the successive LPC forward filters are very stationnary     */
    /* (which is measured a Euclidean distance on LSPs).              */
    /*                                                                */
    /* -------------------------------------------------------------- */

    /* 1st criterion with prediction gains */
    /* ----------------------------------- */
    
    /* Threshold adaptation according to the global stationnarity indicator */
    tmp = shr(*glob_stat, 7);
    tmp_L = L_mult(tmp, 3);
    tmp_L = L_shr(tmp_L, 1);
    tmp = extract_l(tmp_L);
    gap = add(tmp, 205);
    
    if ( (gpred_bint > gpred_f - gap)&&
        (gpred_b > gpred_f - gap)&&
        (gpred_b > 0)&&
        (gpred_bint > 0) ) *mode = 1;
    
    else *mode = 0;
    
    if (*glob_stat < 13000) *mode = 0; /* => Forward mode imposed */
    
    /* 2nd criterion with a distance between 2 successive LSP vectors */
    /* -------------------------------------------------------------- */
    /* Computation of the LPC distance */
    dist_lsp = 0;
    for(i=0; i<M; i++){
        tmp = sub(lsp_old[i],lsp_new[i]);
        dist_lsp = L_mac(dist_lsp, tmp, tmp);
    }
    
    /* Adaptation of the LSPs thresholds */
    if (*glob_stat < 32000) {
        thresh_lpc_L = 0L;
    }
    else {
        thresh_lpc_L = 64424509L;
    }
    
    /* Switching backward -> forward forbidden in case of a LPC stationnary */
    if ((dist_lsp < thresh_lpc_L)&&(*mode == 0)&&(prev_mode == 1)&&(gpred_b > 0)&&(gpred_bint > 0)) {
        
        *mode = 1;
    }
    
    /* Low energy frame => Forward mode chosen */
    /* --------------------------------------- */
    energy = ener_dB(signal_ptr, L_FRAME);
    
    if (energy < 8192) {
        *mode = 0;
        if (*glob_stat > 13000) *glob_stat = 13000;
    }
    else tst_bwd_dominant(bwd_dominant, *mode);
    
    /* Adaptation of the global stationnarity indicator */
    /* ------------------------------------------------ */
    if (energy >= 8192) calc_stat(gpred_b, gpred_f, *mode, prev_mode,
        glob_stat, stat_bwd, val_stat_bwd);
    if(*mode == 0) *C_int = 4506;
    return;
    
}
Exemplo n.º 8
0
void job_controller(int ops, _main_data *main_data)
{
    static int cur_type = -1;
    static int wav_cur_track = -1;
    static int mp3_cur_track = -1;
    static int mp3_nxt_track = -1;
    static pid_t wav_pg_pid = -1;
    static pid_t mp3_pg_pid = -1;
    static pid_t wav_pi_pid = -1;
    static pid_t mp3_pi_pid = -1;
    static int wav_read_fd;
    static int mp3_read_fd;
    static _stat stat;
    static _main_data *saved_main_data;
    static char *wav_file_path, *enc_file_path;
    unsigned wav_current = 0;
    unsigned mp3_current = 0;
    double wav_progress;
    double mp3_progress;
    char msg[ MAX_PLUGIN_OUTPUT_LENGTH ];
    char *str;
    int temp, temp_track;

    switch(ops)
    {
        case JC_START :
            /* called once when the go button is clicked. */
            saved_main_data = main_data;

            if(wav_cur_track != -1)
            {
                err_handler(JOB_IN_PROGRESS_ERR, NULL);
                job_finisher(main_data);
                return;
            }

            if(find_next_job(main_data, wav_cur_track, WAV,
                             &wav_cur_track, &cur_type) < 0)
            {
                /* ok, no more wavs to rip, try looking for mp3s */
                if(find_next_job(main_data, mp3_cur_track, MP3,
                                 &mp3_cur_track, &cur_type) < 0)
                {
                    /* nothing at all found */
                    err_handler(NOTHING_TO_DO_ERR, NULL);
                    job_finisher(main_data);
                    return;
                }

                create_file_names_for_track(main_data, mp3_cur_track,
                                            &wav_file_path, &enc_file_path);

                calc_stat(main_data, &stat, mp3_current,
                          mp3_cur_track, MP3, CALC_START_SESSION);
                calc_stat(main_data, &stat, mp3_current,
                          mp3_cur_track, MP3, CALC_START);

                /* start encoding */
                if(start_ripping_encoding(MP3, main_data->track[ mp3_cur_track ].begin,
                                          main_data->track[ mp3_cur_track ].length,
                                          mp3_cur_track,
                                          wav_file_path,
                                          enc_file_path,
                                          &mp3_pg_pid,
                                          &mp3_pi_pid,
                                          &mp3_read_fd) < 0)
                {
                    job_finisher(main_data);
                    return;
                }

                stat.encoding = TRUE;
            }
            else
            {
                /* found the first wav to rip */
                create_file_names_for_track(main_data, wav_cur_track, &wav_file_path, &enc_file_path);
                calc_stat(main_data, &stat, wav_current, wav_cur_track, WAV, CALC_START_SESSION);
                calc_stat(main_data, &stat, wav_current, wav_cur_track, WAV, CALC_START);

                /* start ripping */
                if(start_ripping_encoding(WAV, main_data->track[ wav_cur_track ].begin,
                                          main_data->track[ wav_cur_track ].length,
                                          wav_cur_track,
                                          wav_file_path,
                                          enc_file_path,
                                          &wav_pg_pid,
                                          &wav_pi_pid,
                                          &wav_read_fd) < 0)
                {
                    job_finisher(main_data);
                    return;
                }

                stat.ripping = TRUE;

                /* Find the next track to encode if any */
                if(find_next_job(main_data, mp3_cur_track, MP3, &mp3_nxt_track, &cur_type) < 0 ||
                        !main_data->track[ mp3_nxt_track ].make_mp3)
                {
                    mp3_nxt_track = -1;
                }
            }

            /* Create wav/mp3 status frame */
            wm_status_frame_handler(WIDGET_CREATE, cur_type, &stat, NULL);

            job_controller_timeout_start();
            return;

        case JC_UPDATE :
            main_data = saved_main_data;

            /* Part 1: check progress on the rip */
            if(stat.ripping == TRUE)
            {
                temp = read_and_process_plugin_output(wav_read_fd, &wav_progress, msg);
                wav_current = main_data->track[ wav_cur_track ].length * wav_progress;

                switch(temp)
                {
                    case PLUGIN_MSG_PARSE_ERR :
                        /* Nothing to do. Let's wait more */
                        break;

                    case PLUGIN_PROGRESS_MSG :
                        /* progress report, update status display */
                        calc_stat(main_data, &stat, wav_current,
                                  wav_cur_track, WAV, CALC_UPDATE);

                        /* Update status widget */
                        if(msg[ 0 ] == '\0')
                        {
                            str = NULL;
                        }
                        else
                        {
                            str = msg;
                        }

                        wm_status_frame_handler(WIDGET_UPDATE, WAV, &stat, str);
                        break;

                    case PLUGIN_NO_MSG_AVAILABLE :

                        /* Check if the plugin has exited
                           It only happens when ripperX failed to execute the plugin */
                        if(wav_pi_pid >= 0)
                            if(waitpid(wav_pi_pid, NULL, WNOHANG) == wav_pi_pid)
                            {
                                err_handler(PLUGIN_NOT_PRESENT_ERR,
                                            _("Maybe ripperX has failed to execute the plugin"));
                                wav_pi_pid = -1;
                                job_controller_timeout_stop();
                                job_controller(JC_ABORT_ALL_DELETE, main_data);
                                return;
                            }

                        /* Check if the job is finished */
                        if(waitpid(wav_pg_pid, NULL, WNOHANG) == wav_pg_pid)
                        {
                            /* One job finished, go for next one */

                            /* kill the plugin */
                            if(waitpid(wav_pi_pid, NULL, WNOHANG) != wav_pi_pid)
                            {
                                kill(-wav_pi_pid, SIGTERM);
                                waitpid(wav_pi_pid, NULL, 0);
                            }

                            /* Close the fd */
                            close(wav_read_fd);

                            /* stop calculating stats */
                            calc_stat(main_data, &stat, wav_current,
                                      wav_cur_track, WAV, CALC_STOP);

                            /* Mark that it exists */
                            main_data->track[ wav_cur_track ].wav_exist = TRUE;

                            /* find next track to rip */
                            temp_track = wav_cur_track;

                            if(find_next_job(main_data, wav_cur_track, WAV, &wav_cur_track, &cur_type) < 0)
                            {
                                /* All finished - no more rips */
                                wav_pg_pid = -1;
                                wav_pi_pid = -1;
                                wav_cur_track = -1;
                                cur_type = -1;
                                stat.ripping = FALSE;

                                /* if we are only ripping, finish up for good */
                                if(!main_data->track[ temp_track ].make_mp3)
                                {
                                    calc_stat(main_data, &stat, wav_current,
                                              temp_track, WAV, CALC_STOP_SESSION);

                                    job_controller_timeout_stop();
                                    job_finisher(main_data);
                                    return;
                                }
                            }
                            else
                            {
                                /* if we are only ripping, update the stats */
                                if(!main_data->track[ temp_track ].make_mp3)
                                {
                                    stat.tracks_done++;
                                    stat.tracks_remain--;
                                }

                                /*  start ripping the next track */
                                create_file_names_for_track(main_data, wav_cur_track,
                                                            &wav_file_path, &enc_file_path);
                                calc_stat(main_data, &stat, wav_current,
                                          wav_cur_track, WAV, CALC_START);

                                if(start_ripping_encoding(WAV,
                                                          main_data->track[ wav_cur_track ].begin,
                                                          main_data->track[ wav_cur_track ].length,
                                                          wav_cur_track,
                                                          wav_file_path,
                                                          enc_file_path,
                                                          &wav_pg_pid, &wav_pi_pid,
                                                          &wav_read_fd) < 0)
                                {
                                    calc_stat(main_data, &stat, wav_current,
                                              wav_cur_track, WAV, CALC_STOP_SESSION);
                                    job_controller_timeout_stop();
                                    job_finisher(main_data);
                                    return;
                                }
                            }
                        } /* end if job is finished section */

                        break;
                } /* end rip switch */
            } /* end rip progress check */

            /* Part 2: check progress on the encode */
            if(stat.encoding == TRUE)
            {
                temp = read_and_process_plugin_output(mp3_read_fd, &mp3_progress, msg);
                mp3_current = main_data->track[ mp3_cur_track ].length * mp3_progress;

                switch(temp)
                {
                    case PLUGIN_MSG_PARSE_ERR :
                        /* Nothing to do. Let's wait more */
                        break;

                    case PLUGIN_PROGRESS_MSG :
                        /* progress report, update status display */
                        calc_stat(main_data, &stat, mp3_current,
                                  mp3_cur_track, MP3, CALC_UPDATE);

                        /* Update status widget */
                        if(msg[ 0 ] == '\0')
                        {
                            str = NULL;
                        }
                        else
                        {
                            str = msg;
                        }

                        wm_status_frame_handler(WIDGET_UPDATE, MP3, &stat, str);
                        break;

                    case PLUGIN_NO_MSG_AVAILABLE :

                        /* Check if the plugin has exited
                           It only happens when ripperX failed to execute the plugin */
                        if(mp3_pi_pid >= 0)
                            if(waitpid(mp3_pi_pid, NULL, WNOHANG) == mp3_pi_pid)
                            {
                                err_handler(PLUGIN_NOT_PRESENT_ERR,
                                            _("Maybe ripperX has failed to execute the plugin"));
                                mp3_pi_pid = -1;
                                job_controller_timeout_stop();
                                job_controller(JC_ABORT_ALL_DELETE, main_data);
                                return;
                            }

                        /* Check if the job is finished */
                        if(waitpid(mp3_pg_pid, NULL, WNOHANG) == mp3_pg_pid)
                        {
                            /* One job finished, go for next one */

                            /* kill the plugin */
                            if(waitpid(mp3_pi_pid, NULL, WNOHANG) != mp3_pi_pid)
                            {
                                kill(-mp3_pi_pid, SIGTERM);
                                waitpid(mp3_pi_pid, NULL, 0);
                            }

                            /* Close the fd */
                            close(mp3_read_fd);

                            /* stop calculating stats */
                            calc_stat(main_data, &stat, mp3_current,
                                      mp3_cur_track, MP3, CALC_STOP);

                            main_data->track[ mp3_cur_track ].mp3_exist = TRUE;

                            /* Delete WAV file if he/she doesn't want it */
                            if(!config.keep_wav)
                            {
                                create_file_names_for_track(main_data, mp3_cur_track,
                                                            &wav_file_path, &enc_file_path);

                                if(unlink(wav_file_path) < 0)
                                {
                                    err_handler(FILE_DELETE_ERR, wav_file_path);
                                }

                                /* Mark that it has been deleted */
                                main_data->track[ mp3_cur_track ].wav_exist = FALSE;

                                /* Delete WAV work directory if this was the last WAV file,
                                   and the mp3 work dir is different */
                                if(stat.tracks_remain <= 1)
                                    if(strcmp(config.wav_path,config.mp3_path) != 0)
                                    {
                                        rmdir(file_path_without_name(wav_file_path));
                                    }
                            }

                            /* find next track to encode */
                            temp_track = mp3_cur_track;

                            if(find_next_job(main_data, mp3_cur_track, MP3, &mp3_nxt_track, &cur_type) < 0)
                            {
                                /* All finished - no more encoding - done for good! */
                                mp3_pg_pid = -1;
                                mp3_pi_pid = -1;
                                mp3_cur_track = mp3_nxt_track = -1;
                                cur_type = -1;
                                calc_stat(main_data, &stat, mp3_current,
                                          temp_track, MP3, CALC_STOP_SESSION);
                                job_controller_timeout_stop();
                                stat.encoding = FALSE;

                                job_finisher(main_data);
                                return;
                            }

                            mp3_cur_track = -1;

                            stat.tracks_done++;
                            stat.tracks_remain--;
                            stat.encoding = FALSE;

                        } /* end if job is finished section */

                        break;
                } /* end encode switch */
            } /* end encode progress check */

            if(!stat.encoding && mp3_nxt_track != -1 && main_data->track[ mp3_nxt_track ].wav_exist)
            {
                /* start encoding */
                mp3_cur_track = mp3_nxt_track;
                mp3_nxt_track = -1;
                create_file_names_for_track(main_data, mp3_cur_track, &wav_file_path, &enc_file_path);
                calc_stat(main_data, &stat, mp3_current, mp3_cur_track, MP3, CALC_START);

                if(start_ripping_encoding(MP3, main_data->track[ mp3_cur_track ].begin,
                                          main_data->track[ mp3_cur_track ].length,
                                          mp3_cur_track,
                                          wav_file_path,
                                          enc_file_path,
                                          &mp3_pg_pid, &mp3_pi_pid,
                                          &mp3_read_fd) < 0)
                {
                    calc_stat(main_data, &stat, mp3_current, mp3_cur_track, MP3, CALC_STOP_SESSION);
                    job_controller_timeout_stop();
                    job_finisher(main_data);
                }

                stat.encoding = TRUE;
            }

            /* end of JC_UPDATE */
            return;
        case JC_PAUSE :
            main_data = saved_main_data;

            if(wav_pg_pid >= 0)
                if(waitpid(wav_pg_pid, NULL, WNOHANG) != wav_pg_pid)
                {
                    kill(wav_pg_pid, SIGTSTP);
                }

            if(wav_pi_pid >= 0)
                if(waitpid(wav_pi_pid, NULL, WNOHANG) != wav_pi_pid)
                {
                    kill(wav_pi_pid, SIGTSTP);
                }

            if(mp3_pg_pid >= 0)
                if(waitpid(mp3_pg_pid, NULL, WNOHANG) != mp3_pg_pid)
                {
                    kill(mp3_pg_pid, SIGTSTP);
                }

            if(mp3_pi_pid >= 0)
                if(waitpid(mp3_pi_pid, NULL, WNOHANG) != mp3_pi_pid)
                {
                    kill(mp3_pi_pid, SIGTSTP);
                }

            calc_stat(main_data, &stat, wav_current, wav_cur_track, WAV, CALC_PAUSE);
            job_controller_timeout_stop();
            return;

        case JC_CONT :
            main_data = saved_main_data;

            if(wav_pg_pid >= 0)
                if(waitpid(wav_pg_pid, NULL, WNOHANG) != wav_pg_pid)
                {
                    kill(wav_pg_pid, SIGCONT);
                }

            if(wav_pi_pid >= 0)
                if(waitpid(wav_pi_pid, NULL, WNOHANG) != wav_pi_pid)
                {
                    kill(wav_pi_pid, SIGCONT);
                }

            if(mp3_pg_pid >= 0)
                if(waitpid(mp3_pg_pid, NULL, WNOHANG) != mp3_pg_pid)
                {
                    kill(mp3_pg_pid, SIGCONT);
                }

            if(mp3_pi_pid >= 0)
                if(waitpid(mp3_pi_pid, NULL, WNOHANG) != mp3_pi_pid)
                {
                    kill(mp3_pi_pid, SIGCONT);
                }

            calc_stat(main_data, &stat, wav_current, wav_cur_track, cur_type, CALC_CONT);
            job_controller_timeout_start();
            return;

        case JC_ABORT :
        case JC_ABORT_DELETE :
        case JC_ABORT_ALL :
        case JC_ABORT_ALL_DELETE :
            main_data = saved_main_data;

            if(wav_pg_pid >= 0)
                if(waitpid(wav_pg_pid, NULL, WNOHANG) != wav_pg_pid)
                {
                    job_controller(JC_CONT, NULL);
                    kill(-wav_pg_pid, SIGTERM);
                    waitpid(wav_pg_pid, NULL, 0);
                }

            if(wav_pi_pid >= 0)
                if(waitpid(wav_pi_pid, NULL, WNOHANG) != wav_pi_pid)
                {
                    kill(-wav_pi_pid, SIGTERM);
                    waitpid(wav_pi_pid, NULL, 0);
                }

            if(mp3_pg_pid >= 0)
                if(waitpid(mp3_pg_pid, NULL, WNOHANG) != mp3_pg_pid)
                {
                    job_controller(JC_CONT, NULL);
                    kill(-mp3_pg_pid, SIGTERM);
                    waitpid(mp3_pg_pid, NULL, 0);
                }

            if(mp3_pi_pid >= 0)
                if(waitpid(mp3_pi_pid, NULL, WNOHANG) != mp3_pi_pid)
                {
                    kill(-mp3_pi_pid, SIGTERM);
                    waitpid(mp3_pi_pid, NULL, 0);
                }

            /* Close the pipe or pty */
            close(wav_read_fd);
            close(mp3_read_fd);
            calc_stat(main_data, &stat, wav_current, wav_cur_track, cur_type, CALC_STOP_SESSION);

            /* Destroy status widget */
            wm_status_frame_handler(WIDGET_DESTROY, WAV, &stat, msg);

            if((ops == JC_ABORT_ALL_DELETE) || (ops == JC_ABORT_DELETE))
            {
                create_file_names_for_track(main_data, wav_cur_track, &wav_file_path, NULL);

                if(wav_cur_track != -1)
                {
                    create_file_names_for_track(main_data, wav_cur_track, &wav_file_path, &enc_file_path);

                    if(unlink(wav_file_path) < 0)
                    {
                        err_handler(FILE_DELETE_ERR, wav_file_path);
                    }
                }

                if(mp3_cur_track != -1)
                {
                    create_file_names_for_track(main_data, mp3_cur_track, NULL, &enc_file_path);

                    if(unlink(enc_file_path) < 0)
                    {
                        err_handler(FILE_DELETE_ERR, enc_file_path);
                    }
                }
            }
            else
            {
                /* Mark that it exists */
                if(wav_cur_track != -1)
                {
                    main_data->track[ wav_cur_track ].wav_exist = TRUE;
                }

                if(mp3_cur_track != -1)
                {
                    main_data->track[ mp3_cur_track ].mp3_exist = TRUE;
                }
            }

            wav_cur_track = -1;
            mp3_cur_track = -1;
            cur_type = -1;
            calc_stat(main_data, &stat, wav_current,
                      wav_cur_track, cur_type, CALC_STOP_SESSION);
            job_controller_timeout_stop();
            job_finisher(main_data);
            return;
    }
}
Exemplo n.º 9
0
int main(int argc, char **argv)
{
#if DEBUG
  feenableexcept(FE_DIVBYZERO| FE_INVALID|FE_OVERFLOW); // enable exceptions
#endif
  if(argc ^ 2){
    printf("Usage: ./sa sa.config\n");
    exit(1);
  }
  if(read_config(argv[1])){           // read config
    printf("READDATA: Can't process %s \n", argv[1]);
    return 1;
  }  
  T = T+SKIP;               // increase T by SKIP to go from 0 to T all the way in time units
  
  // precomputing  
  I_Gamma = 1.0/Gamma;
  I_Alpha = 1.0/Alpha;    
  Bo = B;    
  X0_Be = pow(X0, Beta);
  GaBe  = Gamma * Beta;

  // event probabilites scaling by G;
  PE[0] = PE[0]*(double)G;
  PE[1] = PE[1]*(double)G;
  PE[2] = PE[2]*(double)G;
  
  initrand(Seed);
  init();          // note: method is in init.c  // allocate memory at initial  
  
  int i, j, ev, k = 0; // m;
  unsigned long seed;
  double dt, ct, tt;      // tt: time of simulation; dt: delta time after which next event occurs; ct: counter time to check with skip time for stat calculation
  time_t now;   
 
  for( i = 0; i < Runs; i++){
    tt = 0.0, ct = 0.0;
    k = 0; //m = 0;
    if(Seed == 0){      
      now = time(0);
      seed = ((unsigned long)now);  
      seed = 1454011037;
      initrand(seed);
      printf("\n run: %d rand seed: %lu\n",i+1, seed);
    }
    set_init_xrvpi();   // sets initial strategies vector, roles, valuations and payoffs       
     
    calc_stat(0, i);
    
    for( j = 0; j < N; j++) printf("%.4lf  ", V[0][j]); printf("\n");
    
    // Gillespie's stochastic simulation 
    while(tt < T){     
      // calc lambda; lambda = summation of P[j]s
      for(Lambda = 0, j = EVENTS; j--;)
	Lambda += PE[j];
      // select time for next event
      dt = randexp(Lambda); //printf("dt: %.4lf\n", dt);
      // select next event
      ev = select_event();
      // play the event      
      play_event(ev);
      // update time
      tt += dt;
      // calc stat
      ct += dt;
      if(ct >= SKIP){
        calc_stat(++k, i); // calculates all the stats	
	ct = 0.0;
      }
      // update events associated probabilites if necessary
    }
#if ALLDATAFILE
    calc_stat(-1, -1);    // free file pointers for individual run data files
#if !CLUSTER
    plotallIndividualRun(i, 0);      // note: method is in dataplot.c  
#if GRAPHS
    plotallIndividualRun(i, 1);      // note: method is in dataplot.c
#endif
#endif
    // write traits of final state
    {
      int m, n;
      char tstr[200], str[100];
      sprintf(str, "traits%d.dat", i);
      sprintf(str, "traits%d.dat", i); prep_file(tstr, str);
      FILE *fp = fopen(tstr, "w");
      for(m = 0; m < G; m++){
	for( n = 0; n < GS[m]; n++){
	  fprintf(fp, "%.4lf %.4lf    ", dxi[m][n], dsi[m][n]);
	}
	fprintf(fp, "\n");
      }
      fclose(fp);
    }
#if PUNISH
#if DISP_MATRIX
    plotTraits(i, 0);   // note: method is in dataplot.c
#endif
#if GRAPHS
    plotTraits(i, 1);   // note: method is in dataplot.c
#endif
#endif
#endif
  }  
  // write effort and payoff data
  writefile_effort_fertility();              // note: method is in dataplot.c
  writefile_threshold_aggressiveness();      // note: method is in dataplot.c
  // plot data with graphics using gnuplot
  if(!CLUSTER){    
    plotall(0);                              // note: method is in dataplot.c
#if GRAPHS
    plotall(1);                              // note: method is in dataplot.c
#endif
  }
#if PUNISH
#if DISP_MATRIX
  displayMatrix();                           
#endif  
#endif
  
  cleanup();                                 // note: method is in init.c
  return 0;
}