int sync_get_cell_id(sync_t *q) { if (lte_N_id_2_isvalid(q->N_id_2) && lte_N_id_1_isvalid(q->N_id_1)) { return q->N_id_1*3 + q->N_id_2; } else { return -1; } }
int pss_synch_init_N_id_2(cf_t *pss_signal_freq, uint32_t N_id_2, uint32_t fft_size) { dft_plan_t plan; cf_t pss_signal_pad[2048]; cf_t pss_signal_time[PSS_LEN]; int ret = LIBLTE_ERROR_INVALID_INPUTS; if (lte_N_id_2_isvalid(N_id_2) && fft_size <= 2048) { pss_generate(pss_signal_time, N_id_2); bzero(pss_signal_pad, fft_size * sizeof(cf_t)); bzero(pss_signal_freq, fft_size * sizeof(cf_t)); memcpy(&pss_signal_pad[(fft_size-PSS_LEN)/2], pss_signal_time, PSS_LEN * sizeof(cf_t)); if (dft_plan(&plan, fft_size, BACKWARD, COMPLEX)) { return LIBLTE_ERROR; } dft_plan_set_mirror(&plan, true); dft_plan_set_dc(&plan, true); dft_plan_set_norm(&plan, true); dft_run_c(&plan, pss_signal_pad, pss_signal_freq); vec_conj_cc(pss_signal_freq, pss_signal_freq, fft_size); vec_sc_prod_cfc(pss_signal_freq, 1.0/62.0, pss_signal_freq, fft_size); dft_plan_free(&plan); ret = LIBLTE_SUCCESS; } return ret; }
/** Sets the N_id_2 to search for */ int sss_synch_set_N_id_2(sss_synch_t *q, uint32_t N_id_2) { if (!lte_N_id_2_isvalid(N_id_2)) { fprintf(stderr, "Invalid N_id_2 %d\n", N_id_2); return LIBLTE_ERROR; } else { q->N_id_2 = N_id_2; return LIBLTE_SUCCESS; } }
int sync_set_N_id_2(sync_t *q, uint32_t N_id_2) { if (lte_N_id_2_isvalid(N_id_2)) { q->N_id_2 = N_id_2; return LIBLTE_SUCCESS; } else { fprintf(stderr, "Invalid N_id_2=%d\n", N_id_2); return LIBLTE_ERROR_INVALID_INPUTS; } }
/** Sets the current N_id_2 value. Returns -1 on error, 0 otherwise */ int pss_synch_set_N_id_2(pss_synch_t *q, uint32_t N_id_2) { if (!lte_N_id_2_isvalid((N_id_2))) { fprintf(stderr, "Invalid N_id_2 %d\n", N_id_2); return -1; } else { q->N_id_2 = N_id_2; return 0; } }
/** Returns the index of the PSS correlation peak in a subframe. * The frame starts at corr_peak_pos-subframe_size/2. * The value of the correlation is stored in corr_peak_value. * * Input buffer must be subframe_size long. */ int pss_synch_find_pss(pss_synch_t *q, cf_t *input, float *corr_peak_value) { int ret = LIBLTE_ERROR_INVALID_INPUTS; if (q != NULL && input != NULL) { uint32_t corr_peak_pos; uint32_t conv_output_len; if (!lte_N_id_2_isvalid(q->N_id_2)) { fprintf(stderr, "Error finding PSS peak, N_id_2 not set\n"); return LIBLTE_ERROR; } bzero(&q->pss_signal_freq[q->N_id_2][q->fft_size], q->frame_size * sizeof(cf_t)); memcpy(q->tmp_input, input, q->frame_size * sizeof(cf_t)); bzero(&q->tmp_input[q->frame_size], q->fft_size * sizeof(cf_t)); /* Correlate input with PSS sequence */ #ifdef CONVOLUTION_FFT conv_output_len = conv_fft_cc_run(&q->conv_fft, q->tmp_input, q->pss_signal_freq[q->N_id_2], q->conv_output); #else conv_output_len = conv_cc(input, q->pss_signal_freq[q->N_id_2], q->conv_output, q->frame_size, q->fft_size); #endif /* Find maximum of the absolute value of the correlation */ corr_peak_pos = vec_max_abs_ci(q->conv_output, conv_output_len); if (corr_peak_value) { *corr_peak_value = cabsf(q->conv_output[corr_peak_pos]); } ret = (int) corr_peak_pos; } return ret; }
/** 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; }