1 | // |
---|
2 | // Cognitive radio simulator |
---|
3 | // |
---|
4 | |
---|
5 | #include <stdio.h> |
---|
6 | #include <stdlib.h> |
---|
7 | #include <math.h> |
---|
8 | #include <sigprocc/sigprocc.h> |
---|
9 | #include "cbr.h" |
---|
10 | #include "cognitive_radio_test.h" |
---|
11 | |
---|
12 | // options |
---|
13 | #define PACKET_SIZE 1024 // packet size (number of bits) |
---|
14 | #define NUM_TRIALS 250 // number of epochs |
---|
15 | #define REPORT_TRIALS 10 // print report every so often |
---|
16 | |
---|
17 | int main() { |
---|
18 | // bookkeeping variables and buffers |
---|
19 | unsigned int num_bits=PACKET_SIZE; |
---|
20 | char msg_in[num_bits], msg_out[num_bits]; |
---|
21 | float I[num_bits], Q[num_bits]; |
---|
22 | unsigned int num_symbols, num_bits_out; |
---|
23 | |
---|
24 | float noise_psd = -100.0f; // noise power spectral density |
---|
25 | float noise_power, noise_std; |
---|
26 | |
---|
27 | // quality of service metrics |
---|
28 | unsigned int num_bit_errors; |
---|
29 | float throughput_bps; |
---|
30 | |
---|
31 | // metadata defaults |
---|
32 | struct metadata_t mdt; |
---|
33 | metadata md = &mdt; |
---|
34 | |
---|
35 | md->mod_scheme = MOD_QAM; // MOD_PSK, MOD_QAM, MOD_PAM, MOD_DPSK |
---|
36 | md->mod_bps = 4; // modulation scheme [bits/s] |
---|
37 | md->tx_power_dBW = -3.0f; // transmit power [dBW] |
---|
38 | md->symbol_rate = 250e3f; // symbol rate [symbols/s] |
---|
39 | |
---|
40 | // case-based reasoner |
---|
41 | unsigned int num_cols = 7; |
---|
42 | char * cols[] = { |
---|
43 | "BER", "throughput", |
---|
44 | "mod_scheme", "tx_power", |
---|
45 | "noise_power", "path_loss", |
---|
46 | "utility" |
---|
47 | }; |
---|
48 | float vals[num_cols]; |
---|
49 | vals[0] = 1e-3f; // BER |
---|
50 | vals[1] = 10e3f; // throughput |
---|
51 | vals[2] = 1; // mod_scheme |
---|
52 | vals[3] = -3.0f; // tx_power |
---|
53 | vals[4] = -50.0f; // noise_power |
---|
54 | vals[5] = 125.0f; // path_loss |
---|
55 | vals[6] = 0.762; // utility |
---|
56 | int rc; |
---|
57 | |
---|
58 | // create cbr database/table |
---|
59 | cbr mycbr = cbr_create("ex1", "data", cols, num_cols); |
---|
60 | |
---|
61 | // add row here |
---|
62 | //rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
63 | |
---|
64 | // print |
---|
65 | cbr_print(mycbr); |
---|
66 | |
---|
67 | // cbr search setup |
---|
68 | char * search_names[] = {"BER"}; |
---|
69 | int search_ops[] = {LT}; |
---|
70 | float search_vals[] = {30.0f/(float)PACKET_SIZE}; |
---|
71 | float retvals[num_cols]; |
---|
72 | |
---|
73 | |
---|
74 | unsigned int i; |
---|
75 | // generate random message |
---|
76 | for (i=0; i<num_bits; i++) |
---|
77 | msg_in[i] = (char) (rand() % 2); |
---|
78 | |
---|
79 | printf("epoch\tutility\n"); |
---|
80 | printf("-----\t-------\n"); |
---|
81 | for (i=0; i<NUM_TRIALS; i++) { |
---|
82 | // compute internals |
---|
83 | noise_power = noise_psd + 20*log10f(md->symbol_rate); |
---|
84 | noise_std = powf(10.0f, (noise_power - md->tx_power_dBW) / 10.0f) / 2.0f; |
---|
85 | |
---|
86 | // execute transmitter |
---|
87 | execute_tx(md, msg_in, num_bits, I, Q, &num_symbols); |
---|
88 | |
---|
89 | // execute channel |
---|
90 | execute_channel(md, noise_std, I, Q, num_symbols); |
---|
91 | |
---|
92 | // execute receiver |
---|
93 | execute_rx(md, I, Q, num_symbols, msg_out, &num_bits_out); |
---|
94 | |
---|
95 | // |
---|
96 | // *** compute quality of service metrics *** |
---|
97 | // |
---|
98 | |
---|
99 | // count errors, etc. |
---|
100 | num_bit_errors = count_errors(msg_in, msg_out, num_bits); |
---|
101 | throughput_bps = ((float)(md->mod_bps)) * md->symbol_rate; |
---|
102 | |
---|
103 | if ( ((i+1)%REPORT_TRIALS)==0 || (i+1)==NUM_TRIALS) { |
---|
104 | // print results |
---|
105 | printf("%u\t%.2f\terrors:\t%u\t/ %u, power: %.1f dBW, Fs: %.3f kHz\n", |
---|
106 | i+1, 0.0f, num_bit_errors, num_bits, md->tx_power_dBW, md->symbol_rate/1e3 ); |
---|
107 | } |
---|
108 | |
---|
109 | // |
---|
110 | // *** execute cognition here *** |
---|
111 | // |
---|
112 | |
---|
113 | // store result |
---|
114 | vals[0] = (float) num_bit_errors / PACKET_SIZE; // BER |
---|
115 | vals[1] = throughput_bps; |
---|
116 | //vals[2] = // mod_scheme |
---|
117 | vals[3] = md->tx_power_dBW; // tx_power |
---|
118 | vals[6] = md->tx_power_dBW;// utility |
---|
119 | |
---|
120 | rc = cbr_add_row(mycbr, cols, vals, num_cols); |
---|
121 | |
---|
122 | // execute cbr search |
---|
123 | rc = cbr_search(mycbr, search_names, search_ops, search_vals, 1, retvals); |
---|
124 | |
---|
125 | if (rc==0) { |
---|
126 | // no solution found |
---|
127 | md->tx_power_dBW += 0.2f; |
---|
128 | //printf("no solution found\n"); |
---|
129 | } else { |
---|
130 | // set values |
---|
131 | md->tx_power_dBW = retvals[3]; |
---|
132 | } |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | cbr_free(mycbr); |
---|
137 | |
---|
138 | printf("done.\n"); |
---|
139 | return 0; |
---|
140 | } |
---|