// // Cognitive radio simulator // #include "cognitive_radio_test.h" // transmitter thread void execute_tx( metadata _md, char *_msg_in, unsigned int _msg_len, float * _I, float *_Q, unsigned int *_sym_len) { // symbols buffer char sym[_msg_len]; // pack bits into symbols repack_bytes( _msg_in, // input buffer 1, // input symbol size (number bits) _msg_len, // input buffer size sym, // output buffer _md->mod_bps, // output symbol size (number of bits) _msg_len, // output buffer size _sym_len // number of symbols written to output ); // run modulator unsigned int i, s; modem * mod = modem_create(_md->mod_scheme, _md->mod_bps); for (i=0; i<(*_sym_len); i++) { s = (unsigned int)(sym[i]); modulate(mod, s, &_I[i], &_Q[i]); } } // channel thread void execute_channel(metadata _md, float _std, float * _I, float *_Q, unsigned int _len) { // add noise float noise_i, noise_q; unsigned int i; for (i=0; i<_len; i++) { // generate random noise values randnf(&noise_i, &noise_q); // TODO scale by appropriate amount _I[i] += noise_i * _std; _Q[i] += noise_q * _std; } } // receiver thread void execute_rx( metadata _md, float * _I, float *_Q, unsigned int _sym_len, char *_msg_out, unsigned int *_msg_len) { char msg[_sym_len]; // create demodulator and demodulate modem * demod = modem_create(_md->mod_scheme, _md->mod_bps); unsigned int i, s; for (i=0; i<_sym_len; i++) { demodulate(demod, _I[i], _Q[i], &s); msg[i] = (char) s; } // unpack bytes repack_bytes( msg, // input buffer _md->mod_bps, // input symbol size (number of bits) _sym_len, // num input symbols _msg_out, // output buffer 1, // output symbol size (number of bits) 999999, // output buffer size _msg_len // number of output symbols written ); } unsigned int count_errors(char * _msg_in, char * _msg_out, unsigned int _num_bits) { unsigned int i, num_errors=0; for (i=0; i<_num_bits; i++) num_errors += (_msg_in[i]==_msg_out[i]) ? 0 : 1; return num_errors; }