1
1
import ComplexNumber from '../../complex-number/ComplexNumber' ;
2
2
3
- export const fourierDirectTestCases = [
3
+ export const fourierTestCases = [
4
4
{
5
5
input : [
6
6
{ amplitude : 1 } ,
@@ -47,13 +47,13 @@ export const fourierDirectTestCases = [
47
47
] ,
48
48
output : [
49
49
{
50
- frequency : 0 , amplitude : 0.3333 , phase : 0 , re : 0.3333 , im : 0 ,
50
+ frequency : 0 , amplitude : 0.33333 , phase : 0 , re : 0.33333 , im : 0 ,
51
51
} ,
52
52
{
53
- frequency : 1 , amplitude : 0.3333 , phase : 0 , re : 0.3333 , im : 0 ,
53
+ frequency : 1 , amplitude : 0.33333 , phase : 0 , re : 0.33333 , im : 0 ,
54
54
} ,
55
55
{
56
- frequency : 2 , amplitude : 0.3333 , phase : 0 , re : 0.3333 , im : 0 ,
56
+ frequency : 2 , amplitude : 0.33333 , phase : 0 , re : 0.33333 , im : 0 ,
57
57
} ,
58
58
] ,
59
59
} ,
@@ -179,13 +179,13 @@ export const fourierDirectTestCases = [
179
179
frequency : 0 , amplitude : 1.75 , phase : 0 , re : 1.75 , im : 0 ,
180
180
} ,
181
181
{
182
- frequency : 1 , amplitude : 1.03077 , phase : 14.0362 , re : 0.99999 , im : 0.25 ,
182
+ frequency : 1 , amplitude : 1.03077 , phase : 14.03624 , re : 0.99999 , im : 0.25 ,
183
183
} ,
184
184
{
185
185
frequency : 2 , amplitude : 0.25 , phase : 0 , re : 0.25 , im : 0 ,
186
186
} ,
187
187
{
188
- frequency : 3 , amplitude : 1.03077 , phase : - 14.0362 , re : 1 , im : - 0.25 ,
188
+ frequency : 3 , amplitude : 1.03077 , phase : - 14.03624 , re : 1 , im : - 0.25 ,
189
189
} ,
190
190
] ,
191
191
} ,
@@ -201,7 +201,7 @@ export const fourierDirectTestCases = [
201
201
frequency : 0 , amplitude : 1 , phase : 0 , re : 1 , im : 0 ,
202
202
} ,
203
203
{
204
- frequency : 1 , amplitude : 1.76776 , phase : 8.1301 , re : 1.75 , im : 0.25 ,
204
+ frequency : 1 , amplitude : 1.76776 , phase : 8.13010 , re : 1.75 , im : 0.25 ,
205
205
} ,
206
206
{
207
207
frequency : 2 , amplitude : 0.5 , phase : 180 , re : - 0.5 , im : 0 ,
@@ -240,17 +240,15 @@ export default class FourierTester {
240
240
* @param {function } fourierTransform
241
241
*/
242
242
static testDirectFourierTransform ( fourierTransform ) {
243
- fourierDirectTestCases . forEach ( ( testCase ) => {
243
+ fourierTestCases . forEach ( ( testCase ) => {
244
244
const { input, output : expectedOutput } = testCase ;
245
245
246
- // Convert input into complex numbers.
247
- const complexInput = input . map ( sample => new ComplexNumber ( { re : sample . amplitude } ) ) ;
248
-
249
246
// Try to split input signal into sequence of pure sinusoids.
250
- const currentOutput = fourierTransform ( complexInput ) ;
247
+ const formattedInput = input . map ( sample => sample . amplitude ) ;
248
+ const currentOutput = fourierTransform ( formattedInput ) ;
251
249
252
250
// Check the signal has been split into proper amount of sub-signals.
253
- expect ( currentOutput . length ) . toBeGreaterThanOrEqual ( complexInput . length ) ;
251
+ expect ( currentOutput . length ) . toBeGreaterThanOrEqual ( formattedInput . length ) ;
254
252
255
253
// Now go through all the signals and check their frequency, amplitude and phase.
256
254
expectedOutput . forEach ( ( expectedSignal , frequency ) => {
@@ -267,4 +265,31 @@ export default class FourierTester {
267
265
} ) ;
268
266
} ) ;
269
267
}
268
+
269
+ /**
270
+ * @param {function } inverseFourierTransform
271
+ */
272
+ static testInverseFourierTransform ( inverseFourierTransform ) {
273
+ fourierTestCases . forEach ( ( testCase ) => {
274
+ const { input : expectedOutput , output : inputFrequencies } = testCase ;
275
+
276
+ // Try to join frequencies into time signal.
277
+ const formattedInput = inputFrequencies . map ( ( frequency ) => {
278
+ return new ComplexNumber ( { re : frequency . re , im : frequency . im } ) ;
279
+ } ) ;
280
+ const currentOutput = inverseFourierTransform ( formattedInput ) ;
281
+
282
+ // Check the signal has been combined of proper amount of time samples.
283
+ expect ( currentOutput . length ) . toBeLessThanOrEqual ( formattedInput . length ) ;
284
+
285
+ // Now go through all the amplitudes and check their values.
286
+ expectedOutput . forEach ( ( expectedAmplitudes , timer ) => {
287
+ // Get template data we want to test against.
288
+ const currentAmplitude = currentOutput [ timer ] ;
289
+
290
+ // Check if current amplitude is close enough to the calculated one.
291
+ expect ( currentAmplitude ) . toBeCloseTo ( expectedAmplitudes . amplitude , 4 ) ;
292
+ } ) ;
293
+ } ) ;
294
+ }
270
295
}
0 commit comments