void os2d(complexFloat *v, complexFloat *vo, int dim, int os) { int i; int forward_fft = 1; int inverse_fft = -1; int osize; /* printf("os2d(): dim = %d\tos = %d\n", dim, os); */ /* calculate total size of oversampled array */ osize = dim*dim*os*os; /* printf("os2d(): total size of oversampled array (osize) = %d\n", osize); */ /* 2-D fft vector 'v' */ fft2d (v, dim, forward_fft); /* make sure 'vo' = 0.0 */ for (i = 0; i < osize; i++) *(vo+i) = Czero(); /* zero pad 'v' into 'vout' */ zeroPad(v, vo, dim, os); /* inverse FFT 'vo' */ fft2d (vo, dim*os, inverse_fft); return; }
Polynom::Polynom(Complex<BigInt> value, int deg) { coefB = new Complex<BigInt>[1000]; BigInt zero(0); Complex<BigInt> Czero(zero, zero); for (int i = 0; i < 1000; i++) coefB[i] = Czero; coefB[deg] = value; degree = deg; coefI = 0; coefR = 0; coefD = 0; }
void estdop(char file[],int nDopLines, float *a, float *b,float *c) { #define MULTILOOK 100 complexFloat *signal, *signalNext, *dop; float *x_vec, *y_vec,*phase; int x,line,firstLine; getRec *r; float t1, t2, t3; long double sum; float lastPhase; r=fillOutGetRec(file); firstLine=(r->nLines-nDopLines)/2; signal=(complexFloat *)MALLOC(sizeof(complexFloat)*r->nSamples); signalNext=(complexFloat *)MALLOC(sizeof(complexFloat)*r->nSamples); dop=(complexFloat *)MALLOC(sizeof(complexFloat)*r->nSamples); x_vec=(float *)MALLOC(sizeof(float)*r->nSamples); y_vec=(float *)MALLOC(sizeof(float)*r->nSamples); phase=(float *)MALLOC(sizeof(float)*r->nSamples); for (x = 0; x<r->nSamples; x++) dop[x] = Czero(); getSignalLine(r,firstLine,signalNext,0,r->nSamples); for (line = firstLine+1; line < firstLine+nDopLines; line++) { complexFloat *ptr = signal; signal = signalNext; signalNext = ptr; getSignalLine(r,line,signalNext,0,r->nSamples); for (x = 0; x<r->nSamples; x++) dop[x] = Cadd(dop[x],Cmul(signalNext[x],Cconj(signal[x]))); } /*Multilook the phase data along range.*/ for (x=0;x<r->nSamples/MULTILOOK;x++) { int i; double out_r=0.0,out_i=0.0; for (i=0;i<MULTILOOK;i++) { out_r+=dop[x*MULTILOOK+i].real; out_i+=dop[x*MULTILOOK+i].imag; } dop[x].real = out_r; dop[x].imag = out_i; } /*Phase-unwrap the doppler values into the "phase" array.*/ lastPhase=0; for (x=0;x<r->nSamples/MULTILOOK;x++) { float nextPhase=atan2(dop[x].imag, dop[x].real)*(1.0/(2.0*pi)); while ((nextPhase-lastPhase)<-0.5) nextPhase+=1.0; while ((nextPhase-lastPhase)>0.5) nextPhase-=1.0; phase[x]=nextPhase; lastPhase=nextPhase; } sum = 0.0; for (x = 0; x<r->nSamples/MULTILOOK; x++) { sum += phase[x]; x_vec[x] = x*MULTILOOK; y_vec[x] = phase[x]; } // Don't output this file for now since none of our other software uses it // This file causes the windows uninstaller not to fully uninstall // if (1) /*Output doppler vs. range*/ // { // FILE *f=FOPEN("dop_vs_rng","w"); // for (x=0;x<r->nSamples/MULTILOOK;x++) // fprintf(f,"%.0f\t%f\n",x_vec[x],y_vec[x]); // fclose(f); // } sum = sum / (r->nSamples/MULTILOOK); if (!quietflag) printf(" Constant Average : y = %f\n",(float)sum); if (logflag) { sprintf(logbuf," Constant Average : y = %f\n",(float)sum); printLog(logbuf); } yaxb(x_vec, y_vec, r->nSamples/MULTILOOK, &t1, &t2); if (!quietflag) printf(" Linear Regression : y = %f x + %f\n",t1,t2); if (logflag) { sprintf(logbuf," Linear Regression : y = %f x + %f\n",t1,t2); printLog(logbuf); } yax2bxc(x_vec, y_vec, r->nSamples/MULTILOOK, &t1, &t2, &t3); if (!quietflag) printf(" Quadratic Regression: y = %f x^2 + %f x + %f\n",t1,t2,t3); if (logflag) { sprintf(logbuf," Quadratic Regression: y = %f x^2 + %f x + %f\n",t1,t2,t3); printLog(logbuf); } *a = t3; *b = t2; *c = t1; free((void *)signal); free((void *)signalNext); free((void *)dop); free((void *)x_vec); free((void *)y_vec); free((void *)phase); freeGetRec(r); return; }
/* Start of main progam */ int main(int argc, char *argv[]) { char szOut[255], szImg1[255], szImg2[255], *maskFile; int x1, x2, y1, y2; int goodPoints=0, attemptedPoints=0; FILE *fp_output; meta_parameters *masterMeta, *slaveMeta; int logflag=FALSE, ampFlag=TRUE, currArg=1; while (currArg < (argc-NUM_ARGS)) { char *key = argv[currArg++]; if (strmatches(key,"-log","--log",NULL)) { CHECK_ARG(1); strcpy(logFile,GET_ARG(1)); fLog = FOPEN(logFile, "a"); logflag = TRUE; } else if (strmatches(key,"-mask","--mask",NULL)) { CHECK_ARG(1); maskFile = GET_ARG(1); } else { printf("\n**Invalid option: %s\n", argv[currArg-1]); usage(argv[0]); } } if ((argc-currArg) < NUM_ARGS) { printf("Insufficient arguments.\n"); usage(argv[0]); } // Fetch required arguments strcpy(szImg1,argv[argc - 3]); strcpy(szImg2,argv[argc - 2]); strcpy(szOut, argv[argc - 1]); /* Read metadata */ masterMeta = meta_read(szImg1); slaveMeta = meta_read(szImg2); if (masterMeta->general->line_count != slaveMeta->general->line_count || masterMeta->general->sample_count != slaveMeta->general->sample_count) asfPrintWarning("Input images have different dimension!\n"); else if (masterMeta->general->data_type != slaveMeta->general->data_type) asfPrintError("Input image have different data type!\n"); else if (masterMeta->general->data_type > 5 && masterMeta->general->data_type != COMPLEX_REAL32) asfPrintError("Cannot compare raw images for offsets!\n"); lines = masterMeta->general->line_count; samples = masterMeta->general->sample_count; if (masterMeta->general->data_type == COMPLEX_REAL32) { srcSize = 32; ampFlag = FALSE; cZero = Czero(); } /* Create output file */ fp_output=FOPEN(szOut, "w"); /* Loop over grid, performing forward and backward correlations */ while (getNextPoint(&x1,&y1,&x2,&y2)) { float dx=0.0, dy=0.0, snr=0.0, dxFW, dyFW, snrFW, dxBW, dyBW, snrBW; attemptedPoints++; // Check bounds and mask if (!(outOfBounds(x1, y1, srcSize, maskFile))) { /* ...check forward correlation... */ if (ampFlag) { if (!(findPeak(x1,y1,szImg1,x2,y2,szImg2,&dxFW,&dyFW,&snrFW))) { attemptedPoints--; continue; /* next point if chip in complete background fill */ } } else { getPeak(x1,y1,szImg1,x2,y2,szImg2,&dxFW,&dyFW,&snrFW); } if ((!ampFlag && snrFW>minSNR) || (ampFlag)) { /* ...check backward correlation... */ if (ampFlag) { if (!(findPeak(x2,y2,szImg2,x1,y1,szImg1,&dxBW,&dyBW,&snrBW))) { attemptedPoints--; continue; /* next point if chip in complete background fill */ printf("dxFW: %.2f, dyFW: %.2f\n", dxFW, dyFW); } } else { getPeak(x2,y2,szImg2,x1,y1,szImg1,&dxBW,&dyBW,&snrBW); } dxBW*=-1.0;dyBW*=-1.0; if (((!ampFlag && snrFW>minSNR) || (ampFlag)) && (fabs(dxFW-dxBW) < maxDisp) && (fabs(dyFW-dyBW) < maxDisp)) { dx = (dxFW+dxBW)/2; dy = (dyFW+dyBW)/2; snr = snrFW*snrBW; if (dx < maxDxDy && dy < maxDxDy) goodPoints++; } } fprintf(fp_output,"%6d %6d %8.5f %8.5f %4.2f\n", x1, y1, x2+dx, y2+dy, snr); fflush(fp_output); } } if (goodPoints < attemptedPoints) printf("\n WARNING: %i out of %i points moved by " "more than one pixel!\n\n", (attemptedPoints-goodPoints), attemptedPoints); else printf("\n There is no difference between the images\n\n"); FCLOSE(fp_output); FREE(masterMeta); FREE(slaveMeta); return(0); }
/******* amp_corr: Given two patches, p1 containing the echos from ahead of beam center, p2 containing echos from behind beam center; performs an amplitude cross-correlation in range and returns the range offset, in pixels, between these two images. */ double amp_corr(patch *p1,patch *p2,file *f) { int clip=20;/*Remove this from the far-range end of the images*/ int overlap=30;/*Overlap is the maximum shift we ever expect.*/ FILE *fout; int x,y; int fftLen=smallestPow2(p1->n_range); float scale=1.0/fftLen; complexFloat *amp1,*amp2; float *corr; double ret; amp1=(complexFloat *)MALLOC(sizeof(complexFloat)*fftLen); amp2=(complexFloat *)MALLOC(sizeof(complexFloat)*fftLen); corr=(float *)MALLOC(sizeof(float)*(fftLen+overlap)); if (!quietflag) printf(" Performing amplitude correlation.\n"); for (x=0;x<fftLen+overlap;x++) corr[x]=0.0; cfft1d(fftLen,NULL,0); p1->n_range-=clip; p2->n_range-=clip; for (y=f->firstOutputLine;y<f->firstOutputLine+f->n_az_valid;y++) { for (x=0;x<overlap;x++) { amp1[x]=Czero(); amp2[x].r=Cabs(p2->trans[x*p2->n_az+y])*scale; amp2[x].i=0.0; } for (;x<p1->n_range-overlap;x++) { amp1[x].r=Cabs(p1->trans[x*p1->n_az+y])*scale; amp1[x].i=0.0; amp2[x].r=Cabs(p2->trans[x*p2->n_az+y])*scale; amp2[x].i=0.0; } for (;x<p1->n_range;x++) { amp1[x]=Czero(); amp2[x].r=Cabs(p2->trans[x*p2->n_az+y])*scale; amp2[x].i=0.0; } for (;x<fftLen;x++) amp1[x]=amp2[x]=Czero(); cfft1d(fftLen,amp1,-1); cfft1d(fftLen,amp2,-1); for (x=0;x<fftLen;x++) amp2[x]=Cmul(amp2[x],Cconj(amp1[x])); cfft1d(fftLen,amp2,1); for (x=0;x<fftLen;x++) corr[x]+=Cabs(amp2[x]); } fout=FOPEN(f->out_cpx,"w"); for (x=-overlap;x<overlap;x++) { corr[x+fftLen]=corr[(x+fftLen)%fftLen]; if (corr[x+fftLen]<10000000000000000000000.0) /*Is a good value-- print it out*/ fprintf(fout,"%d %f\n",x,corr[x+fftLen]); else/*Must be an infinity or NaN*/ { fprintf(stderr, " WARNING!! Found infinity at offset %d!\n",x); if (logflag) { sprintf(logbuf," WARNING!! Found infinity at offset %d!\n",x); printLog(logbuf); } corr[x+fftLen]=0.0; } } FCLOSE(fout); FREE(amp1); FREE(amp2); ret = dop_findPeak(corr,fftLen-overlap,2*overlap)-fftLen; FREE(corr); return ret; }
/**************************************** getSignalLine: Fetches and unpacks a single line of signal data into the given array. */ void getSignalLine(const getRec *r,long long lineNo,complexFloat *destArr,int readStart,int readLen) { int x; int left,leftClip,rightClip; int windowShift=0; float agcScale=1.0; complexFloat czero=Czero(); /*If the line is out of bounds, return zeros.*/ if ((lineNo>=r->nLines)||(lineNo<0)) { for (x=0;x<readLen;x++) destArr[x]=czero; return; } /*Fetch window shift and AGC comp. if possible*/ if (r->lines!=NULL) { windowShift=r->lines[lineNo].shiftBy; agcScale=r->lines[lineNo].scaleBy; } /*Compute which part of the line we'll read in.*/ leftClip=left=readStart-windowShift; if (leftClip<0) {leftClip=0; /*left=0;*/} rightClip=left+readLen; if (rightClip>r->nSamples) rightClip=r->nSamples; /*Read line of raw signal data.*/ FSEEK64(r->fp_in,r->header+lineNo*r->lineSize+leftClip*r->sampleSize,0); if (rightClip-leftClip!= fread(r->inputArr,r->sampleSize,rightClip-leftClip,r->fp_in)) { sprintf(errbuf," ERROR: Problem reading signal data file on line %lld!\n",lineNo); /* fprintf(stderr,"Read length = %d, Left = %d Readstart = %d\n",readLen, left,readStart); fprintf(stderr,"Lines = %lld Samples = %lld, rightClip = %d, leftClip = %d window = %d\n" ,r->nLines, r->nSamples, rightClip, leftClip, windowShift); */ printErr(errbuf); } leftClip-=left; rightClip-=left; /*Fill the left side with zeros.*/ for (x=0;x<leftClip;x++) destArr[x]=czero; /*Unpack the read-in data into destArr:*/ if (r->flipIQ=='y') /*is CCSD data (one byte Q, next byte I)*/ for (x=leftClip;x<rightClip;x++) { int index=2*(x-leftClip); destArr[x].real=agcScale*(r->inputArr[index+1]-r->dcOffsetQ); destArr[x].imag=agcScale*(r->inputArr[index]-r->dcOffsetI); } else /*if (r->flipIQ=='n')*/ /*is Raw data (one byte I, next byte Q)*/ for (x=leftClip;x<rightClip;x++) { int index=2*(x-leftClip); destArr[x].real=agcScale*(r->inputArr[index]-r->dcOffsetI); destArr[x].imag=agcScale*(r->inputArr[index+1]-r->dcOffsetQ); } /*Fill the right side with zeros.*/ for (x=rightClip;x<readLen;x++) destArr[x]=czero; }