Skip to content

Commit a191dbc

Browse files
committed
Began rewriting parts for speed
1 parent 8685926 commit a191dbc

11 files changed

+417
-27
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ FIND_PACKAGE( FFTW REQUIRED )
3232
INCLUDE_DIRECTORIES("${FFTW_INCLUDE_DIR}")
3333
FIND_PACKAGE( RTLSDR REQUIRED )
3434
INCLUDE_DIRECTORIES("${RTLSDR_INCLUDE_DIR}")
35+
#FIND_PACKAGE( Curses REQUIRED )
36+
#INCLUDE_DIRECTORIES("${CURSES_INCLUDE_DIR}")
3537

3638
# Add version information to build
3739
SET( ${PROJECT_NAME}_MAJOR_VERSION 0 )

include/LTE-Tracker.h

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
#ifndef HAVE_LTE_TRACKER_H
2+
#define HAVE_LTE_TRACKER_H
3+
4+
//
5+
// Data structures used to communicate between threads.
6+
//
7+
// A packet of information that is sent from the main thread to each
8+
// tracker thread.
9+
typedef struct {
10+
itpp::cvec data;
11+
uint8 slot_num;
12+
uint8 sym_num;
13+
double late;
14+
double frequency_offset;
15+
double frame_timing;
16+
} td_fifo_pdu_t;
17+
// Structure to describe a cell which is currently being tracked.
18+
class tracked_cell_t {
19+
public:
20+
// Initializer
21+
tracked_cell_t(
22+
const uint16 & n_id_cell,
23+
const int8 & n_ports,
24+
const cp_type_t::cp_type_t & cp_type,
25+
const double & frame_timing
26+
) : n_id_cell(n_id_cell), n_ports(n_ports), cp_type(cp_type), frame_timing(frame_timing) {
27+
fifo_peak_size=0;
28+
kill_me=false;
29+
sym_num=0;
30+
slot_num=0;
31+
target_cap_start_time=(cp_type==cp_type_t::NORMAL)?10:32;
32+
filling=0;
33+
buffer.set_size(128);
34+
buffer_offset=0;
35+
ac_fd.set_size(12);
36+
ac_fd=std::complex <double> (0,0);
37+
bulk_phase_offset=0;
38+
tracker_thread_ready=false;
39+
mib_decode_failures=0;
40+
crs_sp=itpp::vec(4);
41+
crs_sp=NAN;
42+
crs_np=itpp::vec(4);
43+
crs_np=NAN;
44+
crs_sp_av=itpp::vec(4);
45+
crs_sp_av=NAN;
46+
crs_np_av=itpp::vec(4);
47+
crs_np_av=NAN;
48+
}
49+
uint8 const n_symb_dl() const {
50+
return (cp_type==cp_type_t::NORMAL)?7:((cp_type==cp_type_t::EXTENDED)?6:-1);
51+
}
52+
boost::mutex mutex;
53+
boost::condition condition;
54+
boost::thread thread;
55+
// Indicates that the tracker process is ready to receive data.
56+
bool tracker_thread_ready;
57+
// These are not allowed to change
58+
const uint16 n_id_cell;
59+
const int8 n_ports;
60+
const cp_type_t::cp_type_t cp_type;
61+
// These are constantly changing
62+
double frame_timing;
63+
std::queue <td_fifo_pdu_t> fifo;
64+
uint32 fifo_peak_size;
65+
bool kill_me;
66+
// Calculated values returned by the cell tracker process.
67+
// Changing constantly.
68+
itpp::cvec ac_fd;
69+
// Tracker process uses these as local variables.
70+
double bulk_phase_offset;
71+
// The producer process (main) will use these members as
72+
// local variables...
73+
uint8 sym_num;
74+
uint8 slot_num;
75+
uint32 target_cap_start_time;
76+
double late;
77+
bool filling;
78+
itpp::cvec buffer;
79+
uint16 buffer_offset;
80+
double mib_decode_failures;
81+
itpp::vec crs_sp;
82+
itpp::vec crs_np;
83+
itpp::vec crs_sp_av;
84+
itpp::vec crs_np_av;
85+
private:
86+
};
87+
// Structure that is used to record all the tracked cells.
88+
typedef struct {
89+
// List of cells which are currently being tracked.
90+
// Only the searcher can add elements to this list.
91+
// Only the main thread can remove elements from this list.
92+
boost::mutex mutex;
93+
std::list <tracked_cell_t *> tracked_cells;
94+
} tracked_cell_list_t;
95+
// Global data shared by all threads
96+
typedef struct {
97+
// The frequency offset of the dongle. This value will be updated
98+
// continuously.
99+
boost::mutex frequency_offset_mutex;
100+
double frequency_offset;
101+
// This value will never change.
102+
double fc;
103+
} global_thread_data_t;
104+
// IPC between main thread and searcher thread covering data capture issues.
105+
typedef struct {
106+
boost::mutex mutex;
107+
boost::condition condition;
108+
bool request;
109+
itpp::cvec capbuf;
110+
double late;
111+
} capbuf_sync_t;
112+
// IPC between main thread and producer thread.
113+
typedef struct {
114+
boost::mutex mutex;
115+
boost::condition condition;
116+
std::deque <uint8> fifo;
117+
uint32 fifo_peak_size;
118+
} sampbuf_sync_t;
119+
120+
// Small helper function to increment the slot number and the symbol number.
121+
inline void slot_sym_inc(
122+
const uint8 n_symb_dl,
123+
uint8 & slot_num,
124+
uint8 & sym_num
125+
) {
126+
sym_num=itpp::mod(sym_num+1,n_symb_dl);
127+
if (sym_num==0)
128+
slot_num=itpp::mod(slot_num+1,20);
129+
}
130+
131+
void producer_thread(
132+
sampbuf_sync_t & sampbuf_sync,
133+
capbuf_sync_t & capbuf_sync,
134+
global_thread_data_t & global_thread_data,
135+
tracked_cell_list_t & tracked_cell_list,
136+
double & fc
137+
);
138+
139+
void tracker_thread(
140+
tracked_cell_t & tracked_cell,
141+
global_thread_data_t & global_thread_data
142+
);
143+
144+
void searcher_thread(
145+
capbuf_sync_t & capbuf_sync,
146+
global_thread_data_t & global_thread_data,
147+
tracked_cell_list_t & tracked_cell_list
148+
);
149+
150+
void display_thread(
151+
sampbuf_sync_t & sampbuf_sync,
152+
global_thread_data_t & global_thread_data,
153+
tracked_cell_list_t & tracked_cell_list
154+
);
155+
156+
#endif
157+

