コード例 #1
0
ファイル: freq_tracer.c プロジェクト: slazav/he3misc
int
main(){
  int i, fmt, val;
  double dt, dx;
  double xm[WIN]; // initial signal


  flt_dbg_file = fopen("data_flt.dat", "w");
  ph_dbg_file  = fopen("data_ph.dat", "w");
  amp_dbg_file = fopen("data_amp.dat", "w");
  fin_dbg_file = fopen("data_fin.dat", "w");

  FILE * tmp_file = fopen("data_tmp.dat", "w");


  { // read signal header
    char *line, *str, *tok, *saveptr;
    int j;
    size_t len=0;

    if (getline(&line, &len, stdin)<1) exit(1);
    fprintf(stderr, ">>> %s\n", line);
    for (j=1,str=line; ;j++,str=NULL){
      tok = strtok_r(str, ",", &saveptr);
      if (tok == NULL) break;
      switch (j){
        case 1: fmt=atoi(tok); break;
        case 5: dt=atof(tok); break;
        case 6: t0=atof(tok); break;
        case 8: dx=atof(tok); break;
      }
    }
    // skip two lines
    for (j=0;j<2;j++) getline(&line, &len, stdin);
    free(line);
  }

  printf("#%13s %14s %14s %14s\n",
    "time", "freq", "amp", "err");
  do {
    for (i=0; i<WIN; i++) {
      scanf("%i", &val);
      if (fmt==0) val=(val>=0)?val-127:val+127;
      xm[i] = val*dx;
      fprintf(tmp_file, "%e %e\n", t0+i*dt, val*dx);

      if (feof(stdin)) break;
    }
    process_window(xm, i, dt);
    fprintf(stderr, state>0?"o":".");

//    if (state)
      printf("%14e %14e %14e %14e\n",
        res.time, res.freq, res.amp, res.err);

  } while (!feof(stdin));

  return 0;
}
int st_noisered_drain(eff_t effp, st_sample_t *obuf, st_size_t *osamp)
{
    reddata_t data = (reddata_t)effp->priv;
    int i;
    int tracks = effp->ininfo.channels;
    for (i = 0; i < tracks; i ++) {
        *osamp = process_window(data, i, tracks, obuf, data->bufdata);
    }
    return (ST_SUCCESS);
}
コード例 #3
0
int ctr_crypto_interface_write_sector(void *io, const void *buffer, size_t buffer_size, size_t sector)
{
	int res = 0;
	ctr_crypto_interface *crypto_io = io;
	const size_t sector_size = ctr_io_sector_size(crypto_io->lower_io);
	const size_t number_of_sectors = FLOOR(buffer_size, sector_size);
	const size_t block_size = AES_BLOCK_SIZE;
	size_t sectors_per_block = block_size / sector_size;

	if (!sectors_per_block)
		sectors_per_block = 1;

	if (number_of_sectors)
	{
		size_t block_sector_lcm = lcm(sector_size, block_size);

		uint8_t window_buffer[4 * block_sector_lcm + sectors_per_block * sector_size];
		write_window window =
		{
			.window = window_buffer,
			.window_size = sizeof(window_buffer),
			.window_offset = 0,
			.buffer = buffer,
			.buffer_size = buffer_size,
			.buffer_offset = 0,
			.current_sector = sector
		};

		setup_window(io, &window, sector, sector_size, block_size);

		res = process_window(io, &window, sector_size, block_size, input, output);
		while(!res)
		{
			res = process_window(io, &window, sector_size, block_size, input, output);
		}
	}
	return res == 1 ? 0 : res;
}
/*
 * Read in windows, and call process_window once we get a whole one.
 */
