Skip to content

Instantly share code, notes, and snippets.

@iiska
Created January 11, 2012 13:19
Show Gist options
  • Save iiska/1594617 to your computer and use it in GitHub Desktop.
Save iiska/1594617 to your computer and use it in GitHub Desktop.
Floating point --> Fixed point --> Bit string conversion for constellation code lookup tables
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
const double qpsk[] = {-1.0, 1.0};
const double qam16[] = {-3.0, -1.0, 3.0, 1.0};
const double qam64[] = {-7.0, -5.0, -1.0, -3.0, 7.0, 5.0, 1.0, 3.0};
/* Generic function for converting short integers to bit string
* representation.
*
* Example:
*
* short s = 19; char output[17];
* short_to_bitstring(s, output);
*
* Output would now contain: "0000000000010011"
*/
void short_to_bitstring(const short s, char *output) {
short mask, i, il;
for (i=0,il=sizeof(s)*8;i<il;++i) {
mask = 1 << i;
output[il-1-i] = ((s & mask) == 0) ? '0' : '1';
}
output[il] = '\0';
}
void print_normalized_lut(const double *arr, const short size,
const double ncoef) {
short i, fp;
char binary[17];
double f;
for (i=0;i<size;++i) {
f = arr[i] * ncoef;
// Convert normalized constellation code to s,1,14 fixed point format
fp = (signed short)(f * (1<<14) + (f >=0 ? 0.5 : -0.5));
short_to_bitstring(fp, binary);
printf("%s\n", binary);
}
}
int main() {
printf("BPSK\n");
print_normalized_lut(qpsk, 2, 1.0);
printf("QPSK\n");
print_normalized_lut(qpsk, 2, 1.0 / sqrt(2.0));
printf("16QAM\n");
print_normalized_lut(qam16, 4, 1.0 / sqrt(10));
printf("64QAM\n");
print_normalized_lut(qam64, 8, 1.0 / sqrt(42));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment