Skip to content Skip to sidebar Skip to footer

Two Dimensional Fft Using Python Results In Slightly Shifted Frequency

I know there have been several questions about using the Fast Fourier Transform (FFT) method in python, but unfortunately none of them could help me with my problem: I want to use

Solution 1:

I found a number of issues

you use 2 * np.pi twice, you should choose one of either linspace or the arg to sine as radians if you want a nice integer number of cycles

additionally np.linspace defaults to endpoint=True, giving you an extra point for 101 instead of 100

fq = 3.0 # frequency of signal to be sampled
N = 100 # Number of sample points within interval, on which signal is considered
x = np.linspace(0, 1, N, endpoint=False) # creating equally spaced vector from 0 to 2pi, with spacing 2pi/N
y = x
xx, yy = np.meshgrid(x, y) # create 2D meshgrid
fnc = np.sin(2 * np.pi * fq * xx) # create a signal, which is simply a sine function with frequency fq = 3.0, modulating the x(!) direction

you can check these issues:

len(x)
Out[228]: 100

plt.plot(fnc[0])

fixing the linspace endpoint now means you have an even number of fft bins so you drop the + 1 in the half calc

matshow() appears to have better defaults, your extent = (0, freq_x.max(), 0, freq_y.max()), in imshow appears to fubar the fft bin numbering

from scipy.fftpack import fft, fftfreq, fftshift
import matplotlib.pyplot as plt
import numpy as np
import math

fq = 3.0# frequency of signal to be sampled
N = 100# Number of sample points within interval, on which signal is considered
x = np.linspace(0, 1, N, endpoint=False)  # creating equally spaced vector from 0 to 2pi, with spacing 2pi/N
y = x
xx, yy = np.meshgrid(x, y)  # create 2D meshgrid
fnc = np.sin(2 * np.pi * fq * xx)  # create a signal, which is simply a sine function with frequency fq = 3.0, modulating the x(!) direction

plt.plot(fnc[0])

ft = np.fft.fft2(fnc)  # calculating the fft coefficients#dx = x[1] - x[0]  # spacing in x (and also y) direction (real space)#sampleFrequency = 2.0 * np.pi / dx#nyquisitFrequency = sampleFrequency / 2.0##freq_x = np.fft.fftfreq(ft.shape[0], d=dx)  # return the DFT sample frequencies #freq_y = np.fft.fftfreq(ft.shape[1], d=dx)##freq_x = np.fft.fftshift(freq_x)  # order sample frequencies, such that 0-th frequency is at center of spectrum #freq_y = np.fft.fftshift(freq_y)

half = len(ft) // 2# calculate half of spectrum length, in order to only show positive frequencies

plt.matshow(
            2 * abs(ft[:half, :half]) / half,
            aspect='auto',
            origin='lower'
            )
plt.grid()
plt.colorbar()
plt.show()

zoomed the plot: enter image description here

Post a Comment for "Two Dimensional Fft Using Python Results In Slightly Shifted Frequency"