GNU Radio's CCSDS Package
comm.h
Go to the documentation of this file.
1 #ifndef COMM
2 #define COMM
3 #include <vector>
4 #include <algorithm>
5 #include <cstdlib>
6 
7 std::vector<unsigned char> data_gen(unsigned int nBits)
8 {
9  std::vector<unsigned char> bits;
10  int temp;
11  for(unsigned int i = 0; i < nBits;i++){
12  temp = (((rand()%2) - 0.5)*2.0);
13  bits.push_back( temp > 0.0 ? 0x01 : 0x00);
14  //std::cout << temp;
15  }
16  return bits;
17 }
18 
19 std::vector<double> randn(double mean, double variance, unsigned int length)
20 {
21  /*{{{*/
22  //double* random = malloc(length * sizeof *random);
23  std::vector<double> awgn;
24 
25  for (unsigned int i = 0; i < length; i++)
26  {
27  double U1 = ((double)rand()/(double)RAND_MAX);
28  double U2 = ((double)rand()/(double)RAND_MAX);
29 
30  double R = sqrt(-2*variance*log(U1));
31  double theta = 2*M_PI*U2;
32 
33  awgn.push_back( mean + R*cos(theta));
34  }
35 
36  return awgn;/*}}}*/
37 }
38 
39 
40 std::vector<double> bpsk(std::vector<unsigned char> data)
41 {
42  std::vector<double> signal;
43  for(unsigned int i = 0; i < data.size();i++)
44  signal.push_back((data[i] > 0) ? 1.0000 : -1.0000);
45  return signal;
46 }
47 
48 std::vector<double> awgn_channel(std::vector<double> signal, double snr, double code_rate, double &sigma)
49 {
50  std::vector<double> noisy_signal;
51  std::vector<double> awgn = randn(0,1,signal.size());
52  double N0 = pow(10.0, -snr / 10.0) / code_rate;
53  sigma = sqrt(N0 / 2.0);
54  for(unsigned int i = 0; i < signal.size();i++)
55  {
56  noisy_signal.push_back(signal[i] + sigma*awgn[i]);
57  }
58  return noisy_signal;
59 }
60 
61 int ber(std::vector<unsigned char> tx,std::vector<unsigned char> rx )
62 {
63  assert(tx.size() == rx.size());
64  int numErrors = 0;
65  for(unsigned int i = 0; i<tx.size();i++)
66  {
67  if (tx[i] == rx[i])
68  numErrors++;
69  }
70  return numErrors;
71 }
72 
73 std::vector<unsigned char> sliceLogAPP(std::vector<double> logapp)
74 {
75  std::vector<unsigned char> bits;
76  for(unsigned int i = 0; i < logapp.size(); i++)
77  bits.push_back(logapp[i] > 0.0 ? 0x00 : 0x01);
78  return bits;
79 }
80 
81 
82 #endif // COMM
83 
std::vector< unsigned char > data_gen(unsigned int nBits)
Definition: comm.h:7
int ber(std::vector< unsigned char > tx, std::vector< unsigned char > rx)
Definition: comm.h:61
std::vector< double > bpsk(std::vector< unsigned char > data)
Definition: comm.h:40
std::vector< double > awgn_channel(std::vector< double > signal, double snr, double code_rate, double &sigma)
Definition: comm.h:48
double mean(double *input, unsigned int length)
std::vector< unsigned char > sliceLogAPP(std::vector< double > logapp)
Definition: comm.h:73
std::vector< double > randn(double mean, double variance, unsigned int length)
Definition: comm.h:19