From 3d271a927940df0d7368c774c0e1ed0881e6d493 Mon Sep 17 00:00:00 2001 From: Piotr Krysik Date: Thu, 28 May 2009 20:27:13 +0200 Subject: added new python script gsm_receive --- src/lib/gsm_receiver_cf.cc | 39 +++++++++------- src/lib/gsm_receiver_cf.h | 4 +- src/python/gsm_receive.py | 108 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 17 deletions(-) create mode 100755 src/python/gsm_receive.py (limited to 'src') diff --git a/src/lib/gsm_receiver_cf.cc b/src/lib/gsm_receiver_cf.cc index 089a563..5453253 100644 --- a/src/lib/gsm_receiver_cf.cc +++ b/src/lib/gsm_receiver_cf.cc @@ -151,7 +151,19 @@ gsm_receiver_cf::general_work(int noutput_items, switch (b_type) { case fcch_burst: { - + int ii; + int first_sample = (GUARD_BITS+TAIL_BITS)*d_OSR; + int last_sample = first_sample+USEFUL_BITS*d_OSR; + double phase_sum = 0; + for(ii = first_sample; ii < last_sample; ii++){ + phase_sum += compute_phase_diff(in[ii], in[ii-1]) - (M_PI / 2) / d_OSR; + } + std::cout << "freq_offset: " << d_freq_offset << "\n"; + + double freq_offset = compute_freq_offset(phase_sum, USEFUL_BITS-TAIL_BITS); + d_freq_offset -= freq_offset; + set_frequency(d_freq_offset); + std::cout << "freq_offset: " << d_freq_offset << "\n"; } break; case sch_burst: { @@ -200,12 +212,14 @@ bool gsm_receiver_cf::find_fcch_burst(const gr_complex *in, const int nitems) int start_pos = -1; float min_phase_diff; float max_phase_diff; + double best_sum; float lowest_max_min_diff = 99999; int to_consume = 0; int sample_number = 0; bool end = false; bool result = false; + double freq_offset; circular_buffer_float::iterator buffer_iter; enum states { @@ -269,12 +283,12 @@ bool gsm_receiver_cf::find_fcch_burst(const gr_complex *in, const int nitems) if (lowest_max_min_diff > max_phase_diff - min_phase_diff) { lowest_max_min_diff = max_phase_diff - min_phase_diff; start_pos = sample_number - FCCH_HITS_NEEDED * d_OSR - FCCH_MAX_MISSES * d_OSR; - d_best_sum = 0; + best_sum = 0; for (buffer_iter = phase_diff_buffer.begin(); buffer_iter != (phase_diff_buffer.end()); buffer_iter++) { - d_best_sum += *buffer_iter - (M_PI / 2) / d_OSR; + best_sum += *buffer_iter - (M_PI / 2) / d_OSR; } } } @@ -297,7 +311,9 @@ bool gsm_receiver_cf::find_fcch_burst(const gr_complex *in, const int nitems) to_consume = start_pos + FCCH_HITS_NEEDED * d_OSR + 1; d_fcch_start_pos = d_counter + start_pos; - compute_freq_offset(); + freq_offset = compute_freq_offset(best_sum, FCCH_HITS_NEEDED); + d_freq_offset -= freq_offset; + end = true; result = true; break; @@ -315,21 +331,19 @@ bool gsm_receiver_cf::find_fcch_burst(const gr_complex *in, const int nitems) return result; } - -double gsm_receiver_cf::compute_freq_offset() +double gsm_receiver_cf::compute_freq_offset(double best_sum, unsigned denominator) { float phase_offset, freq_offset; - phase_offset = d_best_sum / FCCH_HITS_NEEDED; + phase_offset = best_sum / denominator; freq_offset = phase_offset * 1625000.0 / (12.0 * M_PI); - d_freq_offset -= freq_offset; // d_fcch_count++; // d_x_temp += freq_offset; // d_x2_temp += freq_offset * freq_offset; // d_mean = d_x_temp / d_fcch_count; - DCOUT("freq_offset: " << freq_offset );//" d_best_sum: " << d_best_sum + DCOUT("freq_offset: " << freq_offset );//" best_sum: " << best_sum // DCOUT("wariance: " << sqrt((d_x2_temp / d_fcch_count - d_mean * d_mean)) << " fcch_count:" << d_fcch_count << " d_mean: " << d_mean); return freq_offset; @@ -545,12 +559,7 @@ gr_complex gsm_receiver_cf::correlate_sequence(const gr_complex * sequence, cons // return result; // } - -// gr_complex gsm_receiver_cf::calc_energy(int window_len){ -// -// } - -//oblicza dodatnią część autokorelacji +//computes autocorrelation for positive values //TODO consider placing this funtion in a separate class for signal processing inline void gsm_receiver_cf::autocorrelation(const gr_complex * input, gr_complex * out, int length) { diff --git a/src/lib/gsm_receiver_cf.h b/src/lib/gsm_receiver_cf.h index 2bad50e..98df34d 100644 --- a/src/lib/gsm_receiver_cf.h +++ b/src/lib/gsm_receiver_cf.h @@ -232,7 +232,7 @@ class gsm_receiver_cf : public gr_block //variables used to store result of the find_fcch_burst fuction int d_fcch_start_pos; float d_freq_offset; - double d_best_sum; +// double d_best_sum; // int d_fcch_count; //!!! // double d_x_temp, d_x2_temp, d_mean;//!! @@ -255,7 +255,7 @@ class gsm_receiver_cf : public gr_block gsm_receiver_cf(gr_feval_dd *tuner, int osr); bool find_fcch_burst(const gr_complex *in, const int nitems); - double compute_freq_offset(); + double compute_freq_offset(double best_sum, unsigned denominator); void set_frequency(double freq_offset); inline float compute_phase_diff(gr_complex val1, gr_complex val2); diff --git a/src/python/gsm_receive.py b/src/python/gsm_receive.py new file mode 100755 index 0000000..449f7d8 --- /dev/null +++ b/src/python/gsm_receive.py @@ -0,0 +1,108 @@ +#!/usr/bin/env python +#!/usr/bin/env python + +from gnuradio import gr, gru, blks2 +#, gsm +from gnuradio.eng_option import eng_option +from optparse import OptionParser +from os import sys + +for extdir in ['../../debug/src/lib','../../debug/src/lib/.libs']: + if extdir not in sys.path: + sys.path.append(extdir) +import gsm + +class tune(gr.feval_dd): + def __init__(self, top_block): + gr.feval_dd.__init__(self) + self.top_block = top_block + # self.center_freq = 0 + def eval(self, freq_offet): + # self.center_freq = self.center_freq - freq_offet + self.top_block.set_frequency(freq_offet) + return freq_offet + +class gsm_receiver_first_blood(gr.top_block): + def __init__(self): + gr.top_block.__init__(self) + (options, args) = self._przetworz_opcje() + self.tune_callback = tune(self) + self.options = options + self.args = args + self._ustaw_taktowanie() + self.zrodlo = self._ustaw_zrodlo() + self.filtr = self._ustaw_filtr() + self.interpolator = self._ustaw_interpolator() + self.odbiornik = self._ustaw_odbiornik() + self.konwerter = self._ustaw_konwerter() + self.ujscie = self._ustaw_ujscie() + + self.connect(self.zrodlo, self.filtr, self.interpolator, self.odbiornik, self.konwerter, self.ujscie) +# self.connect(self.zrodlo, self.ujscie) + + def _ustaw_ujscie(self): + nazwa_pliku_wy = self.options.outputfile + ujscie = gr.file_sink(gr.sizeof_float, nazwa_pliku_wy) + return ujscie + + def _ustaw_zrodlo(self): + nazwa_pliku = self.options.inputfile + zrodlo = gr.file_source(gr.sizeof_gr_complex, nazwa_pliku, False) + return zrodlo + + def _ustaw_taktowanie(self): + options = self.options + clock_rate = 64e6 + self.clock_rate = clock_rate + self.input_rate = clock_rate / options.decim + self.gsm_symb_rate = 1625000.0 / 6.0 + self.sps = self.input_rate / self.gsm_symb_rate / self.options.osr + + def _ustaw_filtr(self): + filter_cutoff = 145e3 + filter_t_width = 10e3 + offset = 0 +# print "input_rate:", self.input_rate, "sample rate:", self.sps, " filter_cutoff:", filter_cutoff, " filter_t_width:", filter_t_width + filter_taps = gr.firdes.low_pass(1.0, self.input_rate, filter_cutoff, filter_t_width, gr.firdes.WIN_HAMMING) + filtr = gr.freq_xlating_fir_filter_ccf(1, filter_taps, offset, self.input_rate) + return filtr + + def _ustaw_konwerter(self): + v2s = gr.vector_to_stream(gr.sizeof_float, 142) + return v2s + + def _ustaw_interpolator(self): + interpolator = gr.fractional_interpolator_cc(0, self.sps) +# interpolator = blks2.rational_resampler_ccf(13, 6) + return interpolator + + def _ustaw_odbiornik(self): + odbiornik = gsm.receiver_cf(self.tune_callback, self.options.osr) + return odbiornik + + def _przetworz_opcje(self): + parser = OptionParser(option_class=eng_option) + parser.add_option("-d", "--decim", type="int", default=128, + help="Set USRP decimation rate to DECIM [default=%default]") + parser.add_option("-r", "--osr", type="int", default=4, + help="Oversampling ratio [default=%default]") + parser.add_option("-I", "--inputfile", type="string", default="cfile", + help="Input filename") + parser.add_option("-O", "--outputfile", type="string", default="cfile2.out", + help="Output filename") + (options, args) = parser.parse_args () + return (options, args) + + def set_frequency(self, center_freq): + self.filtr.set_center_freq(center_freq) + +def main(): + try: + gsm_receiver_first_blood().run() + except KeyboardInterrupt: + pass + +if __name__ == '__main__': + main() + + -- cgit v1.2.3