changes to help strp silence
[ardour.git] / gtk2_ardour / fft.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sampo Savolainen
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20 #include "fft.h"
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <math.h>
25
26 FFT::FFT(uint32_t windowSize)
27         : _window_size(windowSize),
28           _data_size(_window_size/2),
29         _iterations(0),
30         _hann_window(0)
31 {
32         _fftInput  = (float *) fftwf_malloc(sizeof(float) * _window_size);
33
34         _fftOutput = (float *) fftwf_malloc(sizeof(float) * _window_size);
35
36         _power_at_bin  = (float *) malloc(sizeof(float) * _data_size);
37         _phase_at_bin  = (float *) malloc(sizeof(float) * _data_size);
38
39         _plan = fftwf_plan_r2r_1d(_window_size, _fftInput, _fftOutput, FFTW_R2HC, FFTW_ESTIMATE);
40
41         reset();
42 }
43
44 void
45 FFT::reset()
46 {
47         memset(_power_at_bin, 0, sizeof(float) * _data_size);
48         memset(_phase_at_bin, 0, sizeof(float) * _data_size);
49
50         _iterations = 0;
51 }
52
53 void
54 FFT::analyze(ARDOUR::Sample *input, WindowingType windowing_type)
55 {
56         _iterations++;
57
58         memcpy(_fftInput, input, sizeof(float) * _window_size);
59
60         if (windowing_type == HANN) {
61                 float *window = get_hann_window();
62                 for (uint32_t i = 0; i < _window_size; i++) {
63                         _fftInput[i] *= window[i];
64                 }
65         }
66
67         fftwf_execute(_plan);
68
69         _power_at_bin[0] += _fftOutput[0] * _fftOutput[0];
70         _phase_at_bin[0] += 0.0;
71
72         float power;
73         float phase;
74
75 #define Re (_fftOutput[i])
76 #define Im (_fftOutput[_window_size-i])
77         for (uint32_t i=1; i < _data_size - 1; i++) {
78
79                 power = (Re * Re) + (Im * Im);
80                 phase = atanf(Im / Re);
81
82                 if (Re < 0.0 && Im > 0.0) {
83                         phase += M_PI;
84                 } else if (Re < 0.0 && Im < 0.0) {
85                         phase -= M_PI;
86                 }
87
88                 _power_at_bin[i] += power;
89                 _phase_at_bin[i] += phase;
90         }
91 #undef Re
92 #undef Im
93 }
94
95 void
96 FFT::calculate()
97 {
98         if (_iterations > 1) {
99                 for (uint32_t i=0; i < _data_size - 1; i++) {
100                         _power_at_bin[i] /= (float)_iterations;
101                         _phase_at_bin[i] /= (float)_iterations;
102                 }
103                 _iterations = 1;
104         }
105 }
106
107 float *
108 FFT::get_hann_window()
109 {
110         if (_hann_window)
111                 return _hann_window;
112
113
114         _hann_window = (float *) malloc(sizeof(float) * _window_size);
115
116         double sum = 0.0;
117
118         for (uint32_t i=0; i < _window_size; i++) {
119                 _hann_window[i]=0.81f * ( 0.5f - (0.5f * (float) cos(2.0f * M_PI * (float)i / (float)(_window_size))));
120                 sum += _hann_window[i];
121         }
122
123         double isum = 1.0 / sum;
124
125         for (uint32_t i=0; i < _window_size; i++) {
126                 _hann_window[i] *= isum;
127         }
128
129         return _hann_window;
130 }
131
132
133 FFT::~FFT()
134 {
135         if (_hann_window) {
136                 free(_hann_window);
137         }
138         fftwf_destroy_plan(_plan);
139         free(_power_at_bin);
140         free(_phase_at_bin);
141         free(_fftOutput);
142         free(_fftInput);
143 }