OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Unexpected horizontal line in discrete inverse Fourier transform of Gaussian function with odd number of samples

  • Thread starter Thread starter Peyman
  • Start date Start date
P

Peyman

Guest
I'm implementing a discrete inverse Fourier transform in Python to approximate the inverse Fourier transform of a Gaussian function.

While the output looks promising, I'm encountering an unexpected horizontal line when the number of samples n is an odd number.

For n = 1000 (even), the output looks correct: enter image description here

For n = 1001 (odd), an unexpected horizontal line appears: enter image description here

Why does this horizontal line appear when n is odd? Any insights or suggestions would be greatly appreciated. Thank you!

My Implementation​


Code:
import matplotlib.pyplot as plt
import numpy as np
from sympy import symbols, exp, pi, lambdify, sqrt

# Defining the Fourier transform of a Gaussian function, sqrt(pi) * exp(-omega ** 2 / 4)
x, omega = symbols('x omega')
f_gaussian_symbolic = exp(-omega ** 2 / 4) * sqrt(pi)
f_gaussian_function = lambdify(omega, f_gaussian_symbolic, 'numpy')

def fourier_inverse(f, n):
    """
    This function computes the inverse Fourier transform of a function f.
    :param f: The function to be transformed
    :param n: Number of samples
    """
    omega_max = 20 # The max frequency we want to be sampled
    omega_range = np.linspace(-omega_max, omega_max, n)

    f_values = f(omega_range)
    inverse_f = np.fft.ifftshift(np.fft.ifft(np.fft.fftshift(f_values)))

    delta_omega = omega_range[1] - omega_range[0]
    x_range = 2 * np.pi * np.fft.ifftshift(np.fft.fftfreq(n, d=delta_omega))

    inverse_f *= delta_omega * n / (2 * np.pi)
    return x_range, inverse_f

plt.figure(figsize=(10, 5))
x_range, inverse_f = fourier_inverse(f_gaussian_function, n=1001)
plt.plot(x_range, inverse_f.real)
plt.ylim(-2, 2)
plt.xlim(-4, 4)
plt.show()
<p>I'm implementing a discrete inverse Fourier transform in Python to approximate the inverse Fourier transform of a Gaussian function.</p>
<p>While the output looks promising, I'm encountering an unexpected horizontal line when the number of samples <code>n</code> is an odd number.</p>
<p>For <code>n = 1000</code> (even), the output looks correct:
<a href="https://i.sstatic.net/vMdCk5o7.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vMdCk5o7.png" alt="enter image description here" /></a></p>
<p>For <code>n = 1001</code> (odd), an unexpected horizontal line appears:
<a href="https://i.sstatic.net/Da6yrIq4.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/Da6yrIq4.png" alt="enter image description here" /></a></p>
<p>Why does this horizontal line appear when n is odd?
Any insights or suggestions would be greatly appreciated. Thank you!</p>
<h2>My Implementation</h2>
<pre><code>import matplotlib.pyplot as plt
import numpy as np
from sympy import symbols, exp, pi, lambdify, sqrt

# Defining the Fourier transform of a Gaussian function, sqrt(pi) * exp(-omega ** 2 / 4)
x, omega = symbols('x omega')
f_gaussian_symbolic = exp(-omega ** 2 / 4) * sqrt(pi)
f_gaussian_function = lambdify(omega, f_gaussian_symbolic, 'numpy')

def fourier_inverse(f, n):
"""
This function computes the inverse Fourier transform of a function f.
:param f: The function to be transformed
:param n: Number of samples
"""
omega_max = 20 # The max frequency we want to be sampled
omega_range = np.linspace(-omega_max, omega_max, n)

f_values = f(omega_range)
inverse_f = np.fft.ifftshift(np.fft.ifft(np.fft.fftshift(f_values)))

delta_omega = omega_range[1] - omega_range[0]
x_range = 2 * np.pi * np.fft.ifftshift(np.fft.fftfreq(n, d=delta_omega))

inverse_f *= delta_omega * n / (2 * np.pi)
return x_range, inverse_f

plt.figure(figsize=(10, 5))
x_range, inverse_f = fourier_inverse(f_gaussian_function, n=1001)
plt.plot(x_range, inverse_f.real)
plt.ylim(-2, 2)
plt.xlim(-4, 4)
plt.show()
</code></pre>
 
Top