mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
194 lines
5.4 KiB
C
194 lines
5.4 KiB
C
11 years ago
|
/**
|
||
11 years ago
|
*
|
||
11 years ago
|
* \section COPYRIGHT
|
||
11 years ago
|
*
|
||
9 years ago
|
* Copyright 2013-2015 Software Radio Systems Limited
|
||
11 years ago
|
*
|
||
|
* \section LICENSE
|
||
|
*
|
||
10 years ago
|
* This file is part of the srsLTE library.
|
||
11 years ago
|
*
|
||
10 years ago
|
* srsLTE is free software: you can redistribute it and/or modify
|
||
10 years ago
|
* it under the terms of the GNU Affero General Public License as
|
||
11 years ago
|
* published by the Free Software Foundation, either version 3 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
10 years ago
|
* srsLTE is distributed in the hope that it will be useful,
|
||
11 years ago
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
* GNU Affero General Public License for more details.
|
||
11 years ago
|
*
|
||
10 years ago
|
* A copy of the GNU Affero General Public License can be found in
|
||
11 years ago
|
* the LICENSE file in the top-level directory of this distribution
|
||
|
* and at http://www.gnu.org/licenses/.
|
||
|
*
|
||
11 years ago
|
*/
|
||
|
|
||
11 years ago
|
|
||
11 years ago
|
#include <math.h>
|
||
|
#include <complex.h>
|
||
|
#include <fftw3.h>
|
||
|
#include <string.h>
|
||
|
|
||
10 years ago
|
#include "srslte/dft/dft.h"
|
||
10 years ago
|
#include "srslte/utils/vector.h"
|
||
11 years ago
|
|
||
11 years ago
|
#define dft_ceil(a,b) ((a-1)/b+1)
|
||
|
#define dft_floor(a,b) (a/b)
|
||
11 years ago
|
|
||
10 years ago
|
int srslte_dft_plan(srslte_dft_plan_t *plan, const int dft_points, srslte_dft_dir_t dir,
|
||
|
srslte_dft_mode_t mode) {
|
||
|
if(mode == SRSLTE_DFT_COMPLEX){
|
||
|
return srslte_dft_plan_c(plan,dft_points,dir);
|
||
11 years ago
|
} else {
|
||
10 years ago
|
return srslte_dft_plan_r(plan,dft_points,dir);
|
||
11 years ago
|
}
|
||
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
static void allocate(srslte_dft_plan_t *plan, int size_in, int size_out, int len) {
|
||
11 years ago
|
plan->in = fftwf_malloc(size_in*len);
|
||
|
plan->out = fftwf_malloc(size_out*len);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int srslte_dft_plan_c(srslte_dft_plan_t *plan, const int dft_points, srslte_dft_dir_t dir) {
|
||
11 years ago
|
allocate(plan,sizeof(fftwf_complex),sizeof(fftwf_complex), dft_points);
|
||
10 years ago
|
int sign = (dir == SRSLTE_DFT_FORWARD) ? FFTW_FORWARD : FFTW_BACKWARD;
|
||
11 years ago
|
plan->p = fftwf_plan_dft_1d(dft_points, plan->in, plan->out, sign, 0U);
|
||
|
if (!plan->p) {
|
||
|
return -1;
|
||
|
}
|
||
|
plan->size = dft_points;
|
||
10 years ago
|
plan->mode = SRSLTE_DFT_COMPLEX;
|
||
11 years ago
|
plan->dir = dir;
|
||
10 years ago
|
plan->forward = (dir==SRSLTE_DFT_FORWARD)?true:false;
|
||
11 years ago
|
plan->mirror = false;
|
||
|
plan->db = false;
|
||
|
plan->norm = false;
|
||
11 years ago
|
plan->dc = false;
|
||
11 years ago
|
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
int srslte_dft_plan_r(srslte_dft_plan_t *plan, const int dft_points, srslte_dft_dir_t dir) {
|
||
11 years ago
|
allocate(plan,sizeof(float),sizeof(float), dft_points);
|
||
10 years ago
|
int sign = (dir == SRSLTE_DFT_FORWARD) ? FFTW_R2HC : FFTW_HC2R;
|
||
11 years ago
|
plan->p = fftwf_plan_r2r_1d(dft_points, plan->in, plan->out, sign, 0U);
|
||
|
if (!plan->p) {
|
||
|
return -1;
|
||
|
}
|
||
|
plan->size = dft_points;
|
||
10 years ago
|
plan->mode = SRSLTE_REAL;
|
||
11 years ago
|
plan->dir = dir;
|
||
10 years ago
|
plan->forward = (dir==SRSLTE_DFT_FORWARD)?true:false;
|
||
11 years ago
|
plan->mirror = false;
|
||
|
plan->db = false;
|
||
|
plan->norm = false;
|
||
11 years ago
|
plan->dc = false;
|
||
11 years ago
|
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void srslte_dft_plan_set_mirror(srslte_dft_plan_t *plan, bool val){
|
||
11 years ago
|
plan->mirror = val;
|
||
|
}
|
||
10 years ago
|
void srslte_dft_plan_set_db(srslte_dft_plan_t *plan, bool val){
|
||
11 years ago
|
plan->db = val;
|
||
|
}
|
||
10 years ago
|
void srslte_dft_plan_set_norm(srslte_dft_plan_t *plan, bool val){
|
||
11 years ago
|
plan->norm = val;
|
||
|
}
|
||
10 years ago
|
void srslte_dft_plan_set_dc(srslte_dft_plan_t *plan, bool val){
|
||
11 years ago
|
plan->dc = val;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
static void copy_pre(uint8_t *dst, uint8_t *src, int size_d, int len,
|
||
11 years ago
|
bool forward, bool mirror, bool dc) {
|
||
|
int offset = dc?1:0;
|
||
|
if(mirror && !forward){
|
||
|
int hlen = dft_floor(len,2);
|
||
11 years ago
|
memset(dst,0,size_d*offset);
|
||
11 years ago
|
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);
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
static void copy_post(uint8_t *dst, uint8_t *src, int size_d, int len,
|
||
11 years ago
|
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));
|
||
11 years ago
|
} else {
|
||
|
memcpy(dst,src,size_d*len);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void srslte_dft_run(srslte_dft_plan_t *plan, void *in, void *out) {
|
||
|
if(plan->mode == SRSLTE_DFT_COMPLEX) {
|
||
|
srslte_dft_run_c(plan,in,out);
|
||
11 years ago
|
} else {
|
||
10 years ago
|
srslte_dft_run_r(plan,in,out);
|
||
11 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
void srslte_dft_run_c_zerocopy(srslte_dft_plan_t *plan, cf_t *in, cf_t *out) {
|
||
|
fftwf_execute_dft(plan->p, in, out);
|
||
|
}
|
||
|
|
||
10 years ago
|
void srslte_dft_run_c(srslte_dft_plan_t *plan, cf_t *in, cf_t *out) {
|
||
11 years ago
|
float norm;
|
||
|
int i;
|
||
|
fftwf_complex *f_out = plan->out;
|
||
|
|
||
10 years ago
|
copy_pre((uint8_t*)plan->in, (uint8_t*)in, sizeof(cf_t), plan->size,
|
||
11 years ago
|
plan->forward, plan->mirror, plan->dc);
|
||
11 years ago
|
fftwf_execute(plan->p);
|
||
11 years ago
|
if (plan->norm) {
|
||
10 years ago
|
norm = 1.0/sqrtf(plan->size);
|
||
10 years ago
|
srslte_vec_sc_prod_cfc(f_out, norm, f_out, plan->size);
|
||
11 years ago
|
}
|
||
11 years ago
|
if (plan->db) {
|
||
11 years ago
|
for (i=0;i<plan->size;i++) {
|
||
|
f_out[i] = 10*log10(f_out[i]);
|
||
|
}
|
||
|
}
|
||
10 years ago
|
copy_post((uint8_t*)out, (uint8_t*)plan->out, sizeof(cf_t), plan->size,
|
||
11 years ago
|
plan->forward, plan->mirror, plan->dc);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void srslte_dft_run_r(srslte_dft_plan_t *plan, float *in, float *out) {
|
||
11 years ago
|
float norm;
|
||
|
int i;
|
||
|
int len = plan->size;
|
||
|
float *f_out = plan->out;
|
||
|
|
||
10 years ago
|
memcpy(plan->in,in,sizeof(float)*plan->size);
|
||
11 years ago
|
fftwf_execute(plan->p);
|
||
11 years ago
|
if (plan->norm) {
|
||
10 years ago
|
norm = 1.0/plan->size;
|
||
10 years ago
|
srslte_vec_sc_prod_fff(f_out, norm, f_out, plan->size);
|
||
11 years ago
|
}
|
||
11 years ago
|
if (plan->db) {
|
||
11 years ago
|
for (i=0;i<len;i++) {
|
||
11 years ago
|
f_out[i] = 10*log10(f_out[i]);
|
||
11 years ago
|
}
|
||
|
}
|
||
10 years ago
|
memcpy(out,plan->out,sizeof(float)*plan->size);
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void srslte_dft_plan_free(srslte_dft_plan_t *plan) {
|
||
11 years ago
|
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);
|
||
10 years ago
|
bzero(plan, sizeof(srslte_dft_plan_t));
|
||
11 years ago
|
}
|
||
|
|
||
|
|
||
|
|