Example #1
0
static int timeaverage(const char *filename, const char *formatname, double tint, double time,
		       const char *outfile, long long offset) {
  struct mark5_stream *ms;
  double **data=0, *avif;
  double complex **cdata=0;
  int i, status=0, nused;
  int nif;
  uint64_t totalsamples, nint, samplesDone, navg;
  FILE *out;
  int docomplex = 0;

  //total = unpacked = 0;

  ms = new_mark5_stream(new_mark5_stream_file(filename, offset),
			new_mark5_format_generic_from_string(formatname));

  if(!ms) {
    printf("Error: problem opening %s\n", filename);
    return EXIT_FAILURE;
  }

  mark5_stream_print(ms);

  if (ms->complex_decode != 0)  {
    printf("Complex decode\n");
    docomplex = 1;
  }

  out = fopen(outfile, "w");
  if(!out) {
    fprintf(stderr, "Error: cannot open %s for write\n", outfile);
    delete_mark5_stream(ms);
    return EXIT_FAILURE;
  }

  //R = nbin*freq/ms->samprate;

  nif = ms->nchan;
  totalsamples = (ms->samprate * time);  // Total number of samples to process
  nint  = (ms->samprate * tint/1000.0);  // Samples per itergration

  printf("DEBUG: %Ld samples per integration\n", (long long)nint);
  printf("DEBUG: %Ld total number of samples\n", (long long)totalsamples);

  if (docomplex)  {
    cdata = (complex double **)malloc(nif*sizeof(double complex*));
    for(i = 0; i < nif; i++) {
      cdata[i] = (complex double *)malloc(ChunkSize*sizeof(complex double));
    }
  } else {
    data = (double**)malloc(nif*sizeof(double*));
    for(i = 0; i < nif; i++) {
      data[i] = (double *)malloc(ChunkSize*sizeof(double));
    }
  } 
  avif = (double*)calloc(nif,sizeof(double));
	    
  if (ms->ns < 0 || ms->ns > 1000000000) {
    fflush(stdout);
    fprintf(stderr, "\n***Warning*** The nano-seconds portion of the timestamp is nonsensable: %d; continuing anyway, but don't expect the time alignment to be meaningful.\n\n", ms->ns);
  }
	    
  int iInt = 0;
  samplesDone = 0;
  navg = 0;
  nused = -1;
  while (samplesDone<totalsamples) {
    if(die) break;

    if (docomplex) {
      status = average_complex(ms, &nused, nint, &navg, avif, cdata);
    } else {
      status = average_real(ms, &nused, nint, &navg, avif, data);
    }
    if (status<0) {
      break;
    }

    if (navg>=nint) {
      fprintf(out, "%4d %10.6f", iInt, iInt*nint/(double)ms->samprate);
      iInt++;
      int i;
      for(i=0; i<nif; i++) {
	avif[i] /= navg;
	fprintf(out, " %1f", avif[i]);
	avif[i] = 0;
      }
      fprintf(out, "\n");
      samplesDone += navg;
      navg = 0;
    }
  }
  fclose(out);

  for(i = 0; i < nif; i++) {
    if (docomplex)
      free(cdata[i]);
    else
      free(data[i]);
  }
  if (docomplex)
    free(cdata);
  else
    free(data);
  free(avif);
  delete_mark5_stream(ms);

  if(status >= 0)
    status = EXIT_SUCCESS;
  return status;
}
Example #2
0
static int fold(const char *filename, const char *formatname, int nbin, int nint,
	double freq, const char *outfile, long long offset)
{
	struct mark5_stream *ms;
	double **data=0, **bins=0;
	double complex **cdata=0;
	int **weight;
	int c, i, j, k, status;
	int nif, bin;
	long long total, unpacked;
	FILE *out;
	double R;
	long long sampnum;
	int docorrection = 1;
	int docomplex = 0;

	if(nbin < 0)
	{
		nbin = -nbin;
		docorrection = 0;
	}

	total = unpacked = 0;

	ms = new_mark5_stream(
		new_mark5_stream_file(filename, offset),
		new_mark5_format_generic_from_string(formatname) );

	if(!ms)
	{
		printf("Error: problem opening %s\n", filename);

		return EXIT_FAILURE;
	}

	if(ms->nbit < 2)
	{
		fprintf(stderr, "Warning: 1-bit data supplied.  Results will be\n");
		fprintf(stderr, "useless.  Proceeding anyway!\n\n");
	}

	if(ms->nbit > 2)
	{
		fprintf(stderr, "More than 2 bits: power not being corrected!\n");
		docorrection = 0;
	}

	mark5_stream_print(ms);

	if (ms->complex_decode != 0) 
	  {
	    printf("Complex decode\n");
	    docomplex = 1;
	  }

	sampnum = (long long)((double)ms->ns*(double)ms->samprate*1.0e-9 + 0.5);

	out = fopen(outfile, "w");
	if(!out)
	{
		fprintf(stderr, "Error: cannot open %s for write\n", outfile);
		delete_mark5_stream(ms);

		return EXIT_FAILURE;
	}

	R = nbin*freq/ms->samprate;

	nif = ms->nchan;

	if (!docomplex) 
	  {
	    data = (double **)malloc(nif*sizeof(double *));
	    bins = (double **)malloc(nif*sizeof(double *));
	    weight = (int **)malloc(nif*sizeof(int *));
	    for(i = 0; i < nif; i++)
	      {
		data[i] = (double *)malloc(ChunkSize*sizeof(double));
		bins[i] = (double *)calloc(nbin, sizeof(double));
		weight[i] = (int *)calloc(nbin, sizeof(int));
	      }
	    
	    if(ms->ns < 0 || ms->ns > 1000000000)
	      {
		fflush(stdout);
		fprintf(stderr, "\n***Warning*** The nano-seconds portion of the timestamp is nonsensable: %d; continuing anyway, but don't expect the time alignment to be meaningful.\n\n", ms->ns);
		
		sampnum = 0;
	      }
	    
	    for(j = 0; j < nint; j++)
	      {
		if(die)
		  {
		    break;
		  }
		
		status = mark5_stream_decode_double(ms, ChunkSize, data);
		
		if(status < 0)
		  {
		    break;
		  }
		else
		  {
		    total += ChunkSize;
		    unpacked += status;
		  }
		
		if(ms->consecutivefails > 5)
		  {
		    printf("Too many failures.  consecutive, total fails = %d %d\n", ms->consecutivefails, ms->nvalidatefail);
			
		    break;
		  }

		for(k = 0; k < ChunkSize; k++)
		  {
		    if(data[0][k] != 0.0)
		      {
			bin = ((long long)(sampnum*R)) % nbin;
			for(i = 0; i < nif; i++)
			  {
			    bins[i][bin] += data[i][k]*data[i][k];
			    weight[i][bin]++;
			  }
		      }
		    sampnum++;
		  }
	      }
	  } 
	else 
	  {
	    cdata = (complex double **)malloc(nif*sizeof(double complex*));
	    bins = (double **)malloc(nif*sizeof(double *));
	    weight = (int **)malloc(nif*sizeof(double *));
	    for(i = 0; i < nif; i++)
	      {
		cdata[i] = (complex double *)malloc(ChunkSize*sizeof(complex double));
		bins[i] = (double *)calloc(nbin, sizeof(double));
		weight[i] = (int *)calloc(nbin, sizeof(int));
	      }
	    
	    if(ms->ns < 0 || ms->ns > 1000000000)
	      {
		fflush(stdout);
		fprintf(stderr, "\n***Warning*** The nano-seconds portion of the timestamp is nonsensable: %d; continuing anyway, but don't expect the time alignment to be meaningful.\n\n", ms->ns);
		
		sampnum = 0;
	      }
	    
	    for(j = 0; j < nint; j++)
	      {
		if(die)
		  {
		    break;
		  }
		
		status = mark5_stream_decode_double_complex(ms, ChunkSize, cdata);
		
		if(status < 0)
		  {
		    break;
		  }
		else
		  {
		    total += ChunkSize;
		    unpacked += status;
		  }
		
		if(ms->consecutivefails > 5)
		  {
		    printf("Too many failures.  consecutive, total fails = %d %d\n", ms->consecutivefails, ms->nvalidatefail);
			
		    break;
		  }

		for(k = 0; k < ChunkSize; k++)
		  {
		    if(cdata[0][k] != 0.0)
		      {
			bin = ((long long)(sampnum*R)) % nbin;
			for(i = 0; i < nif; i++)
			  {
			    bins[i][bin] += creal(cdata[i][k])*creal(cdata[i][k]) +
			                   cimag(cdata[i][k])*cimag(cdata[i][k]);
			    weight[i][bin]++;
			  }
		      }
		    sampnum++;
		  }
	      }
	  }

	fprintf(stderr, "%lld / %lld samples unpacked\n", unpacked, total);

	/* normalize */
	for(k = 0; k < nbin; k++)
	{
		for(i = 0; i < nif; i++)
		{
			if(weight[i][k]) 
			{
				bins[i][k] /= weight[i][k];

			}
		}
	}

	/* convert the mean quantized voltage squared to nominal power */
	if(docorrection)
	{
		for(k = 0; k < nbin; k++)
		{
			for(i = 0; i < nif; i++)
			{
				bins[i][k] = correct_2bit_power(bins[i][k]);
			}
		}
	}

	for(c = 0; c < nbin; c++)
	{
		fprintf(out, "%11.9f ", c/(freq*nbin));
		for(i = 0; i < nif; i++)
		{
			fprintf(out, " %f", bins[i][c]);
		}
		fprintf(out, "\n");
	}

	fclose(out);

	for(i = 0; i < nif; i++)
	{
	  if (docomplex)
		free(cdata[i]);
	  else
		free(data[i]);
	  free(bins[i]);
	  free(weight[i]);
	}
	if (docomplex)
	  free(cdata);
	else
	  free(data);
	free(bins);
	free(weight);

	delete_mark5_stream(ms);

	return EXIT_SUCCESS;
}