1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
/*
Sonic Visualiser
An audio file viewer and annotation editor.
Centre for Digital Music, Queen Mary, University of London.
This file copyright 2006 Chris Cannam and QMUL.
FFT code from Don Cross's public domain FFT implementation.
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. See the file
COPYING included with this distribution for more information.
*/
#include "FFTapi.h"
#ifndef HAVE_FFTW3F
#include <cmath>
#include <iostream>
void
fft(unsigned int n, bool inverse, double *ri, double *ii, double *ro, double *io)
{
if (!ri || !ro || !io) return;
unsigned int bits;
unsigned int i, j, k, m;
unsigned int blockSize, blockEnd;
double tr, ti;
if (n < 2) return;
if (n & (n-1)) return;
double angle = 2.0 * M_PI;
if (inverse) angle = -angle;
for (i = 0; ; ++i) {
if (n & (1 << i)) {
bits = i;
break;
}
}
int *table = new int[n];
for (i = 0; i < n; ++i) {
m = i;
for (j = k = 0; j < bits; ++j) {
k = (k << 1) | (m & 1);
m >>= 1;
}
table[i] = k;
}
if (ii) {
for (i = 0; i < n; ++i) {
ro[table[i]] = ri[i];
io[table[i]] = ii[i];
}
} else {
for (i = 0; i < n; ++i) {
ro[table[i]] = ri[i];
io[table[i]] = 0.0;
}
}
blockEnd = 1;
for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
double delta = angle / (double)blockSize;
double sm2 = -sin(-2 * delta);
double sm1 = -sin(-delta);
double cm2 = cos(-2 * delta);
double cm1 = cos(-delta);
double w = 2 * cm1;
double ar[3], ai[3];
for (i = 0; i < n; i += blockSize) {
ar[2] = cm2;
ar[1] = cm1;
ai[2] = sm2;
ai[1] = sm1;
for (j = i, m = 0; m < blockEnd; j++, m++) {
ar[0] = w * ar[1] - ar[2];
ar[2] = ar[1];
ar[1] = ar[0];
ai[0] = w * ai[1] - ai[2];
ai[2] = ai[1];
ai[1] = ai[0];
k = j + blockEnd;
tr = ar[0] * ro[k] - ai[0] * io[k];
ti = ar[0] * io[k] + ai[0] * ro[k];
ro[k] = ro[j] - tr;
io[k] = io[j] - ti;
ro[j] += tr;
io[j] += ti;
}
}
blockEnd = blockSize;
}
/* fftw doesn't normalise, so nor will we
if (inverse) {
double denom = (double)n;
for (i = 0; i < n; i++) {
ro[i] /= denom;
io[i] /= denom;
}
}
*/
delete[] table;
}
struct fftf_plan_ {
int size;
int inverse;
float *real;
fftf_complex *cplx;
};
fftf_plan
fftf_plan_dft_r2c_1d(int n, float *in, fftf_complex *out, unsigned)
{
if (n < 2) return 0;
if (n & (n-1)) return 0;
fftf_plan_ *plan = new fftf_plan_;
plan->size = n;
plan->inverse = 0;
plan->real = in;
plan->cplx = out;
return plan;
}
fftf_plan
fftf_plan_dft_c2r_1d(int n, fftf_complex *in, float *out, unsigned)
{
if (n < 2) return 0;
if (n & (n-1)) return 0;
fftf_plan_ *plan = new fftf_plan_;
plan->size = n;
plan->inverse = 1;
plan->real = out;
plan->cplx = in;
return plan;
}
void
fftf_destroy_plan(fftf_plan p)
{
delete p;
}
void
fftf_execute(const fftf_plan p)
{
float *real = p->real;
fftf_complex *cplx = p->cplx;
int n = p->size;
int forward = !p->inverse;
double *ri = new double[n];
double *ro = new double[n];
double *io = new double[n];
double *ii = 0;
if (!forward) ii = new double[n];
if (forward) {
for (int i = 0; i < n; ++i) {
ri[i] = real[i];
}
} else {
for (int i = 0; i < n/2+1; ++i) {
ri[i] = cplx[i][0];
ii[i] = cplx[i][1];
if (i > 0) {
ri[n-i] = ri[i];
ii[n-i] = -ii[i];
}
}
}
fft(n, !forward, ri, ii, ro, io);
if (forward) {
for (int i = 0; i < n/2+1; ++i) {
cplx[i][0] = ro[i];
cplx[i][1] = io[i];
}
} else {
for (int i = 0; i < n; ++i) {
real[i] = ro[i];
}
}
delete[] ri;
delete[] ro;
delete[] io;
if (ii) delete[] ii;
}
#endif
|