Skip to content

Instantly share code, notes, and snippets.

@ninjitaru
Created July 11, 2014 02:03
Show Gist options
  • Save ninjitaru/776974d2371e69698939 to your computer and use it in GitHub Desktop.
Save ninjitaru/776974d2371e69698939 to your computer and use it in GitHub Desktop.
// only need 112 bit for 14 ascii character
LDPC_Parity_Irregular H;
H.generate(224,
"0 0.27684 0.28342 0 0 0 0 0 0.43974",
"0 0 0 0 0 0.01568 0.85244 0.13188",
"rand", // random unstructured matrix
"500 8"); // optimize girth
LDPC_Generator_Systematic G(&H);
LDPC_Code C(&H, &G);
C.set_exit_conditions(2500);
// C.set_llrcalc(LLR_calc_unit(12,0,7));
NSLog(@"code generated completed\n");
{
bvec InData = zeros_b(112);
NSLog(@"%i",InData.length());
// NSString *input = @"ab123456789012";
char input[] = "ab123987564912";
for(int i = 0; i < 14; i++) {
char c = input[i];
for(int bitIndex = 0; bitIndex < 8; bitIndex++) {
InData.set(i*8+bitIndex, bin((c >> abs(bitIndex-7)) & 1));
}
}
bvec outData = C.encode(InData);
// G.encode(InData,outData);
// cout << InData << endl;
// cout << outData << endl;
BPSK Mod;
vec s = Mod.modulate_bits(outData);
// create noise
vec EbN0db = "0.6:0.2:5";
QLLRvec llr;
for(int n = 0; n < length(EbN0db); n++) {
double N0 = pow(10.0, -EbN0db(1) / 10.0) / C.get_rate();
NSLog(@"error rate %f",N0);
AWGN_Channel chan(N0 / 2);
vec x = chan(s);
vec softbits = Mod.demodulate_soft_bits(x, N0);
// Decode the received bits
// llr = C.get_llrcalc().to_qllr(softbits);
int it = C.bp_decode(C.get_llrcalc().to_qllr(softbits), llr);
if(it >= 0) {
NSLog(@"used %i iteration on %i",it,n);
break;
} else {
llr.clear();
}
NSLog(@"used %i iteration on %i",it,n);
}
cout << llr.length() << endl;
bvec answer = llr.get(0, 111) < 0;
cout << answer << endl;
char *final = (char*)calloc(14,sizeof(char));
for(int i = 0; i < 14; i++) {
for(int bitIndex = 0; bitIndex < 8; bitIndex++) {
bin b = answer.get(i*8+bitIndex);
if(b == 1) {
final[i] |= 1 << abs(bitIndex-7);
}
}
}
cout << final << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment