int average_real(struct mark5_stream *ms, int *nused, uint64_t nint, uint64_t *navg, double *avif, double **data) { int status; int nif = ms->nchan; if (*nused<0 || *nused>=ChunkSize) { // No data available status = mark5_stream_decode_double(ms, ChunkSize, data); if(status < 0) { return(status); } if(ms->consecutivefails > 5) { printf("Too many failures. consecutive, total fails = %d %d\n", ms->consecutivefails, ms->nvalidatefail); return(-1); } *nused = 0; } int i; for (i=*nused; i<ChunkSize; i++) { int j; for (j=0; j<nif; j++) { avif[j] += data[j][i]*data[j][i]; } (*nused)++; (*navg)++; if (*navg>=nint) return(0); } return(0); }
int feedDataStream(DataStream *ds) { int i, status; double scale; status = mark5_stream_decode_double(ds->ms, ds->fftSize, ds->data); if(status < 0) { return status; } fftw_execute(ds->plan); scale = 1.0/(ds->fftSize); if(ds->nChan > 0) { for(i = 0; i < ds->nChan; i++) { ds->spec[i] = ds->zdata[ds->startChan+i]*scale; } } else { for(i = 0; i < -ds->nChan; i++) { /* FIXME : I think this conjugation is needed! -WFB 20100806 */ ds->spec[i] = ~ds->zdata[ds->startChan-i]*scale; } } return 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; }