英文:
computing Fast Fourier Transform of dataset using python
问题
我想使用Python计算给定信号的FFT。x轴是时间(秒),y轴是电压。该信号具有某种周期性,看起来像这样:
[![在这里输入图片描述][1]][1]
在参考了这个[帖子][2]后,我得到了这个图:
[![在这里输入图片描述][3]][3]
这是正确的FFT吗?CSV文件在[这里][4]。代码如下:
```python
import numpy as np
import matplotlib.pyplot as plt
from pandas import read_csv
from scipy.fft import fft
plt.rcParams['figure.dpi'] = 1000
# 载入数据集 #1
dataframe = read_csv('data/1.csv', usecols=[1])
plt.plot(dataframe)
plt.show()
################ 使用scipy进行FFT
# 样本点数
N = 100
# 采样周期
T = 1
# 为信号创建x轴时间长度
x = np.linspace(0, N*T, N)
# 创建与信号值对应的数组
y = dataframe
y = y - np.mean(y)
# 对信号执行FFT
yf = fft(y)
# 创建新的x轴:来自信号的频率
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
# 绘制结果
plt.plot(xf, abs(yf[0:N//2]), label='信号')
plt.grid()
plt.xlabel('频率')
plt.ylabel('谱振幅')
plt.legend(loc=1)
plt.savefig('fft.jpg')
plt.show()
<details>
<summary>英文:</summary>
I want to calculate the fft of a given signal using python. The x axis is time (seconds) and the y axis is a voltage. The signal has some kind of periodicity and looks like this:
[![enter image description here][1]][1]
Following this [post][2], I get this figure:
[![enter image description here][3]][3]
Is this the correct fft?. The csv file is [here][4]. And the code:
import numpy as np
import matplotlib.pyplot as plt
from pandas import read_csv
from scipy.fft import fft
plt.rcParams['figure.dpi'] = 1000
# load the dataset #1
dataframe = read_csv('data/1.csv', usecols=[1])
plt.plot(dataframe)
plt.show()
################ FFT Con scipy
#number of sample points
N = 100
#sampling period
T = 1
#create x-axis for time length of signal
x = np.linspace(0, N*T, N)
#create array that corresponds to values in signal
y = dataframe
y = y - np.mean(y)
#perform FFT on signal
yf = fft(y)
#create new x-axis: frequency from signal
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
#plot results
plt.plot(xf, abs(yf[0:N//2]), label = 'signal')
plt.grid()
plt.xlabel('Frequency')
plt.ylabel('Spectral Amplitude')
plt.legend(loc=1)
plt.savefig('fft.jpg')
plt.show()
[1]: https://i.stack.imgur.com/rRr65.jpg
[2]: https://stackoverflow.com/questions/48622933/python-performing-fft-on-csv-values-using-scipy-documentation
[3]: https://i.stack.imgur.com/K0dyU.png
[4]: https://drive.google.com/file/d/1rRkBwX9Vx2xkjvnnWY0gcmgAU8zO7JoR/view?usp=sharing
</details>
# 答案1
**得分**: 1
傅立叶变换是正确的,但您显示的方式会产生误导。您需要使用实际的采样周期:
```python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('1.csv')
# 水平索引必须是线性的
assert np.all(df.epoch.diff()[1:] == 1)
yf = np.fft.rfft(df.voltage, norm='forward')
ff = np.fft.rfftfreq(n=len(df), d=2)
fig, ax = plt.subplots()
ax.loglog(ff, np.abs(yf))
ax.set_xlabel('频率 (Hz)')
ax.set_ylabel('幅度 (V)')
plt.show()
英文:
The FFT is correct but how you display it is misleading. You need to use your actual sample period:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
df = pd.read_csv('1.csv')
# The horizontal index must be linear
assert np.all(df.epoch.diff()[1:] == 1)
yf = np.fft.rfft(df.voltage, norm='forward')
ff = np.fft.rfftfreq(n=len(df), d=2)
fig, ax = plt.subplots()
ax.loglog(ff, np.abs(yf))
ax.set_xlabel('Frequency (Hz)')
ax.set_ylabel('Amplitude (V)')
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论