summaryrefslogtreecommitdiff
path: root/gsm-tvoid/src/lib/gsm_burst_cf.cc
blob: 73293d8f73abc03e12e29759af6b1b3632f5008a (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157

#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 <string.h>
#include <gri_mmse_fir_interpolator_cc.h>

gsm_burst_cf_sptr gsm_make_burst_cf (gr_feval_ll *t,float sample_rate)
{
  return gsm_burst_cf_sptr (new gsm_burst_cf (t,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 = 0;	// minimum number of output streams
static const int MAX_OUT = 1;	// maximum number of output streams

gsm_burst_cf::gsm_burst_cf (gr_feval_ll *t, 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))  //TODO: pad to ^2 = 256 ?
			),		
	gsm_burst(t),
	d_clock_counter(0.0),
	d_last_sample(0.0,0.0),
	mm(sample_rate / GSM_SYMBOL_RATE),
	d_interp(new gri_mmse_fir_interpolator_cc()
	)

{

	//clocking parameters
	//d_sample_interval = 1.0 / sample_rate;
	//d_omega = sample_rate / GSM_SYMBOL_RATE;
	
//	fprintf(stderr,"Sample interval      : %e\n",d_sample_interval);
//	fprintf(stderr,"Relative sample rate : %g\n",d_omega);
	
	
	//set_relative_rate( mm.d_omega / 156);
	set_relative_rate( 1.0 / (mm.d_omega * 156) );
			
	set_history(4); //need history for interpolator
	
}

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(mm.d_omega) * TS_BITS;
		//fprintf(stderr,"forecast[%d]: %d = %d\n",i,noutput_items,ninput_items_required[i]);
	}
}

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 num_outputs = output_items.size();
	int do_output = num_outputs > 0 ? 1 : 0;

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

	int  ni = ninput - d_interp->ntaps() - 16;  // interpolator need -4/+3 samples NTAPS = 8  , - 16 for safety margin
	
	while (( rval < noutput_items) && ( ii < ni ) ) {
		//clock symbols 
		//TODO: this is very basic and can be improved.  Need tracking...
		//TODO: save complex samples for Viterbi EQ
		
		//get interpolated sample
		gr_complex x_0 = d_interp->interpolate (&in[ii], mm.d_mu);
		
		//calulate phase difference (demod)
		gr_complex conjprod = x_0 * conj(d_last_sample);
		float diff_angle = gr_fast_atan2f(imag(conjprod), real(conjprod));

		//mM&M
		//mm.update(x_0);  //mm_c
		mm.update(diff_angle);  //mm_f
		
		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
				if (do_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
					mm.d_mu -= mm.d_omega / 4.0;
					//d_clock_counter -= GSM_SYMBOL_PERIOD / 4.0; 
					break;
				case QB_FULL04:	//extra bit on timeslot 0 & 4
					if (!(d_ts%4))
						mm.d_mu -= mm.d_omega;
						//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);
			}
		}	   
		
		//process mu / ii advance
		ii += (int)floor(mm.d_mu);
		d_sample_count += (int)floor(mm.d_mu);
		mm.d_mu -= floor(mm.d_mu);
		
		d_last_sample = x_0;
	}
	
	//fprintf(stderr,"/ii=%d/rval=%d\n",ii,rval);

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