/* Returns the CFO estimation given a PSS received sequence * * Source: An Efficient CFO Estimation Algorithm for the Downlink of 3GPP-LTE * Feng Wang and Yu Zhu */ float srslte_pss_cfo_compute(srslte_pss_t* q, const cf_t *pss_recv) { cf_t y0, y1; const cf_t *pss_ptr = pss_recv; if (q->filter_pss_enable) { srslte_pss_filter(q, pss_recv, q->tmp_fft); pss_ptr = (const cf_t*) q->tmp_fft; } y0 = srslte_vec_dot_prod_ccc(q->pss_signal_time[q->N_id_2], pss_ptr, q->fft_size/2); y1 = srslte_vec_dot_prod_ccc(&q->pss_signal_time[q->N_id_2][q->fft_size/2], &pss_ptr[q->fft_size/2], q->fft_size/2); return carg(conjf(y0) * y1)/M_PI; }
/** Finds the PSS sequence previously defined by a call to srslte_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 srslte_sync_set_threshold() * or 0 otherwise. Returns a negative number on error (if N_id_2 has not been set) * * The input signal is not modified. Any CFO correction is done in internal buffers * * The maximum of the correlation peak is always stored in *peak_position */ srslte_sync_find_ret_t srslte_sync_find(srslte_sync_t *q, const cf_t *input, uint32_t find_offset, uint32_t *peak_position) { srslte_sync_find_ret_t ret = SRSLTE_SYNC_ERROR; int peak_pos = 0; if (!q) { return SRSLTE_ERROR_INVALID_INPUTS; } if (input != NULL && srslte_N_id_2_isvalid(q->N_id_2) && fft_size_isvalid(q->fft_size)) { if (peak_position) { *peak_position = 0; } const cf_t *input_ptr = input; /* First CFO estimation stage is integer. * Finds max PSS correlation for shifted +1/0/-1 integer versions. * This should only used once N_id_2 is set */ if (q->cfo_i_enable) { if (cfo_i_estimate(q, input_ptr, find_offset, &peak_pos, &q->cfo_i_value) < 0) { fprintf(stderr, "Error calling finding PSS sequence at : %d \n", peak_pos); return SRSLTE_ERROR; } // Correct it using precomputed signal and store in buffer (don't modify input signal) if (q->cfo_i_value != 0) { srslte_vec_prod_ccc((cf_t*) input_ptr, q->cfo_i_corr[q->cfo_i_value<0?0:1], q->temp, q->frame_size); INFO("Compensating cfo_i=%d\n", q->cfo_i_value); input_ptr = q->temp; } } /* Second stage is coarse fractional CFO estimation using CP. * In case of multi-cell, this can lead to incorrect estimations if CFO from different cells is different */ if (q->cfo_cp_enable) { float cfo_cp = cfo_cp_estimate(q, input_ptr); if (!q->cfo_cp_is_set) { q->cfo_cp_mean = cfo_cp; q->cfo_cp_is_set = true; } else { /* compute exponential moving average CFO */ q->cfo_cp_mean = SRSLTE_VEC_EMA(cfo_cp, q->cfo_cp_mean, q->cfo_ema_alpha); } INFO("CP-CFO: estimated=%f, mean=%f\n", cfo_cp, q->cfo_cp_mean); /* Correct CFO with the averaged CFO estimation */ srslte_cfo_correct(&q->cfo_corr_frame, input_ptr, q->temp, -q->cfo_cp_mean / q->fft_size); input_ptr = q->temp; } /* Find maximum of PSS correlation. If Integer CFO is enabled, correlation is already done */ if (!q->cfo_i_enable) { srslte_pss_set_N_id_2(&q->pss, q->N_id_2); peak_pos = srslte_pss_find_pss(&q->pss, &input_ptr[find_offset], q->threshold>0?&q->peak_value:NULL); if (peak_pos < 0) { fprintf(stderr, "Error calling finding PSS sequence at : %d \n", peak_pos); return SRSLTE_ERROR; } } INFO("PSS: id=%d, peak_pos=%d, peak_value=%f\n", q->N_id_2, peak_pos, q->peak_value); // Save peak position if (peak_position) { *peak_position = (uint32_t) peak_pos; } // In case of decimation, this compensates for the constant time shift caused by the low pass filter if(q->decimate && peak_pos < 0) { peak_pos = 0 ;//peak_pos + q->decimate*(2);// replace 2 with q->filter_size -2; } /* If peak is over threshold, compute CFO and SSS */ if (q->peak_value >= q->threshold || q->threshold == 0) { if (q->cfo_pss_enable && peak_pos >= q->fft_size) { // Filter central bands before PSS-based CFO estimation const cf_t *pss_ptr = &input_ptr[find_offset + peak_pos - q->fft_size]; if (q->pss_filtering_enabled) { srslte_pss_filter(&q->pss, pss_ptr, q->pss_filt); pss_ptr = q->pss_filt; } // PSS-based CFO estimation q->cfo_pss = srslte_pss_cfo_compute(&q->pss, pss_ptr); if (!q->cfo_pss_is_set) { q->cfo_pss_mean = q->cfo_pss; q->cfo_pss_is_set = true; } else if (15000*fabsf(q->cfo_pss) < MAX_CFO_PSS_OFFSET) { q->cfo_pss_mean = SRSLTE_VEC_EMA(q->cfo_pss, q->cfo_pss_mean, q->cfo_ema_alpha); } INFO("PSS-CFO: filter=%s, estimated=%f, mean=%f\n", q->pss_filtering_enabled?"yes":"no", q->cfo_pss, q->cfo_pss_mean); } // If there is enough space for CP and SSS estimation if (peak_pos + find_offset >= 2 * (q->fft_size + SRSLTE_CP_LEN_EXT(q->fft_size))) { // If SSS search is enabled, correlate SSS sequence if (q->sss_en) { // Set an invalid N_id_1 indicating SSS is yet to be detected q->N_id_1 = 1000; int sss_idx = find_offset + peak_pos - 2 * q->fft_size - SRSLTE_CP_LEN(q->fft_size, (SRSLTE_CP_ISNORM(q->cp) ? SRSLTE_CP_NORM_LEN : SRSLTE_CP_EXT_LEN)); const cf_t *sss_ptr = &input_ptr[sss_idx]; // Correct CFO if detected in PSS if (q->cfo_pss_enable) { srslte_cfo_correct(&q->cfo_corr_symbol, sss_ptr, q->sss_filt, -q->cfo_pss_mean / q->fft_size); // Equalize channel if estimated in PSS if (q->sss_channel_equalize && q->pss.chest_on_filter && q->pss_filtering_enabled) { srslte_vec_prod_ccc(&q->sss_filt[q->fft_size/2-SRSLTE_PSS_LEN/2], q->pss.tmp_ce, &q->sss_filt[q->fft_size/2-SRSLTE_PSS_LEN/2], SRSLTE_PSS_LEN); } sss_ptr = q->sss_filt; } if (sync_sss_symbol(q, sss_ptr) < 0) { fprintf(stderr, "Error correlating SSS\n"); return -1; } } // Detect CP length if (q->detect_cp) { srslte_sync_set_cp(q, srslte_sync_detect_cp(q, input_ptr, peak_pos + find_offset)); } else { DEBUG("Not enough room to detect CP length. Peak position: %d\n", peak_pos); } ret = SRSLTE_SYNC_FOUND; } else { ret = SRSLTE_SYNC_FOUND_NOSPACE; } } else { ret = SRSLTE_SYNC_NOFOUND; } DEBUG("SYNC ret=%d N_id_2=%d find_offset=%d frame_len=%d, pos=%d peak=%.2f threshold=%.2f sf_idx=%d, CFO=%.3f kHz\n", ret, q->N_id_2, find_offset, q->frame_size, peak_pos, q->peak_value, q->threshold, q->sf_idx, 15*(srslte_sync_get_cfo(q))); } else if (srslte_N_id_2_isvalid(q->N_id_2)) { fprintf(stderr, "Must call srslte_sync_set_N_id_2() first!\n"); } return ret; }