File: correlator.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (178 lines) | stat: -rw-r--r-- 4,574 bytes parent folder | download | duplicates (2)
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
#include "correlator.h"

uint64_t rotate_64(uint64_t word, uint64_t p)
{
    uint64_t i = word & 0xaaaaaaaaaaaaaaaa;
    uint64_t q = word & 0x5555555555555555;

    switch (p)
    {
    case PHASE_0:
        break;
    case PHASE_90:
        word = ((i ^ 0xaaaaaaaaaaaaaaaa) >> 1) | (q << 1);
        break;
    case PHASE_180:
        word = word ^ 0xffffffffffffffff;
        break;
    case PHASE_270:
        word = (i >> 1) | ((q ^ 0x5555555555555555) << 1);
        break;
    default:
        break;
    }

    return ((word & 0x5555555555555555) << 1) | ((word & 0xAAAAAAAAAAAAAAAA) >> 1);
}

int corr_64(uint64_t v1, uint64_t v2)
{
    int cor = 0;
    uint64_t diff = v1 ^ v2;
    for (; diff; cor++)
        diff &= diff - 1;
    return 64 - cor;
}

uint64_t swapIQ(uint64_t in)
{
    uint64_t i = in & 0xaaaaaaaaaaaaaaaa;
    uint64_t q = in & 0x5555555555555555;
    return (i >> 1) | (q << 1);
}

Correlator::Correlator(constellation_t mod, uint64_t syncword) : d_modulation(mod)
{
    hard_buf = new uint8_t[8192 * 20];

    if (d_modulation == BPSK)
    {
        // Generate syncwords
        syncwords[0] = syncword;
        syncwords[1] = syncword ^ 0xFFFFFFFFFFFFFFFF;
    }
    else if (d_modulation == QPSK)
    {
        // Generate syncwords
        for (int i = 0; i < 4; i++)
            syncwords[i] = rotate_64(syncword, (phase_t)i);

        // Generate rotated syncwords
        for (int i = 4; i < 8; i++)
            syncwords[i] = rotate_64((swapIQ(syncword) ^ 0xFFFFFFFFFFFFFFFF), (phase_t)(i - 4));
    }
}

Correlator::~Correlator()
{
    delete[] hard_buf;
}

int Correlator::correlate(int8_t *soft_input, phase_t &phase, bool &swap, int &cor, int length)
{
    int correlation = 0, offset = 0, pos = 0;

    // Pack into hard symbols
    int bits = 0, bytes = 0;
    uint8_t shifter = 0;
    for (int i = 0; i < length; i++)
    {
        shifter = shifter << 1 | (soft_input[i] > 0);
        bits++;

        if (bits == 8)
        {
            hard_buf[bytes] = shifter;
            bits = 0;
            bytes++;
        }
    }

    uint64_t current = ((uint64_t)hard_buf[0] << 56) | ((uint64_t)hard_buf[1] << 48) | ((uint64_t)hard_buf[2] << 40) |
                       ((uint64_t)hard_buf[3] << 32) | ((uint64_t)hard_buf[4] << 24) | ((uint64_t)hard_buf[5] << 16) |
                       ((uint64_t)hard_buf[6] << 8) | ((uint64_t)hard_buf[7] << 0);

    if (d_modulation == BPSK)
    {
        pos += 8;

        // Check pos 0
        for (int p = 0; p < 2; p++)
        {
            int corr = corr_64(syncwords[p], current);
            if (corr > 45)
            {
                cor = corr;
                phase = p ? PHASE_180 : PHASE_0;
                swap = 0;
                return 0;
            }
        }

        // Check the rest
        for (int i = 0; i < length - 8; i++)
        {
            for (int ii = 0; ii < 8; ii += 1)
            {
                for (int p = 0; p < 2; p++)
                {
                    int corr = corr_64(syncwords[p], current);
                    if (corr > correlation)
                    {
                        correlation = corr;
                        offset = i * 8 + ii;
                        phase = p ? PHASE_180 : PHASE_0;
                        swap = 0;
                    }
                }

                current = (current << 1) | ((hard_buf[pos] >> (7 - ii)) & 0b1);
            }

            pos++;
        }
    }
    else if (d_modulation == QPSK)
    {
        pos += 8;

        // Check pos 0
        for (int p = 0; p < 8; p++)
        {
            int corr = corr_64(syncwords[p], current);
            if (corr > 45)
            {
                cor = corr;
                phase = (phase_t)(p % 4);
                swap = (p / 4) == 0;
                return 0;
            }
        }

        // Check the rest
        for (int i = 0; i < (length / 8) - 8; i++)
        {
            for (int ii = 0; ii < 8; ii += 2)
            {
                for (int p = 0; p < 8; p++)
                {
                    int corr = corr_64(syncwords[p], current);
                    if (corr > correlation)
                    {
                        correlation = corr;
                        offset = i * 8 + ii;
                        phase = (phase_t)(p % 4);
                        swap = (p / 4) == 0;
                    }
                }

                current = (current << 2) | ((hard_buf[pos] >> (6 - ii)) & 0b11);
            }

            pos++;
        }
    }

    cor = correlation;
    return offset;
}