傅里叶变换Python实现

傅里叶变换(FT)[1]

傅里叶变换的目的是可将时域(即时间域)上的信号转变为频域(即频率域)上的信号,随着域的不同,对同一个事物的了解角度也就随之改变,因此在时域中某些不好处理的地方,在频域就可以较为简单的处理。

傅里叶变换公式:


傅里叶变换公式

(w代表频率,t代表时间,e^-iwt为复变函数)

其中:,


欧拉公式

傅里叶逆变换:

傅里叶逆变换公式

傅里叶变换认为一个周期函数(信号)包含多个频率分量,任意函数(信号)f(t)可通过多个周期函数(基函数)相加而合成。

从物理角度理解傅里叶变换是以一组特殊的函数(三角函数)为正交基,对原函数进行线性变换,物理意义便是原函数在各组基函数的投影。


傅里叶变换

以下是代码:

import numpy as np

from numpy import linalg

import matplotlib.pyplot as plt

import math

#时间长度

t_len = 3.14*4

#分割时间

t = np.linspace(0, t_len, 1000)

#傅里叶变换原函数g_t(t)

g_t = []

for i in range(len(t)):

    #测试函数

    temp = 2*math.sin(t[i])+math.sin(2*t[i]+3)

    g_t.append(temp)


#频率范围

f_len = 2

#分割频率

f = np.linspace(0, f_len, 1000)

#傅里叶变换函数实轴函数g_cos(f),x轴函数

g_cos = []

#傅里叶变换函数虚轴函数g_sin(f),y轴函数

g_sin = []

#傅里叶变换 复数模 函数g_f(f),偏离中心距离函数

g_f = []

#傅里叶函数虚轴和实轴存在的意义在于区分振动中不同方向的增益效果

#所谓虚函数只是一种区分方向的方式,不让cos()与sin()在积分前混合

#傅里叶变换,时间进行积分

for c_f in range(len(f)):

    sinsum = 0

    cossum = 0

    for c_t in range(len(t)):

        tempsin = g_t[c_t] * math.sin(f[c_f] * t[c_t] * (-2 * math.pi))

        tempcos = g_t[c_t] * math.cos(f[c_f] * t[c_t] * (-2 * math.pi))     

        sinsum = sinsum + tempsin

        cossum = cossum + tempcos

    g_f.append(math.sqrt((cossum/len(t))**2 + (sinsum/len(t))**2))

    g_sin.append(sinsum/len(t))

    g_cos.append(cossum/len(t))


#逆傅里叶变换还原后函数f_g(t)

f_g = []

#傅里叶变换,频率进行积分

for c_t in range(len(t)):

    sinsum = 0

    cossum = 0

    for c_f in range(len(f)):

        tempsin = g_sin[c_f] * math.sin(f[c_f] * t[c_t] * (-2 * math.pi))

        tempcos = g_cos[c_f] * math.cos(f[c_f] * t[c_t] * (-2 * math.pi))

        sinsum = sinsum + tempsin

        cossum = cossum + tempcos

    f_g.append(2*f_len*t_len*sinsum/len(f) + 2*f_len*t_len*cossum/len(f))

plt.figure("傅里叶变换",figsize=(6, 6))

plt.subplot(2,2,1)

#原函数g_t(t)

plt.scatter(t, g_t, s = 1, color='red')

plt.title("Original Function g_t(t)")

plt.subplot(2,2,2)

#傅里叶变换虚轴函数g_sin(f)

plt.scatter(f, g_sin, s = 1, color='red')

plt.title("Fourier(sin) g_sin(f)")

plt.subplot(2,2,3)

#傅里叶变换 复数模 函数g_f(f)

plt.scatter(f, g_f, s = 1, color='blue')

plt.title("Fourier(module) g_f(f)")

plt.subplot(2,2,4)

#逆傅里叶变换还原后的函数f_g(t)

plt.scatter(t, f_g, s = 1, color='red')

plt.title("restore f_g(t)")

plt.show()


[1] 傅里叶变换概念及公式推导 https://blog.csdn.net/lzzdflg/article/details/78254381

你可能感兴趣的:(傅里叶变换Python实现)