Пример #1
0
int sync_init(sync_t *q, int frame_size) {
  int N_id_2;

  bzero(q, sizeof(sync_t));
  q->force_N_id_2 = -1;
  q->threshold = 1.5;
  q->pss_mode = PEAK_MEAN;
  q->detect_cp = true;
  q->sss_en = true;

  for (N_id_2=0;N_id_2<3;N_id_2++) {
    if (pss_synch_init(&q->pss[N_id_2], frame_size)) {
      fprintf(stderr, "Error initializing PSS object\n");
      return -1;
    }
    if (pss_synch_set_N_id_2(&q->pss[N_id_2], N_id_2)) {
      fprintf(stderr, "Error initializing N_id_2\n");
      return -1;
    }
    if (sss_synch_init(&q->sss[N_id_2])) {
      fprintf(stderr, "Error initializing SSS object\n");
      return -1;
    }
    if (sss_synch_set_N_id_2(&q->sss[N_id_2], N_id_2)) {
      fprintf(stderr, "Error initializing N_id_2\n");
      return -1;
    }
    DEBUG("PSS and SSS initiated N_id_2=%d\n", N_id_2);
  }

  return 0;
}
Пример #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);
}
Пример #3
0
/** Finds the PSS sequence previously defined by a call to sync_set_N_id_2()
 * around the position find_offset in the buffer input. 
 * Returns 1 if the correlation peak exceeds the threshold set by sync_set_threshold() 
 * or 0 otherwise. Returns a negative number on error (if N_id_2 has not been set) 
 * 
 * The maximum of the correlation peak is always stored in *peak_position
 */
int sync_find(sync_t *q, cf_t *input, uint32_t find_offset, uint32_t *peak_position) 
{
  
  int ret = LIBLTE_ERROR_INVALID_INPUTS; 
  
  if (q                 != NULL     &&
      input             != NULL     &&
      lte_N_id_2_isvalid(q->N_id_2) && 
      fft_size_isvalid(q->fft_size))
  {
    int peak_pos;
    
    ret = LIBLTE_SUCCESS; 
    
    if (peak_position) {
      *peak_position = 0; 
    }

    pss_synch_set_N_id_2(&q->pss, q->N_id_2);
  
    peak_pos = pss_synch_find_pss(&q->pss, &input[find_offset], &q->peak_value);
    if (peak_pos < 0) {
      fprintf(stderr, "Error calling finding PSS sequence\n");
      return LIBLTE_ERROR; 
    }
    q->mean_peak_value = VEC_EMA(q->peak_value, q->mean_peak_value, MEANPEAK_EMA_ALPHA);

    if (peak_position) {
      *peak_position = (uint32_t) peak_pos;
    }
    
    /* If peak is over threshold, compute CFO and SSS */
    if (q->peak_value >= q->threshold) {
      
      // Make sure we have enough space to estimate CFO
      if (peak_pos + find_offset >= q->fft_size) {
        float cfo = pss_synch_cfo_compute(&q->pss, &input[find_offset+peak_pos-q->fft_size]);

        /* compute cumulative moving average CFO */
        q->mean_cfo = VEC_EMA(cfo, q->mean_cfo, CFO_EMA_ALPHA);
      } else {
        INFO("No space for CFO computation. Frame starts at \n",peak_pos);
      }

      if (q->detect_cp) {
        if (peak_pos + find_offset >= 2*(q->fft_size + CP_EXT(q->fft_size))) {
          q->cp = sync_detect_cp(q, input, peak_pos + find_offset);
        } else {
          INFO("Not enough room to detect CP length. Peak position: %d\n", peak_pos);
        }
      }
  
      // Try to detect SSS 
      if (q->sss_en) {
        /* Correct CFO with the averaged CFO estimation */
        if (q->mean_cfo && q->correct_cfo) {
          cfo_correct(&q->cfocorr, input, input, -q->mean_cfo / q->fft_size);                 
        }
        
        // Set an invalid N_id_1 indicating SSS is yet to be detected
        q->N_id_1 = 1000; 
        
        if (sync_sss(q, input, find_offset + peak_pos, q->cp) < 0) {
          INFO("No space for SSS processing. Frame starts at %d\n", peak_pos);
        }
      }
      // Return 1 (peak detected) even if we couldn't estimate CFO and SSS
      ret = 1;
    } else {
      ret = 0;
    }
    
    INFO("SYNC ret=%d N_id_2=%d find_offset=%d pos=%d peak=%.2f threshold=%.2f sf_idx=%d, CFO=%.3f KHz\n",
         ret, q->N_id_2, find_offset, peak_pos, q->peak_value, q->threshold, q->sf_idx, 15*q->mean_cfo);

  } else if (lte_N_id_2_isvalid(q->N_id_2)) {
    fprintf(stderr, "Must call sync_set_N_id_2() first!\n");
  }
  
  return ret; 
}