admin管理员组文章数量:1391991
I am working on a project where I need to perform Fourier Transform on synthetic periodic data using both NumPy and TensorFlow. However, I am encountering a discrepancy between the FFT outputs of NumPy and TensorFlow. Specifically, the TensorFlow FFT output seems to be missing the imaginary components, while the NumPy FFT output includes them as expected.
Even though the TensorFlow output has missing imaginary components, using ifft for both NumPy and TensorFlow real-imaginary values reconstructs the signal identically. However, I am unable to predict future steps with TensorFlow. NumPy reconstructs future values as expected.
Why does the TensorFlow FFT output not include the expected imaginary components, while the NumPy FFT output does?
I believe TensorFlow handles frequency components, padding and transformation to complex numbers differently what Numpy does. I just purely convert real values to imaginary with casting real values to complex64.
How can I ensure that the TensorFlow FFT output includes both real and imaginary components correctly? Additionally, how can I predict future values using TensorFlow as I do with NumPy?
This is what I have been trying to do, compare the differences between NumPy and TensorFlow:
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# Define constants
timesteps = 50
future_steps = 20
features = 5 # Number of features
time = np.linspace(0, 10, timesteps) # Time vector
time_extended = np.linspace(0, 10 + (future_steps * (10 / timesteps)), timesteps + future_steps)
# Generate synthetic periodic data
synthetic_data = np.zeros((timesteps, features))
synthetic_data[:, 0] = np.sin(2 * np.pi * 1.0 * time) + 0.5 * np.cos(2 * np.pi * 3.0 * time)
synthetic_data[:, 1] = np.sin(2 * np.pi * 0.5 * time) + np.cos(2 * np.pi * 2.5 * time)
synthetic_data[:, 2] = 1.5 * np.sin(2 * np.pi * 2.0 * time) - 0.3 * np.cos(2 * np.pi * 1.0 * time)
synthetic_data[:, 3] = np.sin(2 * np.pi * 1.5 * time) + 0.2 * np.cos(2 * np.pi * 4.0 * time)
synthetic_data[:, 4] = 0.7 * np.sin(2 * np.pi * 2.8 * time) - 0.4 * np.cos(2 * np.pi * 1.8 * time)
# Compute FFT using NumPy
fft_output_np = np.fft.fft(synthetic_data, axis=0)
frequencies_np = np.fft.fftfreq(timesteps)
amplitudes_np = np.abs(fft_output_np) / timesteps
phases_np = np.angle(fft_output_np)
# Compute FFT using TensorFlow
fft_output_tf = tf.signal.fft(synthetic_data)
amplitudes_tf = tf.abs(fft_output_tf) / tf.cast(timesteps, tf.float32)
phases_tf = tf.math.angle(fft_output_tf)
# Convert TensorFlow tensors to NumPy arrays
fft_output_tf_np = fft_output_tf.numpy()
amplitudes_tf_np = amplitudes_tf.numpy()
phases_tf_np = phases_tf.numpy()
# --------------------------------------------
# ✅ Direct IFFT Reconstruction (Original FFT)
# --------------------------------------------
ifft_reconstructed_np = np.fft.ifft(fft_output_np, axis=0).real
ifft_reconstructed_tf = tf.signal.ifft(fft_output_tf).numpy().real
# --------------------------------------------
# ✅ IFFT Reconstruction from Amplitudes & Phases
# --------------------------------------------
# NumPy Reconstruction
complex_reconstructed_np = amplitudes_np * np.exp(1j * phases_np) * timesteps # Recreate FFT values
ifft_reconstructed_from_ap_np = np.fft.ifft(complex_reconstructed_np, axis=0).real # IFFT
# TensorFlow Reconstruction
complex_reconstructed_tf = tfplex(amplitudes_tf * tf.cos(phases_tf), amplitudes_tf * tf.sin(phases_tf)) * tf.cast(timesteps, tfplex64)
ifft_reconstructed_from_ap_tf = tf.signal.ifft(complex_reconstructed_tf).numpy().real
complex_reconstructed_tf_np = complex_reconstructed_tf.numpy() # Convert to NumPy for printing
# Select a feature for detailed debugging
feature_idx = 0
# ✅ Print Debugging Information
print("\n
本文标签:
版权声明:本文标题:python - Discrepancy Between NumPy and TensorFlow FFT Outputs: Missing Imaginary Components in TensorFlow - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1744729303a2621951.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论