AppDividend
Latest Code Tutorials

# Numpy fft: How to Apply Fourier Transform in Python

The Fourier transform decomposes a function into its constituent frequencies. A particular case is the expression of a musical chord in terms of the volumes and frequencies of its constituent notes.

Fourier transform is an applied concept in the world of Science and Digital Signal Processing. FT(Fourier Transform) provides the frequency domain representation of the original signal.

For example, given the sinusoidal signal in the time domain, the Fourier Transform provides a constituent signal frequency.

Using the Fourier transform, periodic and non-periodic signals can be transformed from the time to the frequency domain. But, first, let’s see how to calculate Fourier transforms in Python.

## Numpy fft

Numpy fft.fft() is a function that computes the one-dimensional discrete Fourier Transform. The numpy fft.fft() method computes the one-dimensional discrete n-point discrete Fourier Transform (DFT) with the efficient Fast Fourier Transform (FFT) algorithm [CT].

If you have already installed numpy and scipy and want to create a simple FFT of the dataset, you can use the numpy fft.fft() function.

### Syntax

numpy.fft.fft(a, n=None, axis=-1, norm=None)

### Parameters

##### array_like

Input array can be complex.

##### n: int, optional

Length of a transformed axis of the output. If n is smaller than the input length, then the input is cropped. If it is larger, then the input is padded with zeros. If n is not given, then the input’s length along the axis specified by the axis is used.

##### axis: int, optional

Axis over which to compute the FFT. If not given, then the last axis is used.

##### norm: {None, “ortho”}, optional

New in version 1.10.0.

Normalization mode (see numpy.fft). Default is None.

## Example

Before writing any code, please install the following packages.

1. Matplotlib: python3 -m pip install -U matplotlib
2. numpy: python3 -m pip install -U numpy
3. scipy: python3 -m pip install -U scipy

Write the following code inside the app.py file.

# app.py

import matplotlib.pyplot as plt
import numpy as np
import scipy.fftpack

# Number of sample points
N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0//(2.0*T), N//2)

fig, ax = plt.subplots()
ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()


## Example 2

Write the following code inside the app.py file.

# app.py

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(256)
sp = np.fft.fft(np.sin(t))
freq = np.fft.fftfreq(t.shape[-1])
plt.plot(freq, sp.real, freq, sp.imag)
plt.show()


#### Output

In the above example, the real input has an FFT Hermitian. For example, symmetric in the real part and anti-symmetric in the imaginary part, as described in the numpy.fft documentation.

## Conclusion

The Fast Fourier Transform (FFT) is one of the most important signal processing and data analysis algorithms. The FFT is a fast, Ο[NlogN] algorithm to compute the Discrete Fourier Transform (DFT), which naively is an Ο[N^2] computation. The DFT, like the more familiar continuous version of the Fourier transform, has a forward and inverse form.

That’s it for this example.

This site uses Akismet to reduce spam. Learn how your comment data is processed.