libwt  1.0.0
A C++ library for the continous wavelet transform.
wavelettransform.hh
1 #ifndef __WAVELETTRANSFORM_HH__
2 #define __WAVELETTRANSFORM_HH__
3 
4 #include "waveletanalysis.hh"
5 #include "convolution.hh"
6 #include "movingaverage.hh"
7 #include <vector>
8 
9 
10 namespace wt {
11 
15 {
16 public:
18  WaveletTransform(const Wavelet &wavelet, const RVector &scales, bool subSample=false);
20  WaveletTransform(const Wavelet &wavelet, double *scales, int Nscales, bool subSample=false);
22  WaveletTransform(const WaveletAnalysis &other, bool subSample=false);
24  virtual ~WaveletTransform();
25 
30  template <class iDerived, class oDerived>
31  void operator() (const Eigen::DenseBase<iDerived> &signal, Eigen::DenseBase<oDerived> &out)
32  {
33  // signal length
34  int N = signal.size();
35  // Get start indices for each transformation block
36  std::vector<size_t> blockIdxs(_filterBank.size());
37  for (size_t j=0; j<_filterBank.size(); j++) {
38  if (0 == j) {
39  blockIdxs[0] = 0;
40  } else {
41  blockIdxs[j] = blockIdxs[j-1] + _filterBank[j-1]->numKernels();
42  }
43  }
44 
45  // Iterate over all convolution filters grouping wavelets with the same size
47  for (size_t j=0; j<_filterBank.size(); j++)
48  {
49  // Get convolution filters
50  Convolution *filters = _filterBank[j];
51  // Get subsampling
52  int M = filters->subSampling();
53  // Get start column in output matrix
54  size_t outCol = blockIdxs[j];
55  // Get number of kernels in group / # columns in out
56  int K = filters->numKernels();
57 
58  if (1 == M) {
59  // w/o sub-sampling -> direct overlap-add convolution
60  filters->apply(signal, out.block(0, outCol, N, K).derived());
61  // continue with next block
62  continue;
63  }
64 
65  /*
66  * Perform convolution with sub-sampling
67  */
68  // Number of samples in the sub-sampled signal
69  int n = WT_IDIV_CEIL(N,M);
70  // allocate some working arrays
71  CVector subsig(n); // <- Will hold the sub-sampled signal
72  CMatrix subres(n, K); // <- Will hold the sub-sampled result
73  // For every shift within the M-fold sub-sampling:
74  for (int m=0; m<M; m++) {
75  // subsample input signal
76  for (int i=0; i<n; i++) {
77  subsig[i] = ((i*M+m) < N) ? CScalar(signal[i*M+m]) : 0;
78  }
79  // Apply overlap-add convolution
80  filters->apply(subsig, subres.derived());
81  // Store results into output buffer (interleaved rows)
82  for (int i=0; i<n; i++) {
83  if ( (i*M+m) < N ) {
84  out.block(i*M+m, outCol, 1, K) = subres.row(i);
85  }
86  }
87  }
88  }
89  }
90 
91 protected:
93  void init_trafo();
94 
95 protected:
97  bool _subSample;
99  std::vector<Convolution *> _filterBank;
100 };
101 
102 }
103 
104 #endif // __WAVELETTRANSFORM_HH__
Abstract base class of all wavelet analyses (transform and synthesis).
Definition: waveletanalysis.hh:16
const Wavelet & wavelet() const
Returns the wavelet instance of this transform.
Definition: waveletanalysis.hh:41
const RVector & scales() const
Returns the scales of the wavelet transform.
Definition: waveletanalysis.hh:33
void operator()(const Eigen::DenseBase< iDerived > &signal, Eigen::DenseBase< oDerived > &out)
Performs the wavelet transform on the given signal and stores the result into the given out matrix...
Definition: wavelettransform.hh:31
void init_trafo()
Actually initializes the transformation.
Definition: wavelettransform.cc:28
WaveletTransform(const Wavelet &wavelet, const RVector &scales, bool subSample=false)
Constructs a wavelet transform from the given wavelet at the specified scales.
Definition: wavelettransform.cc:9
Base class of all wavelet object containers.
Definition: wavelet.hh:40
Implements a complex, continious wavelet transform (i.e.
Definition: wavelettransform.hh:14
size_t numKernels() const
Returns the number of kernels.
Definition: convolution.hh:125
Definition: convolution.hh:7
Implements the overlap-add covolution of a signal with several filter kernels of the same size...
Definition: convolution.hh:19
std::vector< Convolution * > _filterBank
The list of convolution filters applied for the wavelet transform.
Definition: wavelettransform.hh:99
size_t subSampling() const
Returns the sub-sampling assinged to the convolution operation.
Definition: convolution.hh:128
virtual ~WaveletTransform()
Destructor.
Definition: wavelettransform.cc:83
void apply(const Eigen::DenseBase< iDerived > &signal, Eigen::DenseBase< oDerived > &out)
Performs the convolution of the signal passed by signal with the kernels passed to the constructor...
Definition: convolution.hh:32
bool _subSample
If true, the sub-sampling of the input signal is allowed.
Definition: wavelettransform.hh:97