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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/*
SuperCollider real time audio synthesis system
Copyright (c) 2002 James McCartney. All rights reserved.
http://www.audiosynth.com
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include <cmath>
#include "SC_Types.h"
#include "SC_Constants.h"
#include "float.h"
#ifdef _MSC_VER
// hypotf is c99, but not c++
# define hypotf _hypotf
#endif
////////////////////////////////////////////////////////////////////////////////
namespace detail {
const int kSineSize = 8192;
const int kSineMask = kSineSize - 1;
const double kSinePhaseScale = kSineSize / twopi;
const int32 kPolarLUTSize = 2049;
const int32 kPolarLUTSize2 = kPolarLUTSize >> 1;
/* each object file that is including this header will have separate lookup tables */
namespace {
float gMagLUT[kPolarLUTSize];
float gPhaseLUT[kPolarLUTSize];
float gSine[kSineSize + 1];
static bool initTables(void) {
double sineIndexToPhase = twopi / kSineSize;
for (int i = 0; i <= kSineSize; ++i) {
double phase = i * sineIndexToPhase;
float32 d = sin(phase);
gSine[i] = d;
}
double rPolarLUTSize2 = 1. / kPolarLUTSize2;
for (int i = 0; i < kPolarLUTSize; ++i) {
double slope = (i - kPolarLUTSize2) * rPolarLUTSize2;
double angle = atan(slope);
gPhaseLUT[i] = (float)angle;
gMagLUT[i] = (float)(1.f / cos(angle));
}
return true;
}
bool dummy = initTables();
}
struct Polar;
struct Complex {
Complex() {}
Complex(float r, float i): real(r), imag(i) {}
void Set(float r, float i) {
real = r;
imag = i;
}
Complex& operator=(Complex b) {
real = b.real;
imag = b.imag;
return *this;
}
Complex& operator=(float b) {
real = b;
imag = 0.;
return *this;
}
Polar ToPolar();
/**
* Converts cartesian to polar representation, using lookup tables.
* Note: in this implementation the phase values returned lie in the range [-pi/4, 7pi/4]
* rather than the more conventional [0, 2pi] or [-pi, pi].
*/
Polar ToPolarApx();
void ToPolarInPlace();
void ToPolarApxInPlace();
float real, imag;
};
struct Polar {
Polar() {}
Polar(float m, float p): mag(m), phase(p) {}
void Set(float m, float p) {
mag = m;
phase = p;
}
Complex ToComplex() { return Complex(mag * std::cos(phase), mag * std::sin(phase)); }
Complex ToComplexApx() {
uint32 sinindex = (int32)(kSinePhaseScale * phase) & kSineMask;
uint32 cosindex = (sinindex + (kSineSize >> 2)) & kSineMask;
return Complex(mag * gSine[cosindex], mag * gSine[sinindex]);
}
void ToComplexInPlace() {
Complex complx = ToComplex();
mag = complx.real;
phase = complx.imag;
}
void ToComplexApxInPlace() {
Complex complx = ToComplexApx();
mag = complx.real;
phase = complx.imag;
}
float mag, phase;
};
inline Polar Complex::ToPolar() { return Polar(hypotf(imag, real), std::atan2(imag, real)); }
inline Polar Complex::ToPolarApx() {
int32 index;
float absreal = fabs(real);
float absimag = fabs(imag);
float mag, phase, slope;
if (absreal > absimag) {
slope = imag / real;
index = (int32)(kPolarLUTSize2 + kPolarLUTSize2 * slope);
mag = gMagLUT[index] * absreal;
phase = gPhaseLUT[index];
if (real > 0) {
return Polar(mag, phase);
} else {
return Polar(mag, (float)(pi + phase));
}
} else if (absimag > 0) {
slope = real / imag;
index = (int32)(kPolarLUTSize2 + kPolarLUTSize2 * slope);
mag = gMagLUT[index] * absimag;
phase = gPhaseLUT[index];
if (imag > 0) {
return Polar(mag, (float)(pi2 - phase));
} else {
return Polar(mag, (float)(pi32 - phase));
}
} else
return Polar(0, 0);
}
inline void Complex::ToPolarInPlace() {
Polar polar = ToPolar();
real = polar.mag;
imag = polar.phase;
}
inline void Complex::ToPolarApxInPlace() {
Polar polar = ToPolarApx();
real = polar.mag;
imag = polar.phase;
}
}
using detail::Complex;
using detail::Polar;
struct ComplexFT {
float dc, nyq;
Complex complex[1];
};
struct PolarFT {
float dc, nyq;
Polar polar[1];
};
void ToComplex(Polar in, Complex& out);
inline Complex operator+(Complex a, Complex b) { return Complex(a.real + b.real, a.imag + b.imag); }
inline Complex operator+(Complex a, float b) { return Complex(a.real + b, a.imag); }
inline Complex operator+(float a, Complex b) { return Complex(a + b.real, b.imag); }
inline Complex& operator+=(Complex& a, const Complex& b) {
a.real += b.real, a.imag += b.imag;
return a;
}
inline Complex& operator+=(Complex& a, float b) {
a.real += b;
return a;
}
inline Complex operator-(Complex a, Complex b) { return Complex(a.real - b.real, a.imag - b.imag); }
inline Complex operator-(Complex a, float b) { return Complex(a.real - b, a.imag); }
inline Complex operator-(float a, Complex b) { return Complex(a - b.real, b.imag); }
inline Complex operator-=(Complex a, Complex b) {
a.real -= b.real, a.imag -= b.imag;
return a;
}
inline Complex operator-=(Complex a, float b) {
a.real -= b;
return a;
}
inline Complex operator*(Complex a, Complex b) {
return Complex(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real);
}
inline Complex operator*(Complex a, float b) { return Complex(a.real * b, a.imag * b); }
inline Complex operator*(float a, Complex b) { return Complex(b.real * a, b.imag * a); }
inline Complex operator*=(Complex a, Complex b) {
a.Set(a.real * b.real - a.imag * b.imag, a.real * b.imag + a.imag * b.real);
return a;
}
inline Complex operator*=(Complex a, float b) {
a.real *= b;
a.imag *= b;
return a;
}
inline Polar operator*(Polar a, float b) { return Polar(a.mag * b, a.phase); }
inline Polar operator*(float a, Polar b) { return Polar(a * b.mag, b.phase); }
inline Polar operator*=(Polar a, float b) {
a.mag *= b;
return a;
}
|