include/dsp.h

+36-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,42 @@ double sigpower(const myType v) {
3434
#define dft(A) (fft(A)/sqrt(length(A)))
3535

3636
// Shift a vector in frequency
37-
itpp::cvec fshift(const itpp::cvec & v,const double f,const double fs);
38-
itpp::cvec fshift(const itpp::cvec & v,const double f);
37+
//itpp::cvec fshift(const itpp::cvec & v,const double f,const double fs);
38+
//itpp::cvec fshift(const itpp::cvec & v,const double f);
39+
// Shift vector seq up by f Hz assuming that seq was sampled at fs Hz.
40+
inline itpp::cvec fshift(const itpp::cvec &seq,const double f,const double fs) {
41+
//std::complex <double> k=std::complex<double>(0,pi*f/(fs/2));
42+
double k=itpp::pi*f/(fs/2);
43+
const uint32 len=length(seq);
44+
itpp::cvec r(len);
45+
std::complex<double>coeff;
46+
for (uint32 t=0;t<len;t++) {
47+
coeff.real()=cos(k*t);
48+
coeff.imag()=sin(k*t);
49+
//r(t)=seq(t)*exp(k*((double)t));
50+
r(t)=seq(t)*coeff;
51+
}
52+
return r;
53+
}
54+
// Shift vector seq up by f Hz assuming that seq was sampled at 2 Hz.
55+
inline itpp::cvec fshift(const itpp::cvec &seq,const double f) {
56+
return fshift(seq,f,2);
57+
}
58+
inline void fshift_inplace(itpp::cvec &seq,const double f,const double fs) {
59+
//std::complex <double> k=std::complex<double>(0,pi*f/(fs/2));
60+
double k=itpp::pi*f/(fs/2);
61+
const uint32 len=length(seq);
62+
std::complex<double>coeff;
63+
for (uint32 t=0;t<len;t++) {
64+
coeff.real()=cos(k*t);
65+
coeff.imag()=sin(k*t);
66+
//r(t)=seq(t)*exp(k*((double)t));
67+
seq(t)*=coeff;
68+
}
69+
}
70+
inline void fshift_inplace(itpp::cvec &seq,const double f) {
71+
fshift_inplace(seq,f,2);
72+
}
3973

4074
// Shift a vector in time
4175
template <class vectype>

src/CMakeLists.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Create a library of all the shared functions.
22
add_library(LTE_MISC capbuf.cpp constants.cpp itpp_ext.cpp macros.cpp searcher.cpp common.cpp dsp.cpp lte_lib.cpp)
33

4-
SET (common_link_libs ${Boost_LIBRARIES} ${Boost_THREAD_LIBRARY} ${LAPACK_LIBRARIES} ${FFTW_LIBRARIES} ${RTLSDR_LIBRARIES})
4+
SET (common_link_libs ${Boost_LIBRARIES} ${Boost_THREAD_LIBRARY} ${LAPACK_LIBRARIES} ${FFTW_LIBRARIES} ${RTLSDR_LIBRARIES} ${CURSES_LIBRARIES})
55

66
# Create the cell search executable
77
ADD_EXECUTABLE (CellSearch CellSearch.cpp)
@@ -10,8 +10,8 @@ TARGET_LINK_LIBRARIES (CellSearch debug itpp_debug ${common_link_libs})
1010
TARGET_LINK_LIBRARIES (CellSearch optimized itpp ${common_link_libs})
1111

1212
# Create the cell tracker
13-
ADD_EXECUTABLE (LTE-Tracker LTE-Tracker.cpp producer_thread.cpp tracker_thread.cpp searcher_thread.cpp)
14-
TARGET_LINK_LIBRARIES (LTE-Tracker general LTE_MISC)
13+
ADD_EXECUTABLE (LTE-Tracker LTE-Tracker.cpp producer_thread.cpp tracker_thread.cpp searcher_thread.cpp display_thread.cpp)
14+
TARGET_LINK_LIBRARIES (LTE-Tracker general LTE_MISC curses)
1515
TARGET_LINK_LIBRARIES (LTE-Tracker debug itpp_debug ${common_link_libs})
1616
TARGET_LINK_LIBRARIES (LTE-Tracker optimized itpp ${common_link_libs})
1717

src/LTE-Tracker.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,7 @@ static void rtlsdr_callback(
849849
for (uint32 t=0;t<len;t++) {
850850
sampbuf_sync.fifo.push_back(buf[t]);
851851
}
852+
sampbuf_sync.fifo_peak_size=MAX(sampbuf_sync.fifo.size(),sampbuf_sync.fifo_peak_size);
852853
sampbuf_sync.condition.notify_one();
853854
}
854855

@@ -911,6 +912,11 @@ int main(
911912
// Start the producer thread.
912913
boost::thread producer_thr(producer_thread,boost::ref(sampbuf_sync),boost::ref(capbuf_sync),boost::ref(global_thread_data),boost::ref(tracked_cell_list),boost::ref(fc));
913914

915+
sampbuf_sync.fifo_peak_size=0;
916+
917+
// Launch the display thread
918+
boost::thread display_thr(display_thread,boost::ref(sampbuf_sync),boost::ref(global_thread_data),boost::ref(tracked_cell_list));
919+
914920
// Start the async read process. This should never return.
915921
rtlsdr_read_async(dev,rtlsdr_callback,(void *)&sampbuf_sync,0,0);
916922

src/display_thread.cpp

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
// Copyright 2012 Evrytania LLC (http://www.evrytania.com)
2+
//
3+
// Written by James Peroulas <[email protected]>
4+
//
5+
// This program is free software: you can redistribute it and/or modify
6+
// it under the terms of the GNU Affero General Public License as published by
7+
// the Free Software Foundation, either version 3 of the License, or
8+
// (at your option) any later version.
9+
//
10+
// This program is distributed in the hope that it will be useful,
11+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
// GNU Affero General Public License for more details.
14+
//
15+
// You should have received a copy of the GNU Affero General Public License
16+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
18+
#include <stdio.h>
19+
#include <stdlib.h>
20+
#include <getopt.h>
21+
#include <unistd.h>
22+
#include <itpp/itbase.h>
23+
#include <itpp/signal/transforms.h>
24+
#include <boost/math/special_functions/gamma.hpp>
25+
#include <boost/thread.hpp>
26+
#include <boost/thread/condition.hpp>
27+
#include <list>
28+
#include <sstream>
29+
#include <signal.h>
30+
#include <queue>
31+
#include <curses.h>
32+
#include "rtl-sdr.h"
33+
#include "common.h"
34+
#include "macros.h"
35+
#include "lte_lib.h"
36+
#include "constants.h"
37+
#include "capbuf.h"
38+
#include "itpp_ext.h"
39+
#include "searcher.h"
40+
#include "dsp.h"
41+
#include "rtl-sdr.h"
42+
#include "LTE-Tracker.h"
43+
44+
using namespace itpp;
45+
using namespace std;
46+
47+
// Process that takes samples and distributes them to the appropriate
48+
// process.
49+
void display_thread(
50+
sampbuf_sync_t & sampbuf_sync,
51+
global_thread_data_t & global_thread_data,
52+
tracked_cell_list_t & tracked_cell_list
53+
) {
54+
initscr();
55+
// What does this do?
56+
(void)echo();
57+
while (true) {
58+
clear();
59+
printw("LTE-Tracker v%i.%i.%i\n\n",MAJOR_VERSION,MINOR_VERSION,PATCH_LEVEL);
60+
{
61+
boost::mutex::scoped_lock lock(global_thread_data.frequency_offset_mutex);
62+
double frequency_offset=global_thread_data.frequency_offset;
63+
//cout << "System frequency offset: " << frequency_offset << endl;
64+
printw("Global FO: %6.0lf",frequency_offset);
65+
}
66+
67+
{
68+
boost::mutex::scoped_lock lock(sampbuf_sync.mutex);
69+
//cout << "Raw sample buffer size: " << sampbuf_sync.fifo.size() << "/" << sampbuf_sync.fifo_peak_size << endl;
70+
printw(" Raw buffer status: %6li/%6li\n\n",sampbuf_sync.fifo.size(),sampbuf_sync.fifo_peak_size);
71+
}
72+
73+
{
74+
boost::mutex::scoped_lock lock(tracked_cell_list.mutex);
75+
list <tracked_cell_t *>::iterator it=tracked_cell_list.tracked_cells.begin();
76+
while (it!=tracked_cell_list.tracked_cells.end()) {
77+
tracked_cell_t & tracked_cell=(*(*it));
78+
79+
boost::mutex::scoped_lock lock(tracked_cell.mutex);
80+
81+
//cout << "Cell ID " << tracked_cell.n_id_cell << " TO: " << setprecision(10) << (*(*it)).frame_timing << " peak input buffer size: " << tracked_cell.fifo.size() << "/" << tracked_cell.fifo_peak_size << endl;
82+
printw("Cell ID %3i TO: %8.2lf cell buffer status %5i/%5i MIB Failures: %3.0lf\n",
83+
tracked_cell.n_id_cell,
84+
tracked_cell.frame_timing,
85+
tracked_cell.fifo.size(),
86+
tracked_cell.fifo_peak_size,
87+
tracked_cell.mib_decode_failures
88+
);
89+
90+
for (uint8 t=0;t<tracked_cell.n_ports;t++) {
91+
printw(" P%i CRS SP/NP/SNR %5.1lf/%5.1lf/%5.1lf CRS(AVG) SP/NP/SNR %5.1lf/%5.1lf/%5.1lf",
92+
t,
93+
db10(tracked_cell.crs_sp(t)),
94+
db10(tracked_cell.crs_np(t)),
95+
db10(tracked_cell.crs_sp(t)/tracked_cell.crs_np(t)),
96+
db10(tracked_cell.crs_sp_av(t)),
97+
db10(tracked_cell.crs_np_av(t)),
98+
db10(tracked_cell.crs_sp_av(t)/tracked_cell.crs_np_av(t))
99+
);
100+
int8 CB=-1;
101+
for (uint8 k=1;k<12;k++) {
102+
if (abs(tracked_cell.ac_fd(k))<=abs(tracked_cell.ac_fd(0))/2) {
103+
CB=k;
104+
break;
105+
}
106+
}
107+
if (CB==-1) {
108+
printw(" >990 kHz\n");
109+
} else {
110+
printw(" %4i kHz\n",CB*90);
111+
}
112+
}
113+
114+
/*
115+
for (uint8 t=0;t<tracked_cell.n_ports;t++) {
116+
stringstream ss;
117+
ss << abs(tracked_cell.ac_fd()) << endl;
118+
printw("%s",ss.str());
119+
}
120+
*/
121+
122+
++it;
123+
}
124+
}
125+
refresh();
126+
127+
sleep(1);
128+
}
129+
}
130+

src/dsp.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
using namespace itpp;
3030
using namespace std;
3131

32+
/*
3233
// Shift vector seq up by f Hz assuming that seq was sampled at fs Hz.
3334
cvec fshift(const cvec &seq,const double f,const double fs) {
3435
complex <double> k=complex<double>(0,pi*f/(fs/2));
@@ -43,6 +44,7 @@ cvec fshift(const cvec &seq,const double f,const double fs) {
4344
cvec fshift(const cvec &seq,const double f) {
4445
return fshift(seq,f,2);
4546
}
47+
*/
4648

4749
// Perform FFT based interpolation. Assuming input signal is cyclically
4850
// repeating signal sampled at M points, return a cyclically repeating

0 commit comments

Comments
 (0)