最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Discrepancy Between NumPy and TensorFlow FFT Outputs: Missing Imaginary Components in TensorFlow - Stack Overflow

programmeradmin1浏览0评论

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论