srsLTE/srslte/lib/common/src/fft.c

198 lines
5.8 KiB
C
Raw Normal View History

/**
2014-01-28 03:41:17 -08:00
*
* \section COPYRIGHT
2014-01-28 03:41:17 -08:00
*
* Copyright 2013-2014 The libLTE Developers. See the
* COPYRIGHT file at the top-level directory of this distribution.
*
* \section LICENSE
*
* This file is part of the libLTE library.
*
* libLTE is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libLTE is distributed in the hope that it will be useful,
2014-01-28 03:41:17 -08:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* A copy of the GNU Lesser General Public License can be found in
* the LICENSE file in the top-level directory of this distribution
* and at http://www.gnu.org/licenses/.
*
2014-01-28 03:41:17 -08:00
*/
2014-01-31 06:35:43 -08:00
#include <string.h>
2014-01-28 03:41:17 -08:00
#include <strings.h>
#include <stdlib.h>
#include <complex.h>
#include <math.h>
2014-01-28 03:41:17 -08:00
#include "srslte/common/phy_common.h"
#include "srslte/utils/dft.h"
#include "srslte/common/fft.h"
#include "srslte/utils/debug.h"
#include "srslte/utils/vector.h"
2014-01-28 03:41:17 -08:00
2015-03-18 05:41:50 -07:00
int srslte_fft_init_(srslte_fft_t *q, srslte_cp_t cp, uint32_t nof_prb, dft_dir_t dir) {
2015-03-18 05:59:29 -07:00
int symbol_sz = srslte_symbol_sz(nof_prb);
2014-01-28 03:41:17 -08:00
if (symbol_sz < 0) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Error: Invalid nof_prb=%d\n", nof_prb);
return -1;
}
2014-06-20 07:35:37 -07:00
if (dft_plan_c(&q->fft_plan, symbol_sz, dir)) {
2014-06-17 02:11:41 -07:00
fprintf(stderr, "Error: Creating DFT plan\n");
return -1;
}
q->tmp = malloc((uint32_t) symbol_sz * sizeof(cf_t));
2014-06-17 02:11:41 -07:00
if (!q->tmp) {
perror("malloc");
return -1;
}
2014-01-31 06:35:43 -08:00
2014-06-20 07:35:37 -07:00
dft_plan_set_mirror(&q->fft_plan, true);
dft_plan_set_dc(&q->fft_plan, true);
q->symbol_sz = (uint32_t) symbol_sz;
2015-03-18 05:59:29 -07:00
q->nof_symbols = SRSLTE_CP_NSYMB(cp);
q->cp = cp;
q->freq_shift = false;
2015-03-18 05:59:29 -07:00
q->nof_re = nof_prb * SRSLTE_NRE;
2014-06-17 02:11:41 -07:00
q->nof_guards = ((symbol_sz - q->nof_re) / 2);
2015-03-18 05:59:29 -07:00
q->slot_sz = SRSLTE_SLOT_LEN(symbol_sz);
DEBUG("Init %s symbol_sz=%d, nof_symbols=%d, cp=%s, nof_re=%d, nof_guards=%d\n",
2014-06-17 02:11:41 -07:00
dir==FORWARD?"FFT":"iFFT", q->symbol_sz, q->nof_symbols,
2015-03-18 05:59:29 -07:00
q->cp==SRSLTE_SRSLTE_CP_NORM?"Normal":"Extended", q->nof_re, q->nof_guards);
return SRSLTE_SUCCESS;
2014-01-28 03:41:17 -08:00
}
2014-01-31 06:35:43 -08:00
2015-03-18 05:41:50 -07:00
void srslte_fft_free_(srslte_fft_t *q) {
2014-06-17 02:11:41 -07:00
dft_plan_free(&q->fft_plan);
if (q->tmp) {
free(q->tmp);
}
if (q->shift_buffer) {
free(q->shift_buffer);
}
2015-03-18 05:41:50 -07:00
bzero(q, sizeof(srslte_fft_t));
2014-01-28 03:41:17 -08:00
}
2015-03-18 05:41:50 -07:00
int srslte_fft_init(srslte_fft_t *q, srslte_cp_t cp, uint32_t nof_prb) {
return srslte_fft_init_(q, cp, nof_prb, FORWARD);
2014-01-28 03:41:17 -08:00
}
2014-01-31 06:35:43 -08:00
2015-03-18 05:41:50 -07:00
void srslte_fft_free(srslte_fft_t *q) {
srslte_fft_free_(q);
2014-01-28 03:41:17 -08:00
}
2014-01-31 06:35:43 -08:00
2015-03-18 05:41:50 -07:00
int lte_ifft_init(srslte_fft_t *q, srslte_cp_t cp, uint32_t nof_prb) {
uint32_t i;
int ret;
2015-03-18 05:41:50 -07:00
ret = srslte_fft_init_(q, cp, nof_prb, BACKWARD);
if (ret == SRSLTE_SUCCESS) {
dft_plan_set_norm(&q->fft_plan, true);
/* set now zeros at CP */
for (i=0;i<q->nof_symbols;i++) {
bzero(q->tmp, q->nof_guards * sizeof(cf_t));
bzero(&q->tmp[q->nof_re + q->nof_guards], q->nof_guards * sizeof(cf_t));
}
2014-06-17 02:11:41 -07:00
}
return ret;
2014-01-28 03:41:17 -08:00
}
2014-01-31 06:35:43 -08:00
/* Shifts the signal after the iFFT or before the FFT.
* Freq_shift is relative to inter-carrier spacing.
* Caution: This function shall not be called during run-time
*/
2015-03-18 05:41:50 -07:00
int srslte_fft_set_freq_shift(srslte_fft_t *q, float freq_shift) {
2015-03-18 05:59:29 -07:00
q->shift_buffer = vec_malloc(sizeof(cf_t) * SRSLTE_SF_LEN(q->symbol_sz));
if (!q->shift_buffer) {
perror("malloc");
return -1;
}
cf_t *ptr = q->shift_buffer;
for (uint32_t n=0;n<2;n++) {
for (uint32_t i=0;i<q->nof_symbols;i++) {
2015-03-18 05:59:29 -07:00
uint32_t cplen = SRSLTE_CP_ISNORM(q->cp)?SRSLTE_CP_NORM(i, q->symbol_sz):SRSLTE_CP_EXT(q->symbol_sz);
for (uint32_t t=0;t<q->symbol_sz+cplen;t++) {
ptr[t] = cexpf(I*2*M_PI*((float) t-(float)cplen)*freq_shift/q->symbol_sz);
}
ptr += q->symbol_sz+cplen;
}
}
/* Disable DC carrier addition */
dft_plan_set_dc(&q->fft_plan, false);
q->freq_shift = true;
return SRSLTE_SUCCESS;
}
2015-03-18 05:41:50 -07:00
void lte_ifft_free(srslte_fft_t *q) {
srslte_fft_free_(q);
2014-01-28 03:41:17 -08:00
}
/* Transforms input samples into output OFDM symbols.
* Performs FFT on a each symbol and removes CP.
*/
2015-03-18 05:41:50 -07:00
void srslte_fft_run_slot(srslte_fft_t *q, cf_t *input, cf_t *output) {
uint32_t i;
2014-06-17 02:11:41 -07:00
for (i=0;i<q->nof_symbols;i++) {
2015-03-18 05:59:29 -07:00
input += SRSLTE_CP_ISNORM(q->cp)?SRSLTE_CP_NORM(i, q->symbol_sz):SRSLTE_CP_EXT(q->symbol_sz);
2014-06-20 07:35:37 -07:00
dft_run_c(&q->fft_plan, input, q->tmp);
2014-06-17 02:11:41 -07:00
memcpy(output, &q->tmp[q->nof_guards], q->nof_re * sizeof(cf_t));
input += q->symbol_sz;
output += q->nof_re;
}
2014-01-28 03:41:17 -08:00
}
2015-03-18 05:41:50 -07:00
void srslte_fft_run_sf(srslte_fft_t *q, cf_t *input, cf_t *output) {
uint32_t n;
if (q->freq_shift) {
vec_prod_ccc(input, q->shift_buffer, input, 2*q->slot_sz);
}
for (n=0;n<2;n++) {
2015-03-18 05:41:50 -07:00
srslte_fft_run_slot(q, &input[n*q->slot_sz], &output[n*q->nof_re*q->nof_symbols]);
}
}
2014-01-28 03:41:17 -08:00
/* Transforms input OFDM symbols into output samples.
* Performs FFT on a each symbol and adds CP.
*/
2015-03-18 05:41:50 -07:00
void lte_ifft_run_slot(srslte_fft_t *q, cf_t *input, cf_t *output) {
uint32_t i, cp_len;
2014-06-17 02:11:41 -07:00
for (i=0;i<q->nof_symbols;i++) {
2015-03-18 05:59:29 -07:00
cp_len = SRSLTE_CP_ISNORM(q->cp)?SRSLTE_CP_NORM(i, q->symbol_sz):SRSLTE_CP_EXT(q->symbol_sz);
2014-06-17 02:11:41 -07:00
memcpy(&q->tmp[q->nof_guards], input, q->nof_re * sizeof(cf_t));
2014-06-20 07:35:37 -07:00
dft_run_c(&q->fft_plan, q->tmp, &output[cp_len]);
2014-06-17 02:11:41 -07:00
input += q->nof_re;
/* add CP */
memcpy(output, &output[q->symbol_sz], cp_len * sizeof(cf_t));
output += q->symbol_sz + cp_len;
}
2014-01-28 03:41:17 -08:00
}
2015-03-18 05:41:50 -07:00
void srslte_fft_set_normalize(srslte_fft_t *q, bool normalize_enable) {
2015-03-11 09:42:36 -07:00
dft_plan_set_norm(&q->fft_plan, normalize_enable);
}
2015-03-18 05:41:50 -07:00
void lte_ifft_run_sf(srslte_fft_t *q, cf_t *input, cf_t *output) {
uint32_t n;
for (n=0;n<2;n++) {
lte_ifft_run_slot(q, &input[n*q->nof_re*q->nof_symbols], &output[n*q->slot_sz]);
}
if (q->freq_shift) {
vec_prod_ccc(output, q->shift_buffer, output, 2*q->slot_sz);
}
}