arduino-audio-tools
FFTWindows.h
Go to the documentation of this file.
1 
11 #pragma once
12 
13 #include <math.h>
14 
15 namespace audio_tools {
16 
24  public:
25  WindowFunction() = default;
26 
28  virtual void begin(int samples) {
29  this->samples_minus_1 = -1.0f + samples;
30  this->i_samples = samples;
31  this->i_half_samples = samples / 2 - 1;
32  }
33 
35  inline float factor(int idx) {
36  float result = idx < i_half_samples ? factor_internal(idx) : factor_internal(i_samples-idx-1);
37  return result>1.0f ? 1.0f : result;
38  }
39 
41  inline int samples() { return i_samples; }
42 
43  protected:
44  float samples_minus_1 = 0.0f;
45  int i_samples = 0;
46  int i_half_samples = 0;
47  const float twoPi = 6.28318531f;
48  const float fourPi = 12.56637061f;
49  const float sixPi = 18.84955593f;
50 
51  // virtual function provide implementation in subclass
52  virtual float factor_internal(int idx) = 0;
53 
54  // the ratio idx / samples -1
55  inline float ratio(int idx) {
56  return (static_cast<float>(idx)) / samples_minus_1;
57  }
58 
59 };
60 
68  public:
69  BufferedWindow(WindowFunction* wf) { p_wf = wf; }
70  BufferedWindow(BufferedWindow const&) = delete;
71  BufferedWindow& operator=(BufferedWindow const&) = delete;
72 
73  virtual void begin(int samples) override {
74  // process only if there is a change
76  if (p_wf->samples() != samples) {
77  p_wf->begin(samples);
78  len = samples / 2;
79  buffer.resize(len);
80  for (int j = 0; j < len; j++) {
81  buffer[j] = p_wf->factor(j);
82  }
83  }
84  }
85 
86  protected:
87  WindowFunction* p_wf = nullptr;
88  Vector<float> buffer{0};
89  int len;
90 
91  float factor_internal(int idx) override {
92  return idx < len ? buffer[idx] : buffer[i_samples - idx];
93  }
94 };
95 
101 class Rectange : public WindowFunction {
102  public:
103  Rectange() = default;
104  float factor_internal(int idx) { return 1.0f; }
105 };
106 
112 class Hamming : public WindowFunction {
113  public:
114  Hamming() = default;
115  float factor_internal(int idx) {
116  return 0.54f - (0.46f * cos(twoPi * ratio(idx)));
117  }
118 };
119 
125 class Hann : public WindowFunction {
126  public:
127  Hann() = default;
128  float factor_internal(int idx) {
129  return 0.54f * (1.0f - cos(twoPi * ratio(idx)));
130  }
131 };
132 
138 class Triangle : public WindowFunction {
139  public:
140  Triangle() = default;
141  float factor_internal(int idx) {
142  return 1.0f - ((2.0f * fabs((idx - 1) -
143  (static_cast<float>(i_samples - 1) / 2.0f))) /
144  samples_minus_1);
145  }
146 };
147 
154 class Nuttall : public WindowFunction {
155  public:
156  Nuttall() = default;
157  float factor_internal(int idx) override {
158  float r = ratio(idx);
159  return 0.355768f - (0.487396f * (cos(twoPi * r))) +
160  (0.144232f * (cos(fourPi * r))) - (0.012604f * (cos(sixPi * r)));
161  }
162 };
163 
170 class Blackman : public WindowFunction {
171  public:
172  Blackman() = default;
173  float factor_internal(int idx)override {
174  float r = ratio(idx);
175  return 0.42323f - (0.49755f * (cos(twoPi * r))) +
176  (0.07922f * (cos(fourPi * r)));
177  }
178 };
179 
186  public:
187  BlackmanNuttall() = default;
188  float factor_internal(int idx) override{
189  float r = ratio(idx);
190  return 0.3635819f - (0.4891775f * (cos(twoPi * r))) +
191  (0.1365995f * (cos(fourPi * r))) - (0.0106411f * (cos(sixPi * r)));
192  }
193 };
194 
201  public:
202  BlackmanHarris() = default;
203  float factor_internal(int idx) override{
204  float r = ratio(idx);
205  return 0.35875f - (0.48829f * (cos(twoPi * r))) +
206  (0.14128f * (cos(fourPi * r))) - (0.01168f * (cos(sixPi * r)));
207  }
208 };
209 
215 class FlatTop : public WindowFunction {
216  public:
217  FlatTop() = default;
218  float factor_internal(int idx) override{
219  float r = ratio(idx);
220  return 0.2810639f - (0.5208972f * cos(twoPi * r)) +
221  (0.1980399f * cos(fourPi * r));
222  }
223 };
224 
230 class Welch : public WindowFunction {
231  public:
232  Welch() = default;
233  float factor_internal(int idx) override{
234  float tmp = (((idx - 1) - samples_minus_1 / 2.0f) / (samples_minus_1 / 2.0f));
235  return 1.0f - (tmp*tmp);
236  }
237 };
238 
239 } // namespace audio_tools
BlackmanHarris FFT Window function.
Definition: FFTWindows.h:200
Blackman FFT Window function.
Definition: FFTWindows.h:170
BlackmanNuttall FFT Window function.
Definition: FFTWindows.h:185
Buffered window function, so that we do not need to re-calculate the values.
Definition: FFTWindows.h:67
virtual void begin(int samples) override
Setup the window function providing the fft length.
Definition: FFTWindows.h:73
FlatTop FFT Window function.
Definition: FFTWindows.h:215
Hamming FFT Window function.
Definition: FFTWindows.h:112
Hann FFT Window function.
Definition: FFTWindows.h:125
Nuttall FFT Window function.
Definition: FFTWindows.h:154
Rectange FFT Window function.
Definition: FFTWindows.h:101
Triangle FFT Window function.
Definition: FFTWindows.h:138
Welch FFT Window function.
Definition: FFTWindows.h:230
FFT Window Function.
Definition: FFTWindows.h:23
int samples()
Provides the number of samples (fft length)
Definition: FFTWindows.h:41
virtual void begin(int samples)
Setup the window function providing the fft length.
Definition: FFTWindows.h:28
float factor(int idx)
Provides the multipication factor at the indicated position. The result is symetirically mirrored aro...
Definition: FFTWindows.h:35
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:868