11import ComplexNumber from '../../complex-number/ComplexNumber' ;
22
3- export const fourierDirectTestCases = [
3+ export const fourierTestCases = [
44 {
55 input : [
66 { amplitude : 1 } ,
@@ -47,13 +47,13 @@ export const fourierDirectTestCases = [
4747 ] ,
4848 output : [
4949 {
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 ,
5151 } ,
5252 {
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 ,
5454 } ,
5555 {
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 ,
5757 } ,
5858 ] ,
5959 } ,
@@ -179,13 +179,13 @@ export const fourierDirectTestCases = [
179179 frequency : 0 , amplitude : 1.75 , phase : 0 , re : 1.75 , im : 0 ,
180180 } ,
181181 {
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 ,
183183 } ,
184184 {
185185 frequency : 2 , amplitude : 0.25 , phase : 0 , re : 0.25 , im : 0 ,
186186 } ,
187187 {
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 ,
189189 } ,
190190 ] ,
191191 } ,
@@ -201,7 +201,7 @@ export const fourierDirectTestCases = [
201201 frequency : 0 , amplitude : 1 , phase : 0 , re : 1 , im : 0 ,
202202 } ,
203203 {
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 ,
205205 } ,
206206 {
207207 frequency : 2 , amplitude : 0.5 , phase : 180 , re : - 0.5 , im : 0 ,
@@ -240,17 +240,15 @@ export default class FourierTester {
240240 * @param {function } fourierTransform
241241 */
242242 static testDirectFourierTransform ( fourierTransform ) {
243- fourierDirectTestCases . forEach ( ( testCase ) => {
243+ fourierTestCases . forEach ( ( testCase ) => {
244244 const { input, output : expectedOutput } = testCase ;
245245
246- // Convert input into complex numbers.
247- const complexInput = input . map ( sample => new ComplexNumber ( { re : sample . amplitude } ) ) ;
248-
249246 // 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 ) ;
251249
252250 // 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 ) ;
254252
255253 // Now go through all the signals and check their frequency, amplitude and phase.
256254 expectedOutput . forEach ( ( expectedSignal , frequency ) => {
@@ -267,4 +265,31 @@ export default class FourierTester {
267265 } ) ;
268266 } ) ;
269267 }
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+ }
270295}
0 commit comments