1 //
2 // gmskmodem_example.c
3 //
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <getopt.h>
8 #include <math.h>
9 #include "liquid.h"
10 
11 #define OUTPUT_FILENAME "gmskmodem_example.m"
12 
13 // print usage/help message
usage()14 void usage()
15 {
16     printf("gmskmodem_example -- Gaussian minimum-shift keying modem example\n");
17     printf("options (default values in <>):\n");
18     printf("  u/h   : print usage/help\n");
19     printf("  k     : samples/symbol, default: 4\n");
20     printf("  m     : filter delay [symbols], default: 3\n");
21     printf("  n     : number of data symbols, default: 200\n");
22     printf("  b     : bandwidth-time product, 0 <= b <= 1, default: 0.3\n");
23     printf("  s     : SNR [dB], default: 30\n");
24 }
25 
main(int argc,char * argv[])26 int main(int argc, char*argv[]) {
27     // options
28     unsigned int k=4;                   // filter samples/symbol
29     unsigned int m=3;                   // filter delay (symbols)
30     float BT=0.3f;                      // bandwidth-time product
31     unsigned int num_data_symbols=200;  // number of data symbols
32     float SNRdB = 30.0f;                // signal-to-noise ratio [dB]
33     float phi = 0.0f;                   // carrier phase offset
34     float dphi = 0.0f;                  // carrier frequency offset
35 
36     int dopt;
37     while ((dopt = getopt(argc,argv,"uhk:m:n:b:s:")) != EOF) {
38         switch (dopt) {
39         case 'u':
40         case 'h': usage();              return 0;
41         case 'k': k = atoi(optarg); break;
42         case 'm': m = atoi(optarg); break;
43         case 'n': num_data_symbols = atoi(optarg); break;
44         case 'b': BT = atof(optarg); break;
45         case 's': SNRdB = atof(optarg); break;
46         default:
47             exit(1);
48         }
49     }
50 
51     // validate input
52     if (BT <= 0.0f || BT >= 1.0f) {
53         fprintf(stderr,"error: %s, bandwidth-time product must be in (0,1)\n", argv[0]);
54         exit(1);
55     }
56 
57     // derived values
58     unsigned int num_symbols = num_data_symbols + 2*m;
59     unsigned int num_samples = k*num_symbols;
60     float nstd = powf(10.0f,-SNRdB/20.0f);  // noise standard deviation
61 
62     // create modulator
63     gmskmod mod   = gmskmod_create(k, m, BT);
64     gmskmod_print(mod);
65 
66     // create demodulator
67     gmskdem demod = gmskdem_create(k, m, BT);
68     gmskdem_set_eq_bw(demod, 0.01f);
69     gmskdem_print(demod);
70 
71     unsigned int i;
72     unsigned int s[num_symbols];
73     float complex x[num_samples];
74     float complex y[num_samples];
75     unsigned int sym_out[num_symbols];
76 
77     // generate random data sequence
78     for (i=0; i<num_symbols; i++)
79         s[i] = rand() % 2;
80 
81     // modulate signal
82     for (i=0; i<num_symbols; i++)
83         gmskmod_modulate(mod, s[i], &x[k*i]);
84 
85     // add channel impairments
86     for (i=0; i<num_samples; i++) {
87         y[i]  = x[i]*cexpf(_Complex_I*(phi + i*dphi));
88         y[i] += nstd*(randnf() + _Complex_I*randnf())*M_SQRT1_2;
89     }
90 
91     // demodulate signal
92     for (i=0; i<num_symbols; i++)
93         gmskdem_demodulate(demod, &y[k*i], &sym_out[i]);
94 
95     // destroy modem objects
96     gmskmod_destroy(mod);
97     gmskdem_destroy(demod);
98 
99     // print results to screen
100     unsigned int delay = 2*m;
101     unsigned int num_errors=0;
102     for (i=delay; i<num_symbols; i++) {
103         //printf("  %4u : %2u (%2u)\n", i, s[i-delay], sym_out[i]);
104         num_errors += (s[i-delay] == sym_out[i]) ? 0 : 1;
105     }
106     printf("symbol errors : %4u / %4u\n", num_errors, num_data_symbols);
107 
108     // write results to output file
109     FILE * fid = fopen(OUTPUT_FILENAME,"w");
110     fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
111     fprintf(fid,"clear all\n");
112     fprintf(fid,"close all\n");
113     fprintf(fid,"k = %u;\n", k);
114     fprintf(fid,"m = %u;\n", m);
115     fprintf(fid,"BT = %f;\n", BT);
116     fprintf(fid,"num_symbols = %u;\n", num_symbols);
117     fprintf(fid,"num_samples = %u;\n", num_samples);
118 
119     fprintf(fid,"x = zeros(1,num_samples);\n");
120     fprintf(fid,"y = zeros(1,num_samples);\n");
121     for (i=0; i<num_samples; i++) {
122         fprintf(fid,"x(%4u) = %12.8f + j*%12.8f;\n", i+1, crealf(x[i]), cimagf(x[i]));
123         fprintf(fid,"y(%4u) = %12.8f + j*%12.8f;\n", i+1, crealf(y[i]), cimagf(y[i]));
124     }
125     fprintf(fid,"t=[0:(num_samples-1)]/k;\n");
126     fprintf(fid,"figure;\n");
127     fprintf(fid,"plot(t,real(y),t,imag(y));\n");
128 
129     // artificially demodulate (generate receive filter, etc.)
130     float hr[2*k*m+1];
131     liquid_firdes_gmskrx(k,m,BT,0,hr);
132     for (i=0; i<2*k*m+1; i++)
133         fprintf(fid,"hr(%3u) = %12.8f;\n", i+1, hr[i]);
134     fprintf(fid,"z = filter(hr,1,arg( ([y(2:end) 0]).*conj(y) ))/k;\n");
135     fprintf(fid,"figure;\n");
136     fprintf(fid,"plot(t,z,t(k:k:end),z(k:k:end),'or');\n");
137     fprintf(fid,"grid on;\n");
138 
139     fclose(fid);
140     printf("results written to '%s'\n", OUTPUT_FILENAME);
141 
142     return 0;
143 }
144