int st_noisered_flow(eff_t effp, st_sample_t *ibuf, st_sample_t *obuf, 
                    st_size_t *isamp, st_size_t *osamp)
{
    reddata_t data = (reddata_t) effp->priv;
    int samp = min(*isamp, *osamp);
    int tracks = effp->ininfo.channels;
    int track_samples = samp / tracks;
    int ncopy = min(track_samples, WINDOWSIZE-data->bufdata);
    int whole_window = (ncopy + data->bufdata == WINDOWSIZE);
    int oldbuf = data->bufdata;
    int i;
    assert(effp->ininfo.channels == effp->outinfo.channels);

    if (whole_window) {
        data->bufdata = WINDOWSIZE/2;
    } else {
        data->bufdata += ncopy;
    }

    /* Reduce noise on every channel. */
    for (i = 0; i < tracks; i ++) {
        chandata_t* chan = &(data->chandata[i]);
        int j;
        if (chan->window == NULL) {
            chan->window = (float*)calloc(WINDOWSIZE, sizeof(float));
        }
        
        for (j = 0; j < ncopy; j ++) {
            chan->window[oldbuf + j] =
                ST_SAMPLE_TO_FLOAT_DWORD(ibuf[i + tracks * j]);
        }

        if (!whole_window)
            continue;
        else {
            process_window(data, i, tracks, obuf, oldbuf + ncopy);
        }
    }
    
    *isamp = tracks*ncopy;
    if (whole_window) {
        *osamp = tracks*(WINDOWSIZE/2);
    } else {
        *osamp = 0;
    }
    
    return (ST_SUCCESS);
}
コード例 #5
0
ファイル: q2sender.c プロジェクト: stevenrau/434_a2
int main(int argc, char **argv)
{
    int max_window_size;
    int timeout_sec;
    char *receiver_ip;
    char *receiver_port;
    uint32_t seq_num = 0;  /* Use 32-bit sequence num to ensure max capacity before wrapping (likely unnecesary)*/
    struct message *msg;
    char *in_buf = NULL;   /* Buffer to hold the output message sent to the receiver */
    size_t len = 0;        /* Length of text line read in */
    char *out_buf = NULL; /* Buffer to hold the output message struct containing text and seq num */
    struct timeval *timeout;

    /* Get the receiver host and port as well as window size and timeout from the command line */
    if (argc < 5)
    {
        fprintf(stderr, "Usage: %s <receiver_ip> <receiver_port> <max_window_size> <timeout_sec>\n", argv[0]);
        
        exit(1);
    }
    receiver_ip = argv[1];
    receiver_port = argv[2];
    max_window_size = atoi(argv[3]);
    timeout_sec = atoi(argv[4]);
    
    /* Check to make sure input variables are allowed */
    if (atoi(receiver_port) < MIN_PORT_NUM || atoi(receiver_port) > MAX_PORT_NUM)
    {
        fprintf(stderr, "Usage: Port numbers must be between %d and %d\n", 
                         MIN_PORT_NUM, MAX_PORT_NUM);
        
        exit(1);
    }
    
    printf("UDP sender started: \n"
           "\tReceiver IP/Hostname: %s, Receiver Port: %s\n"
           "\tMax message window size: %i  Timeout (sec): %i\n"
	   "\tReady for input...\n\n", receiver_ip, receiver_port, max_window_size, timeout_sec);
    
    /* Allocate space for the sliding window */
    window = calloc(max_window_size, sizeof(struct message));
    
    /* Make space for the timeout timeval struct */
    timeout = calloc(1, sizeof(struct timeval));
    
    /* Make space for the output buffer to hold the message */
    out_buf = calloc(1, sizeof(struct message));
    
    /* Main sender loop that receives user input from stdin and forwards the messages to the receiver
     * via UDP. The user-specified window size and timeout length determine the functional details
     * of the go-back-n sliding window implementation */
    while (1)
    {      
        in_buf = NULL;
        
        /* Setup or reset timeout since select() modifes it */
        timeout->tv_usec = 0;
        timeout->tv_sec = timeout_sec;
        
        /* If the window isn't full, try to send another new message */
        if (num_queued < max_window_size)
        {
            printf("Enter a message: \n");
            
            /* Clear out the memory of the out buffer */
            memset(out_buf, 0, sizeof(struct message));
            
            /* Allocate message space */ 
            msg = calloc(1, sizeof(struct message));
            
            /* Give the message the next sequence number and increment */
            msg->seq = seq_num;
            seq_num++;
            
            /* Read the command line text into the input buffer  */
            getline(&in_buf, &len, stdin);
            
            /* Copy as many characters from the read input buffer into the message struct that will fit */
            memcpy(msg->text, in_buf, MAX_TEXT_LENGTH);
            
            /* Put the message into the output buffer */
            memcpy(out_buf, msg, sizeof(struct message));
            
            /* Handle the message (send it and get a response) */
            handle(out_buf, receiver_ip, receiver_port, timeout);

            /* Free the message space */
            free(msg);
            
            /* Free the space allocated for the in buffer in getline() */
            free(in_buf);
        }
        /* Otherwise handle the queued messages */
        else
        {
            process_window(receiver_ip, receiver_port, timeout);
        }
 
    }
    
    return 0;
}
コード例 #6
0
ファイル: spectgen.c プロジェクト: amithash/spectro
static void spectgen_worker(void *_handle)
{
	float *decode_buffer;
	unsigned int decode_len;
	unsigned int frate;
	unsigned int old_frate = 0;
	unsigned int leftover_len = 0;
	float *buf = NULL;
	int i;
	struct spectgen_struct *handle = (struct spectgen_struct *)_handle;
	if(!handle)
	      goto bailout;

	
	while(1) {
		decode_len = 0;
		decoder_data_pull(handle->session->d_handle, 
					&decode_buffer, &decode_len, &frate);
		if(decode_len == 0)
		      break;

		/* Handle the case when the frame rate changes in the middle.
		 * This is an odd case, but we need to handle it */
		if(!handle->barkband_table_inited || old_frate != frate) {
			old_frate = frate;
			setup_barkband_table(handle, frate);
		}
		/* Compute the weighted sum, this is done in order to 
		 * calculate the average frame rate */
		handle->average_frate += (double)(frate * decode_len);
		handle->total_samples += (double)(decode_len);

		buf = decode_buffer;
		if(leftover_len > 0) {
			buf = malloc(sizeof(float) * (decode_len + leftover_len));
			if(!buf) {
				goto failed_in_loop;
			}
			memcpy(buf, handle->leftover, sizeof(float) * leftover_len);
			memcpy(&buf[leftover_len], decode_buffer, sizeof(float) * decode_len);
			decode_len += leftover_len;
			leftover_len = 0;
			free(decode_buffer);
		}
		if(decode_len >= handle->session->window_size) {
			for(i = 0; i < decode_len - handle->session->window_size; 
						i+=handle->step_size) {
				process_window(handle, &buf[i]);
			}
			if(i < decode_len) {
				memcpy(handle->leftover, &buf[i], 
					sizeof(float) * (decode_len - i));
				leftover_len = decode_len - i;
			}
		} else {
			memcpy(handle->leftover, buf, decode_len * sizeof(float));
			leftover_len = decode_len;
		}
		free(buf);
	}
bailout:
failed_in_loop:
	q_put(&handle->queue, NULL);
}