嵌入式算法-傅里叶变换算法

摘要:傅里叶变换的核心在于,“任何连续周期信号可以由一组适当的正弦曲线组合而成”,在这个基础上对信号的中特定频率的正弦波进行分解或者重组,基于频率方面分析波形。

1、傅里叶变换的意义

近似周期性的方波(橙色),可采用6组正弦波(蓝色)合成,这是傅里叶的基础。6a328d5d0cc0f95e0ab1d06edade3c52.png对数字信号处理或者工程数学有一定基础,就明白傅里叶变换的价值。一般情况下的信号或者波形随时间变化,称为时域信号,时域(Time domain)是描述数学函数或物理信号对时间的关系。而以频率和幅度表示信号称为频域,频域(frequency domain)是描述信号在频率方面特性时用到的一种坐标系。其数学理论上暂且不理,针对嵌入式系统开发,只探讨其物理意义或者应用场景。

时域和频域是信号的基本性质,时域的表示较为形象与直观,比较符合一般认知,而频域分析则更为简练,剖析问题更为深刻和方便。

例如下图的左侧时域信号,其可以分解为2路正弦波的叠加效果,右侧为频域信号,表示2路正弦波的频率和幅度。傅里叶变化可简单理解为求解一段信号或波形,由哪些正弦波组成,也可以反向推导多路正弦波合并后的效果。b6e30ea6bb4c703c0b797b873bd59df6.png基于动画形式表现如下:56ba6f3abf59f73d79e7d93c499c6e61.gif

傅里叶变换是一种信号分析方法,让我们对信号的构成和特点进行深入的、定量的研究。把信号通过频谱的方式进行准确的、定量的描述。将原来难以处理的时域信号转换成了易于分析的频域信号,即傅里叶变换的核心是从时域到频域的变换。

2、变换方式

数字信号属于离散值,对应的称为离散傅里叶变换(DFT),是傅里叶变换在时域和频域上都呈现离散的形式,将时域信号的采样变换为在离散时间傅里叶变换(DTFT)频域的采样。在形式上,变换两端(时域和频域上)的序列是有限长的,而实际上这两组序列都应当被认为是离散周期信号的主值序列。即使对有限长的离散信号作DFT,也应当将其看作经过周期延拓成为周期信号再作变换。

在实际应用中通常采用快速傅里叶变换以高效计算效率,快速傅里叶变换 FFT(Fast Fourier Transformation)是离散傅里叶变换 DFT(Discrete Fourier Transform)的快速算法。采用这种算法能使计算机计算离散傅里叶变换所需要的乘法次数大为减少,特别是被变换的抽样点数N越多,FFT算法计算量的节省就越显著。同理,从频域到时域的变换,称为逆变换,快速傅里叶逆变换 IFFT和离散傅里叶逆变换IDFT。

3、应用

一般嵌入式系统使用快速傅里叶变换是分析某段信号中混合的杂波干扰,或者剔除某个频段后再逆变换。有些高端示波器可以对信号快速傅里叶变换 FFT,直接显示高频杂波的频率或者较大幅度干扰源的频率,以便由针对性的检查电路窜扰。本文主要是讲解使用fft/ifft进行干扰信号的过滤。

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

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

3、对数据进行简单过滤

4、过滤后的数据进行IFFT运算,再导入Excel看还原的效果 

5、关注微信公众号  嵌入式系统

3.1 基于python生成数据源

# 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个点,每个点间隔为 TsTs = 1/fsn = t / Tsn = np.arange(n)y = A*np.sin(2*np.pi*f*n*Ts + phi*(np.pi/180))return yfs = 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')

生成的波形图如下:56468428e1d42d191f149b466cb01bd5.png三种频率的sin以及合成后的红色曲线,变形的正弦波;同时也将数据存入文件sin.txt备用。

3.2 基于C验证算法

将前面生成的数据使用c语言的FFT/IFFT进行处理,输出结果,并导入Excels生成图表展示效果。

