2014-03-03 11:09:26 -08:00
|
|
|
/**
|
2014-01-28 03:41:17 -08:00
|
|
|
*
|
2014-03-03 11:09:26 -08:00
|
|
|
* \section COPYRIGHT
|
2014-01-28 03:41:17 -08:00
|
|
|
*
|
2014-03-03 11:09:26 -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.
|
|
|
|
*
|
2014-03-03 11:09:26 -08:00
|
|
|
* 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-03-03 11:09:26 -08:00
|
|
|
|
2014-01-28 03:41:17 -08:00
|
|
|
#include <math.h>
|
|
|
|
#include <complex.h>
|
|
|
|
#include <fftw3.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2014-06-17 06:20:00 -07:00
|
|
|
#include "liblte/phy/utils/dft.h"
|
2014-07-30 03:05:17 -07:00
|
|
|
#include "liblte/phy/utils/vector.h"
|
2014-01-28 03:41:17 -08:00
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
#define dft_ceil(a,b) ((a-1)/b+1)
|
|
|
|
#define dft_floor(a,b) (a/b)
|
2014-01-28 03:41:17 -08:00
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
int dft_plan(dft_plan_t *plan, const int dft_points, dft_dir_t dir,
|
|
|
|
dft_mode_t mode) {
|
|
|
|
if(mode == COMPLEX){
|
|
|
|
return dft_plan_c(plan,dft_points,dir);
|
|
|
|
} else {
|
|
|
|
return dft_plan_r(plan,dft_points,dir);
|
2014-06-17 02:11:41 -07:00
|
|
|
}
|
|
|
|
return 0;
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void allocate(dft_plan_t *plan, int size_in, int size_out, int len) {
|
2014-06-17 02:11:41 -07:00
|
|
|
plan->in = fftwf_malloc(size_in*len);
|
|
|
|
plan->out = fftwf_malloc(size_out*len);
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
int dft_plan_c(dft_plan_t *plan, const int dft_points, dft_dir_t dir) {
|
2014-06-17 02:11:41 -07:00
|
|
|
allocate(plan,sizeof(fftwf_complex),sizeof(fftwf_complex), dft_points);
|
2014-06-20 07:35:37 -07:00
|
|
|
int sign = (dir == FORWARD) ? FFTW_FORWARD : FFTW_BACKWARD;
|
2014-06-17 02:11:41 -07:00
|
|
|
plan->p = fftwf_plan_dft_1d(dft_points, plan->in, plan->out, sign, 0U);
|
|
|
|
if (!plan->p) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
plan->size = dft_points;
|
2014-06-20 07:35:37 -07:00
|
|
|
plan->mode = COMPLEX;
|
|
|
|
plan->dir = dir;
|
|
|
|
plan->forward = (dir==FORWARD)?true:false;
|
|
|
|
plan->mirror = false;
|
|
|
|
plan->db = false;
|
|
|
|
plan->norm = false;
|
2014-06-20 10:55:47 -07:00
|
|
|
plan->dc = false;
|
2014-01-28 03:41:17 -08:00
|
|
|
|
2014-06-17 02:11:41 -07:00
|
|
|
return 0;
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
int dft_plan_r(dft_plan_t *plan, const int dft_points, dft_dir_t dir) {
|
2014-06-17 02:11:41 -07:00
|
|
|
allocate(plan,sizeof(float),sizeof(float), dft_points);
|
2014-06-20 07:35:37 -07:00
|
|
|
int sign = (dir == FORWARD) ? FFTW_R2HC : FFTW_HC2R;
|
2014-06-17 02:11:41 -07:00
|
|
|
plan->p = fftwf_plan_r2r_1d(dft_points, plan->in, plan->out, sign, 0U);
|
|
|
|
if (!plan->p) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
plan->size = dft_points;
|
2014-06-20 07:35:37 -07:00
|
|
|
plan->mode = REAL;
|
|
|
|
plan->dir = dir;
|
|
|
|
plan->forward = (dir==FORWARD)?true:false;
|
|
|
|
plan->mirror = false;
|
|
|
|
plan->db = false;
|
|
|
|
plan->norm = false;
|
2014-06-20 10:55:47 -07:00
|
|
|
plan->dc = false;
|
2014-01-28 03:41:17 -08:00
|
|
|
|
2014-06-17 02:11:41 -07:00
|
|
|
return 0;
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
void dft_plan_set_mirror(dft_plan_t *plan, bool val){
|
|
|
|
plan->mirror = val;
|
|
|
|
}
|
|
|
|
void dft_plan_set_db(dft_plan_t *plan, bool val){
|
|
|
|
plan->db = val;
|
|
|
|
}
|
|
|
|
void dft_plan_set_norm(dft_plan_t *plan, bool val){
|
|
|
|
plan->norm = val;
|
|
|
|
}
|
|
|
|
void dft_plan_set_dc(dft_plan_t *plan, bool val){
|
|
|
|
plan->dc = val;
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
static void copy_pre(char *dst, char *src, int size_d, int len,
|
|
|
|
bool forward, bool mirror, bool dc) {
|
|
|
|
int offset = dc?1:0;
|
|
|
|
if(mirror && !forward){
|
|
|
|
int hlen = dft_floor(len,2);
|
2014-06-17 02:11:41 -07:00
|
|
|
memset(dst,0,size_d*offset);
|
2014-06-20 07:35:37 -07:00
|
|
|
memcpy(&dst[size_d*offset], &src[size_d*hlen], size_d*(len-hlen-offset));
|
|
|
|
memcpy(&dst[(len-hlen)*size_d], src, size_d*hlen);
|
|
|
|
} else {
|
|
|
|
memcpy(dst,src,size_d*len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void copy_post(char *dst, char *src, int size_d, int len,
|
|
|
|
bool forward, bool mirror, bool dc) {
|
|
|
|
int offset = dc?1:0;
|
|
|
|
if(mirror && forward){
|
|
|
|
int hlen = dft_ceil(len,2);
|
|
|
|
memcpy(dst, &src[size_d*hlen], size_d*(len-hlen));
|
|
|
|
memcpy(&dst[(len-hlen)*size_d], &src[size_d*offset], size_d*(hlen-offset));
|
2014-06-17 02:11:41 -07:00
|
|
|
} else {
|
|
|
|
memcpy(dst,src,size_d*len);
|
|
|
|
}
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void dft_run(dft_plan_t *plan, void *in, void *out) {
|
2014-06-20 07:35:37 -07:00
|
|
|
if(plan->mode == COMPLEX) {
|
|
|
|
dft_run_c(plan,in,out);
|
|
|
|
} else {
|
|
|
|
dft_run_r(plan,in,out);
|
2014-06-17 02:11:41 -07:00
|
|
|
}
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
void dft_run_c(dft_plan_t *plan, dft_c_t *in, dft_c_t *out) {
|
2014-06-17 02:11:41 -07:00
|
|
|
float norm;
|
|
|
|
int i;
|
|
|
|
fftwf_complex *f_out = plan->out;
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
copy_pre((char*)plan->in, (char*)in, sizeof(dft_c_t), plan->size,
|
|
|
|
plan->forward, plan->mirror, plan->dc);
|
2014-06-17 02:11:41 -07:00
|
|
|
fftwf_execute(plan->p);
|
2014-06-20 07:35:37 -07:00
|
|
|
if (plan->norm) {
|
2014-07-30 03:05:17 -07:00
|
|
|
norm = 1.0/sqrtf(plan->size);
|
|
|
|
vec_sc_prod_cfc(f_out, norm, f_out, plan->size);
|
2014-06-17 02:11:41 -07:00
|
|
|
}
|
2014-06-20 07:35:37 -07:00
|
|
|
if (plan->db) {
|
2014-06-17 02:11:41 -07:00
|
|
|
for (i=0;i<plan->size;i++) {
|
|
|
|
f_out[i] = 10*log10(f_out[i]);
|
|
|
|
}
|
|
|
|
}
|
2014-06-20 07:35:37 -07:00
|
|
|
copy_post((char*)out, (char*)plan->out, sizeof(dft_c_t), plan->size,
|
|
|
|
plan->forward, plan->mirror, plan->dc);
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
void dft_run_r(dft_plan_t *plan, dft_r_t *in, dft_r_t *out) {
|
2014-06-17 02:11:41 -07:00
|
|
|
float norm;
|
|
|
|
int i;
|
|
|
|
int len = plan->size;
|
|
|
|
float *f_out = plan->out;
|
|
|
|
|
2014-06-20 07:35:37 -07:00
|
|
|
memcpy(plan->in,in,sizeof(dft_r_t)*plan->size);
|
2014-06-17 02:11:41 -07:00
|
|
|
fftwf_execute(plan->p);
|
2014-06-20 07:35:37 -07:00
|
|
|
if (plan->norm) {
|
2014-07-30 03:05:17 -07:00
|
|
|
norm = 1.0/plan->size;
|
|
|
|
vec_sc_prod_fff(f_out, norm, f_out, plan->size);
|
2014-06-17 02:11:41 -07:00
|
|
|
}
|
2014-06-20 07:35:37 -07:00
|
|
|
if (plan->db) {
|
2014-06-17 02:11:41 -07:00
|
|
|
for (i=0;i<len;i++) {
|
2014-06-20 07:35:37 -07:00
|
|
|
f_out[i] = 10*log10(f_out[i]);
|
2014-06-17 02:11:41 -07:00
|
|
|
}
|
|
|
|
}
|
2014-06-20 07:35:37 -07:00
|
|
|
memcpy(out,plan->out,sizeof(dft_r_t)*plan->size);
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void dft_plan_free(dft_plan_t *plan) {
|
2014-06-17 02:11:41 -07:00
|
|
|
if (!plan) return;
|
|
|
|
if (!plan->size) return;
|
|
|
|
if (plan->in) fftwf_free(plan->in);
|
|
|
|
if (plan->out) fftwf_free(plan->out);
|
|
|
|
if (plan->p) fftwf_destroy_plan(plan->p);
|
|
|
|
bzero(plan, sizeof(dft_plan_t));
|
2014-01-28 03:41:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|