1 /* AFC (Automatic Frequency Correction) Implementation */
3 /* (C) 2010 by Harald Welte <laforge@gnumonks.org>
4 * (C) 2011 by Andreas Eversberg <jolly@eversberg.eu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
24 #include <osmocom/gsm/gsm_utils.h>
26 #include <layer1/toa.h>
27 #include <layer1/avg.h>
28 #include <layer1/sync.h>
30 /* Over how many TDMA frames do we want to average? */
31 #define TOA_PERIOD 250
32 /* How many of our measurements have to be valid? */
33 #define TOA_MIN_MUN_VALID 125
36 #define TOA_SNR_THRESHOLD 2560 /* 2.5 dB in fx6.10 */
39 struct running_avg ravg
; /* running average */
43 static void toa_ravg_output(struct running_avg
*ravg
, int32_t avg
);
45 static struct toa_state toa_state
= {
47 .outfn
= &toa_ravg_output
,
49 .min_valid
= TOA_MIN_MUN_VALID
,
53 void toa_input(int32_t offset
, uint32_t snr
)
57 if (snr
< TOA_SNR_THRESHOLD
|| offset
< 0 || offset
>31)
59 runavg_input(&toa_state
.ravg
, offset
, valid
);
60 runavg_check_output(&toa_state
.ravg
);
65 toa_state
.ravg
.num_samples
= toa_state
.ravg
.num_samples_valid
= 0;
66 toa_state
.ravg
.acc_val
= 0;
69 /* callback function for runavg */
70 static void toa_ravg_output(struct running_avg
*ravg
, int32_t avg
)
73 printf("TOA AVG is not 16 qbits, correcting (got %ld)\n", avg
);
74 l1s
.tpu_offset_correction
= avg
- 16;