Improving Fourier Transform Visualization in Python

In summary, the person is looking for ways to improve the visualisation of their Fourier transform and is considering reordering the components or using a different colour map.
  • #1
Tibo123
6
0
Hello,

My name is Thibaut. I am looking to improve my code in python in order to have a better look a my Fourier transform. as you can see on the image, we barely see any detail of the peaks on the image. Also it's not centred. the zero order peak in on the corner, not in the centre.

Any idea how to fix it?

Thibaut

image: click here
figure_1.png


Python:
import numpy as np
import matplotlib.pyplot as pltx = np.linspace(-100, 100.0, 201.0, endpoint = True)
y = np.linspace(-100, 100.0, 201.0, endpoint = True)
xx, yy = np.meshgrid(x, y, sparse=True)
def f1(a,b):
    return np.piecewise(a, [abs(a) < 2, abs(a) >= 2], [1, 0]) * np.piecewise(b, [abs(b) < 2, abs(b) >= 2], [1, 0])
       
def f2(r,c):
    spacing=10
    a=np.zeros([r,c])
    for ii in range (r):
        for jj in range(c):
           if (ii%spacing)==0 and (jj%spacing)==0 and abs(ii-100)<55 and abs(jj-100)<55:
               a[ii,jj]=1
           else:
               a[ii,jj]=0
    return a

h = np.fft.fft2(f1(xx,yy))
g = np.fft.fft2(f2(201,201))

k=abs(h)*abs(g)plt.imshow(np.abs(k),interpolation='nearest')     
#plt.imshow(f1(xx,yy),interpolation='nearest')
#plt.imshow(f2(200,200),interpolation='nearest')
plt.show()
 
Technology news on Phys.org
  • #2
It is good practice to read the documentation behind some of these "ready-to-use" functions when using them.

By default, the Discrete Fast Fourier Transform in Numpy returns the components in standard order, which contains the zero-frequency term first, followed by the positive frequency terms (in increasing order) up till the Nyquist frequency and finally the negative frequency terms (in decreasing negative order). The reason for this default behaviour is due to the way the FFT algorithm works, if I'm not mistaken. With this knowledge, you can re-order the array yourself, or use the in-built fftshift function.

As for your visualisation, you can always change the colour map to one with better contrast for your data range. The details can be easily found on the matplotlib website.
 

Related to Improving Fourier Transform Visualization in Python

1. What is a Fourier transform and why is it used?

A Fourier transform is a mathematical tool used to analyze the frequency components of a signal or function. It transforms a function from its original form in the time or spatial domain to a representation in the frequency domain. This allows for the identification of different frequencies present in a signal and is useful for tasks such as filtering, compression, and analysis of waveforms.

2. How is the Fourier transform implemented in Python?

In Python, the Fourier transform is typically implemented using the NumPy library's fft module. This module contains functions for computing the fast Fourier transform (FFT) and inverse FFT for both one-dimensional and multi-dimensional arrays. These functions can be used to perform the Fourier transform on a signal or function stored as an array in Python.

3. What is the difference between a discrete Fourier transform (DFT) and a fast Fourier transform (FFT)?

The DFT is a type of Fourier transform that computes the frequency components of a discrete signal or function. It involves a brute-force approach of computing the Fourier coefficients for each frequency component, which can be computationally expensive for large datasets. The FFT is an algorithm that efficiently computes the DFT by taking advantage of symmetries and redundancies in the DFT calculation, resulting in a significantly faster computation.

4. Can the Fourier transform be applied to non-periodic signals?

Yes, the Fourier transform can be applied to both periodic and non-periodic signals. However, for non-periodic signals, the transform is computed over a finite time or spatial interval, resulting in a continuous spectrum instead of discrete frequencies. In this case, the inverse Fourier transform cannot recover the original signal perfectly, but it can still provide valuable information about the frequency components present.

5. Are there any limitations or caveats to using the Fourier transform in Python?

One limitation of using the Fourier transform in Python is that it requires the signal to be evenly sampled. If there are missing data points or gaps in the signal, these can lead to inaccuracies in the Fourier transform. Additionally, the results of the Fourier transform may be affected by the choice of windowing function and the length of the signal used for the transform. It is important to consider these factors and choose appropriate parameters when using the Fourier transform in Python.

Similar threads

  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
5
Views
394
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Calculus and Beyond Homework Help
Replies
7
Views
2K
  • General Math
Replies
1
Views
3K
Back
Top