IT++ Logo

resampling.h

Go to the documentation of this file.
00001 
00030 #ifndef RESAMPLING_H
00031 #define RESAMPLING_H
00032 
00033 #include <itpp/base/mat.h>
00034 
00035 
00036 namespace itpp {
00037 
00043 
00044   template<class T>
00045   Vec<T> repeat(const Vec<T> &v, int norepeats)
00046   {
00047     Vec<T> temp(v.length()*norepeats);
00048 
00049     for(int i=0; i<v.length(); i++) {
00050       for(int j=0;j<norepeats;j++)
00051   temp(i*norepeats+j)=v(i);
00052     }
00053     return temp;
00054   }
00055 
00057   template<class T>
00058   Mat<T> repeat(const Mat<T> &m, int norepeats)
00059   {
00060     Mat<T> temp(m.rows(), m.cols()*norepeats);
00061 
00062     for (int j=0; j<m.cols(); j++) {
00063       for (int i=0;i<norepeats;i++) {
00064   temp.set_col(j*norepeats+i, m.get_col(j));
00065       }
00066     }
00067     return temp;
00068   }
00069 
00071   template<class T>
00072   void upsample(const Vec<T> &v, int usf, Vec<T> &u)
00073   {
00074     it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one" );
00075     u.set_size(v.length()*usf);
00076     u.clear();
00077     for(int i=0;i<v.length();i++)
00078       u(i*usf)=v(i);
00079   }
00080 
00081 
00083   template<class T>
00084   Vec<T> upsample(const Vec<T> &v, int usf)
00085   {
00086     Vec<T> u;
00087     upsample(v,usf,u);
00088     return u;
00089   }
00090 
00092   template<class T>
00093   void upsample(const Mat<T> &v, int usf, Mat<T> &u)
00094   {
00095     it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one" );
00096     u.set_size(v.rows(),v.cols()*usf);
00097     u.clear();
00098     for (int j=0;j<v.cols();j++)
00099       u.set_col(j*usf,v.get_col(j));
00100   }
00101 
00103   template<class T>
00104   Mat<T> upsample(const Mat<T> &v, int usf)
00105   {
00106     Mat<T> u;
00107     upsample(v,usf,u);
00108     return u;
00109   }
00110 
00112   template<class T>
00113   void lininterp(const Mat<T> &m, int usf, Mat<T> &u)
00114   {
00115     it_assert_debug(usf >= 1, "lininterp: upsampling factor must be equal or greater than one" );
00116     int L = (m.cols()-1)*usf+1;
00117     u.set_size(m.rows(),L);
00118     for (int i = 0; i < m.rows(); i++){
00119       for (int j = 0; j < L-1; j++)
00120   u(i,j) = (m(i,j/usf) + (j % usf)/((double)usf)*(m(i,(j+usf)/usf)-m(i,j/usf)));
00121       u(i,L-1) = m(i,m.cols()-1);
00122     }
00123   }
00124 
00135   template<class T>
00136   Mat<T> lininterp(const Mat<T> &m, double f_base, double f_ups,
00137        int nrof_samples, double t_start = 0)
00138   {
00139     double t_base = 1 / f_base;
00140     double t_ups = 1 / f_ups;
00141     int rows = m.rows();
00142     int cols = m.cols();
00143     it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency" );
00144     it_assert_debug((t_start >= 0) && (t_start < cols * t_base), "lininterp(): incorrect start time offset");
00145     it_assert_debug((nrof_samples * t_ups + t_start) <= (cols * t_base), "lininterp(): too many samples required or input data to short");
00146     Mat<T> u(rows, nrof_samples);
00147     double curr_time = t_start;
00148 
00149     int i = 0;
00150     int k = 0;
00151     while (i < cols - 1) {
00152       while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
00153   for (int j = 0; j < rows; j++) {
00154     u(j, k) = (m(j, i) * ((i + 1) * t_base - curr_time)
00155          - m(j, i + 1) * (i * t_base - curr_time)) / t_base;
00156   }
00157   k++;
00158   curr_time += t_ups;
00159       }
00160       i++;
00161     }
00162     return u;
00163   }
00164 
00166   template<class T>
00167   Mat<T> lininterp(const Mat<T> &m, int usf)
00168   {
00169     Mat<T> u;
00170     lininterp(m,usf,u);
00171     return u;
00172   }
00173 
00175   template<class T>
00176   void lininterp(const Vec<T> &v, int usf, Vec<T> &u)
00177   {
00178     it_assert_debug(usf >= 1, "lininterp(): upsampling factor must be equal or greater than one" );
00179     int L = (v.length()-1)*usf+1;
00180     u.set_size(L);
00181     for (int j = 0; j < L-1; j++) {
00182       u(j) = (v(j/usf) + (j % usf)/((double)usf)*(v((j+usf)/usf)-v(j/usf)));
00183     }
00184     u(L-1) = v(v.length()-1);
00185   }
00186 
00188   template<class T>
00189   Vec<T> lininterp(const Vec<T> &v, int usf)
00190   {
00191     Vec<T> u;
00192     lininterp(v,usf,u);
00193     return u;
00194   }
00195 
00206   template<class T>
00207   Vec<T> lininterp(const Vec<T> &v, double f_base, double f_ups,
00208        int nrof_samples, double t_start = 0)
00209   {
00210     double t_base = 1 / f_base;
00211     double t_ups = 1 / f_ups;
00212     int len = v.length();
00213     it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency" );
00214     it_assert_debug((t_start >= 0) && (t_start < len * t_base), "lininterp(): incorrect start time offset");
00215     it_assert_debug((nrof_samples * t_ups + t_start) <= (len * t_base), "lininterp(): too many samples required or input data to short");
00216     Vec<T> u(nrof_samples);
00217     double curr_time = t_start;
00218 
00219     int i = 0;
00220     int k = 0;
00221     while (i < len - 1) {
00222       while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
00223   u(k) = (v(i) * ((i + 1) * t_base - curr_time)
00224     - v(i + 1) * (i * t_base - curr_time)) / t_base;
00225   k++;
00226   curr_time += t_ups;
00227       }
00228       i++;
00229     }
00230     return u;
00231   }
00232 
00237 #ifndef _MSC_VER
00238 
00239   // ----------------------------------------------------------------------
00240   // Instantiations
00241   // ----------------------------------------------------------------------
00242 
00244   extern template vec repeat(const vec &v, int norepeats);
00246   extern template cvec repeat(const cvec &v, int norepeats);
00248   extern template svec repeat(const svec &v, int norepeats);
00250   extern template ivec repeat(const ivec &v, int norepeats);
00252   extern template bvec repeat(const bvec &v, int norepeats);
00253 
00255   extern template mat repeat(const mat &m, int norepeats);
00257   extern template cmat repeat(const cmat &m, int norepeats);
00259   extern template smat repeat(const smat &m, int norepeats);
00261   extern template imat repeat(const imat &m, int norepeats);
00263   extern template bmat repeat(const bmat &m, int norepeats);
00264 
00266   extern template vec upsample(const vec &v, int usf);
00268   extern template cvec upsample(const cvec &v, int usf);
00270   extern template svec upsample(const svec &v, int usf);
00272   extern template ivec upsample(const ivec &v, int usf);
00274   extern template bvec upsample(const bvec &v, int usf);
00275 
00277   extern template mat upsample(const mat &v, int usf);
00279   extern template cmat upsample(const cmat &v, int usf);
00281   extern template smat upsample(const smat &v, int usf);
00283   extern template imat upsample(const imat &v, int usf);
00285   extern template bmat upsample(const bmat &v, int usf);
00286 
00288   extern template void upsample(const vec &v, int usf,  vec &u);
00290   extern template void upsample(const cvec &v, int usf,  cvec &u);
00292   extern template void upsample(const svec &v, int usf,  svec &u);
00294   extern template void upsample(const ivec &v, int usf,  ivec &u);
00296   extern template void upsample(const bvec &v, int usf,  bvec &u);
00297 
00299   extern template void upsample(const mat &v, int usf,  mat &u);
00301   extern template void upsample(const cmat &v, int usf,  cmat &u);
00303   extern template void upsample(const smat &v, int usf,  smat &u);
00305   extern template void upsample(const imat &v, int usf,  imat &u);
00307   extern template void upsample(const bmat &v, int usf,  bmat &u);
00308 
00310   extern template vec lininterp(const vec &v, int usf);
00312   extern template cvec lininterp(const cvec &v, int usf);
00313 
00315   extern template mat lininterp(const mat &v, int usf);
00317   extern template cmat lininterp(const cmat &v, int usf);
00318 
00320   extern template void lininterp(const vec &v, int usf,  vec &u);
00322   extern template void lininterp(const cvec &v, int usf,  cvec &u);
00323 
00325   extern template void lininterp(const mat &v, int usf,  mat &u);
00327   extern template void lininterp(const cmat &v, int usf,  cmat &u);
00328 
00330   extern template mat lininterp(const mat &m, double f_base, double f_ups, int nrof_samples, double t_start);
00332   extern template cmat lininterp(const cmat &m, double f_base, double f_ups, int nrof_samples, double t_start);
00333 
00335   extern template vec lininterp(const vec &v, double f_base, double f_ups, int nrof_samples, double t_start);
00337   extern template cvec lininterp(const cvec &v, double f_base, double f_ups, int nrof_samples, double t_start);
00338 
00339 #endif
00340 
00341 } // namespace itpp
00342 
00343 #endif // #ifndef RESAMPLING_H
00344 
SourceForge Logo

Generated on Sun Sep 14 18:57:06 2008 for IT++ by Doxygen 1.5.6