Esempio n. 1
0
/******************************************************************************
 * Initialise output streams                                                  *
 ******************************************************************************/
void init_output(int jstart, const char *out_dir, const char *out_fn,
                   int oMaxLayers, AED_REAL Longitude, AED_REAL Latitude)
{
    char ts[20];
    char path[1024];
    struct stat sb;

    if ( out_dir != NULL && stat(out_dir, &sb) ) {
        fprintf(stderr, "Directory \"%s\" does not exist - attempting to create it\n", out_dir);
        if ( mkdir(out_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) ) {
            fprintf(stderr, "mkdir failed\n");
            exit(1);
        }
    } else {
        if ( ! S_ISDIR(sb.st_mode) ) {
            fprintf(stderr, "Name given in out_dir (%s) is not a directory\n", out_dir);
            exit(1);
        }
    }

    MaxLayers = oMaxLayers;
    write_time_string(ts,jstart,0);
    snprintf(path, 1024, "%s/%s.nc", out_dir, out_fn);
    ncid = init_glm_ncdf(path, "glm run", Latitude, Longitude, MaxLayers, ts);

    init_csv_output(out_dir);

    //# Initialize WQ output (creates NetCDF variables)
    if (wq_calc) wq_init_glm_output(&ncid, &x_dim, &y_dim, &z_dim, &time_dim);

#ifdef PLOTS
    if ( do_plots ) {
        int i;
        for (i = 0; i < 10; i++) plot_id[i] = -1;
        init_plots(jstart,nDays,CrestLevel);
    }
#endif
}
Esempio n. 2
0
int main(int argc, char **argv) {
  cf_t *input_buffer, *sf_symbols = NULL; 
  int frame_cnt;
  ue_sync_t s; 
  int pos;
  pss_synch_t pss; 
  float peak;
  struct timeval t[3];
  float mean_ce_time=0;
  bool signal_detected; 
  lte_fft_t fft; 
  lte_cell_t cell; 
  
  bzero(&cell, sizeof(lte_cell_t));
  
  parse_args(argc, argv);
    
  #ifndef DISABLE_GRAPHICS
  if (!disable_plots) {
    init_plots();    
  }
  #endif
  
  input_init();
        
  if (ue_sync_init(&s, cuhd_set_rx_srate, cuhd_recv_wrapper, uhd)) {
    fprintf(stderr, "Error initiating UE sync module\n");
    exit(-1);
  }
  
  ue_sync_pbch_enable(&s, true);
  
  signal_detected = true;
  frame_cnt = 0;
  mean_ce_time=0;
  uint32_t valid_frames=0;
  //uint32_t unaligned = 0; 
  while (frame_cnt < nof_frames || nof_frames == -1) {    

    int n = ue_sync_get_buffer(&s, &input_buffer);
    if (n < 0) {
      fprintf(stderr, "Error calling sync work()\n");
      exit(-1);      
    }
    
    if (n == 1 && ue_sync_get_sfidx(&s) == 0) {

      if (signal_detected) {
        cell = ue_sync_get_cell(&s);
        pss_synch_init_fft(&pss, 
                          SF_LEN(lte_symbol_sz(cell.nof_prb), cell.cp), 
                          lte_symbol_sz(cell.nof_prb));
        pss_synch_set_N_id_2(&pss, cell.id%3);
        
        sf_symbols = vec_malloc(SLOT_LEN_RE(cell.nof_prb, cell.cp) * sizeof(cf_t));
        if (!sf_symbols) {
          perror("malloc");
          exit(-1);
        }
        lte_fft_init(&fft, cell.cp, cell.nof_prb);
        signal_detected = false; 
      }
      
      mean_ce_time = (float) (mean_ce_time + (float) t[0].tv_usec * valid_frames) / (valid_frames+1);
      valid_frames++;
      
      #ifndef DISABLE_GRAPHICS
      if (!disable_plots && !(valid_frames % 5) && sf_symbols) {
        
        /* Run FFT for the second slot */
        lte_fft_run_slot(&fft, input_buffer, sf_symbols);
    
        int i;
        int nof_re = SLOT_LEN_RE(cell.nof_prb, cell.cp);
        for (i = 0; i < nof_re; i++) {
          tmp_plot[i] = 10 * log10f(cabsf(sf_symbols[i]));
          if (isinf(tmp_plot[i])) {
            tmp_plot[i] = -80;
          }
        }
        plot_real_setNewData(&poutfft, tmp_plot, nof_re);        
      }
    #endif
      
      pos = pss_synch_find_pss(&pss, input_buffer, &peak, NULL);      
      /*if (pos > 962 || pos < 958) {
        unaligned++;
      }
      */
      printf("CELL_ID: %3d CFO: %+.4f KHz, SFO: %+.4f Khz, TimeOffset: %4d, Exec: %3.2f\r",
              cell.id, ue_sync_get_cfo(&s)/1000, ue_sync_get_sfo(&s)/1000, pos, 
             s.mean_exec_time);
          fflush(stdout);
      if (VERBOSE_ISINFO()) {
        printf("\n");
      }
    }
      
    frame_cnt++;
  }

  printf("\nBye\n");
  exit(0);
}
Esempio n. 3
0
int main(int argc, char **argv) {
  int ret; 
  cf_t *sf_buffer; 
  iodev_t iodev; 
  prog_args_t prog_args; 
  lte_cell_t cell; 
  ue_dl_t ue_dl; 
  bool ue_dl_initiated = false; 
  int64_t sf_cnt;
  uint32_t sf_idx;
  pbch_mib_t mib; 
  bool printed_sib = false; 
  uint32_t rlen; 
  
  parse_args(&prog_args, argc, argv);
  
  if (iodev_init(&iodev, &prog_args.io_config)) {
    fprintf(stderr, "Error initiating input device\n");
    exit(-1);
  }
  
#ifndef DISABLE_GRAPHICS
  if (!prog_args.disable_plots) {
    init_plots();    
  }
#endif
  
  /* Setup SIGINT handler */
  printf("\n --- Press Ctrl+C to exit --- \n");
  signal(SIGINT, sigintHandler);

  /* Initialize frame and subframe counters */
  sf_cnt = 0;
  sf_idx = 0; 
  
  /* Main loop */
  while (!go_exit && (sf_cnt < prog_args.nof_subframes || prog_args.nof_subframes == -1)) {

    ret = iodev_receive(&iodev, &sf_buffer);
    if (ret < 0) {
      fprintf(stderr, "Error reading from input device (%d)\n", ret);
      break;
    }
    
    /* iodev_receive returns 1 if successfully read 1 aligned subframe */
    if (ret == 1) {
      if (!ue_dl_initiated) {
        if (iodev_isUSRP(&iodev)) {
          cell = ue_sync_get_cell(&iodev.sframe);
          mib = ue_sync_get_mib(&iodev.sframe);
        } else {
          cell.id = prog_args.cell_id_file;
          cell.cp = CPNORM; 
          cell.nof_ports = 1; // TODO: Use prog_args 
          cell.nof_prb = prog_args.nof_prb_file; 
          mib.phich_resources = R_1; 
          mib.phich_length = PHICH_NORM;
        }        
        if (ue_dl_init(&ue_dl, cell, mib.phich_resources, mib.phich_length, 1234)) { 
          fprintf(stderr, "Error initiating UE downlink processing module\n");
          exit(-1);
        }
        pdsch_set_rnti(&ue_dl.pdsch, prog_args.rnti);
        ue_dl_initiated = true; 
      } else {
        if (iodev_isUSRP(&iodev)) {
          sf_idx = ue_sync_get_sfidx(&iodev.sframe);
        } 
        rlen = ue_dl_receive(&ue_dl, sf_buffer, data, sf_idx, ue_sync_get_mib(&iodev.sframe).sfn, prog_args.rnti);
        if (rlen < 0) {
          fprintf(stderr, "\nError running receiver\n");fflush(stdout);
          exit(-1);
        }
        if (prog_args.rnti == SIRNTI && !printed_sib && rlen > 0) {
          printf("\n\nDecoded SIB1 Message: ");
          vec_fprint_hex(stdout, data, rlen);
          printf("\n");fflush(stdout);
          printed_sib = true; 
        }
        if (!(sf_cnt % 10)) {         
          printf("Cell ID: %3d, RSSI: %+.2f dBm, CFO: %+.4f KHz, SFO: %+.4f Khz, TimeOffset: %4d, Errors: %4d/%4d, BLER: %.1e\r",
              cell.id, 20*log10f(agc_get_rssi(&iodev.sframe.agc)), iodev.sframe.cur_cfo * 15, iodev.sframe.mean_time_offset / 5, iodev.sframe.peak_idx,
              (int) ue_dl.pkt_errors, (int) ue_dl.pkts_total, (float) ue_dl.pkt_errors / ue_dl.pkts_total);
          fflush(stdout);       
          if (VERBOSE_ISINFO()) {
            printf("\n");
          }
        }  
        #ifndef DISABLE_GRAPHICS
        if (!prog_args.disable_plots && sf_idx == 5) {
          do_plots(&ue_dl, sf_idx);          
        }
        #endif
      }
      if (iodev_isfile(&iodev)) {
        sf_idx++;       
        if (sf_idx == NSUBFRAMES_X_FRAME) {
          sf_idx = 0;
        }        
      }
    }
    if (prog_args.nof_subframes > 0) {
      sf_cnt++;      
    }    
    if (iodev_isfile(&iodev)) {
      usleep(5000);
    }
  }

  if (ue_dl_initiated) {
    ue_dl_free(&ue_dl);    
  }
  iodev_free(&iodev);

  printf("\nBye\n");
  exit(0);
}
Esempio n. 4
0
int main(int argc, char **argv) {
  cf_t *buffer; 
  int frame_cnt, n; 
  void *uhd;
  srslte_pss_synch_t pss; 
  srslte_cfo_t cfocorr, cfocorr64; 
  srslte_sss_synch_t sss; 
  int32_t flen; 
  int peak_idx, last_peak;
  float peak_value; 
  float mean_peak; 
  uint32_t nof_det, nof_nodet, nof_nopeak, nof_nopeakdet;
  cf_t ce[SRSLTE_PSS_LEN]; 
  
  parse_args(argc, argv);

  if (N_id_2_sync == -1) {
    N_id_2_sync = cell_id%3;
  }
  uint32_t N_id_2 = cell_id%3;
  uint32_t N_id_1 = cell_id/3;

#ifndef DISABLE_GRAPHICS
  if (!disable_plots)
    init_plots();
#endif

  float srate = 15000.0*fft_size; 
  
  flen = srate*5/1000;

  printf("Opening UHD device...\n");
  if (cuhd_open(uhd_args, &uhd)) {
    fprintf(stderr, "Error opening uhd\n");
    exit(-1);
  }
  
  if (srate < 10e6) {
    cuhd_set_master_clock_rate(uhd, 4*srate);        
  } else {
    cuhd_set_master_clock_rate(uhd, srate);        
  }

  printf("Set RX rate: %.2f MHz\n", cuhd_set_rx_srate(uhd, srate) / 1000000);
  printf("Set RX gain: %.1f dB\n", cuhd_set_rx_gain(uhd, uhd_gain));
  printf("Set RX freq: %.2f MHz\n", cuhd_set_rx_freq(uhd, uhd_freq) / 1000000);
  cuhd_rx_wait_lo_locked(uhd);
  
  buffer = malloc(sizeof(cf_t) * flen * 2);
  if (!buffer) {
    perror("malloc");
    exit(-1);
  }
    
  if (srslte_pss_synch_init_fft(&pss, flen, fft_size)) {
    fprintf(stderr, "Error initiating PSS\n");
    exit(-1);
  }

  if (srslte_pss_synch_set_N_id_2(&pss, N_id_2_sync)) {
    fprintf(stderr, "Error setting N_id_2=%d\n",N_id_2_sync);
    exit(-1);
  }
  
  srslte_cfo_init(&cfocorr, flen); 
  srslte_cfo_init(&cfocorr64, flen); 
 
  if (srslte_sss_synch_init(&sss, fft_size)) {
    fprintf(stderr, "Error initializing SSS object\n");
    return SRSLTE_ERROR;
  }

  srslte_sss_synch_set_N_id_2(&sss, N_id_2);

  printf("N_id_2: %d\n", N_id_2);  

  cuhd_start_rx_stream(uhd);
  
  printf("Frame length %d samples\n", flen);
  printf("PSS detection threshold: %.2f\n", threshold);
  
  nof_det = nof_nodet = nof_nopeak = nof_nopeakdet = 0;
  frame_cnt = 0;
  last_peak = 0; 
  mean_peak = 0;
  int peak_offset = 0;
  float cfo; 
  float mean_cfo = 0; 
  uint32_t m0, m1; 
  uint32_t sss_error1 = 0, sss_error2 = 0, sss_error3 = 0; 
  uint32_t cp_is_norm = 0; 
  
  srslte_sync_t ssync; 
  bzero(&ssync, sizeof(srslte_sync_t));
  ssync.fft_size = fft_size;
  
  while(frame_cnt < nof_frames || nof_frames == -1) {
    n = cuhd_recv(uhd, buffer, flen - peak_offset, 1);
    if (n < 0) {
      fprintf(stderr, "Error receiving samples\n");
      exit(-1);
    }
    
    peak_idx = srslte_pss_synch_find_pss(&pss, buffer, &peak_value);
    if (peak_idx < 0) {
      fprintf(stderr, "Error finding PSS peak\n");
      exit(-1);
    }
        
    mean_peak = SRSLTE_VEC_CMA(peak_value, mean_peak, frame_cnt);
    
    if (peak_value >= threshold) {
      nof_det++;
        
      if (peak_idx >= fft_size) {

        // Estimate CFO 
        cfo = srslte_pss_synch_cfo_compute(&pss, &buffer[peak_idx-fft_size]);
        mean_cfo = SRSLTE_VEC_CMA(cfo, mean_cfo, frame_cnt);        

        // Correct CFO
        srslte_cfo_correct(&cfocorr, buffer, buffer, -mean_cfo / fft_size);               

        // Estimate channel
        if (srslte_pss_synch_chest(&pss, &buffer[peak_idx-fft_size], ce)) {
          fprintf(stderr, "Error computing channel estimation\n");
          exit(-1);
        }
        
        // Find SSS 
        int sss_idx = peak_idx-2*fft_size-(SRSLTE_CP_ISNORM(cp)?SRSLTE_CP_LEN(fft_size, SRSLTE_CP_NORM_LEN):SRSLTE_CP_LEN(fft_size, SRSLTE_CP_EXT_LEN));             
        if (sss_idx >= 0 && sss_idx < flen-fft_size) {
          srslte_sss_synch_m0m1_partial(&sss, &buffer[sss_idx], 3, NULL, &m0, &m0_value, &m1, &m1_value);
          if (srslte_sss_synch_N_id_1(&sss, m0, m1) != N_id_1) {
            sss_error2++;            
          }
          INFO("Partial N_id_1: %d\n", srslte_sss_synch_N_id_1(&sss, m0, m1));
          srslte_sss_synch_m0m1_diff(&sss, &buffer[sss_idx], &m0, &m0_value, &m1, &m1_value);
          if (srslte_sss_synch_N_id_1(&sss, m0, m1) != N_id_1) {
            sss_error3++;            
          }
          INFO("Diff N_id_1: %d\n", srslte_sss_synch_N_id_1(&sss, m0, m1));
          srslte_sss_synch_m0m1_partial(&sss, &buffer[sss_idx], 1, NULL, &m0, &m0_value, &m1, &m1_value);
          if (srslte_sss_synch_N_id_1(&sss, m0, m1) != N_id_1) {
            sss_error1++;     
          }
          INFO("Full N_id_1: %d\n", srslte_sss_synch_N_id_1(&sss, m0, m1));
        }
        
        // Estimate CP 
        if (peak_idx > 2*(fft_size + SRSLTE_CP_LEN_EXT(fft_size))) {
          srslte_cp_t cp = srslte_sync_detect_cp(&ssync, buffer, peak_idx);
          if (SRSLTE_CP_ISNORM(cp)) {
            cp_is_norm++; 
          }          
        }
        
      } else {
        INFO("No space for CFO computation. Frame starts at \n",peak_idx);
      }
      
      if(srslte_sss_synch_subframe(m0,m1) == 0)
      {
#ifndef DISABLE_GRAPHICS
          if (!disable_plots)
            do_plots_sss(sss.corr_output_m0, sss.corr_output_m1);
#endif
      }
      
    } else {
      nof_nodet++;
    }

    if (frame_cnt > 100) {
      if (abs(last_peak-peak_idx) > 4) {
        if (peak_value >= threshold) {
          nof_nopeakdet++;
        } 
        nof_nopeak++;                  
      } 
    }
    
    frame_cnt++;
   
    printf("[%5d]: Pos: %5d, PSR: %4.1f (~%4.1f) Pdet: %4.2f, "
           "FA: %4.2f, CFO: %+4.1f KHz SSSmiss: %4.2f/%4.2f/%4.2f CPNorm: %.0f%%\r", 
           frame_cnt, 
           peak_idx, 
           peak_value, mean_peak,
           (float) nof_det/frame_cnt, 
           (float) nof_nopeakdet/frame_cnt, mean_cfo*15, 
           (float) sss_error1/nof_det,(float) sss_error2/nof_det,(float) sss_error3/nof_det,
           (float) cp_is_norm/nof_det * 100);
    
    if (SRSLTE_VERBOSE_ISINFO()) {
      printf("\n");
    }
  
#ifndef DISABLE_GRAPHICS
    if (!disable_plots)
      do_plots(pss.conv_output_avg, pss.conv_output_avg[peak_idx], pss.fft_size+pss.frame_size-1, ce);
#endif

    last_peak = peak_idx;

  }
  
  srslte_pss_synch_free(&pss);
  free(buffer);
  cuhd_close(uhd);

  printf("Ok\n");
  exit(0);
}