Skip to content

Commit 336a19b

Browse files
committed
Code works on _realiably_ captured data. Still cannot capture reliable data directly!
1 parent 7dd0220 commit 336a19b

File tree

5 files changed

+86
-32
lines changed

5 files changed

+86
-32
lines changed

src/CellSearch.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ void config_usb(
387387
}
388388

389389
// Sampling frequency
390-
if (rtlsdr_set_sample_rate(dev,itpp::round(1920000*correction))<0) {
390+
if (rtlsdr_set_sample_rate(dev,itpp::round(960000*correction))<0) {
391391
cerr << "Error: unable to set sampling rate" << endl;
392392
exit(-1);
393393
}
@@ -421,11 +421,11 @@ void config_usb(
421421
while (true) {
422422
if (rtlsdr_read_sync(dev,buffer,BLOCK_SIZE,&n_read_current)<0) {
423423
cerr << "Error: synchronous read failed" << endl;
424-
break;
424+
exit(-1);
425425
}
426426
if (n_read_current<BLOCK_SIZE) {
427427
cerr << "Error: short read; samples lost" << endl;
428-
break;
428+
exit(-1);
429429
}
430430
n_read+=n_read_current;
431431
if (n_read>2880000*2)

src/LTE-Tracker.cpp

+62-19
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ void config_usb(
457457
}
458458

459459
// Sampling frequency
460-
if (rtlsdr_set_sample_rate(dev,itpp::round(1920000*correction))<0) {
460+
if (rtlsdr_set_sample_rate(dev,itpp::round(960000*correction))<0) {
461461
cerr << "Error: unable to set sampling rate" << endl;
462462
exit(-1);
463463
}
@@ -486,16 +486,16 @@ void config_usb(
486486
}
487487
uint32 n_read=0;
488488
int n_read_current;
489-
#define BLOCK_SIZE 16*16384
489+
#define BLOCK_SIZE (16*16384)
490490
uint8 * buffer=(uint8 *)malloc(BLOCK_SIZE*sizeof(uint8));
491491
while (true) {
492492
if (rtlsdr_read_sync(dev,buffer,BLOCK_SIZE,&n_read_current)<0) {
493493
cerr << "Error: synchronous read failed" << endl;
494-
break;
494+
exit(-1);
495495
}
496496
if (n_read_current<BLOCK_SIZE) {
497497
cerr << "Error: short read; samples lost" << endl;
498-
break;
498+
exit(-1);
499499
}
500500
n_read+=n_read_current;
501501
if (n_read>2880000*2)
@@ -610,7 +610,14 @@ void read_datafile(
610610
cout << "read itpp format" << endl;
611611
return;
612612
} else {
613-
itpp_ext::rtl_sdr_to_cvec(filename,sig_tx);
613+
cvec sig_tx_pre;
614+
itpp_ext::rtl_sdr_to_cvec(filename,sig_tx_pre);
615+
sig_tx.set_size(length(sig_tx_pre)*2-10);
616+
for (int32 t=0;t<length(sig_tx);t+=2) {
617+
// FIXME: Do proper interpolation
618+
sig_tx(t)=sig_tx_pre(t>>1);
619+
sig_tx(t+1)=(sig_tx_pre(t>>1)+sig_tx_pre((t>>1)+1))/2;
620+
}
614621
// Drop several seconds while AGC converges.
615622
sig_tx=sig_tx(FS_LTE/16*4,-1);
616623
}
@@ -628,7 +635,7 @@ class rtl_wrap {
628635
// Destructor
629636
~rtl_wrap();
630637
complex <double> get_samp();
631-
void reset();
638+
//void reset();
632639
private:
633640
bool use_recorded_data;
634641
cvec sig_tx;
@@ -637,6 +644,10 @@ class rtl_wrap {
637644
double noise_power_sqrt;
638645
bool repeat;
639646
bool rtl_sdr_format;
647+
bool phase_even;
648+
complex <double> samp_d1;
649+
complex <double> samp_d2;
650+
//FILE * file;
640651
};
641652
rtl_wrap::rtl_wrap(
642653
rtlsdr_dev_t * dev,
@@ -650,6 +661,16 @@ rtl_wrap::rtl_wrap(
650661
use_recorded_data=urd;
651662
repeat=rpt;
652663
rtl_sdr_format=rsdf;
664+
phase_even=true;
665+
samp_d1=complex <double> (0,0);
666+
samp_d2=complex <double> (0,0);
667+
//cout << "Opening log file" << endl;
668+
//file=fopen("tracker_log.dat","wb");
669+
//if (!file) {
670+
// cerr << "Error: could not open ddata capture log file" << endl;
671+
// exit(-1);
672+
//}
673+
//cout << "Log file opened!" << endl;
653674
if (isfinite(noise_power)) {
654675
noise_power_sqrt=sqrt(noise_power);
655676
} else {
@@ -680,7 +701,7 @@ complex <double> rtl_wrap::get_samp() {
680701
complex <double> samp;
681702
if (use_recorded_data) {
682703
//samp=sig_tx(offset);
683-
samp=0*sig_tx(offset)+(1.0*J)*exp(J*((double)(cnt++))*2*pi/10000000)*sig_tx(mod(offset-5000,length(sig_tx)));;
704+
samp=1*sig_tx(offset)+(0.0*J)*exp(J*((double)(cnt++))*2*pi/10000000)*sig_tx(mod(offset-5000,length(sig_tx)));;
684705
offset=mod(offset+1,length(sig_tx));
685706
if ((offset==0)&&(!repeat)) {
686707
// Not that if N complex samples are read from the file and repeat is
@@ -697,6 +718,10 @@ complex <double> rtl_wrap::get_samp() {
697718
cerr << "Error: synchronous read failed" << endl;
698719
exit(-1);
699720
}
721+
//if (fwrite(buffer, 1, n_read, file)!=(size_t)n_read) {
722+
// cerr<<"Error: Short write, samples lost, exiting!" << endl;
723+
// exit(-1);
724+
//}
700725
if (n_read<BLOCK_SIZE) {
701726
cerr << "Error: short read; samples lost" << endl;
702727
exit(-1);
@@ -711,9 +736,25 @@ complex <double> rtl_wrap::get_samp() {
711736
return samp;
712737
}
713738
}
739+
/*
740+
complex <double> rtl_wrap::get_samp() {
741+
complex <double> samp;
742+
if (phase_even==true) {
743+
samp_d2=samp_d1;
744+
samp_d1=get_samp_pre();
745+
samp=samp_d1;
746+
} else {
747+
samp=(samp_d1+samp_d2)/2;
748+
}
749+
phase_even=!phase_even;
750+
return samp;
751+
}
752+
*/
753+
/*
714754
void rtl_wrap::reset() {
715755
offset=0;
716756
}
757+
*/
717758

718759
// Perform an initial cell search solely for the purpose of calibrating
719760
// the oscillator.
@@ -740,9 +781,10 @@ double kalibrate(
740781
list <Cell> detected_cells;
741782
// Loop until a cell is found
742783
while (detected_cells.size()<1) {
784+
cvec capbuf;
743785
// Fill capture buffer either from a file or from live data.
744-
cvec capbuf(153600);
745786
if (use_recorded_data) {
787+
capbuf.set_size(153600);
746788
cvec cbload;
747789
read_datafile(filename,rtl_sdr_format,cbload);
748790
//it_ifile itf(filename);
@@ -758,7 +800,7 @@ double kalibrate(
758800
} else {
759801
capture_data(fc,correction,false,false,".",capbuf);
760802
}
761-
cout << "Capbuf power: " << db10(sigpower(capbuf)) << " dB" << endl;
803+
//cout << "Capbuf power: " << db10(sigpower(capbuf)) << " dB" << endl;
762804
if (isfinite(noise_power))
763805
capbuf+=blnoise(length(capbuf))*sqrt(noise_power);
764806

@@ -1400,15 +1442,15 @@ int8 do_mib_decode(
14001442
// Did we find it?
14011443
if (crc_est==c_est(24,-1)) {
14021444
// YES!
1403-
cout << "MIB SUCCESS!" << endl;
1445+
cout << "Cell ID " << tracked_cell.n_id_cell << " MIB SUCCESS!" << endl;
14041446
mib_fifo_synchronized=1;
14051447
mib_fifo_decode_failures=0;
14061448
for (uint8 t=0;t<16;t++) {
14071449
mib_fifo.pop_front();
14081450
}
14091451
} else {
14101452
// No :(
1411-
cout << "MIB failure!" << endl;
1453+
cout << "Cell ID " << tracked_cell.n_id_cell << " MIB failure!" << endl;
14121454
if (mib_fifo_synchronized) {
14131455
mib_fifo_decode_failures++;
14141456
for (uint8 t=0;t<16;t++) {
@@ -1424,7 +1466,7 @@ int8 do_mib_decode(
14241466

14251467
// 10ms of time increases mib_fifo_decode_failures by 0.25.
14261468
// After several seconds of MIB decoding failures, drop the cell.
1427-
if (mib_fifo_decode_failures>=400) {
1469+
if (mib_fifo_decode_failures>=100) {
14281470
cout << "Dropped a cell!" << endl;
14291471
boost::mutex::scoped_lock lock(tracked_cell.mutex);
14301472
tracked_cell.kill_me=true;
@@ -1764,20 +1806,21 @@ void searcher_proc(
17641806
cout << " cell ID: " << (*iterator).n_id_cell() << endl;
17651807
cout << " RX power level: " << db10((*iterator).pss_pow) << " dB" << endl;
17661808
cout << " residual frequency offset: " << (*iterator).freq_superfine << " Hz" << endl;
1767-
cout << "Frame start " << (*iterator).frame_start << endl;
1809+
cout << " frame start: " << (*iterator).frame_start << endl;
17681810
}
17691811

17701812
// Launch a cell tracker process!
17711813
k_factor=k_factor;
1772-
cout << "Timing error is purposely introduced here!!!" << endl;
1773-
tracked_cell_t * new_cell = new tracked_cell_t((*iterator).n_id_cell(),(*iterator).n_ports,(*iterator).cp_type,(*iterator).frame_start/k_factor+capbuf_sync.late+global_1);
1814+
//cout << "Timing error is purposely introduced here!!!" << endl;
1815+
//tracked_cell_t * new_cell = new tracked_cell_t((*iterator).n_id_cell(),(*iterator).n_ports,(*iterator).cp_type,(*iterator).frame_start/k_factor+capbuf_sync.late+global_1);
1816+
tracked_cell_t * new_cell = new tracked_cell_t((*iterator).n_id_cell(),(*iterator).n_ports,(*iterator).cp_type,(*iterator).frame_start/k_factor+capbuf_sync.late);
17741817
(*new_cell).thread=boost::thread(tracker_proc,boost::ref(*new_cell),boost::ref(global_thread_data));
17751818
{
17761819
boost::mutex::scoped_lock lock(tracked_cell_list.mutex);
17771820
tracked_cell_list.tracked_cells.push_back(new_cell);
17781821
}
1779-
cout << "Only one cell is allowed to be detected!!!" << endl;
1780-
sleep(1000000);
1822+
//cout << "Only one cell is allowed to be detected!!!" << endl;
1823+
//sleep(1000000);
17811824

17821825
++iterator;
17831826
}
@@ -1887,7 +1930,7 @@ int main(
18871930
{
18881931
boost::mutex::scoped_lock lock(capbuf_sync.mutex);
18891932
if ((capbuf_sync.request)&&(!searcher_capbuf_filling)&&(abs(WRAP(sample_time-0,-19200.0/2,19200.0/2))<0.5)) {
1890-
cout << "searcher data cap beginning" << endl;
1933+
//cout << "searcher data cap beginning" << endl;
18911934
capbuf_sync.request=false;
18921935
searcher_capbuf_filling=true;
18931936
searcher_capbuf_idx=0;
@@ -1903,7 +1946,7 @@ int main(
19031946
searcher_capbuf_filling=false;
19041947
boost::mutex::scoped_lock lock(capbuf_sync.mutex);
19051948
capbuf_sync.condition.notify_one();
1906-
cout << "searcher data cap finished" << endl;
1949+
//cout << "searcher data cap finished" << endl;
19071950
}
19081951
}
19091952

src/capbuf.cpp

+20-8
Original file line numberDiff line numberDiff line change
@@ -91,40 +91,52 @@ void capture_data(
9191
uint32 n_read=0;
9292
int n_read_current=0;
9393
uint32 n_saved=0;
94-
capbuf.set_size(CAPLENGTH);
94+
//capbuf.set_size(CAPLENGTH);
95+
#define FIR_LENGTH 10
96+
cvec capbuf_0p5x(CAPLENGTH/2+FIR_LENGTH+10);
9597
#ifndef NDEBUG
96-
capbuf=NAN;
98+
//capbuf=NAN;
99+
capbuf_0p5x=NAN;
97100
#endif
98101
while (true) {
99102
// Read some data
100103
if (rtlsdr_read_sync(dev,buffer,BLOCK_SIZE,&n_read_current)<0) {
101104
cerr << "Error: synchronous read failed" << endl;
102-
break;
105+
exit(-1);
103106
}
104107
if (n_read_current<BLOCK_SIZE) {
105108
cerr << "Error: short read; samples lost" << endl;
106-
break;
109+
exit(-1);
107110
}
108111

109112
for (uint32 t=0;t<BLOCK_SIZE;t+=2) {
110113
n_read+=2;
111-
// Ignore first 10ms... Hopefully PLL will lock by then...
114+
// Ignore first 20ms... Hopefully PLL will lock by then...
112115
if (n_read<19200) {
113116
continue;
114117
}
115-
capbuf(n_saved++)=complex<double>((buffer[t]-127.0)/128.0,(buffer[t+1]-127.0)/128.0);
116-
if (n_saved==CAPLENGTH) {
118+
capbuf_0p5x(n_saved++)=complex<double>((buffer[t]-127.0)/128.0,(buffer[t+1]-127.0)/128.0);
119+
if ((signed)n_saved==length(capbuf_0p5x)) {
117120
goto cbuf_full;
118121
}
119122
}
120123
}
121124

122125
cbuf_full:
123126
free(buffer);
124-
if (n_saved!=CAPLENGTH) {
127+
if ((signed)n_saved!=length(capbuf_0p5x)) {
125128
cerr << "Error: unable to fill capture buffer..." << endl;
126129
exit(-1);
127130
}
131+
132+
// Interpolate.
133+
// FIXME: Do proper interpolation.
134+
capbuf.set_size(CAPLENGTH);
135+
capbuf=NAN;
136+
for (uint32 t=0;t<CAPLENGTH;t+=2) {
137+
capbuf(t)=capbuf_0p5x(t>>1);
138+
capbuf(t+1)=(capbuf_0p5x(t>>1)+capbuf_0p5x((t>>1)+1))/2;
139+
}
128140
}
129141

130142
// Save the capture data, if requested.

src/itpp_ext.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ void rtl_sdr_to_cvec(
201201
// Cleanup
202202
free(buffer);
203203
fclose(file);
204-
cout << "Finished reading binary file..." << endl;
205204
}
206205

207206
}

src/rtl_sdr_check.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ void parse_commandline(
248248
// Second order command line checking. Ensure that command line options
249249
// are consistent.
250250
if (!filename_set||!fc_set||!fs_set||!n_id_cell_set||!f_off_set) {
251-
cerr << "Error: required parameter missing" << endl;
251+
cerr << "Error: required parameter missing (try --help)" << endl;
252252
exit(-1);
253253
}
254254
// Frequencies should be on a 100kHz raster.

0 commit comments

Comments
 (0)