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>
32 FFTResult::FFTResult(FFTGraph
*graph
, Gdk::Color color
, string trackname
)
36 _windowSize
= _graph
->windowSize();
37 _dataSize
= _windowSize
/ 2;
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
;
53 _trackname
= trackname
;
57 FFTResult::analyzeWindow(float *window
)
59 float *_hanning
= _graph
->_hanning
;
60 float *_in
= _graph
->_in
;
61 float *_out
= _graph
->_out
;
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];
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
;
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
]);
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
];
125 FFTResult::~FFTResult()
134 FFTResult::avgAt(int x
)
136 if (x
< 0 || x
>= _dataSize
)
143 FFTResult::minAt(int x
)
145 if (x
< 0 || x
>= _dataSize
)
152 FFTResult::maxAt(int x
)
154 if (x
< 0 || x
>= _dataSize
)