forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalTest.cs
More file actions
102 lines (76 loc) · 3.88 KB
/
SignalTest.cs
File metadata and controls
102 lines (76 loc) · 3.88 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tensorflow.NumPy;
using System;
using System.Collections.Generic;
using System.Linq;
using Tensorflow;
using static Tensorflow.Binding;
using Tensorflow.Keras.UnitTest;
namespace TensorFlowNET.UnitTest.Basics
{
[TestClass]
public class SignalTest : EagerModeTestBase
{
[TestMethod]
public void fft()
{
double[] d_real = new double[] { 1.0, 2.0, 3.0, 4.0 };
double[] d_imag = new double[] { -1.0, -3.0, 5.0, 7.0 };
Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_DOUBLE);
Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
Tensor t_complex = tf.complex(t_real, t_imag);
Tensor t_frequency_domain = tf.signal.fft(t_complex);
Tensor f_time_domain = tf.signal.ifft(t_frequency_domain);
Tensor t_real_result = tf.math.real(f_time_domain);
Tensor t_imag_result = tf.math.imag(f_time_domain);
NDArray n_real_result = t_real_result.numpy();
NDArray n_imag_result = t_imag_result.numpy();
double[] d_real_result = n_real_result.ToArray<double>();
double[] d_imag_result = n_imag_result.ToArray<double>();
Assert.IsTrue(base.Equal(d_real_result, d_real));
Assert.IsTrue(base.Equal(d_imag_result, d_imag));
}
[TestMethod]
public void fft2d()
{
double[] d_real = new double[] { 1.0, 2.0, 3.0, 4.0 };
double[] d_imag = new double[] { -1.0, -3.0, 5.0, 7.0 };
Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_DOUBLE);
Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
Tensor t_complex = tf.complex(t_real, t_imag);
Tensor t_complex_2d = tf.reshape(t_complex,new int[] { 2, 2 });
Tensor t_frequency_domain_2d = tf.signal.fft2d(t_complex_2d);
Tensor t_time_domain_2d = tf.signal.ifft2d(t_frequency_domain_2d);
Tensor t_time_domain = tf.reshape(t_time_domain_2d, new int[] { 4 });
Tensor t_real_result = tf.math.real(t_time_domain);
Tensor t_imag_result = tf.math.imag(t_time_domain);
NDArray n_real_result = t_real_result.numpy();
NDArray n_imag_result = t_imag_result.numpy();
double[] d_real_result = n_real_result.ToArray<double>();
double[] d_imag_result = n_imag_result.ToArray<double>();
Assert.IsTrue(base.Equal(d_real_result, d_real));
Assert.IsTrue(base.Equal(d_imag_result, d_imag));
}
[TestMethod]
public void fft3d()
{
double[] d_real = new double[] { 1.0, 2.0, 3.0, 4.0, -3.0, -2.0, -1.0, -4.0 };
double[] d_imag = new double[] { -1.0, -3.0, 5.0, 7.0, 6.0, 4.0, 2.0, 0.0};
Tensor t_real = tf.constant(d_real, dtype: TF_DataType.TF_DOUBLE);
Tensor t_imag = tf.constant(d_imag, dtype: TF_DataType.TF_DOUBLE);
Tensor t_complex = tf.complex(t_real, t_imag);
Tensor t_complex_3d = tf.reshape(t_complex, new int[] { 2, 2, 2 });
Tensor t_frequency_domain_3d = tf.signal.fft2d(t_complex_3d);
Tensor t_time_domain_3d = tf.signal.ifft2d(t_frequency_domain_3d);
Tensor t_time_domain = tf.reshape(t_time_domain_3d, new int[] { 8 });
Tensor t_real_result = tf.math.real(t_time_domain);
Tensor t_imag_result = tf.math.imag(t_time_domain);
NDArray n_real_result = t_real_result.numpy();
NDArray n_imag_result = t_imag_result.numpy();
double[] d_real_result = n_real_result.ToArray<double>();
double[] d_imag_result = n_imag_result.ToArray<double>();
Assert.IsTrue(base.Equal(d_real_result, d_real));
Assert.IsTrue(base.Equal(d_imag_result, d_imag));
}
}
}