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

Generated on Sun Dec 9 17:30:26 2007 for IT++ by Doxygen 1.5.4