Ejemplo n.º 1
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; 
}
Ejemplo 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);
}
Ejemplo n.º 3
0
int sync_run(sync_t *q, cf_t *input) {
  int N_id_2, peak_pos[3], sss_idx_n, sss_idx_e;
  int m0, m1;
  float m0_value_e, m1_value_e,m0_value_n, m1_value_n;
  int slot_id_e, N_id_1_e, slot_id_n, N_id_1_n;
  float peak_value[3];
  float mean_value[3];
  float max=-999;
  int i;
  int peak_detected;

  if (q->force_N_id_2 == -1) {
    for (N_id_2=0;N_id_2<3;N_id_2++) {
      peak_pos[N_id_2] = pss_synch_find_pss(&q->pss[N_id_2], input,
          &peak_value[N_id_2], &mean_value[N_id_2]);
    }
    for (i=0;i<3;i++) {
      if (peak_value[i] > max) {
        max = peak_value[i];
        N_id_2 = i;
      }
    }
  } else {
    N_id_2 = q->force_N_id_2;
    peak_pos[N_id_2] = pss_synch_find_pss(&q->pss[N_id_2], input,
        &peak_value[N_id_2], &mean_value[N_id_2]);
  }

  q->peak_to_avg = peak_value[N_id_2] / mean_value[N_id_2];

  DEBUG("PSS possible peak N_id_2=%d, pos=%d peak=%.2f par=%.2f threshold=%.2f\n",
      N_id_2, peak_pos[N_id_2], peak_value[N_id_2], q->peak_to_avg, q->threshold);

  /* If peak detected */
  peak_detected = 0;
  if (peak_pos[N_id_2] - 128 >= 0) {
    if (q->pss_mode == ABSOLUTE) {
      if (peak_value[N_id_2] > q->threshold) {
        peak_detected = 1;
      }
    } else {
      if (q->peak_to_avg  > q->threshold) {
        peak_detected = 1;
      }
    }
  }
  if (peak_detected) {

    q->cfo = pss_synch_cfo_compute(&q->pss[N_id_2], &input[peak_pos[N_id_2]-128]);

    INFO("PSS peak detected N_id_2=%d, pos=%d peak=%.2f par=%.2f th=%.2f cfo=%.4f\n", N_id_2,
        peak_pos[N_id_2], peak_value[N_id_2], q->peak_to_avg, q->threshold, q->cfo);

    if (q->sss_en) {

      /* Make sure we have enough room to find SSS sequence */
      sss_idx_n = peak_pos[N_id_2]-2*(128+CP(128,CPNORM_LEN));
      sss_idx_e = peak_pos[N_id_2]-2*(128+CP(128,CPEXT_LEN));

      if (q->detect_cp) {
        if (sss_idx_n < 0 || sss_idx_e < 0) {
          INFO("Not enough room to decode SSS (%d, %d)\n", sss_idx_n, sss_idx_e);
          return -1;
        }
      } else {
        if (CP_ISNORM(q->cp)) {
          if (sss_idx_n < 0) {
            INFO("Not enough room to decode SSS (%d)\n", sss_idx_n);
            return -1;
          }
        } else {
          if (sss_idx_e < 0) {
            INFO("Not enough room to decode SSS (%d)\n", sss_idx_e);
            return -1;
          }
        }
      }
      N_id_1_e = -1;
      N_id_1_n = -1;
      slot_id_e = -1;
      slot_id_n = -1;
      /* try Normal CP length */
      if (q->detect_cp || CP_ISNORM(q->cp)) {
        sss_synch_m0m1(&q->sss[N_id_2], &input[sss_idx_n],
            &m0, &m0_value_n, &m1, &m1_value_n);

        slot_id_n = 2 * sss_synch_subframe(m0, m1);
        N_id_1_n = sss_synch_N_id_1(&q->sss[N_id_2], m0, m1);
      }

      if (q->detect_cp || CP_ISEXT(q->cp)) {
        /* Now try Extended CP length */
        sss_synch_m0m1(&q->sss[N_id_2], &input[sss_idx_e],
            &m0, &m0_value_e, &m1, &m1_value_e);

        slot_id_e = 2 * sss_synch_subframe(m0, m1);
        N_id_1_e = sss_synch_N_id_1(&q->sss[N_id_2], m0, m1);
      }

      /* Correlation with extended CP hypoteshis is greater than with normal? */
      if ((q->detect_cp && m0_value_e * m1_value_e > m0_value_n * m1_value_n)
          || CP_ISEXT(q->cp)) {
        q->cp = CPEXT;
        q->slot_id = slot_id_e;
        q->N_id_1 = N_id_1_e;
      /* then is normal CP */
      } else {
        q->cp = CPNORM;
        q->slot_id = slot_id_n;
        q->N_id_1 = N_id_1_n;
      }
      q->N_id_2 = N_id_2;

      INFO("SSS detected N_id_1=%d, slot_idx=%d, %s CP\n",
          q->N_id_1, q->slot_id, CP_ISNORM(q->cp)?"Normal":"Extended");
    }

    return peak_pos[N_id_2];

  } else {
    return -1;
  }
}