-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathcorrelator.cc
129 lines (105 loc) · 4.09 KB
/
correlator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- c++ -*- */
/*
* Copyright 2013 Communications Engineering Lab (CEL) / Karlsruhe Institute of Technology (KIT)
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <gnuradio/io_signature.h>
#include <lte/correlator.h>
#include <cstdio>
#include <cmath>
#include <volk/volk.h>
#include <cstring>
namespace gr {
namespace lte {
correlator::correlator(gr_complex* in1, gr_complex* in2, gr_complex *out, int len):
d_in1(in1),
d_in2(in2),
d_out_p(out),
d_len(len)
{
//internal inputs and outputs
d_in = (gr_complex*) fftwf_malloc(sizeof(gr_complex)*d_len);
d_out = (gr_complex*) fftwf_malloc(sizeof(gr_complex)*d_len);
// FFTW Planner
d_plan_f = fftwf_plan_dft_1d(d_len, reinterpret_cast<fftwf_complex*>(d_in), reinterpret_cast<fftwf_complex*>(d_out), FFTW_FORWARD, FFTW_ESTIMATE);
d_plan_r = fftwf_plan_dft_1d(d_len, reinterpret_cast<fftwf_complex*>(d_in), reinterpret_cast<fftwf_complex*>(d_out), FFTW_BACKWARD, FFTW_ESTIMATE);
//results after fourier transform
d_f1 = (gr_complex*) fftwf_malloc(sizeof(gr_complex)*d_len);
d_f2 = (gr_complex*) fftwf_malloc(sizeof(gr_complex)*d_len);
d_res_f = (gr_complex*) fftwf_malloc(sizeof(gr_complex)*d_len);
//end result
d_res_t = (gr_complex*) fftwf_malloc(sizeof(gr_complex)*(2*d_len-1) );
d_abs = (float*) fftwf_malloc(sizeof(float)*(2*d_len-1) );
}
correlator::~correlator()
{
fftwf_destroy_plan(d_plan_f);
fftwf_destroy_plan(d_plan_r);
fftwf_free(d_in);
fftwf_free(d_out);
fftwf_free(d_f1);
fftwf_free(d_f2);
fftwf_free(d_res_f);
fftwf_free(d_res_t);
fftwf_free(d_abs);
}
void
correlator::execute()
{
// Transform first sequence
memcpy(d_in+1, d_in1, sizeof(gr_complex)*(d_len-1) );
d_in[0] = d_in1[d_len-1];//shift
// Transform and conjugate first input
fftwf_execute(d_plan_f);
memcpy(d_f1, d_out, sizeof(gr_complex)*d_len);
volk_32fc_conjugate_32fc_a(d_f1, d_f1, d_len); //numPoints (number of complex values)
// Transform second sequence
memcpy(d_in, d_in2, sizeof(gr_complex)*d_len);
fftwf_execute(d_plan_f);
memcpy(d_f2, d_out, sizeof(gr_complex)*d_len);
// Multiply in frequency domain
volk_32fc_x2_multiply_32fc_a(d_res_f, d_f1, d_f2, d_len);
// Transform back to time domain
memcpy(d_in, d_res_f, sizeof(gr_complex)*d_len);
fftwf_execute(d_plan_r);
// For now duplicate partly (should be changed later)
memcpy(d_res_t , d_out, sizeof(gr_complex)*d_len );
memcpy(d_res_t+d_len, d_out, sizeof(gr_complex)*(d_len-1));
// make result available
memcpy(d_out_p, d_out, sizeof(gr_complex)*d_len );
}
void
correlator::get_maximum(int &pos, float &max )
{
int rlen = (2*d_len-1);
volk_32fc_magnitude_32f_a(d_abs, d_res_t, rlen);
int fpos = 0;
float fmax = 0.0;
for (int i = 0 ; i < rlen; i++){
if(fmax < d_abs[i]){
fmax = d_abs[i];
fpos = i;
}
}
max = fmax;
pos = fpos;
}
} /* namespace lte */
} /* namespace gr */