|
1 | 1 | import discreteFourierTransform from '../discreteFourierTransform'; |
2 | 2 |
|
| 3 | +/** |
| 4 | + * Helper class of the output Signal. |
| 5 | + */ |
| 6 | +class Sgnl { |
| 7 | + constructor(frequency, amplitude, phase) { |
| 8 | + this.frequency = frequency; |
| 9 | + this.amplitude = amplitude; |
| 10 | + this.phase = phase; |
| 11 | + } |
| 12 | +} |
| 13 | + |
3 | 14 | describe('discreteFourierTransform', () => { |
4 | | - it('should calculate split signal into frequencies', () => { |
5 | | - const frequencies = discreteFourierTransform([1, 0, 0, 0]); |
| 15 | + it('should split signal into frequencies', () => { |
| 16 | + const testCases = [ |
| 17 | + { |
| 18 | + inputAmplitudes: [1], |
| 19 | + outputSignals: [ |
| 20 | + new Sgnl(0, 1, 0), |
| 21 | + ], |
| 22 | + }, |
| 23 | + { |
| 24 | + inputAmplitudes: [1, 0], |
| 25 | + outputSignals: [ |
| 26 | + new Sgnl(0, 0.5, 0), |
| 27 | + new Sgnl(1, 0.5, 0), |
| 28 | + ], |
| 29 | + }, |
| 30 | + { |
| 31 | + inputAmplitudes: [2, 0], |
| 32 | + outputSignals: [ |
| 33 | + new Sgnl(0, 1, 0), |
| 34 | + new Sgnl(1, 1, 0), |
| 35 | + ], |
| 36 | + }, |
| 37 | + { |
| 38 | + inputAmplitudes: [1, 0, 0], |
| 39 | + outputSignals: [ |
| 40 | + new Sgnl(0, 0.33, 0), |
| 41 | + new Sgnl(1, 0.33, 0), |
| 42 | + new Sgnl(2, 0.33, 0), |
| 43 | + ], |
| 44 | + }, |
| 45 | + { |
| 46 | + inputAmplitudes: [1, 0, 0, 0], |
| 47 | + outputSignals: [ |
| 48 | + new Sgnl(0, 0.25, 0), |
| 49 | + new Sgnl(1, 0.25, 0), |
| 50 | + new Sgnl(2, 0.25, 0), |
| 51 | + new Sgnl(3, 0.25, 0), |
| 52 | + ], |
| 53 | + }, |
| 54 | + { |
| 55 | + inputAmplitudes: [0, 1, 0, 0], |
| 56 | + outputSignals: [ |
| 57 | + new Sgnl(0, 0.25, 0), |
| 58 | + new Sgnl(1, 0.25, -90), |
| 59 | + new Sgnl(2, 0.25, 180), |
| 60 | + new Sgnl(3, 0.25, 90), |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + inputAmplitudes: [0, 0, 1, 0], |
| 65 | + outputSignals: [ |
| 66 | + new Sgnl(0, 0.25, 0), |
| 67 | + new Sgnl(1, 0.25, 180), |
| 68 | + new Sgnl(2, 0.25, 0), |
| 69 | + new Sgnl(3, 0.25, 180), |
| 70 | + ], |
| 71 | + }, |
| 72 | + { |
| 73 | + inputAmplitudes: [0, 0, 0, 2], |
| 74 | + outputSignals: [ |
| 75 | + new Sgnl(0, 0.5, 0), |
| 76 | + new Sgnl(1, 0.5, 90), |
| 77 | + new Sgnl(2, 0.5, 180), |
| 78 | + new Sgnl(3, 0.5, -90), |
| 79 | + ], |
| 80 | + }, |
| 81 | + { |
| 82 | + inputAmplitudes: [0, 1, 0, 2], |
| 83 | + outputSignals: [ |
| 84 | + new Sgnl(0, 0.75, 0), |
| 85 | + new Sgnl(1, 0.25, 90), |
| 86 | + new Sgnl(2, 0.75, 180), |
| 87 | + new Sgnl(3, 0.25, -90), |
| 88 | + ], |
| 89 | + }, |
| 90 | + { |
| 91 | + inputAmplitudes: [4, 1, 0, 2], |
| 92 | + outputSignals: [ |
| 93 | + new Sgnl(0, 1.75, 0), |
| 94 | + new Sgnl(1, 1.03, 14), |
| 95 | + new Sgnl(2, 0.25, 0), |
| 96 | + new Sgnl(3, 1.03, -14), |
| 97 | + ], |
| 98 | + }, |
| 99 | + { |
| 100 | + inputAmplitudes: [4, 1, -3, 2], |
| 101 | + outputSignals: [ |
| 102 | + new Sgnl(0, 1, 0), |
| 103 | + new Sgnl(1, 1.77, 8), |
| 104 | + new Sgnl(2, 0.5, 180), |
| 105 | + new Sgnl(3, 1.77, -8), |
| 106 | + ], |
| 107 | + }, |
| 108 | + { |
| 109 | + inputAmplitudes: [1, 2, 3, 4], |
| 110 | + outputSignals: [ |
| 111 | + new Sgnl(0, 2.5, 0), |
| 112 | + new Sgnl(1, 0.71, 135), |
| 113 | + new Sgnl(2, 0.5, 180), |
| 114 | + new Sgnl(3, 0.71, -135), |
| 115 | + ], |
| 116 | + }, |
| 117 | + ]; |
| 118 | + |
| 119 | + testCases.forEach((testCase) => { |
| 120 | + const { inputAmplitudes, outputSignals } = testCase; |
| 121 | + |
| 122 | + // Try to split input signal into sequence of pure sinusoids. |
| 123 | + const signals = discreteFourierTransform(inputAmplitudes); |
| 124 | + |
| 125 | + // Check the signal has been split into proper amount of sub-signals. |
| 126 | + expect(signals.length).toBe(outputSignals.length); |
| 127 | + |
| 128 | + // Now go through all the signals and check their frequency, amplitude and phase. |
| 129 | + signals.forEach((signal, frequency) => { |
| 130 | + // Get polar form of calculated sub-signal since it is more convenient to analyze. |
| 131 | + const signalPolarForm = signal.getPolarForm(false); |
| 132 | + |
| 133 | + // Get template data we want to test against. |
| 134 | + const signalTemplate = outputSignals[frequency]; |
6 | 135 |
|
7 | | - expect(frequencies).toBeDefined(); |
| 136 | + // Check all signal parameters. |
| 137 | + expect(frequency).toBe(signalTemplate.frequency); |
| 138 | + expect(signalPolarForm.radius).toBeCloseTo(signalTemplate.amplitude, 2); |
| 139 | + expect(signalPolarForm.phase).toBeCloseTo(signalTemplate.phase, 0); |
| 140 | + }); |
| 141 | + }); |
8 | 142 | }); |
9 | 143 | }); |
0 commit comments