aboutsummaryrefslogtreecommitdiffstats
path: root/src/libfm/fm.c
blob: 8463dc5d4df657cfa08a6b59ac80d216acacd46a (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* FM modulation processing
 *
 * (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 <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include "../libsample/sample.h"
#include "fm.h"

static int has_init = 0;
static int fast_math = 0;
static float *sin_tab = NULL, *cos_tab = NULL;

/* global init */
int fm_init(int _fast_math)
{
	fast_math = _fast_math;

	if (fast_math) {
		int i;

		sin_tab = calloc(65536+16384, sizeof(*sin_tab));
		if (!sin_tab) {
			fprintf(stderr, "No mem!\n");
			return -ENOMEM;
		}
		cos_tab = sin_tab + 16384;

		/* generate sine and cosine */
		for (i = 0; i < 65536+16384; i++)
			sin_tab[i] = sin(2.0 * M_PI * (double)i / 65536.0);
	}

	has_init = 1;

	return 0;
}

/* global exit */
void fm_exit(void)
{
	if (sin_tab) {
		free(sin_tab);
		sin_tab = cos_tab = NULL;
	}

	has_init = 0;
}

/* init FM modulator */
int fm_mod_init(fm_mod_t *mod, double samplerate, double offset, double amplitude)
{
	int i;

	if (!has_init) {
		fprintf(stderr, "libfm was not initialized, please fix!\n");
		abort();
	}

	memset(mod, 0, sizeof(*mod));
	mod->samplerate = samplerate;
	mod->offset = offset;
	mod->amplitude = amplitude;

	mod->ramp_length = samplerate * 0.001;
	mod->ramp_tab = calloc(mod->ramp_length, sizeof(*mod->ramp_tab));
	if (!mod->ramp_tab) {
		fprintf(stderr, "No mem!\n");
		return -ENOMEM;
	}
	mod->state = MOD_STATE_OFF;

	/* generate ramp up with ramp_length */
	for (i = 0; i < mod->ramp_length; i++)
		mod->ramp_tab[i] = 0.5 - cos(M_PI * i / mod->ramp_length) / 2.0;

	return 0;
}

void fm_mod_exit(fm_mod_t *mod)
{
	if (mod->ramp_tab) {
		free(mod->ramp_tab);
		mod->ramp_tab = NULL;
	}
}

/* do frequency modulation of samples and add them to existing baseband */
void fm_modulate_complex(fm_mod_t *mod, sample_t *frequency, uint8_t *power, int length, float *baseband)
{
	double dev, rate, phase, offset;
	int ramp, ramp_length;
	double *ramp_tab;
	double amplitude;

	rate = mod->samplerate;
	phase = mod->phase;
	offset = mod->offset;
	ramp = mod->ramp;
	ramp_length = mod->ramp_length;
	ramp_tab = mod->ramp_tab;
	amplitude = mod->amplitude;

again:
	switch (mod->state) {
	case MOD_STATE_ON:
		/* modulate */
		while (length) {
			/* is power is not set, ramp down */
			if (!(*power)) {
				mod->state = MOD_STATE_RAMP_DOWN;
				break;
			}
			/* deviation is defined by the frequency value and the offset */
			dev = offset + *frequency++;
			power++;
			length--;
			if (fast_math) {
				phase += 65536.0 * dev / rate;
				if (phase < 0.0)
					phase += 65536.0;
				else if (phase >= 65536.0)
					phase -= 65536.0;
				*baseband++ += cos_tab[(uint16_t)phase] * amplitude;
				*baseband++ += sin_tab[(uint16_t)phase] * amplitude;
			} else {
				phase += 2.0 * M_PI * dev / rate;
				if (phase < 0.0)
					phase += 2.0 * M_PI;
				else if (phase >= 2.0 * M_PI)
					phase -= 2.0 * M_PI;
				*baseband++ += cos(phase) * amplitude;
				*baseband++ += sin(phase) * amplitude;
			}
		}
		break;
	case MOD_STATE_RAMP_DOWN:
		while (length) {
			/* if power is set, ramp up */
			if (*power) {
				mod->state = MOD_STATE_RAMP_UP;
				break;
			}
			if (ramp == 0) {
				mod->state = MOD_STATE_OFF;
				break;
			}
			dev = offset + *frequency++;
			power++;
			length--;
			if (fast_math) {
				phase += 65536.0 * dev / rate;
				if (phase < 0.0)
					phase += 65536.0;
				else if (phase >= 65536.0)
					phase -= 65536.0;
				*baseband++ += cos_tab[(uint16_t)phase] * amplitude * ramp_tab[ramp];
				*baseband++ += sin_tab[(uint16_t)phase] * amplitude * ramp_tab[ramp];
			} else {
				phase += 2.0 * M_PI * dev / rate;
				if (phase < 0.0)
					phase += 2.0 * M_PI;
				else if (phase >= 2.0 * M_PI)
					phase -= 2.0 * M_PI;
				*baseband++ += cos(phase) * amplitude * ramp_tab[ramp];
				*baseband++ += sin(phase) * amplitude * ramp_tab[ramp];
			}
			ramp--;
		}
		break;
	case MOD_STATE_OFF:
		while (length) {
			/* if power is set, ramp up */
			if (*power) {
				mod->state = MOD_STATE_RAMP_UP;
				break;
			}
			/* deviation is defined by the frequency value and the offset */
			dev = offset + *frequency++;
			power++;
			length--;
			if (fast_math) {
				phase += 65536.0 * dev / rate;
				if (phase < 0.0)
					phase += 65536.0;
				else if (phase >= 65536.0)
					phase -= 65536.0;
				*baseband++ += cos_tab[(uint16_t)phase] * amplitude * ramp_tab[ramp];
				*baseband++ += sin_tab[(uint16_t)phase] * amplitude * ramp_tab[ramp];
			} else {
				phase += 2.0 * M_PI * dev / rate;
				if (phase < 0.0)
					phase += 2.0 * M_PI;
				else if (phase >= 2.0 * M_PI)
					phase -= 2.0 * M_PI;
				*baseband++ += cos(phase) * amplitude * ramp_tab[ramp];
				*baseband++ += sin(phase) * amplitude * ramp_tab[ramp];
			}
		}
		break;
	case MOD_STATE_RAMP_UP:
		while (length) {
			/* is power is not set, ramp down */
			if (!(*power)) {
				mod->state = MOD_STATE_RAMP_DOWN;
				break;
			}
			if (ramp == ramp_length - 1) {
				mod->state = MOD_STATE_ON;
				break;
			}
			/* deviation is defined by the frequency value and the offset */
			dev = offset + *frequency++;
			power++;
			length--;
			if (fast_math) {
				phase += 65536.0 * dev / rate;
				if (phase < 0.0)
					phase += 65536.0;
				else if (phase >= 65536.0)
					phase -= 65536.0;
				*baseband++ += cos_tab[(uint16_t)phase] * amplitude * ramp_tab[ramp];
				*baseband++ += sin_tab[(uint16_t)phase] * amplitude * ramp_tab[ramp];
			} else {
				phase += 2.0 * M_PI * dev / rate;
				if (phase < 0.0)
					phase += 2.0 * M_PI;
				else if (phase >= 2.0 * M_PI)
					phase -= 2.0 * M_PI;
				*baseband++ += cos(phase) * amplitude * ramp_tab[ramp];
				*baseband++ += sin(phase) * amplitude * ramp_tab[ramp];
			}
			ramp++;
		}
		break;
	}
	if (length)
		goto again;

	mod->phase = phase;
	mod->ramp = ramp;
}

/* init FM demodulator */
int fm_demod_init(fm_demod_t *demod, double samplerate, double offset, double bandwidth)
{
	if (!has_init) {
		fprintf(stderr, "libfm was not initialized, please fix!\n");
		abort();
	}

	memset(demod, 0, sizeof(*demod));
	demod->samplerate = samplerate;

	if (fast_math)
		demod->rot = 65536.0 * -offset / samplerate;
	else
		demod->rot = 2 * M_PI * -offset / samplerate;

	/* use fourth order (2 iter) filter, since it is as fast as second order (1 iter) filter */
	iir_lowpass_init(&demod->lp[0], bandwidth / 2.0, samplerate, 2);
	iir_lowpass_init(&demod->lp[1], bandwidth / 2.0, samplerate, 2);

	return 0;
}

void fm_demod_exit(fm_demod_t __attribute__ ((unused)) *demod)
{
}

static inline float fast_tan(float z)
{
	const float n1 = 0.97239411f;
	const float n2 = -0.19194795f;
	return (n1 + n2 * z * z) * z;
}

static inline float fast_atan2(float y, float x)
{
	if (x != 0.0) {
		if (fabsf(x) > fabsf(y)) {
			const float z = y / x;
			if (x > 0.0) /* atan2(y,x) = atan(y/x) if x > 0 */
				return fast_tan(z);
			else if (y >= 0.0) /* atan2(y,x) = atan(y/x) + PI if x < 0, y >= 0 */
				return fast_tan(z) + M_PI;
			else /* atan2(y,x) = atan(y/x) - PI if x < 0, y < 0 */
				return fast_tan(z) - M_PI;
		} else { /* Use property atan(y/x) = PI/2 - atan(x/y) if |y/x| > 1 */
			const float z = x / y;
			if (y > 0.0) /* atan2(y,x) = PI/2 - atan(x/y) if |y/x| > 1, y > 0 */
				return -fast_tan(z) + M_PI_2;
			else /* atan2(y,x) = -PI/2 - atan(x/y) if |y/x| > 1, y < 0 */
				return -fast_tan(z) - M_PI_2;
		}
	} else {
		if (y > 0.0) /* x = 0, y > 0 */
			return M_PI_2;
		else if (y < 0.0) /* x = 0, y < 0 */
			return -M_PI_2;
	}
	return 0.0; /* x,y = 0. return 0, because NaN would harm further processing  */
}

/* do frequency demodulation of baseband and write them to samples */
void fm_demodulate_complex(fm_demod_t *demod, sample_t *frequency, int length, float *baseband, sample_t *I, sample_t *Q)
{
	double phase, rot, last_phase, dev, rate;
	double _sin, _cos;
	sample_t i, q;
	int s, ss;

	rate = demod->samplerate;
	phase = demod->phase;
	rot = demod->rot;
	for (s = 0, ss = 0; s < length; s++) {
		phase += rot;
		i = baseband[ss++];
		q = baseband[ss++];
		if (fast_math) {
			if (phase < 0.0)
				phase += 65536.0;
			else if (phase >= 65536.0)
				phase -= 65536.0;
			_sin = sin_tab[(uint16_t)phase];
			_cos = cos_tab[(uint16_t)phase];
		} else {
			if (phase < 0.0)
				phase += 2.0 * M_PI;
			else if (phase >= 2.0 * M_PI)
				phase -= 2.0 * M_PI;
			_sin = sin(phase);
			_cos = cos(phase);
		}
		I[s] = i * _cos - q * _sin;
		Q[s] = i * _sin + q * _cos;
	}
	demod->phase = phase;
	iir_process(&demod->lp[0], I, length);
	iir_process(&demod->lp[1], Q, length);
	last_phase = demod->last_phase;
	for (s = 0; s < length; s++) {
		if (fast_math)
			phase = fast_atan2(Q[s], I[s]);
		else
			phase = atan2(Q[s], I[s]);
		dev = (phase - last_phase) / 2 / M_PI;
		last_phase = phase;
		if (dev < -0.49)
			dev += 1.0;
		else if (dev > 0.49)
			dev -= 1.0;
		dev *= rate;
		frequency[s] = dev;
	}
	demod->last_phase = last_phase;
}

void fm_demodulate_real(fm_demod_t *demod, sample_t *frequency, int length, sample_t *baseband, sample_t *I, sample_t *Q)
{
	double phase, rot, last_phase, dev, rate;
	double _sin, _cos;
	sample_t i;
	int s, ss;

	rate = demod->samplerate;
	phase = demod->phase;
	rot = demod->rot;
	for (s = 0, ss = 0; s < length; s++) {
		phase += rot;
		i = baseband[ss++];
		if (fast_math) {
			if (phase < 0.0)
				phase += 65536.0;
			else if (phase >= 65536.0)
				phase -= 65536.0;
			_sin = sin_tab[(uint16_t)phase];
			_cos = cos_tab[(uint16_t)phase];
		} else {
			if (phase < 0.0)
				phase += 2.0 * M_PI;
			else if (phase >= 2.0 * M_PI)
				phase -= 2.0 * M_PI;
			_sin = sin(phase);
			_cos = cos(phase);
		}
		I[s] = i * _cos;
		Q[s] = i * _sin;
	}
	demod->phase = phase;
	iir_process(&demod->lp[0], I, length);
	iir_process(&demod->lp[1], Q, length);
	last_phase = demod->last_phase;
	for (s = 0; s < length; s++) {
		if (fast_math)
			phase = fast_atan2(Q[s], I[s]);
		else
			phase = atan2(Q[s], I[s]);
		dev = (phase - last_phase) / 2 / M_PI;
		last_phase = phase;
		if (dev < -0.49)
			dev += 1.0;
		else if (dev > 0.49)
			dev -= 1.0;
		dev *= rate;
		frequency[s] = dev;
	}
	demod->last_phase = last_phase;
}