Repository navigation
Expand file tree
/
Copy pathstd_library_frequency.cmajor
More file actions
147 lines (120 loc) · 5.94 KB
/
Copy pathstd_library_frequency.cmajor
File metadata and controls
147 lines (120 loc) · 5.94 KB
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
//
// ,ad888ba, 88
// d8"' "8b
// d8 88,dba,,adba, ,aPP8A.A8 88 The Cmajor Standard Library
// Y8, 88 88 88 88 88 88
// Y8a. .a8P 88 88 88 88, ,88 88 (C)2024 Cmajor Software Ltd
// '"Y888Y"' 88 88 88 '"8bbP"Y8 88 https://cmajor.dev
// ,88
// 888P"
//
// The Cmajor standard library may be used under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// std.frequency
/**
Contains DFT implementations for complex and real numbers
The buffers can be either float32 or float64 (or complex32/complex64).
Buffer sizes must be a power of 2.
*/
namespace std::frequency
{
/// Performs a forward DFT.
/// The parameters must be power-of-2 sized arrays of floats. The input parameter takes the real
/// components, and the output array receives the real and imaginary components.
void realOnlyForwardFFT<FloatArray> (const FloatArray& inputData, FloatArray& outputData)
{
static_assert (FloatArray.isFixedSizeArray && FloatArray.elementType.isFloat, "realOnlyForwardFFT() requires arguments which are float arrays");
static_assert ((FloatArray.size & (FloatArray.size - 1)) == 0, "The arrays passed to realOnlyForwardFFT() must have a size which is a power of 2");
let size = FloatArray.size;
using ComplexType = FloatArray.elementType.isFloat32 ? complex32 : complex64;
ComplexType[FloatArray.size] buffer;
for (wrap<size> i)
buffer[i] = inputData[i];
complexFFT (buffer);
wrap<size> out;
for (wrap<size / 2 + 1> i) outputData[out++] = buffer[i].real;
for (wrap<size / 2> i = 1) outputData[out++] = buffer[i].imag;
}
/// Performs an inverse FFT.
/// The parameters must be power-of-2 sized arrays of floats. The input parameter takes the real
/// and imaginary components, and the output array receives the real components.
void realOnlyInverseFFT<FloatArray> (const FloatArray& inputData, FloatArray& outputData)
{
static_assert (FloatArray.isFixedSizeArray && FloatArray.elementType.isFloat, "realOnlyInverseFFT() requires arguments which are float arrays");
static_assert ((FloatArray.size & (FloatArray.size - 1)) == 0, "The arrays passed to realOnlyInverseFFT() must have a size which is a power of 2");
using ComplexType = FloatArray.elementType.isFloat32 ? complex32 : complex64;
ComplexType[FloatArray.size] buffer;
for (wrap<FloatArray.size> i)
{
if (i == 0)
{
buffer.at (i).real = inputData[i];
}
else if (i > 0 && i <= FloatArray.size/2)
{
buffer.at (i).real = inputData[i];
buffer.at (FloatArray.size - i).real = inputData[i];
}
else
{
buffer.at(i - FloatArray.size / 2).imag = -inputData[i];
buffer.at(FloatArray.size + FloatArray.size / 2 - i).imag = inputData[i];
}
}
complexFFT (buffer);
let scaleFactor = 1.0f / FloatArray.size;
for (wrap<FloatArray.size> i)
outputData[i] = buffer[i].real * scaleFactor;
}
//==============================================================================
/// Performs an in-place forward FFT on complex data.
void complexFFT<ComplexArray> (ComplexArray& data)
{
static_assert (data.isFixedSizeArray && data.elementType.isComplex, "complexFFT() expects an array of complex values as its argument");
static_assert ((data.size & (data.size - 1)) == 0, "The array passed to complexFFT() must have a size which is a power of 2");
let size = data.size;
if const (size != 1)
{
ComplexArray.elementType[size / 2] even, odd;
wrap<size> sourceIndex;
for (wrap<size / 2> targetIndex)
{
even[targetIndex] = data[sourceIndex++];
odd[targetIndex] = data[sourceIndex++];
}
complexFFT (even);
complexFFT (odd);
wrap<size> targetIndex1, targetIndex2 = size / 2;
for (wrap<size / 2> i)
{
let angle = float (-twoPi) * float (i) / size;
let t = ComplexArray.elementType (cos (angle), sin (angle)) * odd[i];
data[targetIndex1++] = even[i] + t;
data[targetIndex2++] = even[i] - t;
}
}
}
/// Performs an in-place inverse FFT on complex data.
void complexIFFT<ComplexArray> (ComplexArray& data)
{
static_assert (data.isFixedSizeArray && data.elementType.isComplex, "complexIFFT() expects an array of complex values as its argument");
static_assert ((data.size & (data.size - 1)) == 0, "The array passed to complexIFFT() must have a size which is a power of 2");
for (wrap<data.size> i)
data[i].imag = -data[i].imag;
complexFFT (data);
let scaleFactor = 1.0f / data.size;
for (wrap<data.size> i)
{
data[i].real *= scaleFactor;
data[i].imag = -data[i].imag * scaleFactor;
}
}
}