-
Notifications
You must be signed in to change notification settings - Fork 935
Expand file tree
/
Copy pathFourierTest.cs
More file actions
284 lines (250 loc) · 10.4 KB
/
FourierTest.cs
File metadata and controls
284 lines (250 loc) · 10.4 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// <copyright file="FourierTest.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2018 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System;
using Complex = System.Numerics.Complex;
using MathNet.Numerics.IntegralTransforms;
using NUnit.Framework;
namespace MathNet.Numerics.Tests.IntegralTransformsTests
{
/// <summary>
/// Fourier test.
/// </summary>
[TestFixture, Category("FFT")]
public class FourierTest
{
[Test]
public void ReferenceDftTransformsRealSineCorrectly32()
{
var samples = Generate.PeriodicMap(16, w => new Complex32((float)Math.Sin(w), 0), 16, 1.0, Constants.Pi2);
// real-odd transforms to imaginary odd
ReferenceDiscreteFourierTransform.Forward(samples, FourierOptions.Matlab);
// all real components must be zero
foreach (var c in samples)
{
Assert.AreEqual(0, c.Real, 1e-6, "real");
}
// all imaginary components except second and last musth be zero
for (var i = 0; i < samples.Length; i++)
{
if (i == 1)
{
Assert.AreEqual(-8, samples[i].Imaginary, 1e-12, "imag second");
}
else if (i == samples.Length - 1)
{
Assert.AreEqual(8, samples[i].Imaginary, 1e-12, "imag last");
}
else
{
Assert.AreEqual(0, samples[i].Imaginary, 1e-6, "imag");
}
}
}
[Test]
public void ReferenceDftTransformsRealSineCorrectly64()
{
var samples = Generate.PeriodicMap(16, w => new Complex(Math.Sin(w), 0), 16, 1.0, Constants.Pi2);
// real-odd transforms to imaginary odd
ReferenceDiscreteFourierTransform.Forward(samples, FourierOptions.Matlab);
// all real components must be zero
foreach (var c in samples)
{
Assert.AreEqual(0, c.Real, 1e-12, "real");
}
// all imaginary components except second and last musth be zero
for (var i = 0; i < samples.Length; i++)
{
if (i == 1)
{
Assert.AreEqual(-8, samples[i].Imaginary, 1e-12, "imag second");
}
else if (i == samples.Length - 1)
{
Assert.AreEqual(8, samples[i].Imaginary, 1e-12, "imag last");
}
else
{
Assert.AreEqual(0, samples[i].Imaginary, 1e-12, "imag");
}
}
}
[Test]
public void FourierDefaultTransformsRealSineCorrectly32()
{
var samples = Generate.PeriodicMap(16, w => new Complex32((float)Math.Sin(w), 0), 16, 1.0, Constants.Pi2);
// real-odd transforms to imaginary odd
Fourier.Forward(samples, FourierOptions.Matlab);
// all real components must be zero
foreach (var c in samples)
{
Assert.AreEqual(0, c.Real, 1e-6, "real");
}
// all imaginary components except second and last musth be zero
for (var i = 0; i < samples.Length; i++)
{
if (i == 1)
{
Assert.AreEqual(-8, samples[i].Imaginary, 1e-12, "imag second");
}
else if (i == samples.Length - 1)
{
Assert.AreEqual(8, samples[i].Imaginary, 1e-12, "imag last");
}
else
{
Assert.AreEqual(0, samples[i].Imaginary, 1e-6, "imag");
}
}
}
[Test]
public void FourierDefaultTransformsRealSineCorrectly64()
{
var samples = Generate.PeriodicMap(16, w => new Complex(Math.Sin(w), 0), 16, 1.0, Constants.Pi2);
// real-odd transforms to imaginary odd
Fourier.Forward(samples, FourierOptions.Matlab);
// all real components must be zero
foreach (var c in samples)
{
Assert.AreEqual(0, c.Real, 1e-12, "real");
}
// all imaginary components except second and last musth be zero
for (var i = 0; i < samples.Length; i++)
{
if (i == 1)
{
Assert.AreEqual(-8, samples[i].Imaginary, 1e-12, "imag second");
}
else if (i == samples.Length - 1)
{
Assert.AreEqual(8, samples[i].Imaginary, 1e-12, "imag last");
}
else
{
Assert.AreEqual(0, samples[i].Imaginary, 1e-12, "imag");
}
}
}
[Test]
public void Forward2DAndInverse2DRoundtrip64()
{
const int rows = 4;
const int cols = 8;
var original = new Complex[rows * cols];
var rng = new System.Random(42);
for (int i = 0; i < original.Length; i++)
{
original[i] = new Complex(rng.NextDouble(), rng.NextDouble());
}
var samples = (Complex[])original.Clone();
Fourier.Forward2D(samples, rows, cols, FourierOptions.Default);
// Verify the transform actually changed the data
bool anyDifferent = false;
for (int i = 0; i < samples.Length; i++)
{
if ((samples[i] - original[i]).Magnitude > 1e-10)
{
anyDifferent = true;
break;
}
}
Assert.IsTrue(anyDifferent, "Forward2D should modify the data");
Fourier.Inverse2D(samples, rows, cols, FourierOptions.Default);
// Verify roundtrip recovers original data
for (int i = 0; i < samples.Length; i++)
{
Assert.AreEqual(original[i].Real, samples[i].Real, 1e-10, $"Real part mismatch at index {i}");
Assert.AreEqual(original[i].Imaginary, samples[i].Imaginary, 1e-10, $"Imaginary part mismatch at index {i}");
}
}
[Test]
public void Forward2DAndInverse2DRoundtrip32()
{
const int rows = 4;
const int cols = 8;
var original = new Complex32[rows * cols];
var rng = new System.Random(42);
for (int i = 0; i < original.Length; i++)
{
original[i] = new Complex32((float)rng.NextDouble(), (float)rng.NextDouble());
}
var samples = (Complex32[])original.Clone();
Fourier.Forward2D(samples, rows, cols, FourierOptions.Default);
Fourier.Inverse2D(samples, rows, cols, FourierOptions.Default);
// Verify roundtrip recovers original data
for (int i = 0; i < samples.Length; i++)
{
Assert.AreEqual(original[i].Real, samples[i].Real, 1e-4, $"Real part mismatch at index {i}");
Assert.AreEqual(original[i].Imaginary, samples[i].Imaginary, 1e-4, $"Imaginary part mismatch at index {i}");
}
}
[Test]
public void Forward2DKnownDCValue64()
{
// A constant 4x4 array should produce all energy in the DC bin
const int rows = 4;
const int cols = 4;
var samples = new Complex[rows * cols];
for (int i = 0; i < samples.Length; i++)
{
samples[i] = new Complex(1.0, 0.0);
}
Fourier.Forward2D(samples, rows, cols, FourierOptions.NoScaling);
// DC component should equal rows*cols = 16
Assert.AreEqual(16.0, samples[0].Real, 1e-10, "DC real");
Assert.AreEqual(0.0, samples[0].Imaginary, 1e-10, "DC imag");
// All other components should be zero
for (int i = 1; i < samples.Length; i++)
{
Assert.AreEqual(0.0, samples[i].Real, 1e-10, $"Non-DC real at {i}");
Assert.AreEqual(0.0, samples[i].Imaginary, 1e-10, $"Non-DC imag at {i}");
}
}
[Test]
public void Forward2DNonPowerOfTwoDimensions64()
{
// Test with non-power-of-two dimensions to exercise Bluestein path
const int rows = 3;
const int cols = 5;
var original = new Complex[rows * cols];
var rng = new System.Random(123);
for (int i = 0; i < original.Length; i++)
{
original[i] = new Complex(rng.NextDouble(), rng.NextDouble());
}
var samples = (Complex[])original.Clone();
Fourier.Forward2D(samples, rows, cols, FourierOptions.Default);
Fourier.Inverse2D(samples, rows, cols, FourierOptions.Default);
for (int i = 0; i < samples.Length; i++)
{
Assert.AreEqual(original[i].Real, samples[i].Real, 1e-10, $"Real part mismatch at index {i}");
Assert.AreEqual(original[i].Imaginary, samples[i].Imaginary, 1e-10, $"Imaginary part mismatch at index {i}");
}
}
}
}