aboutsummaryrefslogtreecommitdiffstats
path: root/src/libsquelch/squelch.c
blob: 10a2ac1435197bad57745ee64c1dc4fd18fb9de0 (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
/* Squelch functions
 *
 * (C) 2017 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string.h>
#include <math.h>
#include "../libdebug/debug.h"
#include "squelch.h"

#define CHAN squelch->kanal

/* How does it work:
 *
 * After init, squelch() is called with the RF level and duration of each chunk.
 * Normally quelch() returns SQUELCH_OPEN. If the RF level is below the
 * threshold level for multe_time, it returns SQUELCH_MUTE. If the RF level is
 * below the threshold level for loss_time, it returns SQUELCH_LOSS, which
 * measns that the carrier was loss.
 *
 * This is done by a counter. Whenever the RF level is below threshold, the mute
 * counter is incremented, whenever the RF level is above threshodl, the mute
 * counter is decremented. When the mute counter reaches mute_time, the mute
 * state is set and the 'mute' condition is returned. When the mute counter
 * rechers 0, the mute state is unset and the 'open' condition is returned.
 *
 * If the mute state is set, the loss counter is incremented. If the mute state
 * is not set, the loss counter is reset. When the loss counter reaches
 * loss_time, the 'loss' condition is returned.
 */

/* NOTE: SQUELCH must be calibrated !AFTER! DC bias, to get the actual noise floor */
#define SQUELCH_INIT_TIME	0.1	/* wait some time before performing squelch */
#define SQUELCH_AUTO_TIME	0.5	/* duration of squelch quelch calibration */
#define SQUELCH_AUTO_OFFSET	10.0	/* auto calibration: offset above noise floor */

void squelch_init(squelch_t *squelch, const char *kanal, double threshold_db, double mute_time, double loss_time)
{
	memset(squelch, 0, sizeof(*squelch));
	squelch->kanal = kanal;
	squelch->threshold_db = threshold_db;
	/* wait for init condition */
	squelch->init_count = 0.0;
	/* measure noise floor for auto threshold mode */
	if (threshold_db == 0.0) {
		/* automatic threshold */
		PDEBUG_CHAN(DDSP, DEBUG_INFO, "RF signal squelch: Use automatic threshold\n");
		squelch->auto_state = 1;
	} else if (!isinf(threshold_db)) {
		/* preset threshold */
		PDEBUG_CHAN(DDSP, DEBUG_INFO, "RF signal squelch: Use preset threshold of %.1f dB\n", threshold_db);
	}
	/* squelch is mute on init */
	squelch->mute_time = mute_time;
	squelch->mute_count = mute_time;
	squelch->mute_state = 1;
	/* loss condition met on init */
	squelch->loss_time = loss_time;
	squelch->loss_state = 1;
}

enum squelch_result squelch(squelch_t *squelch, double rf_level_db, double duration)
{
	/* squelch disabled */
	if (isinf(squelch->threshold_db))
		return SQUELCH_OPEN;

	/* count until start quelch processing */
	squelch->init_count += duration;
	if (squelch->init_count < SQUELCH_INIT_TIME)
		return SQUELCH_MUTE;

	/* measure noise floor and calibrate threashold_db */
	if (squelch->auto_state) {
		squelch->auto_count += duration;
		squelch->auto_level_sum += rf_level_db;
		squelch->auto_level_count++;
		if (squelch->auto_count >= SQUELCH_AUTO_TIME) {
			double noise_db, threshold_db;
			noise_db = squelch->auto_level_sum / (double) squelch->auto_level_count;
			threshold_db = noise_db + SQUELCH_AUTO_OFFSET;
			/* must be 0.1 dB smaller, so we prevent repeated debugging message with similar value */
			if (threshold_db < squelch->threshold_db - 0.1) {
				squelch->threshold_db = threshold_db;
				PDEBUG_CHAN(DDSP, DEBUG_INFO, "RF signal measurement: %.1f dB noise floor, using squelch threshold of %.1f dB\n", noise_db, threshold_db);
			}
			squelch->auto_count = 0.0;
			squelch->auto_level_count = 0;
			squelch->auto_level_sum = 0.0;
		}
	}

	/* enough RF level, so we unmute when mute_count reached 0 */
	if (rf_level_db >= squelch->threshold_db) {
		squelch->mute_count -= duration;
		if (squelch->mute_count <= 0.0) {
			if (squelch->mute_state) {
				PDEBUG_CHAN(DDSP, DEBUG_INFO, "RF signal strong: Unmuting audio (RF %.1f >= %.1f dB)\n", rf_level_db, squelch->threshold_db);
				squelch->mute_state = 0;
			}
			squelch->mute_count = 0.0;
		}
	} else {
		/* RF level too low, so we mute when mute_count reached mute_time */
		squelch->mute_count += duration;
		if (squelch->mute_count >= squelch->mute_time) {
			if (!squelch->mute_state) {
				PDEBUG_CHAN(DDSP, DEBUG_INFO, "RF signal weak: Muting audio (RF %.1f < %.1f dB)\n", rf_level_db, squelch->threshold_db);
				squelch->mute_state = 1;
			}
			squelch->mute_count = squelch->mute_time;
		}
	}

	if (squelch->mute_state) {
		/* at 'mute' condition, count and check for loss */
		squelch->loss_count += duration;
		if (squelch->loss_count >= squelch->loss_time) {
			if (!squelch->loss_state) {
				PDEBUG_CHAN(DDSP, DEBUG_DEBUG, "RF signal loss detected after %.1f seconds\n", squelch->loss_time);
				squelch->loss_state = 1;
				return SQUELCH_LOSS;
			}
		}
		return SQUELCH_MUTE;
	} else {
		/* at unmute condition, reset loss counter */
		squelch->loss_state = 0;
		squelch->loss_count = 0.0;
		return SQUELCH_OPEN;
	}
}