Esempio n. 1
0
int main(int argc,char **argv)
{
  int lines,samps,start_line;   /* Number of image lines and samples          */
  int x,y,ii;                   /* Counters and the filter frequency indicies */
  char *inName, *outName;       /* Input filename                             */
  char metaName[256];           /* Name of meta file                          */
  complexFloat *inBuf;          /* Input/Output Image Buffers                 */
  complexFloat *fftBuf;         /* FFT Buffer for the image                   */
  float *ampBuf,*phsBuf;        /* Amplitude and Phase Buffers                */
  float df, freq[FFT_LEN], f_s; /* Frequency Vector                           */
  FILE *inF,*outF1;             /* Input and Output file pointers             */
  meta_parameters *meta;        /* Meta-file data pointer                     */

/* Usage is shown if the user doesn't give correct number of arguments */
  if(argc!=4) { usage(argv[0]); }

  StartWatch();

/* Get the filename and filter start and end frequencies from the command line */ 
  inName  = argv[1];
  outName = argv[2];
  start_line = atoi(argv[3]);
  create_name(metaName, inName, ".meta");

/* Get the PRF and number of samples from the meta-file */
  meta = meta_read(metaName);
  lines = FFT_LEN;
  samps = meta->general->sample_count;
  f_s   = 1/(meta->sar->azimuth_time_per_pixel);
  printf("Sampling Frequency is %f\n",f_s);

/* Compute the FFT length based on the number of lines. Must be a power of 2 */
  printf("FFT Length is %d\n",FFT_LEN);
  cfft1d(FFT_LEN,NULL,0);

/* Allocate the memory for all the buffers */
  inBuf  = (complexFloat *)MALLOC(sizeof(complexFloat)*samps*FFT_LEN);
  fftBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*FFT_LEN);
  ampBuf = (float *)MALLOC(sizeof(float)*FFT_LEN);
  phsBuf = (float *)MALLOC(sizeof(float)*FFT_LEN);

  df = f_s/FFT_LEN;
  for(ii=0; ii<FFT_LEN; ii++)   freq[ii] = df*ii;

/* Open the Complex Image File */
  if((inF=FOPEN(inName,"rb"))==NULL) {	
    printf("Complex Image file %s could not be opened\n",inName);
    exit(EXIT_FAILURE);
  }

/* Zero out all the arrays and begin processing data */
  for(ii=0; ii<FFT_LEN; ii++) {
    ampBuf[ii]=0;
    phsBuf[ii]=0;
  }

/* Read in the data chunk */
  printf("Reading Data Starting at Line %d\n",start_line);
  get_complexFloat_lines(inF, meta, start_line, FFT_LEN, inBuf);

/* Process the each column, take the average at the end */
  printf("Performing the FFT\n");
  for(x=0; x<samps; x++) {
    if(x%500 == 0)  printf("Processing Column %d\n",x);

    for(y=0; y<FFT_LEN; y++) {
      fftBuf[y].real=0;
      fftBuf[y].imag=0;
    }

    for(y=0; y<lines; y++) {
      fftBuf[y].real=inBuf[y*samps+x].real;
      fftBuf[y].imag=inBuf[y*samps+x].imag;
    }

    cfft1d(FFT_LEN,fftBuf,-1);

    for (ii=0; ii<FFT_LEN; ii++) {
      ampBuf[ii] += sqrt(fftBuf[ii].real*fftBuf[ii].real
                         + fftBuf[ii].imag*fftBuf[ii].imag);	
      if(fftBuf[ii].imag!=0.0 || fftBuf[ii].real!=0.0)
        phsBuf[ii] += atan2(fftBuf[ii].imag, fftBuf[ii].real);
    }
  }
  printf("Finished the FFT\n");

/* Open and write output file */
  strcat(outName,".spectra"); 
  if((outF1=FOPEN(outName,"w"))==NULL) {
    printf("Unable to write output %s\n",outName);
    exit(EXIT_FAILURE);
  }
  for (ii=0; ii<FFT_LEN; ii++) {
    ampBuf[ii] /= samps;
    phsBuf[ii] /= samps;
    fprintf(outF1,"%f %f %f\n",freq[ii],ampBuf[ii],phsBuf[ii]);
  }

