1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-09-12 16:16:35 -04:00
sdrangel/plugins/channel/lora/lorabits.h

70 lines
1.8 KiB
C
Raw Normal View History

2015-01-17 10:59:44 -05:00
/*
Interleaving is "easiest" if the same number of bits is used per symbol as for FEC
Chosen mode "spreading 8, low rate" has 6 bits per symbol, so use 4:6 FEC
*/
// Needs adjusting for different sizes
2015-02-09 18:54:51 -05:00
void LoRaDemod::interleave(char* inout, int size)
2015-01-17 10:59:44 -05:00
{
2015-02-09 18:54:51 -05:00
int i, j;
char in[6 * 2];
short s;
for (j = 0; j < size; j+=6) {
for (i = 0; i < 6; i++)
in[i] = in[i + 6] = inout[i + j];
// top bits are swapped
for (i = 0; i < 6; i++) {
s = (32 & in[2 + i]) | (16 & in[1 + i]) | (8 & in[3 + i])
2015-02-06 13:59:35 -05:00
| (4 & in[4 + i]) | (2 & in[5 + i]) | (1 & in[6 + i]);
2015-02-09 18:54:51 -05:00
// bits are also rotated
s = (s << 3) | (s >> 3);
s &= 63;
s = (s >> i) | (s << (6 - i));
inout[i + j] = s & 63;
}
2015-01-17 10:59:44 -05:00
}
}
2015-01-22 17:30:55 -05:00
short LoRaDemod::toGray(short num)
2015-01-17 10:59:44 -05:00
{
2015-01-22 17:30:55 -05:00
return (num >> 1) ^ num;
2015-01-17 10:59:44 -05:00
}
2015-02-09 18:54:51 -05:00
// ignore FEC, try to extract raw bits
void LoRaDemod::hamming(char* c, int size)
2015-02-05 14:38:46 -05:00
{
int i;
2015-02-09 18:54:51 -05:00
2015-02-05 14:38:46 -05:00
for (i = 0; i < size; i++) {
2015-02-09 18:54:51 -05:00
c[i] = ((c[i] & 1)<<3) | ((c[i] & 2)<<0) | ((c[i] & 4)>>0) | ((c[i] & 8)>>3);
i++;
c[i] = ((c[i] & 1)<<2) | ((c[i] & 2)<<2) | ((c[i] & 4)>>1) | ((c[i] & 8)>>3);
i++;
c[i] = ((c[i] & 1)<<3) | ((c[i] & 2)<<1) | ((c[i] & 4)>>1) | ((c[i] & 8)>>3);
i++;
c[i] = ((c[i] & 1)<<3) | ((c[i] & 2)<<1) | ((c[i] & 4)>>1) | ((c[i] & 8)>>3);
i++;
c[i] = ((c[i] & 1)<<3) | ((c[i] & 2)<<1) | ((c[i] & 4)>>1) | ((c[i] & 8)>>3);
i++;
c[i] = ((c[i] & 1)<<3) | ((c[i] & 2)<<1) | ((c[i] & 4)>>2) | ((c[i] & 8)>>2);
2015-02-05 14:38:46 -05:00
}
2015-02-09 18:54:51 -05:00
c[i] = 0;
2015-02-05 14:38:46 -05:00
}
2015-02-07 05:03:51 -05:00
// example data whitening (4 bit only - needs 6 bit for FEC)
2015-02-05 14:38:46 -05:00
void LoRaDemod::prng(char* inout, int size)
{
const char otp[] = {
2015-02-09 18:54:51 -05:00
"LBMGEJJENKKKJBN@KB@KAEDDMMDONIN@N@KFBGBCMCMCMIBJHBDHNJDJALDHE@A@DFGOBOMIM@M@BBBCBAMBMKDENDLEDKNKNBNHKJ@JADD@EAAADBOCGAGBLCDANFLGDCNGLOMOBIHHMLBHB@BHMJGJBLMLED@B"
2015-02-05 14:38:46 -05:00
};
int i, maxchars;
maxchars = sizeof( otp );
if (size < maxchars)
maxchars = size;
for (i = 0; i < maxchars; i++)
2015-02-09 18:54:51 -05:00
inout[i] ^= 0xf & otp[i];
2015-02-05 14:38:46 -05:00
}