Copiste  0.1
 All Classes Functions Variables Enumerations Friends Pages
spectrum.h
1 /*
2  * This file is part of Copiste.
3  *
4  * Copiste is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Copiste is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Copiste. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 
19 #ifndef INCLUDED_SPECTRUMH
20 #define INCLUDED_SPECTRUMH
21 
22 #include "features/extractor.h"
23 
33 {
34  public:
36  // The algorithm automaticly adapts its data (output, butterfly, window)
37  // to the size of the data it gets.
38  // In order to be more efficient in the actual computing, we try to compute
39  // everything before.
40 
42  SpectrumExtr(int chunkSize = 0);
44  void reallocate();
46  ~SpectrumExtr();
47 
49  // It is the standard FFT algorithm : the recursive design has been translated into
50  // an iterative process. The output is normalized as the values can get quite high
51  // rates.
52 
54  bool extract(std::deque<uint16_t> data, int size);
56  void normalize(int bound);
57 
58  private:
60  // The windows are intended to minimize the lack of precision of the actual computation
61  // of the Fourier Transform. That's a well-known phenomon : when you try to compute
62  // the spectrum of a perfect sinus wave on a finite window, you don't get a pefect dirac
63  // as expected, but a large wave with some noise.
64 
66  static double rectangularWin(int i, int maxSize);
68  static double triangularWin(int i, int maxSize);
70  static double hammingWin(int i, int maxSize);
72  static double blackmanHarrisWin(int i, int maxSize);
73 
75  void createWindowCache();
76 
77  public:
78  enum FTWindow
79  { WINDOW_RECTANGULAR,
80  WINDOW_TRIANGULAR,
81  WINDOW_HAMMING,
82  WINDOW_BH };
83  typedef enum FTWindow FTWindow;
84 
86  // These functions can be used to retrive the result of the computation.
87 
89  float value(int index);
91  uint16_t* spectrum();
93  int size();
95  float min() { return 0; }
97  float max() { return (uint16_t)(-1); }
98 
100  void setFloat(string key, float value) { ; }
102  void setInt(string key, int value);
104  float getFloat(string key) { return 0; }
106  int getInt(string key);
108  void setWindow(FTWindow win);
109 
110  private:
112  // The size of the data sent to the algorithm must be a power of 2.
113 
115  static int regularSize(int size);
117  static int log2(int x);
119  static int pow2(int n);
120 
121  public:
123  void watch();
124 
125  private:
126  uint16_t* mResults;
127  int* mButterfly;
128  double* mWindowCache;
129  FTWindow mCurrentWindow;
130  int mSize;
131  int mBound;
132 };
133 
134 #endif