fun_ofdm  1.0
802.11a Physical Layer for USRP
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Macros Pages
test_sim.cpp
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <boost/date_time/posix_time/posix_time.hpp>
10 #include <boost/date_time/gregorian/gregorian.hpp>
11 #include <boost/program_options.hpp>
12 #include "usrp.h"
13 #include "frame_builder.h"
14 #include "receiver_chain.h"
15 
16 using namespace fun;
17 
18 void test_sim();
19 
20 double freq = 5.26e9;
21 double sample_rate = 5e6;
22 double tx_gain = 30;
23 double rx_gain = 30;
24 double amp = 0.5;
25 //Rate phy_rate = RATE_1_2_BPSK;
26 //Rate phy_rate = RATE_1_2_QAM16;
27 //Rate phy_rate = RATE_2_3_QAM64;
29 
30 int main(int argc, char * argv[]){
31 
32  std::cout << "Running Simulation..." << std::endl;
33  test_sim();
34 
35  return 0;
36 }
37 
38 
43 void test_sim()
44 {
45 
46  frame_builder * fb = new frame_builder();
48 
49  std::string data("I'm a little tea pot, short and stout.....here is my handle.....blah blah blah.....this rhyme sucks!");
50  int repeat = 15;
51 
52  std::vector<unsigned char> payload(data.length()*repeat); //Payload = 1500 bytes
53  for(int x = 0; x < repeat; x++) memcpy(&payload[x*data.length()], &data[0], data.length());
54 
55  // Build a frame
56  std::vector<std::complex<double>> samples = fb->build_frame(payload, phy_rate);
57 
58  int pad_length = samples.size()*1000;
59 
60  // Concatenate num_frames frames together
61  int num_frames = 100;
62  std::cout << "Transmitting " << num_frames << " frames" << std::endl;
63  std::vector<std::complex<double>> samples_con(samples.size() * num_frames + pad_length);
64  for(int x = 0; x < num_frames; x++)
65  {
66  memcpy(&samples_con[x*samples.size()], &samples[0], samples.size() * sizeof(std::complex<double>));
67  }
68 
69  //Pad the end with 0's to flush receive chain
70  std::vector<std::complex<double> > zeros(pad_length);
71  memcpy(&samples_con[num_frames*samples.size()], &zeros[0], zeros.size()*sizeof(std::complex<double>));
72 
73  boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
74 
75  // Run the samples through the receiver chain
76 
77  int chunk_size = 4096;
78 
79  int count = 0;
80  for(int x = 0; x < samples_con.size(); x += chunk_size)
81  {
82  int start = x;
83  int end = x + chunk_size;
84  if(end > samples_con.size()) end = samples_con.size();
85  std::vector<std::complex<double> > chunk(&samples_con[start], &samples_con[end]);
86 
87  std::vector<std::vector<unsigned char> > rec_frames = receiver->process_samples(chunk);
88  count += rec_frames.size();
89 
90  if(rec_frames.size()){
91  for(int i = 0; i < rec_frames.size(); i++){
92  for(int j = 0; j < rec_frames[i].size(); j++)
93  std::cout << rec_frames[i][j];
94  std::cout << std::endl << std::endl;
95  }
96  }
97  }
98 
99  boost::posix_time::time_duration elapsed = boost::posix_time::microsec_clock::local_time() - start;
100 
101  printf("Received %i packets\n", count);
102 
103  printf("Time elapsed: %f\n", elapsed.total_microseconds() / 1000.0);
104 }