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
|
#include "correlator32.h"
uint32_t rotate_64(uint32_t word, uint32_t p)
{
uint32_t i = word & 0xaaaaaaaa;
uint32_t q = word & 0x55555555;
switch (p)
{
case PHASE_0:
break;
case PHASE_90:
word = ((i ^ 0xaaaaaaaa) >> 1) | (q << 1);
break;
case PHASE_180:
word = word ^ 0xffffffff;
break;
case PHASE_270:
word = (i >> 1) | ((q ^ 0x55555555) << 1);
break;
default:
break;
}
return ((word & 0x55555555) << 1) | ((word & 0xAAAAAAAA) >> 1);
}
int corr_64(uint32_t v1, uint32_t v2)
{
int cor = 0;
uint32_t diff = v1 ^ v2;
for (; diff; cor++)
diff &= diff - 1;
return 32 - cor;
}
uint32_t swapIQ(uint32_t in)
{
uint32_t i = in & 0xaaaaaaaa;
uint32_t q = in & 0x55555555;
return (i >> 1) | (q << 1);
}
Correlator32::Correlator32(constellation_t mod, uint32_t syncword) : d_modulation(mod)
{
hard_buf = new uint8_t[8192 * 20];
if (d_modulation == BPSK)
{
// Generate syncwords
syncwords[0] = syncword;
syncwords[1] = syncword ^ 0xFFFFFFFFFF;
}
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) ^ 0xFFFFFFFFFF), (phase_t)(i - 4));
}
}
Correlator32::~Correlator32()
{
delete[] hard_buf;
}
int Correlator32::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++;
}
}
uint32_t current = ((uint32_t)hard_buf[0] << 24) | ((uint32_t)hard_buf[1] << 16) |
((uint32_t)hard_buf[2] << 8) | ((uint32_t)hard_buf[3] << 0);
if (d_modulation == BPSK)
{
pos += 4;
// Check pos 0
for (int p = 0; p < 2; p++)
{
int corr = corr_64(syncwords[p], current);
if (corr > 27)
{
cor = corr;
phase = p ? PHASE_180 : PHASE_0;
swap = 0;
return 0;
}
}
// Check the rest
for (int i = 0; i < (length / 8) - 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 += 4;
// Check pos 0
for (int p = 0; p < 8; p++)
{
int corr = corr_64(syncwords[p], current);
if (corr > 27)
{
cor = corr;
phase = (phase_t)(p % 4);
swap = (p / 4) == 0;
return 0;
}
}
// Check the rest
for (int i = 0; i < length - 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;
}
|