root/vtcross/trunk/src/cognitive_radio_test.c @ 27

Revision 12, 2.4 KB (checked in by jgaeddert, 16 years ago)

initial import of cactus files

Line 
1//
2// Cognitive radio simulator
3//
4
5#include "cognitive_radio_test.h"
6
7// transmitter thread
8void execute_tx(
9        metadata _md,
10        char *_msg_in,
11        unsigned int _msg_len,
12        float * _I,
13        float *_Q,
14        unsigned int *_sym_len)
15{
16    // symbols buffer
17    char sym[_msg_len];
18
19    // pack bits into symbols
20    repack_bytes(
21        _msg_in,        // input buffer
22        1,              // input symbol size (number bits)
23        _msg_len,       // input buffer size
24        sym,            // output buffer
25        _md->mod_bps,   // output symbol size (number of bits)
26        _msg_len,       // output buffer size
27        _sym_len        // number of symbols written to output
28        );
29
30    // run modulator
31    unsigned int i, s;
32    modem * mod = modem_create(_md->mod_scheme, _md->mod_bps);
33    for (i=0; i<(*_sym_len); i++) {
34        s = (unsigned int)(sym[i]);
35        modulate(mod, s, &_I[i], &_Q[i]);
36    }
37}
38
39// channel thread
40void execute_channel(metadata _md, float _std, float * _I, float *_Q, unsigned int _len)
41{
42    // add noise
43    float noise_i, noise_q;
44    unsigned int i;
45    for (i=0; i<_len; i++) {
46        // generate random noise values
47        randnf(&noise_i, &noise_q);
48
49        // TODO scale by appropriate amount
50        _I[i] += noise_i * _std;
51        _Q[i] += noise_q * _std;
52    }
53}
54
55// receiver thread
56void execute_rx(
57        metadata _md,
58        float * _I,
59        float *_Q,
60        unsigned int _sym_len,
61        char *_msg_out,
62        unsigned int *_msg_len)
63{
64    char msg[_sym_len];
65
66    // create demodulator and demodulate
67    modem * demod = modem_create(_md->mod_scheme, _md->mod_bps);
68    unsigned int i, s;
69    for (i=0; i<_sym_len; i++) {
70        demodulate(demod, _I[i], _Q[i], &s);
71        msg[i] = (char) s;
72    }
73
74    // unpack bytes
75    repack_bytes(
76        msg,            // input buffer
77        _md->mod_bps,   // input symbol size (number of bits)
78        _sym_len,       // num input symbols
79        _msg_out,       // output buffer
80        1,              // output symbol size (number of bits)
81        999999,         // output buffer size
82        _msg_len        // number of output symbols written
83        );
84
85}
86
87unsigned int count_errors(char * _msg_in, char * _msg_out, unsigned int _num_bits)
88{
89    unsigned int i, num_errors=0;
90    for (i=0; i<_num_bits; i++)
91        num_errors += (_msg_in[i]==_msg_out[i]) ? 0 : 1;
92
93    return num_errors;
94}
Note: See TracBrowser for help on using the browser.