1 //
2 // complementary_codes_example.c
3 //
4 // This example demonstrates how to generate complementary binary
5 // codes in liquid. A pair of codes is generated using the bsequence
6 // interface, their auto-correlations are computed, and the result is
7 // summed and printed to the screen. The results are also printed to an
8 // output file, which plots the sequences and their auto-correlations.
9 //
10 // SEE ALSO: bsequence_example.c
11 // msequence_example.c
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <getopt.h>
16 #include "liquid.h"
17
18 #define OUTPUT_FILENAME "complementary_codes_example.m"
19
20 // print usage/help message
usage()21 void usage()
22 {
23 printf("complementary_codes_example [options]\n");
24 printf(" u/h : print usage\n");
25 printf(" n : sequence length, 8,16,32,64,... (default: 32)\n");
26 }
27
28
main(int argc,char * argv[])29 int main(int argc, char*argv[])
30 {
31 // options
32 unsigned int n = 32;
33
34 int dopt;
35 while ((dopt = getopt(argc,argv,"uhn:")) != EOF) {
36 switch (dopt) {
37 case 'u':
38 case 'h': usage(); return 0;
39 case 'n': n = atoi(optarg); break;
40 default:
41 exit(1);
42 }
43 }
44
45 // validate input
46 if ( (1<<liquid_nextpow2(n)) != n ) {
47 fprintf(stderr,"error: %s, sequence length must be a power of 2\n", argv[0]);
48 exit(1);
49 }
50
51 // create and initialize codes
52 bsequence a = bsequence_create(n);
53 bsequence b = bsequence_create(n);
54 bsequence_create_ccodes(a, b);
55
56 // print
57 bsequence_print(a);
58 bsequence_print(b);
59
60 // generate test sequences
61 bsequence ax = bsequence_create(n);
62 bsequence bx = bsequence_create(n);
63 bsequence_create_ccodes(ax, bx);
64
65 // compute auto-correlations
66 unsigned int i;
67 int raa[n];
68 int rbb[n];
69 for (i=0; i<n; i++) {
70 raa[i] = 2*bsequence_correlate(a,ax) - n;
71 rbb[i] = 2*bsequence_correlate(b,bx) - n;
72
73 bsequence_circshift(ax);
74 bsequence_circshift(bx);
75
76 // print to screen
77 printf(" raa[%4u] = %4d, rbb[%4u] = %4d, sum = %4d\n",
78 i, raa[i], i, rbb[i], raa[i] + rbb[i]);
79 }
80
81 // print results to file
82 FILE * fid = fopen(OUTPUT_FILENAME,"w");
83 if (!fid) {
84 fprintf(stderr,"error: %s, cannot open output file '%s' for writing\n", argv[0], OUTPUT_FILENAME);
85 exit(1);
86 }
87
88 fprintf(fid,"%% %s : auto-generated file\n", OUTPUT_FILENAME);
89 fprintf(fid,"clear all;\n");
90 fprintf(fid,"close all;\n\n");
91 fprintf(fid,"n = %u;\n", n);
92 fprintf(fid,"a = zeros(1,n);\n");
93 fprintf(fid,"b = zeros(1,n);\n");
94 fprintf(fid,"raa = zeros(1,n);\n");
95 fprintf(fid,"rbb = zeros(1,n);\n");
96
97 for (i=0; i<n; i++) {
98 fprintf(fid,"a(%6u) = %1u;\n", i+1, bsequence_index(a,n-i-1));
99 fprintf(fid,"b(%6u) = %1u;\n", i+1, bsequence_index(b,n-i-1));
100
101 fprintf(fid,"raa(%6u) = %12.8f;\n", i+1, raa[i] / (float)n);
102 fprintf(fid,"rbb(%6u) = %12.8f;\n", i+1, rbb[i] / (float)n);
103 }
104
105 // plot results
106 fprintf(fid,"figure;\n");
107 fprintf(fid,"t = 0:(n-1);\n");
108 fprintf(fid,"subplot(3,1,1);\n");
109 fprintf(fid," stairs(t,a);\n");
110 fprintf(fid," axis([-1 n -0.2 1.2]);\n");
111 //fprintf(fid," xlabel('delay (samples)');\n");
112 fprintf(fid," ylabel('a');\n");
113 fprintf(fid,"subplot(3,1,2);\n");
114 fprintf(fid," stairs(t,b);\n");
115 fprintf(fid," axis([-1 n -0.2 1.2]);\n");
116 //fprintf(fid," xlabel('delay (samples)');\n");
117 fprintf(fid," ylabel('b');\n");
118 fprintf(fid,"subplot(3,1,3);\n");
119 fprintf(fid," plot(t,raa,t,rbb,t,[raa+rbb]/2);\n");
120 fprintf(fid," axis([-1 n -0.5 1.2]);\n");
121 fprintf(fid," xlabel('delay (samples)');\n");
122 fprintf(fid," ylabel('auto-correlation');\n");
123
124 fclose(fid);
125 printf("results written to %s.\n", OUTPUT_FILENAME);
126
127 // clean up memory
128 bsequence_destroy(a);
129 bsequence_destroy(b);
130 bsequence_destroy(ax);
131 bsequence_destroy(bx);
132
133 return 0;
134 }
135
136