Python 傅里叶变换 Fourier Transform

Python 傅里叶变换 Fourier Transform

flyfish

0 解释什么是Period 和 Amplitude

import matplotlib.pyplot as plt
import numpy as np

plt.style.use('seaborn-poster')
%matplotlib inline
x = np.linspace(0, 20, 201)
y = np.sin(x)

plt.figure(figsize = (8, 6))
plt.plot(x, y, 'b')
plt.ylabel('Amplitude')
plt.xlabel('Location (x)')
plt.show()

在这里插入图片描述
一图胜千言
在这里插入图片描述

Fast Fourier Transform (FFT) 快速傅里叶变换
快速傅里叶逆变换

1 在时域中绘制信号

import matplotlib.pyplot as plt
import numpy as np
from scipy.fftpack import fft, ifft
# sampling rate 也叫 Sampling frequency 
sr = 2000
# sampling interval 也叫 Sampling period  
ts = 1.0/sr
t = np.arange(0,1,ts)#start, stop, step #[0.000e+00 5.000e-04 1.000e-03 ... 9.985e-01 9.990e-01 9.995e-01]
#(len(t))==2000

freq_feature = 1
x = 3*np.sin(2*np.pi*freq_feature*t)

freq_feature =3
x += 2*np.sin(2*np.pi*freq_feature*t)

freq_feature = 7
x += 0.5* np.sin(2*np.pi*freq_feature*t)

plt.figure(figsize = (8, 6))
print(len(x))#Length of signal
plt.plot(t, x)
plt.ylabel('Amplitude')

plt.show()

p e r i o d = 1 f r e q u e n c y period = \frac{1}{frequency} period=frequency1
在这里插入图片描述

2 绘制快速傅里叶变换图

X = fft(x)
N = len(X)
n = np.arange(N)#[   0    1    2 ... 1997 1998 1999]
T = N/sr
freq = n/T 
X = fft(x)

plt.figure(figsize = (8, 6))

plt.plot(freq, np.abs(X))

plt.xlabel('Freq (Hz)')
plt.ylabel('FFT Amplitude |X(freq)|')

在这里插入图片描述

因为1、3、7相对2000,在坐标轴上太小把上述代码freq_feature 改成100,300,700更容易看出特点

freq_feature = 100
x = 3*np.sin(2*np.pi*freq_feature*t)

freq_feature =300
x += 2*np.sin(2*np.pi*freq_feature*t)

freq_feature = 700
x += 0.5* np.sin(2*np.pi*freq_feature*t)

在这里插入图片描述

继续绘制freq_feature =1、3、7的傅里叶变换图

plt.figure(figsize = (8, 6))

plt.stem(freq, np.abs(X))
plt.xlim(0, 10)
plt.xlabel('Freq (Hz)')
plt.ylabel('FFT Amplitude |X(freq)|')
plt.show()

在这里插入图片描述
这样就看的更清楚了

3 通过ifft快速傅里叶逆变换还原数据

plt.figure(figsize = (8, 6))
plt.plot(t, ifft(X), 'r')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude(ifft)')

在这里插入图片描述

因为fft之后的结果是对称的所以可以只绘制one-sided
在这里插入图片描述

4 可以转换成分钟显示

t_h = 1/f_oneside / (60)
plt.figure(figsize=(8,6))
plt.plot(t_h, np.abs(X[:n_oneside])/n_oneside)
plt.xlabel('Period ($m$)')
plt.show()

在这里插入图片描述

5 完整代码

# -*- coding: utf-8 -*-

import matplotlib.pyplot as plt
import numpy as np
from scipy.fftpack import fft, ifft
# sampling rate 也叫 Sampling frequency 
sr = 2000
# sampling interval 也叫 Sampling period  
ts = 1.0/sr
t = np.arange(0,1,ts)#start, stop, step #[0.000e+00 5.000e-04 1.000e-03 ... 9.985e-01 9.990e-01 9.995e-01]
#(len(t))==2000

freq_feature = 1
x = 3*np.sin(2*np.pi*freq_feature*t)

freq_feature =3
x += 2*np.sin(2*np.pi*freq_feature*t)

freq_feature = 7
x += 0.5* np.sin(2*np.pi*freq_feature*t)

plt.figure(figsize = (8, 6))
print(len(x))#Length of signal
plt.plot(t, x)
plt.ylabel('Amplitude')

plt.show()



X = fft(x)
N = len(X)
n = np.arange(N)#[   0    1    2 ... 1997 1998 1999]
T = N/sr
freq = n/T 
X = fft(x)

plt.figure(figsize = (8, 6))

plt.stem(freq, np.abs(X))
plt.xlim(0, 10)
plt.xlabel('Freq (Hz)')
plt.ylabel('FFT Amplitude |X(freq)|')
plt.show()


plt.figure(figsize = (8, 6))
plt.plot(t, ifft(X), 'r')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude(ifft)')



# Get the one-sided specturm
n_oneside = N//2
# get the one side frequency
f_oneside = freq[:n_oneside]+1
print("f_oneside:",f_oneside)

plt.figure(figsize = (12, 6))
plt.plot(f_oneside, np.abs(X[:n_oneside]))
plt.xlabel('Freq (Hz)')
plt.ylabel('FFT Amplitude one-sided')
plt.show()



t_h = 1/f_oneside / (60)
plt.figure(figsize=(8,6))
plt.plot(t_h, np.abs(X[:n_oneside])/n_oneside)
plt.xlabel('Period ($m$)')
plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二分掌柜的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值