/* Close up & leave */
  meta_free(meta);
  FCLOSE(outF1);
  StopWatch();
  return 0;
}
Esempio n. 2
0
int main(int argc,char **argv)
{
  int lines,samps;                   /* Number of image lines and samples     */
  int fftLen,start_line;             /* FFT length, & line to start processing at*/
  int x,y,i,k;                       /* Counters                              */
  int f_lo1,f_lo2,f_hi1,f_hi2;       /* Filter frequency indicies             */
  int chunk_size,chunk_int;          /* Size of current datablock, & temp value*/
  int last_chunk;                    /* Size of the last chunk                */
  int compensate_for_last_chunk=1;   /* If last chunk = 0 dont loop for last chunk*/
  char *inName, *parmName, *outName; /* Input filename                        */
  float filtStart[2], filtEnd[2];    /* Filter start and stop variables       */
  float df, stop;                    /* Delta and counter variables           */
  complexFloat *inBuf,*outBuf;       /* Input/Output Image Buffers            */
  complexFloat *fftBuf;              /* FFT Buffer for the image              */
  float *ampBuf,*phsBuf;             /* Amplitude and Phase Buffers           */
  float *time_vector,A,B,shift;      /* Time vector, & freq modulation shift vars*/
  float chunk_float;                 /* Temporary value                       */
  FILE *inF, *freqF, *outF1;         /* Input and Output file pointers        */
  float cur_time, f_s;               /* Current time to increment the time vector by */
  meta_parameters *inMeta, *outMeta; /* Meta info about the images            */

/* Usage is shown if the user doesn't give 3 arguements */
  if(argc!=4) { usage(argv[0]); }

  StartWatch();
  printf("Program cpx_filter\n");

/* Get the filename and filter start and end frequencies from the command line*/ 
  inName=argv[1];
  if(findExt(inName)==NULL)
          inName = appendExt(argv[1],".cpx");
  outName=argv[2];
  parmName=argv[3];

/* Get input metadata. Make sure data_type is complex */
  inMeta = meta_read(inName);
  if (inMeta->general->data_type < COMPLEX_BYTE) {
   switch (inMeta->general->data_type) {
     case ASF_BYTE:      inMeta->general->data_type=COMPLEX_BYTE;      break;
     case INTEGER16: inMeta->general->data_type=COMPLEX_INTEGER16; break;
     case INTEGER32: inMeta->general->data_type=COMPLEX_INTEGER32; break;
     case REAL32:    inMeta->general->data_type=COMPLEX_REAL32;    break;
     case REAL64:    inMeta->general->data_type=COMPLEX_REAL64;    break;
     case COMPLEX_BYTE:
     case COMPLEX_INTEGER16:
     case COMPLEX_INTEGER32:
     case COMPLEX_REAL32:
     case COMPLEX_REAL64: break;
    }
    meta_write (inMeta, inName);
  }

/* Open the frequency parameter file and read the parameters */
  if((freqF=FOPEN(parmName,"r"))==NULL) {
    printf("Frequency Parameter File %s could not be Opened!\n",parmName);
    exit(EXIT_FAILURE);
  }
  fscanf(freqF,"%f\n",&f_s);
  fscanf(freqF,"%f\n",&shift);
  fscanf(freqF,"%f %f\n", &filtStart[0], &filtEnd[0]);
  fscanf(freqF,"%f %f\n", &filtStart[1], &filtEnd[1]);

  printf("\n");
  printf("Input file is %s\n",inName);
  printf("Output file is %s.cpx\n",outName);
  printf("Parameter file is %s\n",parmName);
  printf("Filtering from frequencies %.2f to %.2f Hz and %.2f to %.2f in Azimuth\n",filtStart[0],filtEnd[0],filtStart[1],filtEnd[1]);
  printf("The sampling frequency is %f Hz\n",f_s);
  printf("Shifting the spectrum by %.2f Hz\n",shift);

/* Get the number of lines and samples from the input meta file */
  lines = inMeta->general->line_count;
  samps = inMeta->general->sample_count;

  chunk_size  = BLOCK_SIZE;
  chunk_float = (float)lines/chunk_size;
  chunk_int   = lines/chunk_size;
  last_chunk  = (int)((chunk_float-(float)chunk_int) * (float)BLOCK_SIZE + 0.5);
  if( (2*TOSS_SIZE) > last_chunk)
    compensate_for_last_chunk=0;
  printf("Chunk Size is set to %d, the last chunk is %d lines\n",
         chunk_size, last_chunk);

/* Compute the FFT length based on the number of lines. Must be a power of 2 */
  i      = (log10(chunk_size)/log10(2)) + 1;
  fftLen = pow(2,i);
  printf("FFT Length is %d\n",fftLen);
  cfft1d(fftLen,NULL,0);

  printf("The Input Image has %d lines and %d samples\n",lines,samps);

/* Allocate the memory for all the buffers */
  inBuf  = (complexFloat *)MALLOC(sizeof(complexFloat)*samps*fftLen);
  outBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*samps*fftLen);
  fftBuf = (complexFloat *)MALLOC(sizeof(complexFloat)*fftLen);
  ampBuf = (float *)MALLOC(sizeof(float)*fftLen);
  phsBuf = (float *)MALLOC(sizeof(float)*fftLen);
  time_vector = (float *)MALLOC(sizeof(float)*lines);

