resampling.h
Go to the documentation of this file.
#ifndef RESAMPLING_H
#define RESAMPLING_H
#include <itpp/base/mat.h>
#include <itpp/itexports.h>
namespace itpp
{
template<class T>
Vec<T> repeat(const Vec<T> &v, int norepeats)
{
Vec<T> temp(v.length()*norepeats);
for (int i = 0; i < v.length(); i++) {
for (int j = 0;j < norepeats;j++)
temp(i*norepeats + j) = v(i);
}
return temp;
}
template<class T>
Mat<T> repeat(const Mat<T> &m, int norepeats)
{
Mat<T> temp(m.rows(), m.cols()*norepeats);
for (int j = 0; j < m.cols(); j++) {
for (int i = 0;i < norepeats;i++) {
temp.set_col(j*norepeats + i, m.get_col(j));
}
}
return temp;
}
template<class T>
void upsample(const Vec<T> &v, int usf, Vec<T> &u)
{
it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
u.set_size(v.length()*usf);
u.clear();
for (int i = 0;i < v.length();i++)
u(i*usf) = v(i);
}
template<class T>
Vec<T> upsample(const Vec<T> &v, int usf)
{
Vec<T> u;
upsample(v, usf, u);
return u;
}
template<class T>
void upsample(const Mat<T> &v, int usf, Mat<T> &u)
{
it_assert_debug(usf >= 1, "upsample: upsampling factor must be equal or greater than one");
u.set_size(v.rows(), v.cols()*usf);
u.clear();
for (int j = 0;j < v.cols();j++)
u.set_col(j*usf, v.get_col(j));
}
template<class T>
Mat<T> upsample(const Mat<T> &v, int usf)
{
Mat<T> u;
upsample(v, usf, u);
return u;
}
template<class T>
void lininterp(const Mat<T> &m, int usf, Mat<T> &u)
{
it_assert_debug(usf >= 1, "lininterp: upsampling factor must be equal or greater than one");
int L = (m.cols() - 1) * usf + 1;
u.set_size(m.rows(), L);
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < L - 1; j++)
u(i, j) = (m(i, j / usf) + (j % usf) / ((double)usf) * (m(i, (j + usf) / usf) - m(i, j / usf)));
u(i, L - 1) = m(i, m.cols() - 1);
}
}
template<class T>
Mat<T> lininterp(const Mat<T> &m, double f_base, double f_ups,
int nrof_samples, double t_start = 0)
{
double t_base = 1 / f_base;
double t_ups = 1 / f_ups;
int rows = m.rows();
int cols = m.cols();
it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
it_assert_debug((t_start >= 0) && (t_start < cols * t_base), "lininterp(): incorrect start time offset");
it_assert_debug((nrof_samples * t_ups + t_start) <= (cols * t_base), "lininterp(): too many samples required or input data to short");
Mat<T> u(rows, nrof_samples);
double curr_time = t_start;
int i = 0;
int k = 0;
while (i < cols - 1) {
while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
for (int j = 0; j < rows; j++) {
u(j, k) = (m(j, i) * ((i + 1) * t_base - curr_time)
- m(j, i + 1) * (i * t_base - curr_time)) / t_base;
}
k++;
curr_time += t_ups;
}
i++;
}
return u;
}
template<class T>
Mat<T> lininterp(const Mat<T> &m, int usf)
{
Mat<T> u;
lininterp(m, usf, u);
return u;
}
template<class T>
void lininterp(const Vec<T> &v, int usf, Vec<T> &u)
{
it_assert_debug(usf >= 1, "lininterp(): upsampling factor must be equal or greater than one");
int L = (v.length() - 1) * usf + 1;
u.set_size(L);
for (int j = 0; j < L - 1; j++) {
u(j) = (v(j / usf) + (j % usf) / ((double)usf) * (v((j + usf) / usf) - v(j / usf)));
}
u(L - 1) = v(v.length() - 1);
}
template<class T>
Vec<T> lininterp(const Vec<T> &v, int usf)
{
Vec<T> u;
lininterp(v, usf, u);
return u;
}
template<class T>
Vec<T> lininterp(const Vec<T> &v, double f_base, double f_ups,
int nrof_samples, double t_start = 0)
{
double t_base = 1 / f_base;
double t_ups = 1 / f_ups;
int len = v.length();
it_assert_debug(f_ups > f_base, "lininterp(): upsampled frequency must be greater than base frequency");
it_assert_debug((t_start >= 0) && (t_start < len * t_base), "lininterp(): incorrect start time offset");
it_assert_debug((nrof_samples * t_ups + t_start) <= (len * t_base), "lininterp(): too many samples required or input data to short");
Vec<T> u(nrof_samples);
double curr_time = t_start;
int i = 0;
int k = 0;
while (i < len - 1) {
while ((curr_time < (i + 1) * t_base) && (k < nrof_samples)) {
u(k) = (v(i) * ((i + 1) * t_base - curr_time)
- v(i + 1) * (i * t_base - curr_time)) / t_base;
k++;
curr_time += t_ups;
}
i++;
}
return u;
}
// ----------------------------------------------------------------------
// Instantiations
// ----------------------------------------------------------------------
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec repeat(const vec &v, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec repeat(const cvec &v, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec repeat(const svec &v, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec repeat(const ivec &v, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec repeat(const bvec &v, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat repeat(const mat &m, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat repeat(const cmat &m, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat repeat(const smat &m, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat repeat(const imat &m, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat repeat(const bmat &m, int norepeats);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec upsample(const vec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec upsample(const cvec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT svec upsample(const svec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT ivec upsample(const ivec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bvec upsample(const bvec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat upsample(const mat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat upsample(const cmat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT smat upsample(const smat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT imat upsample(const imat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT bmat upsample(const bmat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const vec &v, int usf, vec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const cvec &v, int usf, cvec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const svec &v, int usf, svec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const ivec &v, int usf, ivec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const bvec &v, int usf, bvec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const mat &v, int usf, mat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const cmat &v, int usf, cmat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const smat &v, int usf, smat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const imat &v, int usf, imat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void upsample(const bmat &v, int usf, bmat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec lininterp(const vec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec lininterp(const cvec &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat lininterp(const mat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat lininterp(const cmat &v, int usf);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const vec &v, int usf, vec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const cvec &v, int usf, cvec &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const mat &v, int usf, mat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT void lininterp(const cmat &v, int usf, cmat &u);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT mat lininterp(const mat &m, double f_base, double f_ups, int nrof_samples, double t_start);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cmat lininterp(const cmat &m, double f_base, double f_ups, int nrof_samples, double t_start);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT vec lininterp(const vec &v, double f_base, double f_ups, int nrof_samples, double t_start);
ITPP_EXPORT_TEMPLATE template ITPP_EXPORT cvec lininterp(const cvec &v, double f_base, double f_ups, int nrof_samples, double t_start);
} // namespace itpp
#endif // #ifndef RESAMPLING_H
itpp::Mat::set_size
void set_size(int rows, int cols, bool copy=false)
Set size of matrix. If copy = true then keep the data before resizing.
Definition: mat.h:647
itpp::Mat::cols
int cols() const
The number of columns.
Definition: mat.h:235
itpp::Mat::rows
int rows() const
The number of rows.
Definition: mat.h:237
itpp::Mat< T >
itpp::Vec::set_size
void set_size(int size, bool copy=false)
Set length of vector. if copy = true then keeping the old values.
Definition: vec.h:663
it_assert_debug
#define it_assert_debug(t, s)
Abort if t is not true and NDEBUG is not defined.
Definition: itassert.h:107
itpp::Mat::set_col
void set_col(int c, const Vec< Num_T > &v)
Set column c to vector v.
Definition: mat.h:936
itpp::Mat::clear
void clear()
Set matrix equal to the all zero matrix.
Definition: mat.h:245
mat.h
Matrix Class Definitions.
itpp::Vec< T >
itpp::Vec::clear
void clear()
Set the vector to the all zero vector.
Definition: vec.h:280
itpp::Mat::get_col
Vec< Num_T > get_col(int c) const
Get column c.
Definition: mat.h:889
itpp::Vec::length
int length() const
The size of the vector.
Definition: vec.h:269
bmat
Mat< bin > bmat
bin matrix
Definition: mat.h:508

Generated on Thu Mar 20 2014 12:26:43 for IT++ by Doxygen 1.8.6