Embedded algorithm - Fourier transform algorithm

Article citation

https://mp.weixin.qq.com/s/5VIpNWci9YLY3m4gcYd6-w
https://blog.csdn.net/jason_sssss/article/details/118933988

Summary

The core of Fourier transform is that "any continuous periodic signal can be composed of a set of appropriate sinusoids." On this basis, the sine waves of specific frequencies in the signal are decomposed or reorganized, and the waveform is analyzed based on the frequency.

1. The meaning of Fourier transform

The approximately periodic square wave (red) can be synthesized using 6 groups of sine waves (blue), which is the basis of Fourier.
Insert image description here
If you have a certain foundation in digital signal processing or engineering mathematics, you will understand the value of Fourier transform. In general, signals or waveforms that change with time are called time domain signals. Time domain describes the relationship between mathematical functions or physical signals to time. Representing a signal in terms of frequency and amplitude is called the frequency domain. The frequency domain is a coordinate system used to describe the frequency characteristics of a signal. Its mathematical theory will be ignored for the time being, and only its physical meaning or application scenarios will be discussed for embedded system development.

The time domain and frequency domain are the basic properties of signals. The time domain representation is more vivid and intuitive, and is more in line with general cognition, while the frequency domain analysis is more concise, and the analysis of problems is more profound and convenient.

For example, the time domain signal on the left side of the figure below can be decomposed into the superposition effect of 2 sine waves, and the frequency domain signal on the right side represents the frequency and amplitude of the 2 sine waves. The Fourier transform can be simply understood as finding out which sine waves a segment of signal or waveform consists of. It can also be used to derive the effect of combining multiple sine waves in reverse.
Insert image description here
Based on the animation form, the performance is as follows:
Insert image description here
Fourier transform is a signal analysis method that allows us to conduct in-depth and quantitative research on the composition and characteristics of the signal. . Accurately and quantitatively describe the signal through spectrum. The original difficult-to-process time domain signal is converted into an easy-to-analyze frequency domain signal, that is, the core of the Fourier transform is the transformation from the time domain to the frequency domain.

2. Principle

Digital signals are discrete values, corresponding to the discrete Fourier transform (DFT). The Fourier transform takes a discrete form in both the time domain and the frequency domain. It transforms the sampling of the time domain signal into a discrete time Fourier transform. Leaf transform (DTFT) frequency domain sampling. Formally, the sequences at both ends of the transform (in the time domain and frequency domain) are finitely long, but in fact both sets of sequences should be considered as main value sequences of discrete periodic signals. Even if DFT is performed on a finite-length discrete signal, it should be regarded as a periodic signal after periodic extension and then transformed.

In practical applications, Fast Fourier Transform is usually used to achieve high computational efficiency. Fast Fourier Transform (Fast Fourier Transformation) is a fast algorithm of Discrete Fourier Transform (DFT). Using this algorithm can greatly reduce the number of multiplications required by the computer to calculate the discrete Fourier transform. In particular, the more sampling points N are transformed, the more significant the calculation savings of the FFT algorithm will be. In the same way, the transformation from frequency domain to time domain is called inverse transform, inverse fast Fourier transform IFFT and inverse discrete Fourier transform IDFT.

(1) Optimization principle of fast Fourier transform (FFT)

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

(2) Derivation of base-2FFT algorithm

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

(3) Implementation of base-2FFT algorithm

Insert image description here

3. Application

Generally, fast Fourier transform is used in embedded systems to analyze the mixed clutter interference in a certain signal segment, or to eliminate a certain frequency band and then inversely transform it. Some high-end oscilloscopes can perform fast Fourier transform (FFT) on the signal and directly display the frequency of high-frequency clutter or the frequency of larger-amplitude interference sources, so that circuit interference can be checked in a targeted manner. This article mainly explains the use of fft/ifft to filter interference signals.

(1) Generate data source based on python

1、python生成正弦波,以及混合波形,提取数值作为c语言FFT/IFFT的数据源 

2、先进行FFT,输出幅频数据,导入Excel看看效果 

3、对数据进行简单过滤

4、过滤后的数据进行IFFT运算,再导入Excel看还原的效果 
# This is a Python script.

import numpy as np
import matplotlib.pyplot as plt
def sin_wave(A, f, fs, phi, t):
    """
    :params A:    振幅
    :params f:    信号频率
    :params fs:   采样频率
    :params phi:  相位
    :params t:    时间长度
    # 若时间序列长度为 t=1s,
    # 采样频率 fs=1000 Hz, 则采样时间间隔 Ts=1/fs=0.001s
    # 采样点个数为 n=t/Ts=1/0.001=1000, 即有1000个点,每个点间隔为 Ts
    """
    Ts = 1/fs
    n = t / Ts
    n = np.arange(n)   #生成一个包含从0到n-1的整数的数组,表示时间序列的索引。
    y = A*np.sin(2*np.pi*f*n*Ts + phi*(np.pi/180))  #将角度值phi转换为弧度值的
    return y

fs = 360*40
my_sin1 = sin_wave(100, 100, fs=fs, phi=0, t=0.08)
my_sin2 = sin_wave(20, 500, fs=fs, phi=0, t=0.08)
my_sin3 = sin_wave(10, 1100, fs=fs, phi=0, t=0.08)

x = np.arange(0, 0.08, 1/fs)
plt.xlabel('x-t (samRate=14400)')
plt.ylabel('y-A')
plt.grid()      #用于在绘图时添加网格线
plt.plot(x, my_sin1, 'k--',label="f=100")
plt.plot(x, my_sin2, 'b--',label="f=500")
plt.plot(x, my_sin3, 'g--',label="f=1100")
plt.plot(x, my_sin1+my_sin2+my_sin3, 'r',label="mix")
plt.legend()   #用于在图表中添加图例
plt.show()

np.savetxt("sin.txt",my_sin1+my_sin2+my_sin3, fmt='%.06f')  #用于将数组数据保存到文本文件

def print_name(name):
    print(f'Hi, {
      
      name}')  


if __name__ == '__main__':
    print_name('fft test')

The generated waveform is as follows:
Insert image description here

(2) Based on C verification algorithm

Guess you like

Origin blog.csdn.net/wdxabc1/article/details/134003823
Recommended