/* Open the Complex Image File */
  if((inF=FOPEN(inName,"rb"))==NULL) {        
    printf("Complex Image file %s could not be opened\n",inName);
    exit(EXIT_FAILURE);
  }

  strcat(outName,".cpx"); 
  if((outF1=FOPEN(outName,"wb"))==NULL) {
    printf("Unable to write output %s\n",outName);
    exit(EXIT_FAILURE);
  }

  outMeta = meta_copy(inMeta);
  outMeta->general->line_count = 
          chunk_int*(BLOCK_SIZE-2*TOSS_SIZE)+(last_chunk-2*TOSS_SIZE);
  outMeta->general->sample_count = samps; 
  meta_write(outMeta, outName);


/* Find the filter frequency index values */
  df   = f_s/fftLen;
  stop = 0;
  i    = 0;
  while(stop<filtStart[0]) {
    f_lo1 = i;
    stop  = df*i;
    i++;
  }

  stop = 0;
  i    = 0;
  while(stop<filtStart[1]) {
    f_lo2=i;
    stop=df*i;
    i++;
  }

  i    = fftLen;
  stop = df*i;  
  while(stop>filtEnd[0]) {
    f_hi1 = i;
    stop  = df*i;   
    i--;
  }

  i    = fftLen;
  stop = df*i;
  while(stop>filtEnd[1]) {
    f_hi2 = i;
    stop  = df*i;
    i--;
  }

