1 //
2 // flexframesync_reconfig_example.c
3 //
4 // Demonstrates the reconfigurability of the flexframegen and
5 // flexframesync objects.
6 //
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <math.h>
12 #include <time.h>
13 #include <getopt.h>
14 
15 #include "liquid.h"
16 
17 #define OUTPUT_FILENAME  "flexframesync_reconfig_example.m"
18 
usage()19 void usage()
20 {
21     printf("flexframesync_example [options]\n");
22     printf("  u/h   : print usage\n");
23     printf("  s     : signal-to-noise ratio [dB], default: 30\n");
24     printf("  n     : number of frames, default: 3\n");
25 }
26 
main(int argc,char * argv[])27 int main(int argc, char *argv[]) {
28     srand( time(NULL) );
29 
30     // define parameters
31     float SNRdB = 30.0f;
32     float noise_floor = -30.0f;
33     unsigned int num_frames = 3;
34 
35     // get options
36     int dopt;
37     while((dopt = getopt(argc,argv,"uhvqs:f:m:p:n:")) != EOF){
38         switch (dopt) {
39         case 'u':
40         case 'h': usage();                      return 0;
41         case 's': SNRdB = atof(optarg);         break;
42         case 'n': num_frames = atoi(optarg);    break;
43         default:
44             exit(1);
45         }
46     }
47 
48     // create flexframegen object
49     flexframegenprops_s fgprops;
50     flexframegenprops_init_default(&fgprops);
51     flexframegen fg = flexframegen_create(NULL);
52 
53     // frame data
54     unsigned char   header[14];
55     unsigned char * payload = NULL;
56 
57     // create flexframesync object with default properties
58     flexframesync fs = flexframesync_create(NULL,NULL);
59 
60     // channel
61     float nstd  = powf(10.0f, noise_floor/20.0f);         // noise std. dev.
62     float gamma = powf(10.0f, (SNRdB+noise_floor)/20.0f); // channel gain
63 
64     unsigned int i;
65     // initialize header, payload
66     for (i=0; i<14; i++)
67         header[i] = i;
68 
69     // frame buffers, properties
70     unsigned int  buf_len = 256;
71     float complex buf[buf_len];
72 
73     unsigned int j;
74     for (j=0; j<num_frames; j++) {
75         // configure frame generator properties
76         unsigned int payload_len = (rand() % 256) + 1;   // random payload length
77         fgprops.check            = LIQUID_CRC_NONE;      // data validity check
78         fgprops.fec0             = LIQUID_FEC_NONE;      // inner FEC scheme
79         fgprops.fec1             = LIQUID_FEC_NONE;      // outer FEC scheme
80         fgprops.mod_scheme       = (rand() % 2) ? LIQUID_MODEM_QPSK : LIQUID_MODEM_QAM16;
81 
82         // reallocate memory for payload
83         payload = realloc(payload, payload_len*sizeof(unsigned char));
84 
85         // initialize payload
86         for (i=0; i<payload_len; i++)
87             payload[i] = rand() & 0xff;
88 
89         // set properties and assemble the frame
90         flexframegen_setprops(fg, &fgprops);
91         flexframegen_assemble(fg, header, payload, payload_len);
92         printf("frame %u, ", j);
93         flexframegen_print(fg);
94 
95         // write the frame in blocks
96         int frame_complete = 0;
97         while (!frame_complete) {
98             // write samples to buffer
99             frame_complete = flexframegen_write_samples(fg, buf, buf_len);
100 
101             // add channel impairments (gain and noise)
102             for (i=0; i<buf_len; i++)
103                 buf[i] = buf[i]*gamma + nstd * (randnf() + _Complex_I*randnf()) * M_SQRT1_2;
104 
105             // push through sync
106             flexframesync_execute(fs, buf, buf_len);
107         }
108 
109     } // num frames
110 
111     // print frame data statistics
112     flexframesync_print(fs);
113 
114     // clean up allocated memory
115     flexframegen_destroy(fg);
116     flexframesync_destroy(fs);
117     free(payload);
118 
119     printf("done.\n");
120     return 0;
121 }
122 
123