automated build stuff continues
[ardour2.git] / gtk2_ardour / fft_result.cc
blob37c045b7b052a1b38bd299263060de4a575d0f5c
1 /*
2 Copyright (C) 2006 Paul Davis
3 Written by Sampo Savolainen
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.
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.
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.
21 #include <fft_result.h>
22 #include <fft_graph.h>
23 #include <cstdlib>
24 #include <cstring>
25 #include <string>
26 #include <cmath>
28 #include <iostream>
30 using namespace std;
32 FFTResult::FFTResult(FFTGraph *graph, Gdk::Color color, string trackname)
34 _graph = graph;
36 _windowSize = _graph->windowSize();
37 _dataSize = _windowSize / 2;
39 _averages = 0;
41 _data_avg = (float *) malloc(sizeof(float) * _dataSize);
42 memset(_data_avg,0,sizeof(float) * _dataSize);
44 _data_min = (float *) malloc(sizeof(float) * _dataSize);
45 _data_max = (float *) malloc(sizeof(float) * _dataSize);
47 for (int i = 0; i < _dataSize; i++) {
48 _data_min[i] = FLT_MAX;
49 _data_max[i] = FLT_MIN;
52 _color = color;
53 _trackname = trackname;
56 void
57 FFTResult::analyzeWindow(float *window)
59 float *_hanning = _graph->_hanning;
60 float *_in = _graph->_in;
61 float *_out = _graph->_out;
63 int i;
64 // Copy the data and apply the hanning window
65 for (i = 0; i < _windowSize; i++) {
66 _in[i] = window[ i ] * _hanning[ i ];
69 fftwf_execute(_graph->_plan);
71 float b = _out[0] * _out[0];
73 _data_avg[0] += b;
74 if (b < _data_min[0]) _data_min[0] = b;
75 if (b > _data_max[0]) _data_max[0] = b;
77 for (i=1; i < _dataSize - 1; i++) { // TODO: check with Jesse whether this is really correct
78 b = (_out[i] * _out[i]);
80 _data_avg[i] += b; // + (_out[_windowSize-i] * _out[_windowSize-i]);, TODO: thanks to Stefan Kost
82 if (_data_min[i] > b) _data_min[i] = b;
83 if (_data_max[i] < b ) _data_max[i] = b;
87 _averages++;
90 void
91 FFTResult::finalize()
93 if (_averages == 0) {
94 _minimum = 0.0;
95 _maximum = 0.0;
96 return;
99 // Average & scale
100 for (int i = 0; i < _dataSize; i++) {
101 _data_avg[i] /= _averages;
102 _data_avg[i] = 10.0f * log10f(_data_avg[i]);
104 _data_min[i] = 10.0f * log10f(_data_min[i]);
105 if (_data_min[i] < -10000.0f) {
106 _data_min[i] = -10000.0f;
108 _data_max[i] = 10.0f * log10f(_data_max[i]);
111 // find min & max
112 _minimum = _maximum = _data_avg[0];
114 for (int i = 1; i < _dataSize; i++) {
115 if (_data_avg[i] < _minimum && !isinf(_data_avg[i])) {
116 _minimum = _data_avg[i];
117 } else if (_data_avg[i] > _maximum && !isinf(_data_avg[i])) {
118 _maximum = _data_avg[i];
122 _averages = 0;
125 FFTResult::~FFTResult()
127 free(_data_avg);
128 free(_data_min);
129 free(_data_max);
133 float
134 FFTResult::avgAt(int x)
136 if (x < 0 || x>= _dataSize)
137 return 0.0f;
139 return _data_avg[x];
142 float
143 FFTResult::minAt(int x)
145 if (x < 0 || x>= _dataSize)
146 return 0.0f;
148 return _data_min[x];
151 float
152 FFTResult::maxAt(int x)
154 if (x < 0 || x>= _dataSize)
155 return 0.0f;
157 return _data_max[x];