/* Zero out all the arrays and begin processing data */
  cur_time = 0;
  for(i=0; i<fftLen; i++)
  {
    ampBuf[i] = 0;
    phsBuf[i] = 0;
  }

  for(k=0; k<chunk_int+compensate_for_last_chunk; k++)
  {
    printf("\nProcessing Chunk %d of %d\n",k,lines/chunk_size);

    start_line = (k==0) ? 0 : (k*chunk_size)-(2*TOSS_SIZE);

    if (k==chunk_int)
      chunk_size=last_chunk;

    cur_time=start_line*(1/f_s);

  /* Read in the data chunk & put in proper endian order */
    printf("Reading %d Lines Starting at Line %d\n",chunk_size,start_line);
    get_complexFloat_lines(inF, inMeta, start_line, chunk_size, inBuf);

  /* Process the each column */
    printf("Performing the FFT and Filtering Operations\n");
    for(x=0; x<samps; x++)
    {
      if(x%1000 == 0)  printf("Processing Column %d\n",x);

      for(y=0;y<fftLen;y++)
      {
        fftBuf[y].real = 0;
        fftBuf[y].imag = 0;
      }

      for(y=0;y<chunk_size;y++)
      {
        fftBuf[y].real = inBuf[y*samps+x].real;
        fftBuf[y].imag = inBuf[y*samps+x].imag;
      }

      cfft1d(fftLen,fftBuf,-1);

      for (i=0; i<fftLen; i++) 
      {
        ampBuf[i] = sqrt(fftBuf[i].real*fftBuf[i].real
                    + fftBuf[i].imag*fftBuf[i].imag);        

        if(fftBuf[i].imag!=0.0 || fftBuf[i].real!=0.0)
          phsBuf[i] = atan2(fftBuf[i].imag,fftBuf[i].real);
        else
          phsBuf[i] = 0;

        if(((i>f_lo1)&&(i<f_hi1)) || ((i>f_lo2) && (i<f_hi2)))
        {
          ampBuf[i] = 0;
          phsBuf[i] = 0;
        }

        fftBuf[i].real = ampBuf[i]*cos(phsBuf[i]);
        fftBuf[i].imag = ampBuf[i]*sin(phsBuf[i]);        
      }

      cfft1d(fftLen,fftBuf,1);

      for(i=0;i<chunk_size;i++)
      {        
        outBuf[i*samps+x].real = fftBuf[i].real;
        outBuf[i*samps+x].imag = fftBuf[i].imag;
      }        
    }
    printf("Finished the FFT and Filtering Operations\n");

  /* Perform the time-domain frequency shift */ 
    if(shift != 0.0)
    {
      for(i=0; i<chunk_size; i++)
        time_vector[i] = cur_time+(1/f_s)*i;

      printf("\nPerforming time-domain frequency shift of %.2f Hz\n",shift);
      for(y=0; y<chunk_size; y++)
      {
        for(x=0; x<samps; x++)
        {
          A = outBuf[y*samps+x].real;
          B = outBuf[y*samps+x].imag;
          outBuf[y*samps+x].real = A*cos(2*pi*shift*time_vector[y])
                                   - B*sin(2*pi*shift*time_vector[y]);
          outBuf[y*samps+x].imag = B*cos(2*pi*shift*time_vector[y])
                                   + A*sin(2*pi*shift*time_vector[y]);
        }
      }
    }

  /* Write out the data file in big endian format */
    printf("Writing the output lines %d to %d in file %s\n",
      start_line, start_line+chunk_size-TOSS_SIZE, outName);
    put_complexFloat_lines(outF1, outMeta, start_line,
      samps*(chunk_size-TOSS_SIZE), outBuf+(samps*TOSS_SIZE));
  }

  printf("\n");

  FCLOSE(outF1);
  StopWatch();
  return 0;
}
Esempio n. 3
0
void TSAppSrvFun::Exec(const TStrKdV& FldNmValPrV, const PSAppSrvRqEnv& RqEnv) {
	const PNotify& Notify = RqEnv->GetWebSrv()->GetNotify();
	PHttpResp HttpResp;
	try {
        // log the call
        Notify->OnStatus(TStr::Fmt("[AppSrv] RequestStart %s", FunNm.CStr()));
		TTmStopWatch StopWatch(true);
		// execute the actual function, according to the type
		PSIn BodySIn; TStr ContTypeVal;
		if (GetFunOutType() == saotXml) {
			PXmlDoc ResXmlDoc = ExecXml(FldNmValPrV, RqEnv);        
			TStr ResXmlStr; ResXmlDoc->SaveStr(ResXmlStr);
			BodySIn = TMIn::New(XmlHdStr + ResXmlStr);
			ContTypeVal = THttp::TextXmlFldVal;
		} else if (GetFunOutType() == saotJSon) {
			TStr ResStr = ExecJSon(FldNmValPrV, RqEnv);
			BodySIn = TMIn::New(ResStr);
			ContTypeVal = THttp::AppJSonFldVal;
		} else {
			BodySIn = ExecSIn(FldNmValPrV, RqEnv, ContTypeVal);
		}
		// log finis of the call
		Notify->OnStatus(TStr::Fmt("[AppSrv] RequestFinish %s [%d ms]", 
				FunNm.CStr(), StopWatch.GetMSecInt()));
		// prepare response
		HttpResp = THttpResp::New(THttp::OkStatusCd, 
			ContTypeVal, false, BodySIn);
    } catch (PExcept Except) {
		// known internal error
		TStr ResStr;
		if (GetFunOutType() == saotXml) {
			PXmlTok TopTok = TXmlTok::New("error");
			TopTok->AddSubTok(TXmlTok::New("message", Except->GetMsgStr()));
			TopTok->AddSubTok(TXmlTok::New("location", Except->GetLocStr()));
			PXmlDoc ErrorXmlDoc = TXmlDoc::New(TopTok); 
			ErrorXmlDoc->SaveStr(ResStr);
		} else if (GetFunOutType() == saotJSon) {
			PJsonVal ResVal = TJsonVal::NewObj();
			ResVal->AddToObj("message", Except->GetMsgStr());
			ResVal->AddToObj("location", Except->GetLocStr());
			ResStr = TJsonVal::GetStrFromVal(TJsonVal::NewObj("error", ResVal));
		}
		// prepare response
		HttpResp = THttpResp::New(THttp::InternalErrStatusCd, 
            THttp::TextHtmlFldVal, false, 
			TMIn::New(XmlHdStr + ResStr));
    } catch (...) {
		// unknown internal error
		TStr ResStr;
		if (GetFunOutType() == saotXml) {
			PXmlDoc ErrorXmlDoc = TXmlDoc::New(TXmlTok::New("error")); 
			ErrorXmlDoc->SaveStr(ResStr);
		} else if (GetFunOutType() == saotJSon) {
			PJsonVal ResVal = TJsonVal::NewObj("error", "Unknown");
			ResStr = TJsonVal::GetStrFromVal(ResVal);
		}
		// prepare response
        HttpResp = THttpResp::New(THttp::InternalErrStatusCd, 
            THttp::TextHtmlFldVal, false, 
			TMIn::New(XmlHdStr + ResStr));
    }
	// send response
	RqEnv->GetWebSrv()->SendHttpResp(RqEnv->GetSockId(), HttpResp); 
}
Esempio n. 4
0
int main (int argc, char *argv [])
{
/*Structures: these are passed to the sub-routines which need them.*/
    patch *p1,*p2;
    satellite *s;
    rangeRef *r;
    getRec *signalGetRec;
    file *f;
/*Variables.*/
    int n_az,n_range;/*Region to be processed.*/
    int patchNo;/*Loop counter.*/
    double pixShift,dopDel,old_dop;
    int refLen,lineToBeRead;
    int saved_NLA;
    struct ARDOP_PARAMS params,old_params;
    meta_parameters *meta;

    StartWatch();
    if (!quietflag) printf("   Doppler Determination:\n");
    if (logflag) printLog("   Doppler Determination:\n");
    if (!parse_cla(argc,argv,&params,&meta))
      printErr("   ERROR: Usage: dop_prf as ardop\n");

/*Set the initial doppler parameters.*/
    if (!quietflag) printf("   Processing to doppler at %f prf...\n",params.fd);
    if (logflag) {
      sprintf(logbuf,"   Processing to doppler at %f prf...\n",params.fd);
      printLog(logbuf);
    }
    read_params(params.in1,&old_params);/*Read in user's old parameters*/

/*Set params so we're processing at least 300 samples and 500 lines.*/
    refLen=params.fs*params.pulsedur;
    params.nla=smallestPow2(300+refLen)-refLen;
    params.na_valid=500;
    ardop_setup(&params,meta,&n_az,&n_range,&s,&r,&f,&signalGetRec);

    if (!quietflag) {
      printf("   Processing a %d az by %d range patch...\n",n_az,n_range);
      printf("   Of the %d azimuth lines, only %d are valid.\n",n_az,f->n_az_valid);
    }

/*
Create "patch"es of data.
*/
    p1=newPatch(n_az,n_range);
    p2=newPatch(n_az,n_range);

/*Process both patches.*/
    lineToBeRead = f->firstLineToProcess;

    ac_direction=-1;
    setPatchLoc(p1,s,f->skipFile,f->skipSamp,lineToBeRead);/*Update patch parameters for location.*/
    processPatch(p1,signalGetRec,r,s);/*SAR Process patch.*/

    ac_direction=1;
    setPatchLoc(p2,s,f->skipFile,f->skipSamp,lineToBeRead);/*Update patch parameters for location.*/
    processPatch(p2,signalGetRec,r,s);/*SAR Process patch.*/

/*Do range amplitude correlation.*/
    pixShift=amp_corr(p1,p2,f);
    StopWatch();

    dopDel=2*pixShift*f->rngpix/(s->wavl*s->refPerRange/2*(p1->slantToFirst+p1->slantPer*p1->n_range/2));
    if (!quietflag) printf("   The image's doppler is off by %.2f pixels, or %.2f PRF.\n",
                           pixShift,dopDel);
    if (logflag) {
      sprintf(logbuf,"   The image's doppler is off by %.2f pixels, or %.2f PRF.\n",
        pixShift,dopDel);
      printLog(logbuf);
    }

    /*dopDel-=0.2; <-- Constant frequency offset?*/
    dopDel=floor(dopDel+0.5);

    if (!quietflag) printf("   The doppler coefficients should be:\n"
                           "   %.10f %.10f %.10f\n\n",s->orig_fd-dopDel,s->orig_fdd,s->orig_fddd);
    if (!quietflag) printf("   These coefficients have been written into '%s'\n",appendExt(params.in1,".in"));
    if (logflag) {
      sprintf(logbuf,"   The doppler coefficients should be:\n"
              "   %.10f %.10f %.10f\n\n",s->orig_fd-dopDel,s->orig_fdd,s->orig_fddd);
      printLog(logbuf);
      sprintf(logbuf,"   These coefficients have been written into '%s'\n",appendExt(params.in1,".in"));
      printLog(logbuf);
    }

    old_params.fd=s->orig_fd-dopDel;
    old_params.fdd=s->orig_fdd;
    old_params.fddd=s->orig_fddd;

    print_params(params.in1,&old_params,"dop_prf");

    destroyPatch(p1);
    destroyPatch(p2);

    return(0);
}