Improvements on the FFT analysis tool
[ardour.git] / gtk2_ardour / fft_result.cc
1 /*
2     Copyright (C) 2006 Paul Davis
3         Written by 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
21 #include <fft_result.h>
22 #include <fft_graph.h>
23 #include <cstdlib>
24 #include <string>
25 #include <cmath>
26
27 #include <iostream>
28
29 using namespace std;
30
31 FFTResult::FFTResult(FFTGraph *graph, Gdk::Color color, string trackname)
32 {
33         _graph = graph;
34         
35         _windowSize = _graph->windowSize();
36         _dataSize   = _windowSize / 2;
37
38         _averages = 0;
39
40         _data_avg = (float *) malloc(sizeof(float) * _dataSize);
41         memset(_data_avg,0,sizeof(float) * _dataSize);
42
43         _data_min = (float *) malloc(sizeof(float) * _dataSize);
44         _data_max = (float *) malloc(sizeof(float) * _dataSize);
45
46         for (int i = 0; i < _dataSize; i++) {
47                 _data_min[i] = FLT_MAX;
48                 _data_max[i] = FLT_MIN;
49         }
50
51         _color     = color;
52         _trackname = trackname;
53 }
54
55 void
56 FFTResult::analyzeWindow(float *window)
57 {
58         float *_hanning = _graph->_hanning;
59         float *_in = _graph->_in;
60         float *_out = _graph->_out;
61
62         int i;
63         // Copy the data and apply the hanning window
64         for (i = 0; i < _windowSize; i++) {
65                 _in[i] = window[ i ] * _hanning[ i ];
66         }
67
68         fftwf_execute(_graph->_plan);
69
70         float b = _out[0] * _out[0];
71
72         _data_avg[0] += b;
73         if (b < _data_min[0]) _data_min[0] = b;
74         if (b > _data_max[0]) _data_max[0] = b;
75         
76         for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct
77                 b = (_out[i] * _out[i]);
78
79                 _data_avg[i] += b;  // + (_out[_windowSize-i] * _out[_windowSize-i]);, TODO: thanks to Stefan Kost
80                 
81                 if (_data_min[i] > b)  _data_min[i] = b;
82                 if (_data_max[i] < b ) _data_max[i] = b;
83         }
84
85
86         _averages++;
87 }
88
89 void
90 FFTResult::finalize()
91 {
92         if (_averages == 0) {
93                 _minimum = 0.0;
94                 _maximum = 0.0;
95                 return;
96         }
97
98         // Average & scale
99         for (int i = 0; i < _dataSize; i++) {
100                 _data_avg[i] /= _averages;
101                 _data_avg[i]  = 10.0f * log10f(_data_avg[i]); 
102
103                 _data_min[i]  = 10.0f * log10f(_data_min[i]); 
104                 if (_data_min[i] < -10000.0f) {
105                         _data_min[i] = -10000.0f;
106                 }
107                 _data_max[i]  = 10.0f * log10f(_data_max[i]); 
108         }
109
110         // find min & max
111         _minimum = _maximum = _data_avg[0];
112         
113         for (int i = 1; i < _dataSize; i++) {
114                 if (_data_avg[i] < _minimum        && !isinf(_data_avg[i])) {
115                         _minimum = _data_avg[i];
116                 } else if (_data_avg[i] > _maximum && !isinf(_data_avg[i])) {
117                         _maximum = _data_avg[i];
118                 }
119         }
120
121         _averages = 0;
122 }
123
124 FFTResult::~FFTResult()
125 {
126         free(_data_avg);
127         free(_data_min);
128         free(_data_max);
129 }
130
131
132 float
133 FFTResult::avgAt(int x)
134 {
135         if (x < 0 || x>= _dataSize)
136                 return 0.0f;
137
138         return _data_avg[x];
139 }
140
141 float
142 FFTResult::minAt(int x)
143 {
144         if (x < 0 || x>= _dataSize)
145                 return 0.0f;
146
147         return _data_min[x];
148 }
149
150 float
151 FFTResult::maxAt(int x)
152 {
153         if (x < 0 || x>= _dataSize)
154                 return 0.0f;
155
156         return _data_max[x];
157 }
158