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

Generated on Sat Jul 9 2011 15:21:31 for IT++ by Doxygen 1.7.4