#include <math.h>
#include <stdio.h>
#include "string.h"typedef struct
{float real;float imag;
} complex_t;#ifndef PI
#define PI             (3.14159265)
#endif#define TYPE_FFT_E     float    /* Type is the same with complex_t member */typedef complex_t TYPE_FFT;  /* Define complex_t in Config.h */
typedef unsigned int                      uint32_t;#define     SAMPLE_NODES    (1024)
complex_t   fft_buff[SAMPLE_NODES];//python生成的3个sin混合的波形数组
float my_sin_wave_table[] =
{0.000000,13.308217,25.359460,35.142296,42.082633,46.140080,47.788611,47.887148,47.470293,47.507139,48.682920,51.252751,\55.000000,59.307883,63.326941,66.199152,67.286436,66.350198,63.639610,59.866963,56.074110,53.418823,52.928233,55.274323,\60.621778,68.582540,78.287693,88.561119,98.156743,106.007043,111.428222,114.237346,114.756729,113.706291,112.009861,\110.560634,110.000000,110.560634,112.009861,113.706291,114.756729,114.237346,111.428222,106.007043,98.156743,88.561119,\78.287693,68.582540,60.621778,55.274323,52.928233,53.418823,56.074110,59.866963,63.639610,66.350198,67.286436,66.199152,\63.326941,59.307883,55.000000,51.252751,48.682920,47.507139,47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,\25.359460,13.308217,0.000000,-13.308217,-25.359460,-35.142296,-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,\-47.507139,-48.682920,-51.252751,-55.000000,-59.307883,-63.326941,-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,\-56.074110,-53.418823,-52.928233,-55.274323,-60.621778,-68.582540,-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,\-114.237346,-114.756729,-113.706291,-112.009861,-110.560634,-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,\-114.237346,-111.428222,-106.007043,-98.156743,-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,\-53.418823,-56.074110,-59.866963,-63.639610,-66.350198,-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,\-51.252751,-48.682920,-47.507139,-47.470293,-47.887148,-47.788611,-46.140080,-42.082633,-35.142296,-25.359460,\-13.308217,-0.000000,13.308217,25.359460,35.142296,42.082633,46.140080,47.788611,47.887148,47.470293,47.507139,\48.682920,51.252751,55.000000,59.307883,63.326941,66.199152,67.286436,66.350198,63.639610,59.866963,56.074110,\53.418823,52.928233,55.274323,60.621778,68.582540,78.287693,88.561119,98.156743,106.007043,111.428222,114.237346,\114.756729,113.706291,112.009861,110.560634,110.000000,110.560634,112.009861,113.706291,114.756729,114.237346,\111.428222,106.007043,98.156743,88.561119,78.287693,68.582540,60.621778,55.274323,52.928233,53.418823,56.074110,\59.866963,63.639610,66.350198,67.286436,66.199152,63.326941,59.307883,55.000000,51.252751,48.682920,47.507139,\47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,25.359460,13.308217,0.000000,-13.308217,-25.359460,\-35.142296,-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,-47.507139,-48.682920,-51.252751,-55.000000,\-59.307883,-63.326941,-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,-56.074110,-53.418823,-52.928233,\-55.274323,-60.621778,-68.582540,-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,-114.237346,-114.756729,\-113.706291,-112.009861,-110.560634,-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,-114.237346,-111.428222,\-106.007043,-98.156743,-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,-53.418823,-56.074110,-59.866963,\-63.639610,-66.350198,-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,-51.252751,-48.682920,-47.507139,-47.470293,\-47.887148,-47.788611,-46.140080,-42.082633,-35.142296,-25.359460,-13.308217,-0.000000,13.308217,25.359460,35.142296,\42.082633,46.140080,47.788611,47.887148,47.470293,47.507139,48.682920,51.252751,55.000000,59.307883,63.326941,66.199152,\67.286436,66.350198,63.639610,59.866963,56.074110,53.418823,52.928233,55.274323,60.621778,68.582540,78.287693,88.561119,\98.156743,106.007043,111.428222,114.237346,114.756729,113.706291,112.009861,110.560634,110.000000,110.560634,112.009861,\113.706291,114.756729,114.237346,111.428222,106.007043,98.156743,88.561119,78.287693,68.582540,60.621778,55.274323,\52.928233,53.418823,56.074110,59.866963,63.639610,66.350198,67.286436,66.199152,63.326941,59.307883,55.000000,51.252751,\48.682920,47.507139,47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,25.359460,13.308217,0.000000,-13.308217,\-25.359460,-35.142296,-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,-47.507139,-48.682920,-51.252751,-55.000000,\-59.307883,-63.326941,-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,-56.074110,-53.418823,-52.928233,-55.274323,\-60.621778,-68.582540,-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,-114.237346,-114.756729,-113.706291,\-112.009861,-110.560634,-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,-114.237346,-111.428222,-106.007043,\-98.156743,-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,-53.418823,-56.074110,-59.866963,-63.639610,\-66.350198,-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,-51.252751,-48.682920,-47.507139,-47.470293,-47.887148,\-47.788611,-46.140080,-42.082633,-35.142296,-25.359460,-13.308217,-0.000000,13.308217,25.359460,35.142296,42.082633,\46.140080,47.788611,47.887148,47.470293,47.507139,48.682920,51.252751,55.000000,59.307883,63.326941,66.199152,67.286436,\66.350198,63.639610,59.866963,56.074110,53.418823,52.928233,55.274323,60.621778,68.582540,78.287693,88.561119,98.156743,\106.007043,111.428222,114.237346,114.756729,113.706291,112.009861,110.560634,110.000000,110.560634,112.009861,113.706291,\114.756729,114.237346,111.428222,106.007043,98.156743,88.561119,78.287693,68.582540,60.621778,55.274323,52.928233,53.418823,\56.074110,59.866963,63.639610,66.350198,67.286436,66.199152,63.326941,59.307883,55.000000,51.252751,48.682920,47.507139,\47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,25.359460,13.308217,0.000000,-13.308217,-25.359460,-35.142296,\-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,-47.507139,-48.682920,-51.252751,-55.000000,-59.307883,-63.326941,\-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,-56.074110,-53.418823,-52.928233,-55.274323,-60.621778,-68.582540,\-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,-114.237346,-114.756729,-113.706291,-112.009861,-110.560634,\-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,-114.237346,-111.428222,-106.007043,-98.156743,-88.561119,\-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,-53.418823,-56.074110,-59.866963,-63.639610,-66.350198,-67.286436,\-66.199152,-63.326941,-59.307883,-55.000000,-51.252751,-48.682920,-47.507139,-47.470293,-47.887148,-47.788611,-46.140080,\-42.082633,-35.142296,-25.359460,-13.308217,-0.000000,13.308217,25.359460,35.142296,42.082633,46.140080,47.788611,47.887148,\47.470293,47.507139,48.682920,51.252751,55.000000,59.307883,63.326941,66.199152,67.286436,66.350198,63.639610,59.866963,\56.074110,53.418823,52.928233,55.274323,60.621778,68.582540,78.287693,88.561119,98.156743,106.007043,111.428222,114.237346,\114.756729,113.706291,112.009861,110.560634,110.000000,110.560634,112.009861,113.706291,114.756729,114.237346,111.428222,\106.007043,98.156743,88.561119,78.287693,68.582540,60.621778,55.274323,52.928233,53.418823,56.074110,59.866963,63.639610,\66.350198,67.286436,66.199152,63.326941,59.307883,55.000000,51.252751,48.682920,47.507139,47.470293,47.887148,47.788611,\46.140080,42.082633,35.142296,25.359460,13.308217,0.000000,-13.308217,-25.359460,-35.142296,-42.082633,-46.140080,-47.788611,\-47.887148,-47.470293,-47.507139,-48.682920,-51.252751,-55.000000,-59.307883,-63.326941,-66.199152,-67.286436,-66.350198,\-63.639610,-59.866963,-56.074110,-53.418823,-52.928233,-55.274323,-60.621778,-68.582540,-78.287693,-88.561119,-98.156743,\-106.007043,-111.428222,-114.237346,-114.756729,-113.706291,-112.009861,-110.560634,-110.000000,-110.560634,-112.009861,\-113.706291,-114.756729,-114.237346,-111.428222,-106.007043,-98.156743,-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,\-52.928233,-53.418823,-56.074110,-59.866963,-63.639610,-66.350198,-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,\-51.252751,-48.682920,-47.507139,-47.470293,-47.887148,-47.788611,-46.140080,-42.082633,-35.142296,-25.359460,-13.308217,\-0.000000,13.308217,25.359460,35.142296,42.082633,46.140080,47.788611,47.887148,47.470293,47.507139,48.682920,51.252751,\55.000000,59.307883,63.326941,66.199152,67.286436,66.350198,63.639610,59.866963,56.074110,53.418823,52.928233,55.274323,\60.621778,68.582540,78.287693,88.561119,98.156743,106.007043,111.428222,114.237346,114.756729,113.706291,112.009861,\110.560634,110.000000,110.560634,112.009861,113.706291,114.756729,114.237346,111.428222,106.007043,98.156743,88.561119,\78.287693,68.582540,60.621778,55.274323,52.928233,53.418823,56.074110,59.866963,63.639610,66.350198,67.286436,66.199152,\63.326941,59.307883,55.000000,51.252751,48.682920,47.507139,47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,\25.359460,13.308217,0.000000,-13.308217,-25.359460,-35.142296,-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,\-47.507139,-48.682920,-51.252751,-55.000000,-59.307883,-63.326941,-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,\-56.074110,-53.418823,-52.928233,-55.274323,-60.621778,-68.582540,-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,\-114.237346,-114.756729,-113.706291,-112.009861,-110.560634,-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,\-114.237346,-111.428222,-106.007043,-98.156743,-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,-53.418823,\-56.074110,-59.866963,-63.639610,-66.350198,-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,-51.252751,-48.682920,\-47.507139,-47.470293,-47.887148,-47.788611,-46.140080,-42.082633,-35.142296,-25.359460,-13.308217,-0.000000,13.308217,\25.359460,35.142296,42.082633,46.140080,47.788611,47.887148,47.470293,47.507139,48.682920,51.252751,55.000000,59.307883,\63.326941,66.199152,67.286436,66.350198,63.639610,59.866963,56.074110,53.418823,52.928233,55.274323,60.621778,68.582540,\78.287693,88.561119,98.156743,106.007043,111.428222,114.237346,114.756729,113.706291,112.009861,110.560634,110.000000,\110.560634,112.009861,113.706291,114.756729,114.237346,111.428222,106.007043,98.156743,88.561119,78.287693,68.582540,\60.621778,55.274323,52.928233,53.418823,56.074110,59.866963,63.639610,66.350198,67.286436,66.199152,63.326941,59.307883,\55.000000,51.252751,48.682920,47.507139,47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,25.359460,13.308217,\0.000000,-13.308217,-25.359460,-35.142296,-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,-47.507139,-48.682920,\-51.252751,-55.000000,-59.307883,-63.326941,-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,-56.074110,-53.418823,\-52.928233,-55.274323,-60.621778,-68.582540,-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,-114.237346,-114.756729,\-113.706291,-112.009861,-110.560634,-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,-114.237346,-111.428222,\-106.007043,-98.156743,-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,-53.418823,-56.074110,-59.866963,\-63.639610,-66.350198,-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,-51.252751,-48.682920,-47.507139,-47.470293,\-47.887148,-47.788611,-46.140080,-42.082633,-35.142296,-25.359460,-13.308217,-0.000000,13.308217,25.359460,35.142296,\42.082633,46.140080,47.788611,47.887148,47.470293,47.507139,48.682920,51.252751,55.000000,59.307883,63.326941,66.199152,\67.286436,66.350198,63.639610,59.866963,56.074110,53.418823,52.928233,55.274323,60.621778,68.582540,78.287693,88.561119,\98.156743,106.007043,111.428222,114.237346,114.756729,113.706291,112.009861,110.560634,110.000000,110.560634,112.009861,\113.706291,114.756729,114.237346,111.428222,106.007043,98.156743,88.561119,78.287693,68.582540,60.621778,55.274323,52.928233,\53.418823,56.074110,59.866963,63.639610,66.350198,67.286436,66.199152,63.326941,59.307883,55.000000,51.252751,48.682920,\47.507139,47.470293,47.887148,47.788611,46.140080,42.082633,35.142296,25.359460,13.308217,0.000000,-13.308217,-25.359460,\-35.142296,-42.082633,-46.140080,-47.788611,-47.887148,-47.470293,-47.507139,-48.682920,-51.252751,-55.000000,-59.307883,\-63.326941,-66.199152,-67.286436,-66.350198,-63.639610,-59.866963,-56.074110,-53.418823,-52.928233,-55.274323,-60.621778,\-68.582540,-78.287693,-88.561119,-98.156743,-106.007043,-111.428222,-114.237346,-114.756729,-113.706291,-112.009861,\-110.560634,-110.000000,-110.560634,-112.009861,-113.706291,-114.756729,-114.237346,-111.428222,-106.007043,-98.156743,\-88.561119,-78.287693,-68.582540,-60.621778,-55.274323,-52.928233,-53.418823,-56.074110,-59.866963,-63.639610,-66.350198,\-67.286436,-66.199152,-63.326941,-59.307883,-55.000000,-51.252751,-48.682920,-47.507139,-47.470293,-47.887148,-47.788611,\-46.140080,-42.082633,-35.142296,-25.359460,-13.308217,
};//fft算法来自开源 https://github.com/xiahouzuoxin/fftconst float sin_tb[] =    // 精度(PI PI/2 PI/4 PI/8 PI/16 ... PI/(2^k))
{0.000000, 1.000000, 0.707107, 0.382683, 0.195090, 0.098017,0.049068, 0.024541, 0.012272, 0.006136, 0.003068, 0.001534,0.000767, 0.000383, 0.000192, 0.000096, 0.000048, 0.000024,0.000012, 0.000006, 0.000003};const float cos_tb[] =    // 精度(PI PI/2 PI/4 PI/8 PI/16 ... PI/(2^k))
{-1.000000, 0.000000, 0.707107, 0.923880, 0.980785, 0.995185,0.998795, 0.999699, 0.999925, 0.999981, 0.999995, 0.999999,1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000,1.000000, 1.000000, 1.000000};int ones_32(uint32_t n)
{unsigned int c = 0 ;for(c = 0; n; ++c){n &= (n - 1) ;}return c ;
}uint32_t floor_log2_32(uint32_t fft_buff)
{fft_buff |= (fft_buff >> 1);fft_buff |= (fft_buff >> 2);fft_buff |= (fft_buff >> 4);fft_buff |= (fft_buff >> 8);fft_buff |= (fft_buff >> 16);return (ones_32(fft_buff >> 1));
}/** FFT Algorithm* === Inputs ===* fft_buff : complex numbers* N : nodes of FFT. @N should be power of 2, that is 2^(*)* === Output ===* the @fft_buff contains the result of FFT algorithm, so the original data* in @fft_buff is destroyed, please store them before using FFT.*/
int fft(TYPE_FFT *fft_buff, uint32_t N)
{int i, j, l, k, ip;static uint32_t M = 0;static int le, le2;static TYPE_FFT_E sR, sI, tR, tI, uR, uI;M = floor_log2_32(N);/** bit reversal sorting*/l = N >> 1;j = l;ip = N - 2;for(i = 1; i <= ip; i++){if(i < j){tR = fft_buff[j].real;tI = fft_buff[j].imag;fft_buff[j].real = fft_buff[i].real;fft_buff[j].imag = fft_buff[i].imag;fft_buff[i].real = tR;fft_buff[i].imag = tI;}k = l;while(k <= j){j = j - k;k = k >> 1;}j = j + k;}/** For Loops*/for(l = 1; l <= M; l++)  /* loop for ceil{log2(N)} */{le  = (int)(1 << l);le2 = (int)(le >> 1);uR = 1;uI = 0;k = floor_log2_32(le2);sR = cos_tb[k];sI = -sin_tb[k];for(j = 1; j <= le2; j++)  /* loop for each sub DFT */{for(i = j - 1; i < N; i += le) /* loop for each butterfly */{ip = i + le2;tR = fft_buff[ip].real * uR - fft_buff[ip].imag * uI;tI = fft_buff[ip].real * uI + fft_buff[ip].imag * uR;fft_buff[ip].real = fft_buff[i].real - tR;fft_buff[ip].imag = fft_buff[i].imag - tI;fft_buff[i].real += tR;fft_buff[i].imag += tI;}  /* Next i */tR = uR;uR = tR * sR - uI * sI;uI = tR * sI + uI * sR;} /* Next j */} /* Next l */return 0;
}/** Inverse FFT Algorithm* === Inputs ===* fft_buff : complex numbers* N : nodes of FFT. @N should be power of 2, that is 2^(*)* === Output ===* the @fft_buff contains the result of FFT algorithm, so the original data* in @fft_buff is destroyed, please store them before using FFT.*/int ifft(TYPE_FFT *fft_buff, uint32_t N)
{int k = 0;for(k = 0; k <= N - 1; k++){fft_buff[k].imag = -fft_buff[k].imag;}fft(fft_buff, N);    /* using FFT */for(k = 0; k <= N - 1; k++){fft_buff[k].real = fft_buff[k].real / N;fft_buff[k].imag = -fft_buff[k].imag / N;}return 0;
}static void import_data(void)
{int i;for(i = 0; i < SAMPLE_NODES; i++){fft_buff[i].real = my_sin_wave_table[i];//取前1024个数进行fft变换fft_buff[i].imag  = 0.0f;}
}int main(int argc, char *argv[])
{int i;int f;//频率float a;//幅度int fd;float t;printf("FFT\r\n");import_data();fft(fft_buff, SAMPLE_NODES);//fft后的结果在fft_buff//将其实部与虚部处理,输出频点与幅度值,导入Excel看效果//因为是周期性质,取前半部分即可//数据采样频率是360*40,均分到SAMPLE_NODES,则对应频点间隔是 360*40/SAMPLE_NODESfd=360*40/SAMPLE_NODES;for(i = 0; i < SAMPLE_NODES / 2; i++){f = i *fd;a = (double)sqrt(fft_buff[i].real * fft_buff[i].real + fft_buff[i].imag * fft_buff[i].imag)/ (SAMPLE_NODES / 2);//转换幅度//printf("%d,%f\n", f, a);//>>导入excel查看幅频图效果}//过滤高频部分//将幅度小于某个值的,以30为例过滤for(i = 0; i < SAMPLE_NODES; i++){a = (double)sqrt(fft_buff[i].real * fft_buff[i].real + fft_buff[i].imag * fft_buff[i].imag)/ (SAMPLE_NODES / 2);if(a<30){fft_buff[i].real = 0;fft_buff[i].imag = 0;}}//再进行逆运算还原ifft(fft_buff, SAMPLE_NODES);for(i = 0; i < SAMPLE_NODES; i++){t=1.0/(360*40)*i;//结合采样频率步进,方便查看波形效果//printf("%f,%f\n", t,fft_buff[i].real);//>>导入excel查看还原后的sin效果}return 0;
}

3.3 幅频图

混合波形使用FFT后的幅频图:7b62d598e761e1c6261254937c197e11.png看图可知对应的频点和幅度,与原始混合波形的3种正弦波吻合。

栅栏效应:其中频点与采样频率和FFT转换的采样数有关,需要结合实际定义,因为数字离散信号,频点并不是连续的,如上大概相差14,因为数据点数N为1024点,原始信号的采样频率为fs=14400Hz,则频率间隔fs/N约等于14Hz。这样看频谱图只有fs/N倍数的,例如没有410Hz,只能看到相邻频率406Hz和420Hz处频谱泄漏的数据,相当于透过栅栏观赏风景,只能看到频谱的一部分,而其它频率点看不见,因此很可能部分有用的频率成分被漏掉,这种现象被称为栅栏效应。

3.4 逆变换还原

FFT后的幅频数据过滤其中2个频点的小幅度波形,还原后的效果如下图:3c453f9affd3445b0831e145cb91ae7a.png过滤还原后的波形,与原始图,前面python生成的黑色虚线吻合。

4、应用

傅里叶变换在数字信号领域中应用广泛,在嵌入式系统中,一般用来分析ADC高速采集的数据、滤波处理,或者对音频进行简单分析。但傅里叶变换算法内存消耗有点大,低端设备可能无法运行。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/464375.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Server 2008 R2 AD RMS完整部署:AD部署篇

在上一篇里我们完成了基本环境的准备&#xff0c;这一篇主要是讲AD的部署&#xff0c;完成之后我们需要把RMS服务器和两个客户端都加入到域中。好了废话就不说了&#xff0c;首先在我们创建的DC虚机上登录&#xff0c;并对计算机名称进行修改。 完成后先不要忙着重启&#xff0…

2018-2019-1 20165219 实验三 实时系统

2018-2019-1 20165219 实验三 实时系统 任务一 实验要求 学习使用Linux命令wc(1) 基于Linux Socket程序设计实现wc&#xff08;1&#xff09;服务器(端口号是你学号的后6位)和客户端 客户端传一个文本文件给服务器 服务器返加文本文件中的单词数 客户端 #include<netinet/in…

音视频基础知识大全

今天跟几个朋友聊天&#xff0c;他们问我是不是在腾讯&#xff0c;在腾讯做什么&#xff1f;然后我就说了我做的事情&#xff0c;我说我在腾讯做音频方面的开发&#xff0c;对外的产品主要是腾讯会议。然后他我一个朋友就截图了他的腾讯会议的软件截图过来。音视频是一个很好玩…

详解Android Touch事件的传递机制

1、基础知识 (1) 所有Touch事件都被封装成了MotionEvent对象&#xff0c;包括Touch的位置、时间、历史记录以及第几个手指(多指触摸)等。 (2) 事件类型分为ACTION_DOWN, ACTION_UP, ACTION_MOVE, ACTION_POINTER_DOWN, ACTION_POINTER_UP, ACTION_CANCEL&#xff0c;每个事件都…

项目专访|在ESP32上移植Linux

大家好&#xff0c;我是写代码的篮球球痴&#xff0c;最近在网上看到一个非常有意思的项目&#xff0c;有大神把Linux内核移植到ESP32上&#xff0c;并成功运行。在国外网站上&#xff0c;有很多人在议论这件事情https://hackaday.com/2021/07/21/its-linux-but-on-an-esp32/ES…

4.3英寸屏双核 LG Prada K2通过FCC认证

我们此前曝光过一款型号为LG Prada K2的智能手机&#xff0c;这款手机不仅配置出色而且外观极为时尚。如今&#xff0c;来自国外媒体的最新消息显示&#xff0c;LG Prada K2已经通过FCC认证&#xff0c;这意味着它距离上市已经为期不远。 LG Prada K2通过FCC认证 LG Prada K2是…

国内MCU厂商也开始卷了

最近我大学老师准备做一个项目&#xff0c;需要用到STM32的芯片&#xff0c;然后我就帮忙着一起找&#xff0c;不找不知道&#xff0c;找了才知道这个东西真的是贵。然后我就看了GD32&#xff0c;不了解不知道&#xff0c;这个东西也是真的贵。然后我又去看的MM32&#xff0c;结…

Google Flash转换HTML5工具Swiffy测试版发布

Google今年六月推出的Flash转换HTML5工具已经从实验室转为测试版&#xff0c;并新增一个Flash Professional扩充模组&#xff0c;可以直接将档案储存为HTML5格式。一开始Swiffy仅以网站方式&#xff0c;让使用者上传Flash的.swf档案&#xff0c;支持Flash所使用的swf 8格式与Ac…

什么是MCU里应尽量遵循的寄存器谨慎赋值法?

有人觉得搞MCU是一件简单的事情&#xff0c;往往觉得简单的事反而出错更多&#xff0c;今天转发一篇我朋友痞子衡的文章&#xff0c;说说MCU里面的寄存器的事。大家好&#xff0c;我是痞子衡&#xff0c;是正经搞技术的痞子。今天痞子衡给大家介绍的是改动i.MXRT1xxx里IOMUXC_G…

惠普电脑如何下载c语言软件,惠普笔记本电脑上的语言栏怎么弄

满意答案mygood12f32013.01.23采纳率&#xff1a;53% 等级&#xff1a;11已帮助&#xff1a;6723人首先运行“regedit”&#xff0c;展开“HKEY_CURRENT_USER/keyboard layout/preload”分支&#xff0c;在其下新建一个字符串值&#xff0c;命名为“1”&#xff0c;并将它对…

单件模式(Singleton Pattern)

1、单件模式试用与窗口管理器或者是打印假脱机、数据库连接池&#xff1b;2、目的&#xff1a;在单独得对象实例中&#xff0c;集中了这个对象所属类得所有权利&#xff1b;3、DoNet解决方案 public class DotNetSigleton { private static readonly DotNetSiglet…

LeetCode:二进制手表【401】

LeetCode&#xff1a;二进制手表【401】 题目描述 二进制手表顶部有 4 个 LED 代表小时&#xff08;0-11&#xff09;&#xff0c;底部的 6 个 LED 代表分钟&#xff08;0-59&#xff09;。 每个 LED 代表一个 0 或 1&#xff0c;最低位在右侧。 例如&#xff0c;上面的二进制手…

反馈电路中相位补偿,到底是什么鬼?

帮朋友做镍氢充电器&#xff0c;利用镍氢电池充满电时电压有一个微小的下降这个特点来识别是否已经充满&#xff0c;比如1.2V的镍氢电池&#xff0c;快充满的时候&#xff0c;电压在1.35V&#xff0c;之后逐步下降&#xff0c;电压可以低于1.30V。所以需要单片机间歇检测电池两…

RoseHA集群:RHEL+RoseMirror+Oracle【2】

博主正在参加“2011年度IT博客大赛”&#xff0c;欢迎帮顶&#xff01; 投票地址 --------------------------------------------- 三&#xff0e;安装Rose Mirror HA&#xff1a;准备工作&#xff1a;在安装 Rose Mirror HA 前检查相应的软件包是否安装&#xff0c;如果没有安…

c语言笔试面试面试题,最新华为c语言笔试面试题

最新华为c语言笔试面试题本文是百分网小编搜索整理的一份最新华为c语言笔试面试题&#xff0c;有需要的朋友们一起看看吧!想了解更多相关信息请持续关注我们应届毕业生考试网!1、局部变量能否和全局变量重名答&#xff1a;能&#xff0c;局部会屏蔽全局。要用全局变量&#xff…

【项目管理】CMMI-需求跟踪矩阵模版

需求菜单/功能模块需求名称需求变更类型&#xff08;新增、修改&#xff09;需求状态&#xff08;已建议、已批准、已设计、已实现、已验证、已删除&#xff09;优先级&#xff08;高、中、低&#xff09;软件需求&#xff08;工作产品、章节号&#xff09;概要设计&#xff08…

嵌入式工程师薪资调查

大家好&#xff0c;嵌入式行业薪资一直是大家关心的问题&#xff0c;刚好我的好朋友发起了一个调查&#xff0c;大家可以匿名写下自己的薪资&#xff0c;也可以看看其他的薪资水平。3月4月份确实是找工作的好机会&#xff0c;求职和跳槽最好的月份要来了吗 &#xff0c;最近看到…

在STM32单片机上跑神经网络算法

摘要&#xff1a;为什么可以在STM32上面跑神经网络&#xff1f;简而言之就是使用STM32CubeMX中的X-Cube-AI扩展包将当前比较热门的AI框架进行C代码的转化&#xff0c;以支持在嵌入式设备上使用&#xff0c;目前使用X-Cube-AI需要在STM32CubeMX版本5.0以上&#xff0c;支持转化的…

CF Edu54 E. Vasya and a Tree DFS+树状数组

Vasya and a Tree 题意&#xff1a; 给定一棵树&#xff0c;对树有3e5的操作&#xff0c;每次操作为&#xff0c;把树上某个节点的不超过d的子节点都加上值x; 思路&#xff1a; 多开一个vector记录每个点上的操作。dfs这颗树&#xff0c;同时以深度开一个树状数组&#xff0c;踩…

通信教程 | 串口丢数据常见的原因

UART&#xff1a;Universal Asynchronous Receiver / Transmitter&#xff0c;通用异步收发传输器&#xff0c;即我们通常说的串口。串口是工程师最常用的串行外设之一&#xff0c;但在实际应用中还是会经常遇到各种问题。比如&#xff1a;丢失一字节数据。今天我们就结合STM32…