00001 #include <itpp/itcomm.h> 00002 00003 using namespace itpp; 00004 00005 //These lines are needed for use of cout and endl 00006 using std::cout; 00007 using std::endl; 00008 00009 int main() 00010 { 00011 //Scalars: 00012 int SF, Ncode, sc, i, j, NumOfBits, MaxIterations, MaxNrOfErrors, MinNrOfErrors; 00013 double Eb; 00014 00015 //Vectors and matrixes: 00016 vec EbN0dB, EbN0, N0, ber; 00017 smat all_codes, spread_codesI, spread_codesQ; 00018 bvec transmited_bits, received_bits; 00019 cvec transmited_symbols, received_symbols, transmited_chips, received_chips; 00020 00021 //Classes: 00022 Multicode_Spread_2d mc_spread; 00023 AWGN_Channel channel; 00024 QPSK qpsk; 00025 BERC berc; 00026 00027 //Initialize the spreading: 00028 SF = 4; //The spreading factor is 4 00029 Ncode = 4; //Use all four codes in the multi-code spreader 00030 all_codes = to_smat(hadamard(SF)); //Calculate the spreading codes 00031 00032 //Set the spreading codes: 00033 spread_codesI.set_size(Ncode, SF, false); 00034 spread_codesQ.set_size(Ncode, SF, false); 00035 for (sc = 0; sc<Ncode; sc++) { 00036 spread_codesI.set_row(sc, all_codes.get_row(sc)); 00037 spread_codesQ.set_row(sc, all_codes.get_row(sc)); 00038 } 00039 mc_spread.set_codes(to_mat(spread_codesI), to_mat(spread_codesQ)); 00040 00041 //Specify simulation specific variables: 00042 EbN0dB = linspace(-2,14,17); 00043 EbN0 = pow(10,EbN0dB/10); 00044 Eb = 1.0; 00045 N0 = Eb * pow(EbN0,-1.0); 00046 NumOfBits = 50000; 00047 MaxIterations = 10; 00048 MaxNrOfErrors = 200; 00049 MinNrOfErrors = 5; 00050 ber.set_size(EbN0dB.size(), false); 00051 ber.clear(); 00052 00053 //Randomize the random number generators: 00054 RNG_randomize(); 00055 00056 for (i=0; i<EbN0dB.length(); i++) { 00057 00058 cout << endl << "Simulating point nr " << i+1 << endl; 00059 berc.clear(); 00060 channel.set_noise(N0(i)/2.0); 00061 00062 for (j=0; j<MaxIterations; j++) { 00063 00064 transmited_bits = randb(NumOfBits); 00065 transmited_symbols = qpsk.modulate_bits(transmited_bits); 00066 00067 //This is where we do the multi-code spreading: 00068 transmited_chips = mc_spread.spread(transmited_symbols); 00069 00070 received_chips = channel(transmited_chips); 00071 00072 //The multi-code despreading: 00073 //The second argument tells the despreader that the offset is zero chips. 00074 //This offset is usefull on channels with delay. 00075 received_symbols = mc_spread.despread(received_chips, 0); 00076 00077 received_bits = qpsk.demodulate_bits(received_symbols); 00078 00079 berc.count(transmited_bits,received_bits); 00080 ber(i) = berc.get_errorrate(); 00081 00082 cout << " Itteration " << j+1 << ": ber = " << berc.get_errorrate() << endl; 00083 if (berc.get_errors()>MaxNrOfErrors) { 00084 cout << "Breaking on point " << i+1 << " with " << berc.get_errors() << " errors." << endl; break; 00085 } 00086 00087 } 00088 00089 if (berc.get_errors() < MinNrOfErrors) { 00090 cout << "Exiting Simulation on point " << i+1 << endl; break; 00091 } 00092 00093 } 00094 00095 //Print results: 00096 cout << endl; 00097 cout << "EbN0dB = " << EbN0dB << endl; 00098 cout << "ber = " << ber << endl; 00099 00100 //Exit program: 00101 return 0; 00102 00103 } 00104
Generated on Sat Jun 6 13:48:51 2009 for RMOL by Doxygen 1.5.7.1