srsLTE/srslte/lib/sync/cfo.c

92 lines
2.4 KiB
C
Raw Normal View History

2014-03-03 15:54:02 -08:00
/**
*
* \section COPYRIGHT
*
2015-11-13 04:22:33 -08:00
* Copyright 2013-2015 Software Radio Systems Limited
2014-03-03 15:54:02 -08:00
*
* \section LICENSE
*
* This file is part of the srsLTE library.
2014-03-03 15:54:02 -08:00
*
* srsLTE is free software: you can redistribute it and/or modify
2015-05-08 08:05:40 -07:00
* it under the terms of the GNU Affero General Public License as
2014-03-03 15:54:02 -08:00
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* srsLTE is distributed in the hope that it will be useful,
2014-03-03 15:54:02 -08:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2015-05-08 08:05:40 -07:00
* GNU Affero General Public License for more details.
2014-03-03 15:54:02 -08:00
*
2015-05-08 08:05:40 -07:00
* A copy of the GNU Affero General Public License can be found in
2014-03-03 15:54:02 -08:00
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
*/
#include <strings.h>
#include <stdlib.h>
#include <math.h>
#include "srslte/utils/cexptab.h"
#include "srslte/sync/cfo.h"
#include "srslte/utils/vector.h"
#include "srslte/utils/debug.h"
2014-03-03 15:54:02 -08:00
2015-03-18 11:14:24 -07:00
int srslte_cfo_init(srslte_cfo_t *h, uint32_t nsamples) {
int ret = SRSLTE_ERROR;
2015-03-18 11:14:24 -07:00
bzero(h, sizeof(srslte_cfo_t));
2014-03-03 15:54:02 -08:00
2015-03-18 11:14:24 -07:00
if (srslte_cexptab_init(&h->tab, SRSLTE_CFO_CEXPTAB_SIZE)) {
2014-06-17 02:11:41 -07:00
goto clean;
}
2015-03-18 11:14:24 -07:00
h->cur_cexp = srslte_vec_malloc(sizeof(cf_t) * nsamples);
2014-06-17 02:11:41 -07:00
if (!h->cur_cexp) {
goto clean;
}
2015-03-18 11:14:24 -07:00
h->tol = SRSLTE_CFO_TOLERANCE;
2014-06-17 02:11:41 -07:00
h->last_freq = 0;
h->nsamples = nsamples;
2015-03-18 11:14:24 -07:00
srslte_cexptab_gen(&h->tab, h->cur_cexp, h->last_freq, h->nsamples);
2014-03-03 15:54:02 -08:00
ret = SRSLTE_SUCCESS;
2014-03-03 15:54:02 -08:00
clean:
if (ret == SRSLTE_ERROR) {
2015-03-18 11:14:24 -07:00
srslte_cfo_free(h);
2014-06-17 02:11:41 -07:00
}
return ret;
2014-03-03 15:54:02 -08:00
}
2015-03-18 11:14:24 -07:00
void srslte_cfo_free(srslte_cfo_t *h) {
srslte_cexptab_free(&h->tab);
2014-06-17 02:11:41 -07:00
if (h->cur_cexp) {
free(h->cur_cexp);
}
bzero(h, sizeof(cf_t));
2014-03-03 15:54:02 -08:00
}
2015-03-18 11:14:24 -07:00
void srslte_cfo_set_tol(srslte_cfo_t *h, float tol) {
2014-06-17 02:11:41 -07:00
h->tol = tol;
2014-03-03 15:54:02 -08:00
}
2015-03-18 11:14:24 -07:00
int srslte_cfo_realloc(srslte_cfo_t *h, uint32_t samples) {
h->cur_cexp = realloc(h->cur_cexp, sizeof(cf_t) * samples);
if (!h->cur_cexp) {
perror("realloc");
return SRSLTE_ERROR;
}
2015-03-18 11:14:24 -07:00
srslte_cexptab_gen(&h->tab, h->cur_cexp, h->last_freq, samples);
h->nsamples = samples;
return SRSLTE_SUCCESS;
}
2015-03-18 11:14:24 -07:00
void srslte_cfo_correct(srslte_cfo_t *h, cf_t *input, cf_t *output, float freq) {
2014-06-17 02:11:41 -07:00
if (fabs(h->last_freq - freq) > h->tol) {
h->last_freq = freq;
2015-03-18 11:14:24 -07:00
srslte_cexptab_gen(&h->tab, h->cur_cexp, h->last_freq, h->nsamples);
DEBUG("CFO generating new table for frequency %.4fe-6\n", freq*1e6);
2014-06-17 02:11:41 -07:00
}
2015-03-18 11:14:24 -07:00
srslte_vec_prod_ccc(h->cur_cexp, input, output, h->nsamples);
2014-03-03 15:54:02 -08:00
}