forked from xdsopl/robot36
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddc.c
74 lines (69 loc) · 2.12 KB
/
ddc.c
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
/*
robot36 - encode and decode images using SSTV in Robot 36 mode
Written in 2011 by <Ahmet Inan> <[email protected]>
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#include <complex.h>
#include <math.h>
#include <stdlib.h>
#include "window.h"
#include "ddc.h"
void do_ddc(struct ddc *ddc, float *input, complex float *output)
{
int N = ddc->N;
int M = ddc->M;
int L = ddc->L;
int offset = 0;
for (int k = 0; k < L; k++) {
float *x = input + (((L * M - 1) - offset) / L);
complex float *b = ddc->b + (N * (offset % L));
offset += M;
complex float sum = 0.0;
for (int i = 0; i < N; i++)
sum += b[i] * x[i];
output[k] = ddc->osc * sum;
ddc->osc *= ddc->d;
ddc->osc /= cabsf(ddc->osc);
}
}
struct ddc *alloc_ddc(int L, int M, float carrier, float bw, float rate, int taps, float (*window)(float, float, float), float a)
{
float lstep = 1.0 / ((float)L * rate);
float ostep = (float)M * lstep;
struct ddc *ddc = malloc(sizeof(struct ddc));
ddc->N = (taps + L - 1) / L;
ddc->b = malloc(sizeof(complex float) * ddc->N * L);
ddc->osc = I;
ddc->d = cexpf(-I * 2.0 * M_PI * carrier * ostep);
ddc->L = L;
ddc->M = M;
complex float *b = malloc(sizeof(complex float) * taps);
float sum = 0.0;
for (int i = 0; i < taps; i++) {
float N = taps;
float n = i;
float x = n - (N - 1.0) / 2.0;
float l = 2.0 * bw * lstep;
float h = l * sinc(l * x);
float w = window(n, N, a);
float t = w * h;
sum += t;
complex float o = cexpf(I * 2.0 * M_PI * carrier * lstep * n);
b[i] = t * o * (float)L;
}
for (int i = 0; i < taps; i++)
b[i] /= sum;
for (int i = 0; i < ddc->N * L; i++)
ddc->b[i] = 0.0;
for (int i = 0; i < L; i++)
for (int j = i, k = 0; j < taps; j += L, k++)
ddc->b[i * ddc->N + k] = b[j];
free(b);
return ddc;
}
void free_ddc(struct ddc *ddc)
{
free(ddc->b);
free(ddc);
}