2.5 FFT Module API Manual#

1. Overview#

The FFT (Fast Fourier Transform) module is used to perform a Fourier transform on input time-domain data, converting it into frequency-domain data and returning the corresponding frequency magnitudes. Through FFT operations, time-domain signals can be effectively transformed into frequency-domain signals, facilitating the analysis of the signal’s frequency components.

2. API Introduction#

The FFT module provides an FFT class, supporting three main functions: run(), freq(), and amplitude(), which are used for performing Fast Fourier Transform, frequency calculation, and amplitude calculation, respectively.

2.1 Class machine.FFT#

Description

This class is used to create an FFT object and perform the Fourier transform on the input data.

Syntax

from machine import FFT
import array

# Define time-domain data
data = array.array('i', [1, 2, 3, 4, 5, 6, 7, 8])

# Create an FFT object, perform a 64-point FFT operation, with an offset of 0
fft1 = FFT(data, 64, 0)

Parameters

Parameter Name

Description

Type

Input/Output

data

Input time-domain data, of type bytearray.

Input

points

Number of points for the FFT operation, supports 64, 128, 256, 512, 1024, 2048, and 4096 points.

Input

shift

Data offset, default is 0.

Input

Return Values

Return Value

Description

0

Operation successful.

Non-zero

Operation failed.

2.1.1 run() Method#

Description

This function is used to obtain the frequency-domain data after the Fourier transform.

Syntax

res = fft1.run()

Parameters

None

Return Values

Return Value

Description

res

Returns a list containing the frequency-domain data, which includes points tuples, each containing 2 elements: the real part and the imaginary part.

2.1.2 freq() Method#

Description

This function is used to obtain the calculated frequency values.

Syntax

res = FFT.freq(points, sample_rate)

Parameters

Parameter Name

Description

Input/Output

points

Number of points involved in the FFT operation.

Input

sample_rate

Data sampling rate.

Input

Return Values

Return Value

Description

res

Returns a list containing the frequency values for each frequency point.

2.1.3 amplitude() Method#

Description

This function is used to calculate the amplitude of each frequency point. It is mainly used for testing purposes, and users can write more complex amplitude processing functions in Python.

Syntax

amp = FFT.amplitude(FFT_res)

Parameters

Parameter Name

Description

Input/Output

FFT_res

The FFT calculation result returned by the run() function.

Input

Return Values

Return Value

Description

amp

Returns a list containing the amplitude of each frequency point.

Comments list

Comments list

Comments
Log in