Wavelet - A library for online estimation of the Continuous Wavelet Transform  0.1-alpha
filterbank.hpp
1 /*
2  * filterbank.h
3  *
4  * Wavelet Filter Bank
5  *
6  * Contact:
7  * - Jules Françoise <jules.francoise@ircam.fr>
8  *
9  * This code has been authored by <a href="http://julesfrancoise.com">Jules Françoise</a>
10  * in the framework of the <a href="http://skatvg.iuav.it/">SkAT-VG</a> European project,
11  * with <a href="frederic-bevilacqua.net">Frederic Bevilacqua</a>, in the
12  * <a href="http://ismm.ircam.fr">Sound Music Movement Interaction</a> team of the
13  * <a href="http://www.ircam.fr/stms.html?&L=1">STMS Lab</a> - IRCAM - CNRS - UPMC (2011-2015).
14  *
15  * Copyright (C) 2015 Ircam-Centre Pompidou.
16  *
17  * This File is part of Wavelet.
18  *
19  * Wavelet is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * Wavelet is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with Wavelet. If not, see <http://www.gnu.org/licenses/>.
31  */
32 
33 #ifndef filterbank_h
34 #define filterbank_h
35 
36 #include "wavelet.hpp"
37 #include "lowpass.hpp"
38 #include "../wavelets/morlet.hpp"
39 #include "../wavelets/paul.hpp"
40 #include <map>
41 #include <boost/circular_buffer.hpp>
42 #ifdef USE_ARMA
43 #include <armadillo>
44 #endif
45 
46 namespace wavelet {
50  enum Family : unsigned char {
55  MORLET = 0,
56 
61  PAUL = 1
62  };
63 
67  const Family DEFAULT_FAMILY = MORLET;
68 
77  class Filterbank : public AttributeHandler {
78  public:
82  enum Optimisation : unsigned char {
86  NONE = 0,
87 
91  STANDARD = 1,
92 
97  };
98 
99 #pragma mark -
100 #pragma mark === Public Interface ===
101 #pragma mark > Constructors
102 
103 
112  Filterbank(float samplerate,
113  float frequency_min,
114  float frequency_max,
115  float bands_per_octave);
116 
121  Filterbank(Filterbank const& src);
122 
127  Filterbank& operator=(Filterbank const& src);
128 
132  virtual ~Filterbank();
133 
135 
136 #pragma mark > Accessors
137 
138 
168  template <typename T>
169  void setAttribute(std::string attr_name,
170  T attr_value)
171  {
172  try {
173  setAttribute_internal(attr_name, boost::any(attr_value));
174  } catch(const boost::bad_any_cast &) {
175  throw std::runtime_error("Argument value type does not match Attribute type");
176  }
177  }
178 
207  template <typename T>
208  T getAttribute(std::string attr_name) const
209  {
210  try {
211  return boost::any_cast<T>(getAttribute_internal(attr_name));
212  } catch(const boost::bad_any_cast &) {
213  throw std::runtime_error("Return value type does not match Attribute type");
214  }
215  }
216 
218 
219 #pragma mark > Online Estimation
220 
221 
226  void update(float value);
227 
231  void reset();
232 
234 
235 #pragma mark > Offline Estimation
236 #ifdef USE_ARMA
237 
238 
246  arma::cx_mat process(std::vector<double> values);
247 
254  arma::cx_mat process_online(std::vector<double> values);
255 
257 #endif
258 
259 #pragma mark > Utilities
260 
261 
267  std::size_t size() const;
268 
273  std::string info() const;
274 
279  std::vector<int> delaysInSamples() const;
280 
281 #ifdef SWIGPYTHON
282 
287  std::string __str__() {
288  return info();
289  }
290 #endif
291 
293 
294 #pragma mark -
295 #pragma mark === Public Attributes ===
296 
300 
305 
310 
315 
320 
324  std::vector<double> scales;
325 
329  std::vector<double> frequencies;
330 
334  std::vector<int> downsampling_factors;
335 
339  std::vector< std::complex<double> > result_complex;
340 
344  std::vector<double> result_power;
345 
347 #ifndef WAVELET_TESTING
348  protected:
349 #endif
350 #pragma mark -
351 #pragma mark === Protected Methods ===
352 
353 
358  void init();
359 
364  virtual void onAttributeChange(AttributeBase* attr_pointer);
365 
373  virtual void setAttribute_internal(std::string attr_name,
374  boost::any const& attr_value);
375 
383  virtual boost::any getAttribute_internal(std::string attr_name) const;
384 
386 
387 #pragma mark -
388 #pragma mark === Protected Attributes ===
389 
392  std::map<int, boost::circular_buffer<float>> data_;
393 
397  std::map<int, LowpassFilter> filters_;
398 
402  std::vector< std::shared_ptr<Wavelet> > wavelets_;
403 
407  std::shared_ptr<Wavelet> reference_wavelet_;
408 
412  int frame_index_;
413 
415  };
416 
417 #pragma mark -
418 #pragma mark === Attribute Specializations ===
419  template <>
421  void checkLimits<Family>(Family const& value,
422  Family const& limit_min,
423  Family const& limit_max);
424 
425  template <>
426  void checkLimits<Filterbank::Optimisation>(Filterbank::Optimisation const& value,
427  Filterbank::Optimisation const& limit_min,
428  Filterbank::Optimisation const& limit_max);
429 
430  template <>
432 
433  template <>
436 }
437 
438 #endif
Filterbank(float samplerate, float frequency_min, float frequency_max, float bands_per_octave)
Constructor.
Minimal-delay Wavelet Filterbank.
Definition: filterbank.hpp:77
std::string __str__()
"print" method for python => returns the results of write method
Definition: filterbank.hpp:287
std::vector< double > scales
Scales of each band in the filterbank.
Definition: filterbank.hpp:324
std::vector< int > delaysInSamples() const
get the delays in sample for each filter
Attribute< float > bands_per_octave
Number of bands per octave of the Filterbank.
Definition: filterbank.hpp:309
Filterbank & operator=(Filterbank const &src)
Assignment operator.
std::vector< double > frequencies
Equivalent Fourier frequencies of each band in the filterbank.
Definition: filterbank.hpp:329
Standard Optimisation (Wavelet Downsampling with frame-based calculations)
Definition: filterbank.hpp:91
std::string info() const
get info on the current configuration
No optimisation (no wavelet downsampling)
Definition: filterbank.hpp:86
Attribute< float > frequency_min
Minimum Frequency of the Filterbank (Hz)
Definition: filterbank.hpp:299
std::vector< int > downsampling_factors
Downsampling factor for each band.
Definition: filterbank.hpp:334
Definition: attribute.hpp:42
Attribute< float > frequency_max
Maximum Frequency of the Filterbank (Hz)
Definition: filterbank.hpp:304
void update(float value)
update the filter with an incoming value
std::size_t size() const
get number of bands
std::vector< std::complex< double > > result_complex
Results of the filtering process (scalogram slice)
Definition: filterbank.hpp:339
virtual ~Filterbank()
Destructor.
Attribute< Family > family
Wavelet Family.
Definition: filterbank.hpp:319
Agressive Optimisation (Wavelet Downsampling with Signal Downsampling)
Definition: filterbank.hpp:96
Attribute< Optimisation > optimisation
Optimisation mode the filterbank implementation.
Definition: filterbank.hpp:314
T getAttribute(std::string attr_name) const
get attribute value by name
Definition: filterbank.hpp:208
void setAttribute(std::string attr_name, T attr_value)
set attribute value by name
Definition: filterbank.hpp:169
std::vector< double > result_power
Resulting Power of the filtering process (power scalogram slice)
Definition: filterbank.hpp:344
void reset()
clear the current data buffer
Optimisation
Optimisation level of the filterbank implementation.
Definition: filterbank.hpp:82