summaryrefslogtreecommitdiff
path: root/gsm-tvoid/src/lib/gsm_burst_cf.cc
blob: a91c5697d5b99faa7af1554a6a989fbec31d4cd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gsm_burst_cf.h>
#include <gr_io_signature.h>
#include <gr_math.h>
#include <stdio.h>
#include <gri_mmse_fir_interpolator_cc.h>

gsm_burst_cf_sptr gsm_make_burst_cf (float sample_rate)
{
  return gsm_burst_cf_sptr (new gsm_burst_cf (sample_rate));
}

static const int MIN_IN = 1;	// minimum number of input streams
static const int MAX_IN = 1;	// maximum number of input streams
static const int MIN_OUT = 1;	// minimum number of output streams
static const int MAX_OUT = 1;	// maximum number of output streams

gsm_burst_cf::gsm_burst_cf (float sample_rate) : 
	gr_block (	"burst_cf",
				gr_make_io_signature (MIN_IN, MAX_IN, sizeof (gr_complex)),
				gr_make_io_signature (MIN_OUT, MAX_OUT, USEFUL_BITS * sizeof (float))),
	d_clock_counter(0.0),
	d_last_sample(0.0,0.0),
	d_interp(new gri_mmse_fir_interpolator_cc())

{

	//clocking parameters
	d_sample_interval = 1.0 / sample_rate;
	d_relative_sample_rate = sample_rate / GSM_SYMBOL_RATE;
	
	fprintf(stderr,"Sample interval      : %e\n",d_sample_interval);
	fprintf(stderr,"Relative sample rate : %g\n",d_relative_sample_rate);
		
	set_history(4); 
	
}

gsm_burst_cf::~gsm_burst_cf ()
{
	delete d_interp;
}

void gsm_burst_cf::forecast (int noutput_items, gr_vector_int &ninput_items_required)
{
  unsigned ninputs = ninput_items_required.size ();
  for (unsigned i = 0; i < ninputs; i++)
    ninput_items_required[i] = noutput_items * (int)ceil(d_relative_sample_rate) * BBUF_SIZE + history();
}


int gsm_burst_cf::general_work (int noutput_items,
				   gr_vector_int &ninput_items,
				   gr_vector_const_void_star &input_items,
				   gr_vector_void_star &output_items)
{
	const gr_complex *in = (const gr_complex *) input_items[0];
	float *out = (float *) output_items[0];
	
	int ii=0;
	int rval = 0;  //default to no output

	int ninput = ninput_items[0];
	//fprintf(stderr,"#i=%d/#o=%d",n_input,noutput_items);

	int  ni = ninput - d_interp->ntaps();  // interpolator need -4/+3 samples NTAPS = 8
	
	while (( rval < noutput_items) && ( ii < ni ) ) {
		//clock symbols 
		//TODO: this is very basic and can be improved.  Need tracking...
		//TODO: use burst_start offsets as timing feedback
		//TODO: save complex samples for Viterbi EQ
		if ( d_clock_counter >= GSM_SYMBOL_PERIOD) {

			d_clock_counter -= GSM_SYMBOL_PERIOD; //reset clock for next sample, keep the remainder

			//float mu = 1.0 - d_clock_counter / GSM_SYMBOL_PERIOD;
			float mu = d_clock_counter / GSM_SYMBOL_PERIOD;
			gr_complex sample = d_interp->interpolate (&in[ii], mu);	//FIXME: this seems noisy, make sure it is being used correctly

			gr_complex conjprod = sample * conj(d_last_sample);
			float diff_angle = gr_fast_atan2f(imag(conjprod), real(conjprod));

			d_last_sample = sample;
	
			assert(d_bbuf_pos <= BBUF_SIZE );
			
			if (d_bbuf_pos >= 0)	//could be negative offset from burst alignment.  TODO: perhaps better just to add some padding to the buffer
				d_burst_buffer[d_bbuf_pos] = diff_angle;
			
			d_bbuf_pos++;
			
			if ( d_bbuf_pos >= BBUF_SIZE ) { 
			
				if (get_burst()) {
					//found a burst, send to output
					//ensure that output data is in range
					int b = d_burst_start;
					if (b < 0)
						b = 0;
					else if (b >= 2 * MAX_CORR_DIST)
						b = 2 * MAX_CORR_DIST - 1;
		
					memcpy(out+rval*USEFUL_BITS, d_burst_buffer + b, USEFUL_BITS*sizeof(float));
					rval++;

					switch ( d_clock_options & QB_MASK ) {
					case QB_QUARTER: //extra 1/4 bit each burst
						d_clock_counter -= GSM_SYMBOL_PERIOD / 4.0; 
						break;
					case QB_FULL04:	//extra bit on timeslot 0 & 4
						if (!(d_ts%4))
							d_clock_counter -= GSM_SYMBOL_PERIOD; 
						break;
					case QB_NONE:	//don't adjust for quarter bits at all
					default:
						break;
					}
					
					d_last_burst_s_count = d_sample_count;	
					
					//fprintf(stderr,"clock: %f, pos: %d\n",d_clock_counter,d_bbuf_pos);
				}
			}	   
		}

		d_clock_counter += d_sample_interval;
		d_sample_count++;
 		ii++;
	}
	
	//fprintf(stderr,"/ii=%d/rval=%d\n",ii,rval);

	consume_each (ii);
	
	return rval;
}
personal git repositories of Harald Welte. Your mileage may vary