diff --git a/impl/src/layer1/correlator.c b/impl/src/layer1/correlator.c index d69ac00..f1c5426 100644 --- a/impl/src/layer1/correlator.c +++ b/impl/src/layer1/correlator.c @@ -97,6 +97,11 @@ float complex correlator_step(correlator_ctx_t *ctx, float complex sample) result += ctx->search_sequence[m] * ctx->input_history[nm]; } + // the current mean phase can be basically calculated for free here, as the + // sum of rotated symbols is already available, so we do so and cache that + // value. + ctx->phase_deviation = cargf(result); + return result; } @@ -118,19 +123,10 @@ const float complex* correlator_get_conj_search_sequence(correlator_ctx_t *ctx) } -float correlator_get_mean_phase_deviation(correlator_ctx_t *ctx, size_t L) +float correlator_get_mean_phase_deviation(correlator_ctx_t *ctx) { - float complex mean_unrotated_symbol = 0; - - size_t n = (ctx->history_ptr - ctx->search_sequence_len + 1) & ctx->buffer_size_mask; - - for(size_t m = ctx->search_sequence_len - L; m < ctx->search_sequence_len; m++) { - size_t nm = (n + m) & ctx->buffer_size_mask; - mean_unrotated_symbol += ctx->search_sequence[m] * ctx->input_history[nm]; - } - - // no need to divide by ctx->search_sequence_len because division does not change the angle - return cargf(mean_unrotated_symbol); + // return cached value from last call to correlator_step(). + return ctx->phase_deviation; } @@ -147,5 +143,12 @@ float correlator_get_mean_frequency_deviation(correlator_ctx_t *ctx, size_t L, f z[m - m0] = conjf(ctx->search_sequence[m]) * ctx->input_history[nm]; } - return freq_est_data_free(z, L, phase_offset); + float freq = freq_est_data_free(z, L, NULL); + + // we calculate the final phase based on the phase estimated from the + // preamble and not the phase from the frequency estimator because this + // method is more reliable. + *phase_offset = ctx->phase_deviation + (ctx->search_sequence_len+1)/2.0f * freq; + + return freq; } diff --git a/impl/src/layer1/correlator.h b/impl/src/layer1/correlator.h index 353ec3f..ef38293 100644 --- a/impl/src/layer1/correlator.h +++ b/impl/src/layer1/correlator.h @@ -24,6 +24,8 @@ typedef struct float complex *search_sequence; float complex *input_history; + float phase_deviation; + /*! * \brief The history pointer. * @@ -120,10 +122,9 @@ const float complex* correlator_get_conj_search_sequence(correlator_ctx_t *ctx); * found. * * \param ctx The correlator context to use. - * \param L Number of symbols to use (at the end of the sequence) * \returns The mean phase deviation in radians. */ -float correlator_get_mean_phase_deviation(correlator_ctx_t *ctx, size_t L); +float correlator_get_mean_phase_deviation(correlator_ctx_t *ctx); /*! * \brief Calculate mean frequency deviation between current input and search sequence.