使用Python计算数据集的快速傅里叶变换。

huangapple go评论74阅读模式
英文:

computing Fast Fourier Transform of dataset using python

问题

我想使用Python计算给定信号的FFTx轴是时间),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[&#39;figure.dpi&#39;] = 1000
    
    # load the dataset #1
    dataframe = read_csv(&#39;data/1.csv&#39;, 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 = &#39;signal&#39;)
    plt.grid()
    plt.xlabel(&#39;Frequency&#39;)
    plt.ylabel(&#39;Spectral Amplitude&#39;)
    plt.legend(loc=1)
    plt.savefig(&#39;fft.jpg&#39;)
    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()

使用Python计算数据集的快速傅里叶变换。

英文:

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(&#39;1.csv&#39;)

# The horizontal index must be linear
assert np.all(df.epoch.diff()[1:] == 1)

yf = np.fft.rfft(df.voltage, norm=&#39;forward&#39;)
ff = np.fft.rfftfreq(n=len(df), d=2)

fig, ax = plt.subplots()
ax.loglog(ff, np.abs(yf))
ax.set_xlabel(&#39;Frequency (Hz)&#39;)
ax.set_ylabel(&#39;Amplitude (V)&#39;)
plt.show()

使用Python计算数据集的快速傅里叶变换。

huangapple
  • 本文由 发表于 2023年7月20日 19:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729203.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定