/* main.c QRA64 mode encode/decode tests (c) 2016 - Nico Palermo, IV3NWV Thanks to Andrea Montefusco IW0HDV for his help on adapting the sources to OSs other than MS Windows ------------------------------------------------------------------------------ This file is part of the qracodes project, a Forward Error Control encoding/decoding package based on Q-ary RA (Repeat and Accumulate) LDPC codes. Files in this package: main.c - this file qra64.c/.h - qra64 mode encode/decoding functions ../qracodes/normrnd.{c,h} - random gaussian number generator ../qracodes/npfwht.{c,h} - Fast Walsh-Hadamard Transforms ../qracodes/pdmath.{c,h} - Elementary math on probability distributions ../qracodes/qra12_63_64_irr_b.{c,h} - Tables for a QRA(12,63) irregular RA code over GF(64) ../qracodes/qra13_64_64_irr_e.{c,h} - Tables for a QRA(13,64) irregular RA code over GF(64) ../qracodes/qracodes.{c,h} - QRA codes encoding/decoding functions ------------------------------------------------------------------------------- qracodes is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. qracodes is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with qracodes source distribution. If not, see . ----------------------------------------------------------------------------- The code used by the QRA64 mode is the code: QRA13_64_64_IRR_E: K=13 N=64 Q=64 irregular QRA code (defined in qra13_64_64_irr_e.{h,c}). This code has been designed to include a CRC as the 13th information symbol and improve the code UER (Undetected Error Rate). The CRC symbol is not sent along the channel (the codes are punctured) and the resulting code is still a (12,63) code with an effective code rate of R = 12/63. */ // OS dependent defines and includes ------------------------------------------ #if _WIN32 // note the underscore: without it, it's not msdn official! // Windows (x64 and x86) #include // required only for GetTickCount(...) #include // _beginthread #endif #if __linux__ #include #include unsigned GetTickCount(void) { struct timespec ts; unsigned theTick = 0U; clock_gettime( CLOCK_REALTIME, &ts ); theTick = ts.tv_nsec / 1000000; theTick += ts.tv_sec * 1000; return theTick; } #endif #if __APPLE__ #endif #include #include #include #include "qra64.h" #include "../qracodes/normrnd.h" // gaussian numbers generator // ---------------------------------------------------------------------------- // channel types #define CHANNEL_AWGN 0 #define CHANNEL_RAYLEIGH 1 #define CHANNEL_FASTFADE 2 #define JT65_SNR_EBNO_OFFSET 29.1f // with the synch used in JT65 #define QRA64_SNR_EBNO_OFFSET 31.0f // with the costas array synch void printwordd(char *msg, int *x, int size) { int k; printf("\n%s ",msg); for (k=0;k-15) if (channel_type == CHANNEL_AWGN) for (k=0;k=0) { // decoded printf("K1JT rx: received with apcode=%d %s\n",rc, decode_type[rc]); // Step 2a: K1JT replies to IV3NWV (with no grid) printf("K1JT tx: IV3NWV K1JT\n"); encodemsg_jt65(x,CALL_IV3NWV,CALL_K1JT, GRID_BLANK); qra64_encode(codec_k1jt, y, x); rx = mfskchannel(y,channel_type,EbNodB); // Step 2b: IV3NWV attempts to decode [? ? ?], [IV3NWV ? ?] or [IV3NWV ?] rc = qra64_decode(codec_iv3nwv, 0, xdec,rx); if (rc>=0) { // decoded printf("IV3NWV rx: received with apcode=%d %s\n",rc, decode_type[rc]); // Step 3a: IV3NWV replies to K1JT with a 73 printf("IV3NWV tx: K1JT IV3NWV 73\n"); encodemsg_jt65(x,CALL_K1JT,CALL_IV3NWV, GRID_73); qra64_encode(codec_iv3nwv, y, x); rx = mfskchannel(y,channel_type,EbNodB); // Step 3b: K1JT attempts to decode [? ? ?] or [K1JT IV3NWV ?] rc = qra64_decode(codec_k1jt, 0, xdec,rx); if (rc>=0) { // decoded printf("K1JT rx: received with apcode=%d %s\n",rc, decode_type[rc]); // Step 4a: K1JT replies to IV3NWV with a 73 printf("K1JT tx: IV3NWV K1JT 73\n"); encodemsg_jt65(x,CALL_IV3NWV,CALL_K1JT, GRID_73); qra64_encode(codec_k1jt, y, x); rx = mfskchannel(y,channel_type,EbNodB); // Step 4b: IV3NWV attempts to decode [? ? ?], [IV3NWV ? ?], or [IV3NWV ?] rc = qra64_decode(codec_iv3nwv, 0, xdec,rx); if (rc>=0) { // decoded printf("IV3NWV rx: received with apcode=%d %s\n",rc, decode_type[rc]); return 0; } } } } printf("no decode\n"); return -1; } int test_proc_2(int channel_type, float EbNodB, int mode) { /* Here we simulate the decoder of K1JT after K1JT has sent a msg [IV3NWV K1JT] and IV3NWV sends him the msg [K1JT IV3NWV JN66]. If mode=QRA_NOAP, K1JT decoder attempts to decode only msgs of type [? ? ?]. If mode=QRA_AUTOP, K1JT decoder will attempt to decode also the msgs [K1JT IV3NWV] and [K1JT IV3NWV ?]. In the case a decode is successful the return code of the qra64_decode function indicates the amount of a-priori information required to decode the received message according to this table: rc=0 [? ? ?] AP0 rc=1 [CQ ? ?] AP27 rc=2 [CQ ? ] AP42 rc=3 [CALL ? ?] AP29 rc=4 [CALL ? ] AP44 rc=5 [CALL CALL ?] AP57 rc=6 [? CALL ?] AP29 rc=7 [? CALL ] AP44 rc=8 [CALL CALL GRID] AP72 rc=9 [CQ CALL ?] AP55 rc=10 [CQ CALL ] AP70 rc=11 [CQ CALL GRID] AP70 The return code is <0 when decoding is unsuccessful This test simulates the situation ntx times and reports how many times a particular type decode among the above 6 cases succeded. */ int x[QRA64_K], xdec[QRA64_K]; int y[QRA64_N]; float *rx; float ebnodbest, ebnodbavg=0; int rc,k; int ndecok[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int nundet = 0; int ntx = 200,ndec=0; qra64codec *codec_iv3nwv = qra64_init(mode); // codec for IV3NWV qra64codec *codec_k1jt = qra64_init(mode); // codec for K1JT printf("\nQRA64 Test #2 - Decoding with AP knowledge (SNR-Eb/No offset = %.1f dB)\n\n", QRA64_SNR_EBNO_OFFSET); // This will enable K1JT's decoder to look for calls directed to him [K1JT ? ?/b] // printf("K1JT decoder enabled for [K1JT ? ?/blank]\n"); // qra64_apset(codec_k1jt, CALL_K1JT,0,0,APTYPE_MYCALL); // This will enable K1JT's decoder to look for IV3NWV calls directed to him [K1JT IV3NWV ?/b] // printf("K1JT decoder enabled for [K1JT IV3NWV ?]\n"); // qra64_apset(codec_k1jt, CALL_CQ,CALL_IV3NWV,0,APTYPE_BOTHCALLS); // This will enable K1JT's decoder to look for msges sent by IV3NWV [? IV3NWV ?] // printf("K1JT decoder enabled for [? IV3NWV ?/blank]\n"); // qra64_apset(codec_k1jt, 0,CALL_IV3NWV,GRID_BLANK,APTYPE_HISCALL); // This will enable K1JT's decoder to look for full-knowledge [K1JT IV3NWV JN66] msgs printf("K1JT decoder enabled for [K1JT IV3NWV JN66]\n"); qra64_apset(codec_k1jt, CALL_K1JT,CALL_IV3NWV,GRID_JN66,APTYPE_FULL); // This will enable K1JT's decoder to look for calls from IV3NWV [CQ IV3NWV ?/b] msgs printf("K1JT decoder enabled for [CQ IV3NWV ?/b/JN66]\n"); qra64_apset(codec_k1jt, 0,CALL_IV3NWV,GRID_JN66,APTYPE_CQHISCALL); // Dx station IV3NWV calls printf("\nIV3NWV encoder sends msg: [K1JT IV3NWV JN66]\n\n"); encodemsg_jt65(x,CALL_CQ,CALL_IV3NWV,GRID_JN66); // printf("\nIV3NWV encoder sends msg: [CQ IV3NWV JN66]\n\n"); // encodemsg_jt65(x,CALL_CQ,CALL_IV3NWV,GRID_JN66); // printf("\nIV3NWV encoder sends msg: [CQ IV3NWV]\n\n"); // encodemsg_jt65(x,CALL_CQ,CALL_IV3NWV,GRID_BLANK); qra64_encode(codec_iv3nwv, y, x); printf("Simulating K1JT decoder up to AP72\n"); for (k=0;k=0) { ebnodbavg +=ebnodbest; if (memcmp(xdec,x,12*sizeof(int))==0) ndecok[rc]++; else nundet++; } } printf("\n\n"); printf("Transimtted msgs:%d\nDecoded msgs:\n\n",ntx); for (k=0;k<12;k++) { printf("%3d with %s\n",ndecok[k],decode_type[k]); ndec += ndecok[k]; } printf("\nTotal: %d/%d (%d undetected errors)\n\n",ndec,ntx,nundet); printf(""); ebnodbavg/=(ndec+nundet); printf("Estimated SNR (average in dB) = %.2f dB\n\n",ebnodbavg-QRA64_SNR_EBNO_OFFSET); return 0; } int test_fastfading(float EbNodB, float B90, int fadingModel, int submode, int apmode, int olddec, int channel_type, int ntx) { int x[QRA64_K], xdec[QRA64_K]; int y[QRA64_N]; float *rx; float ebnodbest, ebnodbavg=0; int rc,k; float rxolddec[QRA64_N*QRA64_M]; // holds the energies at nominal tone freqs int ndecok[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; int nundet = 0; int ndec=0; qra64codec *codec_iv3nwv; qra64codec *codec_k1jt; codec_iv3nwv=qra64_init(QRA_NOAP); codec_k1jt =qra64_init(apmode); if (channel_type==2) { // fast-fading case printf("Simulating the fast-fading channel\n"); printf("B90=%.2f Hz - Fading Model=%s - Submode=QRA64%c\n",B90,fadingModel?"Lorentz":"Gauss",submode+'A'); printf("Decoder metric = %s\n",olddec?"AWGN":"Matched to fast-fading signal"); } else { printf("Simulating the %s channel\n",channel_type?"Rayleigh block fading":"AWGN"); printf("Decoder metric = AWGN\n"); } printf("\nEncoding msg [K1JT IV3NWV JN66]\n"); encodemsg_jt65(x,CALL_K1JT,CALL_IV3NWV,GRID_JN66); // printf("["); // for (k=0;k<11;k++) printf("%02hX ",x[k]); printf("%02hX]\n",x[11]); qra64_encode(codec_iv3nwv, y, x); printf("%d transmissions will be simulated\n\n",ntx); if (apmode==QRA_USERAP) { // This will enable K1JT's decoder to look for cq/qrz calls [CQ/QRZ ? ?/b] printf("K1JT decoder enabled for [CQ ? ?/blank]\n"); qra64_apset(codec_k1jt, CALL_K1JT,0,0,APTYPE_CQQRZ); // This will enable K1JT's decoder to look for calls directed to him [K1JT ? ?/b] printf("K1JT decoder enabled for [K1JT ? ?/blank]\n"); qra64_apset(codec_k1jt, CALL_K1JT,0,0,APTYPE_MYCALL); // This will enable K1JT's decoder to look for msges sent by IV3NWV [? IV3NWV ?] printf("K1JT decoder enabled for [? IV3NWV ?/blank]\n"); qra64_apset(codec_k1jt, 0,CALL_IV3NWV,GRID_BLANK,APTYPE_HISCALL); // This will enable K1JT's decoder to look for IV3NWV calls directed to him [K1JT IV3NWV ?/b] printf("K1JT decoder enabled for [K1JT IV3NWV ?]\n"); qra64_apset(codec_k1jt, CALL_K1JT,CALL_IV3NWV,0,APTYPE_BOTHCALLS); // This will enable K1JT's decoder to look for full-knowledge [K1JT IV3NWV JN66] msgs printf("K1JT decoder enabled for [K1JT IV3NWV JN66]\n"); qra64_apset(codec_k1jt, CALL_K1JT,CALL_IV3NWV,GRID_JN66,APTYPE_FULL); // This will enable K1JT's decoder to look for calls from IV3NWV [CQ IV3NWV ?/b] msgs printf("K1JT decoder enabled for [CQ IV3NWV ?/b/JN66]\n"); qra64_apset(codec_k1jt, 0,CALL_IV3NWV,GRID_JN66,APTYPE_CQHISCALL); } printf("\nNow decoding with K1JT's decoder...\n"); /* if (channel_type==2) // simulate a fast-faded signal printf("Simulating a fast-fading channel with given B90 and spread type\n"); else printf("Simulating a %s channel\n",channel_type?"Rayleigh block fading":"AWGN"); */ for (k=0;k=0) { ebnodbavg +=ebnodbest; if (memcmp(xdec,x,12*sizeof(int))==0) ndecok[rc]++; else { fprintf(stderr,"\nUndetected error with rc=%d\n",rc); nundet++; } } } printf(" %5.1f %%\r",100.0*k/ntx); printf("\n\n"); printf("Msgs transmitted:%d\nMsg decoded:\n\n",ntx); for (k=0;k<12;k++) { printf("rc=%2d %3d with %s\n",k,ndecok[k],decode_type[k]); ndec += ndecok[k]; } printf("\nTotal: %d/%d (%d undetected errors)\n\n",ndec,ntx,nundet); printf(""); if (ndec>0) { ebnodbavg/=(ndec+nundet); printf("Estimated SNR (average in dB) = %.2f dB\n\n",ebnodbavg-QRA64_SNR_EBNO_OFFSET); } return 0; } void syntax(void) { printf("\nQRA64 Mode Tests\n"); printf("2016, Nico Palermo - IV3NWV\n\n"); printf("---------------------------\n\n"); printf("Syntax: qra64 [-s] [-c] [-a] [-t] [-h]\n"); printf("Options: \n"); printf(" -s : set simulation SNR in 2500 Hz BW (default:-27.5 dB)\n"); printf(" -c : set channel type 0=AWGN (default) 1=Rayleigh 2=Fast-fading\n"); printf(" -a : set decode type 0=NOAP 1=AUTOAP (default) 2=USERAP\n"); printf(" -t: 0=simulate seq of msgs between IV3NWV and K1JT (default)\n"); printf(" 1=simulate K1JT receiving K1JT IV3NWV JN66\n"); printf(" 2=simulate fast-fading/awgn/rayliegh decoders performance\n"); printf(" -n : simulate the transmission of ntx codewords (default=100)\n"); printf("Options used only for fast-fading simulations (-c2):\n"); printf(" -b : 90%% fading bandwidth in Hz [1..230 Hz] (default = 2.5 Hz)\n"); printf(" -m : fading model. 0=Gauss, 1=Lorentz (default = Lorentz)\n"); printf(" -q : qra64 submode. 0=QRA64A,... 4=QRA64E (default = QRA64A)\n"); printf(" -d : use the old awgn decoder\n"); printf(" -h: this help\n"); printf("Example:\n"); printf(" qra64 -t2 -c2 -a2 -b50 -m1 -q2 -n10000 -s-26\n"); printf(" runs the error performance test (-t2)\n"); printf(" with USER_AP (-a2)\n"); printf(" simulating a fast fading channel (-c2)\n"); printf(" with B90 = 50 Hz (-b50), Lorentz Doppler (-m1), mode QRA64C (-q2)\n"); printf(" ntx = 10000 codewords (-n10000) and SNR = -26 dB (-s-26)\n"); } int main(int argc, char* argv[]) { int k, rc, nok=0; float SNRdB = -27.5f; unsigned int channel = CHANNEL_AWGN; unsigned int mode = QRA_AUTOAP; unsigned int testtype=0; int nqso = 100; float EbNodB; float B90 = 2.5; int fadingModel = 1; int submode = 0; int olddec = 0; int ntx = 100; // Parse the command line while(--argc) { argv++; if (strncmp(*argv,"-h",2)==0) { syntax(); return 0; } else if (strncmp(*argv,"-n",2)==0) { ntx = ( int)atoi((*argv)+2); if (ntx<100 || ntx>1000000) { printf("Invalid -n option. ntx must be in the range [100..1000000]\n"); syntax(); return -1; } } else if (strncmp(*argv,"-a",2)==0) { mode = ( int)atoi((*argv)+2); if (mode>2) { printf("Invalid decoding mode\n"); syntax(); return -1; } } else if (strncmp(*argv,"-s",2)==0) { SNRdB = (float)atof((*argv)+2); if (SNRdB>20 || SNRdB<-50) { printf("SNR should be in the range [-50..20]\n"); syntax(); return -1; } } else if (strncmp(*argv,"-t",2)==0) { testtype = ( int)atoi((*argv)+2); if (testtype>2) { printf("Invalid test type\n"); syntax(); return -1; } } else if (strncmp(*argv,"-c",2)==0) { channel = ( int)atoi((*argv)+2); if (channel>CHANNEL_FASTFADE) { printf("Invalid channel type\n"); syntax(); return -1; } } else if (strncmp(*argv,"-b",2)==0) { B90 = (float)atof((*argv)+2); if (B90<1 || B90>230) { printf("Invalid B90\n"); syntax(); return -1; } } else if (strncmp(*argv,"-m",2)==0) { fadingModel = (int)atoi((*argv)+2); if (fadingModel<0 || fadingModel>1) { printf("Invalid fading model\n"); syntax(); return -1; } } else if (strncmp(*argv,"-q",2)==0) { submode = (int)atoi((*argv)+2); if (submode<0 || submode>4) { printf("Invalid submode\n"); syntax(); return -1; } } else if (strncmp(*argv,"-d",2)==0) { olddec = 1; } else { printf("Invalid option\n"); syntax(); return -1; } } if (testtype<2) // old tests if (channel==CHANNEL_FASTFADE) { printf("Invalid Option. Test type 0 and 1 supports only AWGN or Rayleigh Channel model\n"); return -1; } EbNodB = SNRdB+QRA64_SNR_EBNO_OFFSET; #if defined(__linux__) || defined(__unix__) srand48(GetTickCount()); #endif if (testtype==0) { for (k=0;k