Browse Source

Added: fft, ifft, fft2d, ifft2d, fft3d, ifft3d

tags/v0.100.5-BERT-load
BalashovK 2 years ago
parent
commit
0acd25b406
3 changed files with 149 additions and 24 deletions
  1. +40
    -0
      src/TensorFlowNET.Core/APIs/tf.signal.cs
  2. +6
    -24
      src/TensorFlowNET.Core/Operations/gen_ops.cs
  3. +103
    -0
      test/TensorFlowNET.Graph.UnitTest/SignalTest.cs

+ 40
- 0
src/TensorFlowNET.Core/APIs/tf.signal.cs View File

@@ -0,0 +1,40 @@
/*****************************************************************************
Copyright 2023 Konstantin Balashov All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

using Tensorflow.Operations;

namespace Tensorflow
{
public partial class tensorflow
{
public SignalApi signal { get; } = new SignalApi();
public class SignalApi
{
public Tensor fft(Tensor input, string name = null)
=> gen_ops.f_f_t(input, name: name);
public Tensor ifft(Tensor input, string name = null)
=> gen_ops.i_f_f_t(input, name: name);
public Tensor fft2d(Tensor input, string name = null)
=> gen_ops.f_f_t2d(input, name: name);
public Tensor ifft2d(Tensor input, string name = null)
=> gen_ops.i_f_f_t2d(input, name: name);
public Tensor fft3d(Tensor input, string name = null)
=> gen_ops.f_f_t3d(input, name: name);
public Tensor ifft3d(Tensor input, string name = null)
=> gen_ops.i_f_f_t3d(input, name: name);
}
}
}

+ 6
- 24
src/TensorFlowNET.Core/Operations/gen_ops.cs View File

@@ -10475,10 +10475,7 @@ namespace Tensorflow.Operations
/// </remarks>
public static Tensor f_f_t(Tensor input, string name = "FFT")
{
var dict = new Dictionary<string, object>();
dict["input"] = input;
var op = tf.OpDefLib._apply_op_helper("FFT", name: name, keywords: dict);
return op.output;
return tf.Context.ExecuteOp("FFT", name, new ExecuteOpArgs(input));
}

/// <summary>
@@ -10505,10 +10502,7 @@ namespace Tensorflow.Operations
/// </remarks>
public static Tensor f_f_t2d(Tensor input, string name = "FFT2D")
{
var dict = new Dictionary<string, object>();
dict["input"] = input;
var op = tf.OpDefLib._apply_op_helper("FFT2D", name: name, keywords: dict);
return op.output;
return tf.Context.ExecuteOp("FFT2D", name, new ExecuteOpArgs(input));
}

/// <summary>
@@ -10535,10 +10529,7 @@ namespace Tensorflow.Operations
/// </remarks>
public static Tensor f_f_t3d(Tensor input, string name = "FFT3D")
{
var dict = new Dictionary<string, object>();
dict["input"] = input;
var op = tf.OpDefLib._apply_op_helper("FFT3D", name: name, keywords: dict);
return op.output;
return tf.Context.ExecuteOp("FFT3D", name, new ExecuteOpArgs(input));
}

/// <summary>
@@ -12861,10 +12852,7 @@ namespace Tensorflow.Operations
/// </remarks>
public static Tensor i_f_f_t(Tensor input, string name = "IFFT")
{
var dict = new Dictionary<string, object>();
dict["input"] = input;
var op = tf.OpDefLib._apply_op_helper("IFFT", name: name, keywords: dict);
return op.output;
return tf.Context.ExecuteOp("IFFT", name, new ExecuteOpArgs(input));
}

/// <summary>
@@ -12891,10 +12879,7 @@ namespace Tensorflow.Operations
/// </remarks>
public static Tensor i_f_f_t2d(Tensor input, string name = "IFFT2D")
{
var dict = new Dictionary<string, object>();
dict["input"] = input;
var op = tf.OpDefLib._apply_op_helper("IFFT2D", name: name, keywords: dict);
return op.output;
return tf.Context.ExecuteOp("IFFT2D", name, new ExecuteOpArgs(input));
}

/// <summary>
@@ -12921,10 +12906,7 @@ namespace Tensorflow.Operations
/// </remarks>
public static Tensor i_f_f_t3d(Tensor input, string name = "IFFT3D")
{
var dict = new Dictionary<string, object>();
dict["input"] = input;
var op = tf.OpDefLib._apply_op_helper("IFFT3D", name: name, keywords: dict);
return op.output;
return tf.Context.ExecuteOp("IFFT3D", name, new ExecuteOpArgs(input));
}

/// <summary>


+ 103
- 0
test/TensorFlowNET.Graph.UnitTest/SignalTest.cs View File

@@ -0,0 +1,103 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Tensorflow.NumPy;
using System;
using System.Collections.Generic;
using System.Linq;
using Tensorflow;
using static Tensorflow.Binding;
using Buffer = Tensorflow.Buffer;
using TensorFlowNET.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));
}
}
}

Loading…
Cancel
Save