Skip to content

Commit cac2138

Browse files
committedApr 10, 2014
Tracker works with hackrf now! AGC seems important, and needs to be implemented
1 parent c33ffdb commit cac2138

File tree

6 files changed

+121
-51
lines changed

6 files changed

+121
-51
lines changed
 

‎include/LTE-Tracker.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,17 @@ class global_thread_data_t {
208208
boost::mutex::scoped_lock lock(opencl_device_mutex);
209209
opencl_device_private=f;
210210
}
211+
212+
inline int dev_use() {
213+
boost::mutex::scoped_lock lock(dev_use_mutex);
214+
int r=dev_use_private;
215+
return r;
216+
}
217+
inline void dev_use(const int & f) {
218+
boost::mutex::scoped_lock lock(dev_use_mutex);
219+
dev_use_private=f;
220+
}
221+
211222
inline uint16 opencl_platform() {
212223
boost::mutex::scoped_lock lock(opencl_platform_mutex);
213224
uint16 r=opencl_platform_private;
@@ -325,6 +336,9 @@ class global_thread_data_t {
325336
boost::mutex opencl_platform_mutex;
326337
uint16 opencl_platform_private;
327338

339+
boost::mutex dev_use_mutex;
340+
int dev_use_private;
341+
328342
boost::mutex opencl_device_mutex;
329343
uint16 opencl_device_private;
330344

@@ -372,7 +386,7 @@ typedef struct {
372386
typedef struct {
373387
boost::mutex mutex;
374388
boost::condition condition;
375-
std::deque <uint8> fifo;
389+
std::deque <int8> fifo;
376390
uint32 fifo_peak_size;
377391
} sampbuf_sync_t;
378392

‎src/CellSearch.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -608,8 +608,8 @@ int config_hackrf(
608608
double & fs_programmed
609609
) {
610610

611-
unsigned int lna_gain=16; // default value
612-
unsigned int vga_gain=60; // default value
611+
unsigned int lna_gain=32; // default value
612+
unsigned int vga_gain=40; // default value
613613

614614
int result = hackrf_init();
615615
if( result != HACKRF_SUCCESS ) {
@@ -943,7 +943,7 @@ int main(
943943
fci = n_fc_multi_try; // end of loop
944944
continue;
945945
}
946-
// cout << capbuf(0, 100) << "\n";
946+
cout << ( sum( abs(real(capbuf)) )/length(capbuf) ) << "\n";
947947

948948
freq_correction = fc_programmed*(correction-1)/correction;
949949
// if (!dongle_used) { // if dongle is not used, do correction explicitly. Because if dongle is used, the correction is done when tuning dongle's frequency.

‎src/LTE-Tracker.cpp

+90-36
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,8 @@ int config_hackrf(
698698
hackrf_device *& device,
699699
double & fs_programmed
700700
) {
701-
unsigned int lna_gain=16; // default value
702-
unsigned int vga_gain=60; // default value
701+
unsigned int lna_gain=32; // default value
702+
unsigned int vga_gain=40; // default value
703703

704704
int result = hackrf_init();
705705
if( result != HACKRF_SUCCESS ) {
@@ -1174,25 +1174,25 @@ double kalibrate(
11741174
return best.freq_superfine;
11751175
}
11761176

1177+
sampbuf_sync_t sampbuf_sync;
1178+
11771179
#ifdef HAVE_HACKRF
11781180

11791181
static int hackrf_callback(hackrf_transfer* transfer)
1180-
{
1181-
sampbuf_sync_t & sampbuf_sync=*((sampbuf_sync_t *)(transfer->rx_ctx));
1182-
1183-
//cout << "Callback with " << len << " samples" << endl;
1184-
1182+
{
1183+
// cout << "1\n";
11851184
if (transfer->valid_length==0) {
11861185
cerr << "Error: received no samples from HACKRF device..." << endl;
11871186
ABORT(-1);
11881187
}
11891188

11901189
boost::mutex::scoped_lock lock(sampbuf_sync.mutex);
11911190
for (uint32 t=0;t<(uint32)transfer->valid_length;t++) {
1192-
sampbuf_sync.fifo.push_back(transfer->buffer[t]);
1191+
sampbuf_sync.fifo.push_back((int8)transfer->buffer[t]);
11931192
}
11941193
sampbuf_sync.fifo_peak_size=MAX(sampbuf_sync.fifo.size(),sampbuf_sync.fifo_peak_size);
11951194
sampbuf_sync.condition.notify_one();
1195+
11961196
return(0);
11971197
}
11981198

@@ -1214,7 +1214,7 @@ static void rtlsdr_callback(
12141214

12151215
boost::mutex::scoped_lock lock(sampbuf_sync.mutex);
12161216
for (uint32 t=0;t<len;t++) {
1217-
sampbuf_sync.fifo.push_back(buf[t]);
1217+
sampbuf_sync.fifo.push_back(buf[t]-128);
12181218
}
12191219
sampbuf_sync.fifo_peak_size=MAX(sampbuf_sync.fifo.size(),sampbuf_sync.fifo_peak_size);
12201220
sampbuf_sync.condition.notify_one();
@@ -1305,8 +1305,32 @@ int main(
13051305
double initial_k_factor = 1;
13061306
double initial_freq_offset=kalibrate(fc_requested,fs_programmed,ppm,correction,correction_new,use_recorded_data,filename,rtl_sdr_format,noise_power,drop_secs,repeat,dev,hackrf_dev,dev_use,fc_programmed,initial_sampling_carrier_twist,initial_k_factor,record_bin_filename,load_bin_filename,opencl_platform,opencl_device,filter_workitem,xcorr_workitem,num_reserve);
13071307

1308+
// // ---------------- stop and close hackrf
1309+
// Real_Timer tt;
1310+
// tt.tic();
1311+
// while(tt.get_time()<2) {;}
1312+
// if ( dev_use == dev_type_t::HACKRF ) {
1313+
// #ifdef HAVE_HACKRF
1314+
// int result = hackrf_stop_rx(hackrf_dev);
1315+
// if( result != HACKRF_SUCCESS ) {
1316+
// printf("hackrf_stop_rx() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1317+
// ABORT(-1);
1318+
// }
1319+
// result = hackrf_close(hackrf_dev);
1320+
// if( result != HACKRF_SUCCESS ) {
1321+
// printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1322+
// ABORT(-1);
1323+
// }
1324+
// result = hackrf_exit();
1325+
// if( result != HACKRF_SUCCESS ) {
1326+
// printf("hackrf_exit() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1327+
// ABORT(-1);
1328+
// }
1329+
// #endif
1330+
// }
1331+
13081332
// Data shared between threads
1309-
sampbuf_sync_t sampbuf_sync;
1333+
// sampbuf_sync_t sampbuf_sync;
13101334
tracked_cell_list_t tracked_cell_list;
13111335
capbuf_sync_t capbuf_sync;
13121336
global_thread_data_t global_thread_data(fc_requested,fc_programmed,fs_programmed);
@@ -1322,6 +1346,8 @@ int main(
13221346
global_thread_data.frequency_offset(initial_freq_offset);
13231347
global_thread_data.initial_frequency_offset(initial_freq_offset);
13241348

1349+
global_thread_data.dev_use(dev_use);
1350+
13251351
global_thread_data.k_factor(initial_k_factor);
13261352
global_thread_data.correction(correction_new);
13271353
global_thread_data.sampling_carrier_twist(initial_sampling_carrier_twist);
@@ -1363,8 +1389,10 @@ int main(
13631389
// for (uint32 t=0;t<192000;t++) {
13641390
for (uint32 t=0;t<(uint32)( file_data.length() );t++) {
13651391
complex <double> samp=file_data[offset]+randn_c()*sqrt(noise_power);
1366-
uint8 samp_real=RAIL(round_i(real(samp)*128.0+128.0),0,255); // 127 should be 128?
1367-
uint8 samp_imag=RAIL(round_i(imag(samp)*128.0+128.0),0,255); // 127 should be 128?
1392+
// uint8 samp_real=RAIL(round_i(real(samp)*128.0+128.0),0,255); // 127 should be 128?
1393+
// uint8 samp_imag=RAIL(round_i(imag(samp)*128.0+128.0),0,255); // 127 should be 128?
1394+
int8 samp_real=round_i(real(samp)*128.0); // 127 should be 128?
1395+
int8 samp_imag=round_i(imag(samp)*128.0); // 127 should be 128?
13681396
sampbuf_sync.fifo.push_back(samp_real);
13691397
sampbuf_sync.fifo.push_back(samp_imag);
13701398
offset++;
@@ -1394,32 +1422,58 @@ int main(
13941422
// Start the async read process. This should never return.
13951423
rtlsdr_read_async(dev,rtlsdr_callback,(void *)&sampbuf_sync,0,0);
13961424
} else if (dev_use == dev_type_t::HACKRF) {
1397-
#ifdef HAVE_HACKRF
1398-
1399-
int result = hackrf_stop_rx(hackrf_dev);
1400-
if( result != HACKRF_SUCCESS ) {
1401-
printf("hackrf_stop_rx() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1402-
ABORT(-1);
1403-
}
1404-
1405-
result = hackrf_start_rx(hackrf_dev, hackrf_callback, (void *)&sampbuf_sync);
1406-
1407-
if( result != HACKRF_SUCCESS ) {
1408-
printf("hackrf_start_rx() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1409-
ABORT(-1);
1425+
cvec capbuf;
1426+
while(1) {
1427+
capture_data(fc_requested,correction,false,record_bin_filename,use_recorded_data,load_bin_filename,".",dev,hackrf_dev, dev_use,capbuf,fc_programmed,fs_programmed,false);
1428+
// cout << "cap\n";
1429+
boost::mutex::scoped_lock lock(sampbuf_sync.mutex);
1430+
for (uint32 t=0;t<(uint32)length(capbuf);t++) {
1431+
sampbuf_sync.fifo.push_back( (int8)(round_i( real( capbuf[t] )*128) ) );
1432+
sampbuf_sync.fifo.push_back( (int8)(round_i( imag( capbuf[t] )*128) ) );
1433+
}
1434+
sampbuf_sync.fifo_peak_size=MAX(sampbuf_sync.fifo.size(),sampbuf_sync.fifo_peak_size);
1435+
sampbuf_sync.condition.notify_one();
14101436
}
1437+
// #ifdef HAVE_HACKRF
1438+
//
1439+
// tt.tic();
1440+
// while(tt.get_time()<10) {;}
1441+
// if ( config_hackrf(initial_sampling_carrier_twist,correction,device_index,fc_requested,hackrf_dev,fs_programmed) != 0 ) {
1442+
// cout << "HACKRF re-config failed!\n";
1443+
// }
1444+
// int result;
1445+
// result = hackrf_stop_rx(hackrf_dev);
1446+
// if( result != HACKRF_SUCCESS ) {
1447+
// printf("hackrf_stop_rx() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1448+
// ABORT(-1);
1449+
// }
1450+
//
1451+
// result = hackrf_start_rx(hackrf_dev, hackrf_callback, NULL);
1452+
//
1453+
// if( result != HACKRF_SUCCESS ) {
1454+
// printf("hackrf_start_rx() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1455+
// ABORT(-1);
1456+
// }
1457+
//
1458+
//// while(1);
1459+
// int tmp_count=0;
1460+
//
1461+
// result = hackrf_is_streaming(hackrf_dev);
1462+
// printf("hackrf_is_streaming() failed: %s (%d)\n", hackrf_error_name((hackrf_error)result), result);
1463+
//
1464+
// while(hackrf_is_streaming(hackrf_dev) == HACKRF_TRUE) {
1465+
// cout << (++tmp_count) << "\n";
1466+
// }
1467+
//
1468+
// cout << "HACKRF streaming exit abnormally!\n";
1469+
// ABORT(-1);
14111470

1412-
while(hackrf_is_streaming(hackrf_dev) == HACKRF_TRUE) {;}
1413-
1414-
cout << "HACKRF streaming exit abnormally!\n";
1415-
ABORT(-1);
1416-
1417-
#else
1418-
1419-
cout << "HACKRF can't be used when lib is not included!\n";
1420-
ABORT(-1);
1421-
1422-
#endif
1471+
// #else
1472+
//
1473+
// cout << "HACKRF can't be used when lib is not included!\n";
1474+
// ABORT(-1);
1475+
//
1476+
// #endif
14231477

14241478
} else {
14251479
cout << "No valid device present.\n";

‎src/capbuf.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ int capture_data(
372372
unsigned char *capbuf_raw = new unsigned char[2*CAPLENGTH];
373373

374374
if (read_all_in_bin) {
375-
capbuf.set_size(0);
375+
capbuf.set_size(0, false);
376376

377377
FILE *fp = fopen(load_bin_filename, "rb");
378378
if (fp == NULL)
@@ -408,7 +408,7 @@ int capture_data(
408408

409409
fclose(fp);
410410
} else {
411-
capbuf.set_size(CAPLENGTH);
411+
capbuf.set_size(CAPLENGTH, false);
412412

413413
FILE *fp = fopen(load_bin_filename, "rb");
414414
if (fp == NULL)
@@ -508,7 +508,7 @@ int capture_data(
508508
rtlsdr_read_async(dev,capbuf_rtlsdr_callback,(void *)&cp,0,0);
509509

510510
// Convert to complex
511-
capbuf.set_size(CAPLENGTH);
511+
capbuf.set_size(CAPLENGTH, false);
512512
#ifndef NDEBUG
513513
capbuf=NAN;
514514
#endif
@@ -566,7 +566,7 @@ int capture_data(
566566
// }
567567

568568
// Convert to complex
569-
capbuf.set_size(CAPLENGTH);
569+
capbuf.set_size(CAPLENGTH, false);
570570
for (uint32 t=0;t<CAPLENGTH;t++) {
571571
// capbuf(t)=complex<double>((((double)hackrf_rx_buf[(t<<1)])-128.0)/128.0,(((double)hackrf_rx_buf[(t<<1)+1])-128.0)/128.0);
572572
capbuf(t)=complex<double>((((double)hackrf_rx_buf[(t<<1)])-0.0)/128.0,(((double)hackrf_rx_buf[(t<<1)+1])-0.0)/128.0);

‎src/producer_thread.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ void producer_thread(
101101
const double frequency_offset=global_thread_data.frequency_offset();
102102
double k_factor = 1.0; // if not twisted, make k_factor useless
103103

104-
if (global_thread_data.sampling_carrier_twist()){
105-
k_factor=(global_thread_data.fc_programmed-frequency_offset)/global_thread_data.fc_programmed;
106-
} else {
107-
k_factor=global_thread_data.k_factor(); // !!!!!!BE CAREFUL OF UPDATING K_FACTOR IN NON-TWIST MODE
104+
if (global_thread_data.dev_use()!=dev_type_t::HACKRF) {
105+
if (global_thread_data.sampling_carrier_twist()){
106+
k_factor=(global_thread_data.fc_programmed-frequency_offset)/global_thread_data.fc_programmed;
107+
} else {
108+
k_factor=global_thread_data.k_factor(); // !!!!!!BE CAREFUL OF UPDATING K_FACTOR IN NON-TWIST MODE
109+
}
108110
}
109111

110112
//const double k_factor_inv=1/k_factor;
@@ -134,9 +136,9 @@ void producer_thread(
134136
n_samples=t;
135137
break;
136138
}
137-
sample_temp.real()=(sampbuf_sync.fifo.front()-128.0)/128.0; // 127 should be 128?
139+
sample_temp.real()=(sampbuf_sync.fifo.front())/128.0; // 127 should be 128?
138140
sampbuf_sync.fifo.pop_front();
139-
sample_temp.imag()=(sampbuf_sync.fifo.front()-128.0)/128.0; // 127 should be 128?
141+
sample_temp.imag()=(sampbuf_sync.fifo.front())/128.0; // 127 should be 128?
140142
sampbuf_sync.fifo.pop_front();
141143
samples(t)=sample_temp;
142144
sample_time+=(FS_LTE/16)/(fs_programmed*k_factor);

‎src/searcher.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -2312,7 +2312,7 @@ void sampling_ppm_f_search_set_by_pss(
23122312

23132313
ivec sort_idx = sort_index(max_peak_all);
23142314
sort_idx = reverse(sort_idx); // from ascending to descending
2315-
DBG( cout << "Hit PAR " << 10.0*log10( peak_to_avg.get( sort_idx(0, max_reserve-1) ) ) << "dB\n"; )
2315+
cout << "Hit PAR " << 10.0*log10( peak_to_avg.get( sort_idx(0, max_reserve-1) ) ) << "dB\n";
23162316

23172317
ivec above_par_idx = to_ivec( peak_to_avg.get( sort_idx(0, max_reserve-1) ) > pow(10.0, 8.5/10.0) );
23182318
uint16 len_sort_idx = sum(above_par_idx);

0 commit comments

Comments
 (0)
Please sign in to comment.