From 6497de667a5d1f6f1770066ea64bc624407957ff Mon Sep 17 00:00:00 2001 From: piotr Date: Thu, 4 Jun 2009 13:12:03 +0200 Subject: normal bursts detection now works, but the code is a little dirty --- src/lib/decoder/sch.c | 6 +- src/lib/gsm_constants.h | 14 ++-- src/lib/gsm_receiver_cf.cc | 167 +++++++++++++++++++++++++++++++++++++++++---- src/lib/gsm_receiver_cf.h | 10 ++- 4 files changed, 171 insertions(+), 26 deletions(-) (limited to 'src/lib') diff --git a/src/lib/decoder/sch.c b/src/lib/decoder/sch.c index 9f570c9..e16d14b 100644 --- a/src/lib/decoder/sch.c +++ b/src/lib/decoder/sch.c @@ -3,7 +3,7 @@ #include #include #include -#include "burst_types.h" +#include /* * Synchronization channel. @@ -251,8 +251,8 @@ int decode_sch(const unsigned char *buf, int * t1_o, int * t2_o, int * t3_o, int // extract encoded data from synchronization burst /* buf, 39 bit */ /* buf + 39 + 64 = 103, 39 */ - memcpy(data, buf, SB_EDATA_LEN_1); - memcpy(data + SB_EDATA_LEN_1, buf + SB_EDATA_LEN_1 + N_SYNC_BITS, SB_EDATA_LEN_2); + memcpy(data, buf, SCH_DATA_LEN); + memcpy(data + SCH_DATA_LEN, buf + SCH_DATA_LEN + N_SYNC_BITS, SCH_DATA_LEN); // Viterbi decode if (errors = conv_decode(data, decoded_data)) { diff --git a/src/lib/gsm_constants.h b/src/lib/gsm_constants.h index 1e449b3..ded8cb4 100644 --- a/src/lib/gsm_constants.h +++ b/src/lib/gsm_constants.h @@ -16,6 +16,7 @@ #define FCCH_BITS USEFUL_BITS #define BURST_SIZE (USEFUL_BITS+2*TAIL_BITS) +#define SCH_DATA_LEN 39 #define TS_BITS (TAIL_BITS+USEFUL_BITS+TAIL_BITS+GUARD_BITS) //a full TS (156 bits) #define TS_PER_FRAME 8 #define FRAME_BITS (TS_PER_FRAME * TS_BITS + 2) // 156.25 * 8 @@ -30,7 +31,7 @@ #define CHAN_IMP_RESP_LENGTH 5 -static const int SYNC_BITS[] = { +static const unsigned char SYNC_BITS[] = { 1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, @@ -39,7 +40,7 @@ static const int SYNC_BITS[] = { const unsigned FCCH_FRAMES[] = {0,10,20,30,40}; const unsigned SCH_FRAMES[] = {1,11,21,31,41}; - +const unsigned BCCH_FRAMES[] = {2,3,4,5, 12}; //remove 12 // Sync : .+...++.+..+++.++++++.++++++....++.+..+.+.+++.+.+...+..++++..+.. // Diff Encoded Sync: .++..+.+++.+..++.....++.....+...+.+++.+++++..+++++..++.+...+.++. @@ -52,10 +53,11 @@ const unsigned SCH_FRAMES[] = {1,11,21,31,41}; #define TSC5 5 #define TSC6 6 #define TSC7 7 -#define TS_FCCH 8 -#define TS_DUMMY 9 +#define TS_DUMMY 8 + +#define TRAIN_SEQ_NUM 9 -static const unsigned char train_seq[10][N_TRAIN_BITS] = { +static const unsigned char train_seq[TRAIN_SEQ_NUM][N_TRAIN_BITS] = { {0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1}, {0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1}, {0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0}, @@ -64,10 +66,10 @@ static const unsigned char train_seq[10][N_TRAIN_BITS] = { {0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0}, {1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1}, {1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0}, - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, //#9 FCCH ;-) {0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1} // DUMMY }; + //Dummy burst 0xFB 76 0A 4E 09 10 1F 1C 5C 5C 57 4A 33 39 E9 F1 2F A8 static const unsigned char dummy_burst[] = { 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, diff --git a/src/lib/gsm_receiver_cf.cc b/src/lib/gsm_receiver_cf.cc index 127c674..ce59bee 100644 --- a/src/lib/gsm_receiver_cf.cc +++ b/src/lib/gsm_receiver_cf.cc @@ -37,6 +37,7 @@ #define FCCH_BUFFER_SIZE (FCCH_HITS_NEEDED) #define SYNC_SEARCH_RANGE 30 +#define TRAIN_SEARCH_RANGE 40 //TODO !! - move this methods to some else place @@ -76,7 +77,12 @@ gsm_receiver_cf::gsm_receiver_cf(gr_feval_dd *tuner, int osr) // d_x_temp(0),//!! // d_x2_temp(0)//!! { - gmsk_mapper(SYNC_BITS, d_sch_training_seq, N_SYNC_BITS); + int i; + gmsk_mapper(SYNC_BITS, N_SYNC_BITS, d_sch_training_seq, gr_complex(0.0,-1.0)); + + for (i = 0; i < TRAIN_SEQ_NUM; i++) { + gmsk_mapper(train_seq[i], N_TRAIN_BITS, d_norm_training_seq[i], gr_complex(1.0,0.0)); + } } /* @@ -142,10 +148,12 @@ gsm_receiver_cf::general_work(int noutput_items, DCOUT("sch burst_start: " << burst_start); d_burst_nr.set(t1, t2, t3, 0); DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3); - d_channel_conf.set_multiframe_type(0, multiframe_51); - d_channel_conf.set_burst_types(0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); - d_channel_conf.set_burst_types(0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); + d_channel_conf.set_multiframe_type(TSC0, multiframe_51); + d_channel_conf.set_burst_types(TSC0, FCCH_FRAMES, sizeof(FCCH_FRAMES) / sizeof(unsigned), fcch_burst); + d_channel_conf.set_burst_types(TSC0, SCH_FRAMES, sizeof(SCH_FRAMES) / sizeof(unsigned), sch_burst); + d_channel_conf.set_burst_types(TSC0, BCCH_FRAMES, sizeof(BCCH_FRAMES) / sizeof(unsigned), normal_burst); d_burst_nr++; + consume_each(burst_start + BURST_SIZE * d_OSR); d_state = synchronized; } else { @@ -159,7 +167,7 @@ gsm_receiver_cf::general_work(int noutput_items, //in this state receiver is synchronized and it processes bursts according to burst type for given burst number case synchronized: { - gr_complex chan_imp_resp[100];//!! + gr_complex chan_imp_resp[d_chan_imp_length*d_OSR]; burst_type b_type = d_channel_conf.get_burst_type(d_burst_nr); int burst_start; int offset = 0; @@ -169,7 +177,7 @@ gsm_receiver_cf::general_work(int noutput_items, switch (b_type) { case fcch_burst: { int ii; - int first_sample = ceil((GUARD_PERIOD + 2*TAIL_BITS) * d_OSR)+1; + int first_sample = ceil((GUARD_PERIOD + 2 * TAIL_BITS) * d_OSR) + 1; int last_sample = first_sample + USEFUL_BITS * d_OSR; double phase_sum = 0; for (ii = first_sample; ii < last_sample; ii++) { @@ -177,18 +185,18 @@ gsm_receiver_cf::general_work(int noutput_items, phase_sum += phase_diff; } double freq_offset = compute_freq_offset(phase_sum, last_sample - first_sample); - if(abs(freq_offset) > FCCH_MAX_FREQ_OFFSET){ + if (abs(freq_offset) > FCCH_MAX_FREQ_OFFSET) { d_freq_offset -= freq_offset; set_frequency(d_freq_offset); DCOUT("adjusting frequency, new frequency offset: " << d_freq_offset << "\n"); } - } break; + case sch_burst: { int t1, t2, t3, d_ncc, d_bcc; burst_start = get_sch_chan_imp_resp(in, chan_imp_resp); - detect_burst(in, chan_imp_resp, burst_start, output_binary); + detect_burst(in, &d_channel_imp_resp[0], burst_start, output_binary); if (decode_sch(&output_binary[3], &t1, &t2, &t3, &d_ncc, &d_bcc) == 0) { // d_burst_nr.set(t1, t2, t3, 0); DCOUT("bcc: " << d_bcc << " ncc: " << d_ncc << " t1: " << t1 << " t2: " << t2 << " t3: " << t3); @@ -198,8 +206,25 @@ gsm_receiver_cf::general_work(int noutput_items, } } break; + case normal_burst: - +// std::cout << "# name: norm_complex\n" ; +// std::cout << "# type: complex matrix\n" ; +// std::cout << "# rows: 1\n" ; +// std::cout << "# columns: " << floor(d_OSR*(TS_BITS+GUARD_PERIOD)) << "\n"; + burst_start = get_norm_chan_imp_resp(in, chan_imp_resp, TRAIN_SEARCH_RANGE); +// std::cout << burst_start << "\n" ; + detect_burst(in, &d_channel_imp_resp[0], burst_start, output_binary); +// printf("burst = [ "); +// + for (int i = 0; i < BURST_SIZE ; i++) { + printf(" %d", output_binary[i]); + } + printf("];\n"); +// +// for(int i=0; i= input_length*d_OSR) + break; + output[n] += input[a+ii] * filter[ii]; + ii++; + } + } +} + +int gsm_receiver_cf::get_norm_chan_imp_resp(const gr_complex *in, gr_complex * chan_imp_resp, unsigned search_range) +{ + vector_complex correlation_buffer; + vector_float power_buffer; + vector_float window_energy_buffer; + + int strongest_window_nr; + int burst_start = 0; + int chan_imp_resp_center; + float max_correlation = 0; + float energy = 0; + int search_start_pos = floor((TRAIN_POS + GUARD_PERIOD) * d_OSR); + int search_stop_pos = search_start_pos + search_range * d_OSR; +// std::cout << "# name: correlation\n" ; +// std::cout << "# type: complex matrix\n" ; +// std::cout << "# rows: 1\n" ; +// std::cout << "# columns: " << (search_stop_pos - search_start_pos) << "\n"; + + + for (int ii = search_start_pos; ii < search_stop_pos; ii++) { +// for (int ii = SYNC_POS * d_OSR; ii < (SYNC_POS + SYNC_SEARCH_RANGE) *d_OSR; ii++) { +// for (int ii = 1; ii < (150) *d_OSR; ii++) { + gr_complex correlation = correlate_sequence(&d_norm_training_seq[d_bcc][5], &in[ii], N_TRAIN_BITS - 10); + +// std::cout << correlation << "\n" ; + correlation_buffer.push_back(correlation); + power_buffer.push_back(pow(abs(correlation), 2)); + } + + //compute window energies + vector_float::iterator iter = power_buffer.begin(); + bool loop_end = false; + while (iter != power_buffer.end()) { + vector_float::iterator iter_ii = iter; + energy = 0; + + for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++, iter_ii++) { + if (iter_ii == power_buffer.end()) { + loop_end = true; + break; + } + energy += (*iter_ii); + } + if (loop_end) { + break; + } + iter++; +// std::cout << energy << "\n"; + window_energy_buffer.push_back(energy); + } + + + + strongest_window_nr = max_element(window_energy_buffer.begin(), window_energy_buffer.end()) - window_energy_buffer.begin(); + d_channel_imp_resp.clear(); + strongest_window_nr = 36; + + max_correlation = 0; + for (int ii = 0; ii < (d_chan_imp_length)*d_OSR; ii++) { + gr_complex correlation = correlation_buffer[strongest_window_nr + ii]; + if (abs(correlation) > max_correlation) { + chan_imp_resp_center = ii; + max_correlation = abs(correlation); + } + d_channel_imp_resp.push_back(correlation); + chan_imp_resp[ii] = correlation; + } + + std::cout << "center: " << strongest_window_nr + chan_imp_resp_center << " stronegest window nr: " << strongest_window_nr << "\n"; + + burst_start = search_start_pos + strongest_window_nr + chan_imp_resp_center - 66 * d_OSR - 2 * d_OSR + 2; + return burst_start; +} + +void gsm_receiver_cf::detect_norm_burst(const gr_complex * in, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary) +{ + float output[BURST_SIZE]; + gr_complex rhh_temp[CHAN_IMP_RESP_LENGTH*d_OSR]; + gr_complex rhh[CHAN_IMP_RESP_LENGTH]; + gr_complex filtered_burst[BURST_SIZE]; + int start_state = 3; + unsigned int stop_states[2] = {4, 12}; + + autocorrelation(chan_imp_resp, rhh_temp, d_chan_imp_length*d_OSR); + for (int ii = 0; ii < (d_chan_imp_length); ii++) { + rhh[ii] = conj(rhh_temp[ii*d_OSR]); + } + + mafi_norm(&in[burst_start], BURST_SIZE, chan_imp_resp, d_chan_imp_length*d_OSR, filtered_burst); + + viterbi_detector(filtered_burst, BURST_SIZE, rhh, start_state, stop_states, 2, output); + + for (int i = 0; i < BURST_SIZE ; i++) { + output_binary[i] = (output[i] > 0); + } +} + +inline void gsm_receiver_cf::mafi_norm(const gr_complex * input, int input_length, gr_complex * filter, int filter_length, gr_complex * output) +{ + int ii = 0, n, a; + for (n = 0; n < input_length; n++) { a = n * d_OSR; output[n] = 0; @@ -594,6 +734,5 @@ inline void gsm_receiver_cf::mafi(const gr_complex * input, int input_length, gr output[n] += input[a+ii] * filter[ii]; //!!conj ii++; } - output[n] = output[n] * gr_complex(0, -1); //!!this shouldn't be here } } diff --git a/src/lib/gsm_receiver_cf.h b/src/lib/gsm_receiver_cf.h index cc3fc15..796bb5b 100644 --- a/src/lib/gsm_receiver_cf.h +++ b/src/lib/gsm_receiver_cf.h @@ -247,7 +247,8 @@ class gsm_receiver_cf : public gr_block const int d_chan_imp_length; gr_complex d_sch_training_seq[N_SYNC_BITS]; //encoded training sequence of a SCH burst - + gr_complex d_norm_training_seq[TRAIN_SEQ_NUM][N_TRAIN_BITS]; + gr_feval_dd *d_tuner; unsigned d_counter; @@ -284,11 +285,14 @@ class gsm_receiver_cf : public gr_block bool find_sch_burst(const gr_complex *in, const int nitems , float *out); int get_sch_chan_imp_resp(const gr_complex *in, gr_complex * chan_imp_resp); void detect_burst(const gr_complex * in, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary); - void gmsk_mapper(const int * input, gr_complex * gmsk_output, int ninput); + void gmsk_mapper(const unsigned char * input, int ninput, gr_complex * gmsk_output, gr_complex start_point); gr_complex correlate_sequence(const gr_complex * sequence, const gr_complex * input_signal, int ninput); inline void autocorrelation(const gr_complex * input, gr_complex * out, int length); inline void mafi(const gr_complex * input, int input_length, gr_complex * filter, int filter_length, gr_complex * output); - + int get_norm_chan_imp_resp(const gr_complex *in, gr_complex * chan_imp_resp, unsigned search_range); + void detect_norm_burst(const gr_complex * in, gr_complex * chan_imp_resp, int burst_start, unsigned char * output_binary); + inline void mafi_norm(const gr_complex * input, int input_length, gr_complex * filter, int filter_length, gr_complex * output); + public: ~gsm_receiver_cf(); void forecast(int noutput_items, gr_vector_int &ninput_items_required); -- cgit v1.2.3