aboutsummaryrefslogtreecommitdiffstats
path: root/src/cnetz/fsk_fm_demod.h
blob: abc86634dcd2c3c90744ee4529d7a663bb5a2166 (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
#define BITS_PER_SUPERFRAME	12672.0	/* super frame (Oberrahmen) has duration of excactly 2.4 seconds */
#define BITS_PER_BLOCK		198.0	/* block has duration of excactly 37.5 milli seconds */
#define BITS_PER_SPK_BLOCK	66.0	/* spk block has a duration of exactly 12.5 milli seconds */

/* fsk rx sync state */
enum fsk_sync {
	FSK_SYNC_NONE = 0,
	FSK_SYNC_POSITIVE,
	FSK_SYNC_NEGATIVE,
};

typedef struct cnetz cnetz_t;

typedef struct fsk_fm_demod {
	cnetz_t		*cnetz;			/* pointer back to cnetz instance */

	/* clock */
	double		bit_time;		/* current time in bits inside superframe */
	double		bit_time_uncorrected;	/* same as above, but not corrected by sync */

	/* bit detection */
	int16_t		bit_buffer_spl[40];	/* samples ring buffer */
	int		bit_buffer_len;		/* number of samples in ring buffer */
	int		bit_buffer_half;	/* half of ring buffer */
	int		bit_buffer_pos;		/* current position to write next sample */
	int		level_threshold;	/* threshold for detection of next level change */
	double		bits_per_sample;	/* duration of one sample in bits */
	double		next_bit;		/* count time to detect bits */
	int		bit_count;		/* counts bits, to match 4 bits at distributed signaling */
	int		last_change_positive;	/* flags last level change direction */
	enum fsk_sync	sync;			/* set, if we are in sync and what polarity we receive */
	double		sync_level;		/* what was the level, when we received the sync */
	double		sync_time;		/* when did we receive sync, relative to super frame */
	double		sync_jitter;		/* what was the jitter of the sync */

	/* speech */
	int16_t		speech_buffer[3000];	/* holds one chunk of 12.5ms */
	int		speech_size;
	int		speech_count;

	/* bit decoder */
	uint64_t	rx_sync;		/* sync shift register */
	char		rx_buffer[151];		/* 150 bits + termination */
	int		rx_buffer_count;	/* counter when receiving bits */

	/* statistics */
	int		change_levels[256];	/* ring buffer to store levels */
	double		change_when[256];	/* ring buffer to store time when level has changed */
	uint8_t		change_pos;		/* index for next write */
} fsk_fm_demod_t;

int fsk_fm_init(fsk_fm_demod_t *fsk, cnetz_t *cnetz, int samplerate, double bitrate);
void fsk_fm_demod(fsk_fm_demod_t *fsk, int16_t *samples, int length);
void fsk_correct_sync(cnetz_t *cnetz, double offset);