IT++ Logo

commfunc.cpp

Go to the documentation of this file.
00001 
00030 #include <itpp/comm/commfunc.h>
00031 #include <itpp/base/converters.h>
00032 #include <itpp/base/specmat.h>
00033 #include <itpp/base/matfunc.h>
00034 #include <itpp/base/binary.h>
00035 #include <itpp/base/sort.h>
00036 
00037 namespace itpp
00038 {
00039 
00040 bmat graycode(int m)
00041 {
00042   if (m == 1) {
00043     smat temp = "0;1";
00044     return to_bmat(temp);
00045   }
00046   else {
00047     bvec temp(1 << (m - 1));
00048     bmat bb = graycode(m - 1);
00049     bmat out(1 << m, m);
00050     out.zeros();
00051     out.set_col(0, concat(zeros_b(1 << (m - 1)), ones_b(1 << (m - 1))));
00052     for (int i = 0; i < m - 1; i++) {
00053       temp = bb.get_col(i);
00054       out.set_col(i + 1, concat(temp, reverse(temp)));
00055     }
00056     return out;
00057   }
00058 }
00059 
00060 int hamming_distance(const bvec &a, const bvec &b)
00061 {
00062   int i, n = 0;
00063 
00064   it_assert_debug(a.size() == b.size(), "hamming_distance()");
00065   for (i = 0; i < a.size(); i++)
00066     if (a(i) != b(i))
00067       n++;
00068 
00069   return n;
00070 }
00071 
00072 int weight(const bvec &a)
00073 {
00074   int i, n = 0;
00075 
00076   for (i = 0; i < a.size(); i++)
00077     if (a(i) == bin(1))
00078       n++;
00079 
00080   return n;
00081 }
00082 
00083 vec waterfilling(const vec &alpha, double P) // added by EGL April 2007
00084 {
00085   int n = length(alpha);
00086   it_assert(n > 0, "waterfilling(): alpha vector cannot have zero length");
00087   it_assert(P > 0, "waterfilling(): Power constraint must be positive");
00088 
00089   ivec ind = sort_index(alpha); // indices in increasing order
00090   it_assert(alpha(ind(0)) > 0, "waterfilling(): Gains must be positive");
00091 
00092   // find lambda
00093   double lambda = 0.0;
00094   for (int m = 0; m < n; m++) {
00095     // try m,...,n-1 nonzero allocation
00096     double t = 0;
00097     for (int j = m; j < n; j++) {
00098       t += 1.0 / alpha(ind(j));
00099     }
00100     t = (t + P) / (n - m);
00101     lambda = 1.0 / t;
00102     if (lambda < alpha(ind(m)))
00103       break;
00104   }
00105 
00106   vec result(n);
00107   for (int j = 0; j < n; j++) {
00108     result(j) = ((lambda < alpha(j)) ? (1.0 / lambda - 1.0 / alpha(j)) : 0.0);
00109   }
00110 
00111   return result;
00112 }
00113 
00114 } // namespace itpp
SourceForge Logo

Generated on Sun Jul 26 08:36:50 2009 for IT++ by Doxygen 1.5.9