int main(int argc, char *argv[]) {
  char *outfile=NULL, buf[256];
  struct stat statbuf;
  int nbytes,nread,nsamples;
  std::string tmpbuf("");
  int i=0,j;

  if ((argc < 2) || (argc > 3)) {
    fprintf(stderr,"%s infile [outfile]\n",argv[0]);
    exit(1);
  }
  char *infile=argv[1];
  if (argc==3) {
    outfile=argv[2];
  } 
  FILE *in=fopen(infile,"r");
  FILE *out;
  if (outfile) {
    out=fopen(outfile,"w");
  } else {
    out=stdout;
  }
  stat(infile,&statbuf);
  nbytes=statbuf.st_size;
  fseek(in,0,SEEK_SET);
  tmpbuf.reserve(nbytes);
  // read entire file into a buffer.
  while ((nread=(int)fread(buf,1,sizeof(buf),in))) {
    tmpbuf+=std::string(&(buf[0]),nread);
  }
  // parse the header
  header.parse_xml(tmpbuf);
  // decode the data
  std::vector<unsigned char> datav(
    xml_decode_field<unsigned char>(tmpbuf,"data") 
  );
  tmpbuf.clear();
  nsamples=header.group_info->data_desc.nsamples;
  nbytes=nsamples*header.group_info->recorder_cfg->bits_per_sample/8;
  if (datav.size() < nbytes) {
    fprintf(stderr,"Data size does not match number of samples\n");
    exit(1);
  }
  // convert the data to floating point
  sah_complex *fpdata=(sah_complex *)calloc(nsamples,sizeof(sah_complex));
  sah_complex *fpout=(sah_complex *)calloc(2048,sizeof(sah_complex));
  sah_complex *tmpb=(sah_complex *)calloc(1024,sizeof(sah_complex));
  if (!fpdata || !fpout) {
    fprintf(stderr,"Memory allocation failure\n");
    exit(1);
  }
  bits_to_floats(&(datav[0]),fpdata,nsamples);
  datav.clear();

  sah_complex workbuf[2048];
  fftwf_plan reverse=fftwf_plan_dft_1d(2048,workbuf,fpout,FFTW_BACKWARD,FFTW_MEASURE);
  fftwf_plan forward=fftwf_plan_dft_1d(1024,tmpb,workbuf,FFTW_FORWARD,FFTW_MEASURE|FFTW_PRESERVE_INPUT);

  while (i<nsamples) {
    // Do the forward transform.
    fftwf_execute_dft(forward, fpdata+i, workbuf);
    // shift 64 frequency bins
    memmove((void *)(workbuf+64), (void *)workbuf, 1024*sizeof(sah_complex));
    // now move the upper 64 into the low bins
    memmove((void *)workbuf,(void *)(workbuf+1024),64*sizeof(sah_complex));
    // clear the upper range
    memset((void *)(workbuf+1024),0,64*sizeof(sah_complex));
    // Do the reverse transform
    fftwf_execute_dft(reverse,workbuf,fpout);
    //
    for (j=0; j<2048; j++) {
      fprintf(out,"%f\n",fpout[j][0]/1024.0);
    }
    i+=1024;
  }
  exit(0);
}
Example #2
0
IDL_VPTR readwu(int argc, IDL_VPTR argv[], char *argk) {
  IDL_VPTR filename=NULL;
  static IDL_VARIABLE rv;
  rv.type=IDL_TYP_INT;
  rv.flags=IDL_V_CONST|IDL_V_NULL;
  rv.value.i=-1;

  char *outfile=NULL, buf[256];
  struct stat statbuf;
  int nbytes,nread,nsamples;
  std::string tmpbuf("");
  int i=0,j;

  if (argc != 1) {
    fprintf(stderr,"argc=%d\n",argc);
    fprintf(stderr,"array=readwu(wufile_name)\n");
    return &rv;
  }
  IDL_STRING *infile=NULL;
  if (argv[0]->type != IDL_TYP_STRING) {
    IDL_MessageFromBlock(readwu_msg_block,0,IDL_MSG_RET,"Parameter 1 must be type STRING");
  } else {
    infile=(IDL_STRING *)(&argv[0]->value.s);
  }
  FILE *in=fopen(infile->s,"r");
  if (!in) {
    IDL_MessageFromBlock(readwu_msg_block,0,IDL_MSG_RET,"File not found");
    return &rv;
  } 
  stat(infile->s,&statbuf);
  nbytes=statbuf.st_size;
  fseek(in,0,SEEK_SET);
  tmpbuf.reserve(nbytes);
  // read entire file into a buffer.
  while ((nread=(int)fread(buf,1,sizeof(buf),in))) {
    tmpbuf+=std::string(&(buf[0]),nread);
  }
  // parse the header
  header.parse_xml(tmpbuf);
  // decode the data
  std::vector<unsigned char> datav(
    xml_decode_field<unsigned char>(tmpbuf,"data") 
  );
  tmpbuf.clear();
  nsamples=header.group_info->data_desc.nsamples;
  nbytes=nsamples*header.group_info->recorder_cfg->bits_per_sample/8;
  if (datav.size() < nbytes) {
    fprintf(stderr,"Data size does not match number of samples\n");
    return &rv;
  }
  // convert the data to floating point
  sah_complex *fpdata=(sah_complex *)IDL_MemAlloc(nsamples*sizeof(sah_complex),0,IDL_MSG_RET);
  if (!fpdata) {
    fprintf(stderr,"Unable to allocate memory!\r\n");
    return &rv;
  } 
  bits_to_floats(&(datav[0]),fpdata,nsamples);
  datav.clear();
  IDL_MEMINT dims[]={nsamples};
  return IDL_ImportArray(1,dims,IDL_TYP_COMPLEX,(UCHAR *)fpdata,NULL,NULL);
}