2008-10-13 15:45:20 -04:00
|
|
|
/*
|
|
|
|
Copyright (C) 2008 Paul Davis
|
|
|
|
Author: Sampo Savolainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
#include "fft.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2011-03-03 12:58:11 -05:00
|
|
|
using namespace GTKArdour;
|
|
|
|
|
2008-10-13 15:45:20 -04:00
|
|
|
FFT::FFT(uint32_t windowSize)
|
2008-10-15 15:21:26 -04:00
|
|
|
: _window_size(windowSize),
|
|
|
|
_data_size(_window_size/2),
|
2008-10-20 14:57:34 -04:00
|
|
|
_iterations(0),
|
|
|
|
_hann_window(0)
|
2008-10-13 15:45:20 -04:00
|
|
|
{
|
2008-10-15 15:21:26 -04:00
|
|
|
_fftInput = (float *) fftwf_malloc(sizeof(float) * _window_size);
|
2008-10-13 15:45:20 -04:00
|
|
|
|
2008-10-15 15:21:26 -04:00
|
|
|
_fftOutput = (float *) fftwf_malloc(sizeof(float) * _window_size);
|
2008-10-13 15:45:20 -04:00
|
|
|
|
2008-10-15 15:21:26 -04:00
|
|
|
_power_at_bin = (float *) malloc(sizeof(float) * _data_size);
|
|
|
|
_phase_at_bin = (float *) malloc(sizeof(float) * _data_size);
|
2008-10-13 15:45:20 -04:00
|
|
|
|
2008-10-15 15:21:26 -04:00
|
|
|
_plan = fftwf_plan_r2r_1d(_window_size, _fftInput, _fftOutput, FFTW_R2HC, FFTW_ESTIMATE);
|
2008-10-13 15:45:20 -04:00
|
|
|
|
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
FFT::reset()
|
|
|
|
{
|
2008-10-15 15:21:26 -04:00
|
|
|
memset(_power_at_bin, 0, sizeof(float) * _data_size);
|
|
|
|
memset(_phase_at_bin, 0, sizeof(float) * _data_size);
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2008-10-13 15:45:20 -04:00
|
|
|
_iterations = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2008-10-20 14:57:34 -04:00
|
|
|
FFT::analyze(ARDOUR::Sample *input, WindowingType windowing_type)
|
2008-10-13 15:45:20 -04:00
|
|
|
{
|
|
|
|
_iterations++;
|
|
|
|
|
2008-10-15 15:21:26 -04:00
|
|
|
memcpy(_fftInput, input, sizeof(float) * _window_size);
|
2008-10-13 15:45:20 -04:00
|
|
|
|
2008-10-20 14:57:34 -04:00
|
|
|
if (windowing_type == HANN) {
|
|
|
|
float *window = get_hann_window();
|
|
|
|
for (uint32_t i = 0; i < _window_size; i++) {
|
|
|
|
_fftInput[i] *= window[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-13 15:45:20 -04:00
|
|
|
fftwf_execute(_plan);
|
|
|
|
|
2008-10-15 15:21:26 -04:00
|
|
|
_power_at_bin[0] += _fftOutput[0] * _fftOutput[0];
|
|
|
|
_phase_at_bin[0] += 0.0;
|
2008-10-13 15:45:20 -04:00
|
|
|
|
|
|
|
float power;
|
|
|
|
float phase;
|
|
|
|
|
|
|
|
#define Re (_fftOutput[i])
|
2008-10-15 15:21:26 -04:00
|
|
|
#define Im (_fftOutput[_window_size-i])
|
2019-02-27 21:08:21 -05:00
|
|
|
for (uint32_t i = 1; i < _data_size - 1; ++i) {
|
2008-10-13 15:45:20 -04:00
|
|
|
|
|
|
|
power = (Re * Re) + (Im * Im);
|
2019-06-04 08:41:50 -04:00
|
|
|
if (power < 1e-16) {
|
|
|
|
phase = 0;
|
|
|
|
} else {
|
|
|
|
phase = atanf (Im / Re);
|
|
|
|
if (Re < 0.0 && Im > 0.0) {
|
|
|
|
phase += M_PI;
|
|
|
|
} else if (Re < 0.0 && Im < 0.0) {
|
|
|
|
phase -= M_PI;
|
|
|
|
}
|
2008-10-13 15:45:20 -04:00
|
|
|
}
|
|
|
|
|
2008-10-15 15:21:26 -04:00
|
|
|
_power_at_bin[i] += power;
|
|
|
|
_phase_at_bin[i] += phase;
|
2008-10-13 15:45:20 -04:00
|
|
|
}
|
|
|
|
#undef Re
|
|
|
|
#undef Im
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-02-27 21:08:21 -05:00
|
|
|
FFT::calculate ()
|
2008-10-13 15:45:20 -04:00
|
|
|
{
|
|
|
|
if (_iterations > 1) {
|
2019-04-13 11:48:27 -04:00
|
|
|
for (uint32_t i=0; i < _data_size - 1; i++) {
|
2008-10-15 15:21:26 -04:00
|
|
|
_power_at_bin[i] /= (float)_iterations;
|
|
|
|
_phase_at_bin[i] /= (float)_iterations;
|
2008-10-13 15:45:20 -04:00
|
|
|
}
|
|
|
|
_iterations = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-27 21:08:21 -05:00
|
|
|
float*
|
|
|
|
FFT::get_hann_window ()
|
2008-10-20 14:57:34 -04:00
|
|
|
{
|
2019-02-27 21:08:21 -05:00
|
|
|
if (_hann_window) {
|
2008-10-20 14:57:34 -04:00
|
|
|
return _hann_window;
|
2019-02-27 21:08:21 -05:00
|
|
|
}
|
2008-10-20 14:57:34 -04:00
|
|
|
|
2019-02-27 21:08:21 -05:00
|
|
|
_hann_window = (float*) malloc (sizeof (float) * _window_size);
|
2008-10-20 14:57:34 -04:00
|
|
|
|
|
|
|
double sum = 0.0;
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2019-02-27 21:08:21 -05:00
|
|
|
for (uint32_t i = 0; i < _window_size; ++i) {
|
|
|
|
_hann_window[i] = 0.81f * (0.5f - (0.5f * (float) cos (2.0f * M_PI * (float)i / (float)(_window_size))));
|
|
|
|
sum += _hann_window[i];
|
|
|
|
}
|
2008-10-20 14:57:34 -04:00
|
|
|
|
2019-02-27 21:08:21 -05:00
|
|
|
double isum = 1.0 / sum;
|
2009-10-14 12:10:01 -04:00
|
|
|
|
2019-02-27 21:08:21 -05:00
|
|
|
for (uint32_t i = 0; i < _window_size; ++i) {
|
|
|
|
_hann_window[i] *= isum;
|
|
|
|
}
|
2008-10-20 14:57:34 -04:00
|
|
|
|
|
|
|
return _hann_window;
|
|
|
|
}
|
|
|
|
|
2008-10-13 15:45:20 -04:00
|
|
|
FFT::~FFT()
|
|
|
|
{
|
2008-10-20 14:57:34 -04:00
|
|
|
if (_hann_window) {
|
|
|
|
free(_hann_window);
|
|
|
|
}
|
2008-10-13 15:45:20 -04:00
|
|
|
fftwf_destroy_plan(_plan);
|
2008-10-15 15:21:26 -04:00
|
|
|
free(_power_at_bin);
|
|
|
|
free(_phase_at_bin);
|
2016-08-28 05:23:57 -04:00
|
|
|
fftwf_free(_fftOutput);
|
|
|
|
fftwf_free(_fftInput);
|
2008-10-13 15:45:20 -04:00
|
|
|
}
|