1 //
2 // modem_example.c
3 //
4 // This example demonstates the digital modulator/demodulator
5 // (modem) object.  Data symbols are modulated into complex
6 // samples which are then demodulated without noise or phase
7 // offsets.  The user may select the modulation scheme via
8 // the command-line interface.
9 // SEE ALSO: modem_arb_example.c
10 //
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include "liquid.h"
16 
17 #define OUTPUT_FILENAME "modem_example.m"
18 
19 // print usage/help message
usage()20 void usage()
21 {
22     printf("modem_example [options]\n");
23     printf("  h     : print help\n");
24     printf("  v/q   : verbose/quiet\n");
25     printf("  m     : modulation scheme (qam16 default)\n");
26     liquid_print_modulation_schemes();
27 }
28 
29 
main(int argc,char * argv[])30 int main(int argc, char*argv[])
31 {
32     // create mod/demod objects
33     modulation_scheme ms = LIQUID_MODEM_QAM16;
34     int verbose = 1;
35 
36     int dopt;
37     while ((dopt = getopt(argc,argv,"hvqm:")) != EOF) {
38         switch (dopt) {
39         case 'h':   usage();        return 0;
40         case 'v':   verbose = 1;    break;
41         case 'q':   verbose = 0;    break;
42         case 'm':
43             ms = liquid_getopt_str2mod(optarg);
44             if (ms == LIQUID_MODEM_UNKNOWN) {
45                 fprintf(stderr,"error: %s, unknown/unsupported modulation scheme '%s'\n", argv[0], optarg);
46                 return 1;
47             }
48             break;
49         default:
50             exit(1);
51         }
52     }
53 
54     // create the modem objects
55     modem mod   = modem_create(ms);
56     modem demod = modem_create(ms);
57 
58     // ensure bits/symbol matches modem description (only
59     // applicable to certain specific modems)
60     unsigned int bps = modem_get_bps(mod);
61 
62     modem_print(mod);
63 
64     // open output file
65     FILE*fid = fopen(OUTPUT_FILENAME,"w");
66     fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
67     fprintf(fid,"clear all;\n");
68     fprintf(fid,"close all;\n\n");
69     fprintf(fid,"m = %u;\n", bps);
70     fprintf(fid,"M = %u;\n", 1<<bps);
71     fprintf(fid,"c = zeros(1,M);\n");
72     fprintf(fid,"i_str = cell(1,M);\n");
73 
74     unsigned int i; // modulated symbol
75     unsigned int s; // demodulated symbol
76     unsigned int num_symbols = 1<<bps;
77     float complex x;
78     unsigned int num_sym_errors = 0;
79     unsigned int num_bit_errors = 0;
80 
81     for (i=0; i<num_symbols; i++) {
82         modem_modulate(mod, i, &x);
83         modem_demodulate(demod, x, &s);
84 
85         if (verbose)
86             printf("%4u : %12.8f + j*%12.8f\n", i, crealf(x), cimagf(x));
87 
88         num_sym_errors += i == s ? 0 : 1;
89         num_bit_errors += count_bit_errors(i,s);
90 
91         // write symbol to output file
92         fprintf(fid,"c(%3u) = %12.4e + j*%12.4e;\n", i+1, crealf(x), cimagf(x));
93         fprintf(fid,"i_str{%3u} = [num2str(%3u)];\n", i+1, i);
94     }
95     printf("num sym errors: %4u / %4u\n", num_sym_errors, num_symbols);
96     printf("num bit errors: %4u / %4u\n", num_bit_errors, num_symbols*bps);
97 
98     // plot results
99     fprintf(fid,"\n\n");
100     fprintf(fid,"figure;\n");
101     fprintf(fid,"plot(c,'o','MarkerSize',2);\n");
102     fprintf(fid,"hold on;\n");
103     fprintf(fid,"text(real(c)+0.02, imag(c)+0.02, i_str);\n");
104     fprintf(fid,"hold off;\n");
105     fprintf(fid,"axis([-1 1 -1 1]*1.6);\n");
106     fprintf(fid,"axis square;\n");
107     fprintf(fid,"grid on;\n");
108     fprintf(fid,"xlabel('in phase');\n");
109     fprintf(fid,"ylabel('quadrature phase');\n");
110 
111     fclose(fid);
112     printf("results written to %s.\n", OUTPUT_FILENAME);
113 
114     modem_destroy(mod);
115     modem_destroy(demod);
116     return 0;
117 }
118