1+ using Microsoft . VisualStudio . TestTools . UnitTesting ;
2+ using Tensorflow . NumPy ;
3+ using System ;
4+ using System . Collections . Generic ;
5+ using System . Linq ;
6+ using Tensorflow ;
7+ using static Tensorflow . Binding ;
8+ using Buffer = Tensorflow . Buffer ;
9+ using TensorFlowNET . Keras . UnitTest ;
10+
11+ namespace TensorFlowNET . UnitTest . Basics
12+ {
13+ [ TestClass ]
14+ public class SignalTest : EagerModeTestBase
15+ {
16+ [ TestMethod ]
17+ public void fft ( )
18+ {
19+ double [ ] d_real = new double [ ] { 1.0 , 2.0 , 3.0 , 4.0 } ;
20+ double [ ] d_imag = new double [ ] { - 1.0 , - 3.0 , 5.0 , 7.0 } ;
21+
22+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_DOUBLE ) ;
23+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_DOUBLE ) ;
24+
25+ Tensor t_complex = tf . complex ( t_real , t_imag ) ;
26+
27+ Tensor t_frequency_domain = tf . signal . fft ( t_complex ) ;
28+ Tensor f_time_domain = tf . signal . ifft ( t_frequency_domain ) ;
29+
30+ Tensor t_real_result = tf . math . real ( f_time_domain ) ;
31+ Tensor t_imag_result = tf . math . imag ( f_time_domain ) ;
32+
33+ NDArray n_real_result = t_real_result . numpy ( ) ;
34+ NDArray n_imag_result = t_imag_result . numpy ( ) ;
35+
36+ double [ ] d_real_result = n_real_result . ToArray < double > ( ) ;
37+ double [ ] d_imag_result = n_imag_result . ToArray < double > ( ) ;
38+
39+ Assert . IsTrue ( base . Equal ( d_real_result , d_real ) ) ;
40+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag ) ) ;
41+ }
42+ [ TestMethod ]
43+ public void fft2d ( )
44+ {
45+ double [ ] d_real = new double [ ] { 1.0 , 2.0 , 3.0 , 4.0 } ;
46+ double [ ] d_imag = new double [ ] { - 1.0 , - 3.0 , 5.0 , 7.0 } ;
47+
48+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_DOUBLE ) ;
49+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_DOUBLE ) ;
50+
51+ Tensor t_complex = tf . complex ( t_real , t_imag ) ;
52+
53+ Tensor t_complex_2d = tf . reshape ( t_complex , new int [ ] { 2 , 2 } ) ;
54+
55+ Tensor t_frequency_domain_2d = tf . signal . fft2d ( t_complex_2d ) ;
56+ Tensor t_time_domain_2d = tf . signal . ifft2d ( t_frequency_domain_2d ) ;
57+
58+ Tensor t_time_domain = tf . reshape ( t_time_domain_2d , new int [ ] { 4 } ) ;
59+
60+ Tensor t_real_result = tf . math . real ( t_time_domain ) ;
61+ Tensor t_imag_result = tf . math . imag ( t_time_domain ) ;
62+
63+ NDArray n_real_result = t_real_result . numpy ( ) ;
64+ NDArray n_imag_result = t_imag_result . numpy ( ) ;
65+
66+ double [ ] d_real_result = n_real_result . ToArray < double > ( ) ;
67+ double [ ] d_imag_result = n_imag_result . ToArray < double > ( ) ;
68+
69+ Assert . IsTrue ( base . Equal ( d_real_result , d_real ) ) ;
70+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag ) ) ;
71+ }
72+ [ TestMethod ]
73+ public void fft3d ( )
74+ {
75+ double [ ] d_real = new double [ ] { 1.0 , 2.0 , 3.0 , 4.0 , - 3.0 , - 2.0 , - 1.0 , - 4.0 } ;
76+ double [ ] d_imag = new double [ ] { - 1.0 , - 3.0 , 5.0 , 7.0 , 6.0 , 4.0 , 2.0 , 0.0 } ;
77+
78+ Tensor t_real = tf . constant ( d_real , dtype : TF_DataType . TF_DOUBLE ) ;
79+ Tensor t_imag = tf . constant ( d_imag , dtype : TF_DataType . TF_DOUBLE ) ;
80+
81+ Tensor t_complex = tf . complex ( t_real , t_imag ) ;
82+
83+ Tensor t_complex_3d = tf . reshape ( t_complex , new int [ ] { 2 , 2 , 2 } ) ;
84+
85+ Tensor t_frequency_domain_3d = tf . signal . fft2d ( t_complex_3d ) ;
86+ Tensor t_time_domain_3d = tf . signal . ifft2d ( t_frequency_domain_3d ) ;
87+
88+ Tensor t_time_domain = tf . reshape ( t_time_domain_3d , new int [ ] { 8 } ) ;
89+
90+ Tensor t_real_result = tf . math . real ( t_time_domain ) ;
91+ Tensor t_imag_result = tf . math . imag ( t_time_domain ) ;
92+
93+ NDArray n_real_result = t_real_result . numpy ( ) ;
94+ NDArray n_imag_result = t_imag_result . numpy ( ) ;
95+
96+ double [ ] d_real_result = n_real_result . ToArray < double > ( ) ;
97+ double [ ] d_imag_result = n_imag_result . ToArray < double > ( ) ;
98+
99+ Assert . IsTrue ( base . Equal ( d_real_result , d_real ) ) ;
100+ Assert . IsTrue ( base . Equal ( d_imag_result , d_imag ) ) ;
101+ }
102+ }
103+ }
0 commit comments