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

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

computing Fast Fourier Transform of dataset using python

问题

  1. 我想使用Python计算给定信号的FFTx轴是时间),y轴是电压该信号具有某种周期性看起来像这样
  2. [![在这里输入图片描述][1]][1]
  3. 在参考了这个[帖子][2]后我得到了这个图
  4. [![在这里输入图片描述][3]][3]
  5. 这是正确的FFTCSV文件在[这里][4]代码如下
  6. ```python
  7. import numpy as np
  8. import matplotlib.pyplot as plt
  9. from pandas import read_csv
  10. from scipy.fft import fft
  11. plt.rcParams['figure.dpi'] = 1000
  12. # 载入数据集 #1
  13. dataframe = read_csv('data/1.csv', usecols=[1])
  14. plt.plot(dataframe)
  15. plt.show()
  16. ################ 使用scipy进行FFT
  17. # 样本点数
  18. N = 100
  19. # 采样周期
  20. T = 1
  21. # 为信号创建x轴时间长度
  22. x = np.linspace(0, N*T, N)
  23. # 创建与信号值对应的数组
  24. y = dataframe
  25. y = y - np.mean(y)
  26. # 对信号执行FFT
  27. yf = fft(y)
  28. # 创建新的x轴:来自信号的频率
  29. xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
  30. # 绘制结果
  31. plt.plot(xf, abs(yf[0:N//2]), label='信号')
  32. plt.grid()
  33. plt.xlabel('频率')
  34. plt.ylabel('谱振幅')
  35. plt.legend(loc=1)
  36. plt.savefig('fft.jpg')
  37. plt.show()
  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. [![enter image description here][1]][1]
  5. Following this [post][2], I get this figure:
  6. [![enter image description here][3]][3]
  7. Is this the correct fft?. The csv file is [here][4]. And the code:
  8. import numpy as np
  9. import matplotlib.pyplot as plt
  10. from pandas import read_csv
  11. from scipy.fft import fft
  12. plt.rcParams[&#39;figure.dpi&#39;] = 1000
  13. # load the dataset #1
  14. dataframe = read_csv(&#39;data/1.csv&#39;, usecols=[1])
  15. plt.plot(dataframe)
  16. plt.show()
  17. ################ FFT Con scipy
  18. #number of sample points
  19. N = 100
  20. #sampling period
  21. T = 1
  22. #create x-axis for time length of signal
  23. x = np.linspace(0, N*T, N)
  24. #create array that corresponds to values in signal
  25. y = dataframe
  26. y = y - np.mean(y)
  27. #perform FFT on signal
  28. yf = fft(y)
  29. #create new x-axis: frequency from signal
  30. xf = np.linspace(0.0, 1.0/(2.0*T), N//2)
  31. #plot results
  32. plt.plot(xf, abs(yf[0:N//2]), label = &#39;signal&#39;)
  33. plt.grid()
  34. plt.xlabel(&#39;Frequency&#39;)
  35. plt.ylabel(&#39;Spectral Amplitude&#39;)
  36. plt.legend(loc=1)
  37. plt.savefig(&#39;fft.jpg&#39;)
  38. plt.show()
  39. [1]: https://i.stack.imgur.com/rRr65.jpg
  40. [2]: https://stackoverflow.com/questions/48622933/python-performing-fft-on-csv-values-using-scipy-documentation
  41. [3]: https://i.stack.imgur.com/K0dyU.png
  42. [4]: https://drive.google.com/file/d/1rRkBwX9Vx2xkjvnnWY0gcmgAU8zO7JoR/view?usp=sharing
  43. </details>
  44. # 答案1
  45. **得分**: 1
  46. 傅立叶变换是正确的,但您显示的方式会产生误导。您需要使用实际的采样周期:
  47. ```python
  48. import numpy as np
  49. import pandas as pd
  50. import matplotlib.pyplot as plt
  51. df = pd.read_csv('1.csv')
  52. # 水平索引必须是线性的
  53. assert np.all(df.epoch.diff()[1:] == 1)
  54. yf = np.fft.rfft(df.voltage, norm='forward')
  55. ff = np.fft.rfftfreq(n=len(df), d=2)
  56. fig, ax = plt.subplots()
  57. ax.loglog(ff, np.abs(yf))
  58. ax.set_xlabel('频率 (Hz)')
  59. ax.set_ylabel('幅度 (V)')
  60. plt.show()

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

英文:

The FFT is correct but how you display it is misleading. You need to use your actual sample period:

  1. import numpy as np
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. df = pd.read_csv(&#39;1.csv&#39;)
  5. # The horizontal index must be linear
  6. assert np.all(df.epoch.diff()[1:] == 1)
  7. yf = np.fft.rfft(df.voltage, norm=&#39;forward&#39;)
  8. ff = np.fft.rfftfreq(n=len(df), d=2)
  9. fig, ax = plt.subplots()
  10. ax.loglog(ff, np.abs(yf))
  11. ax.set_xlabel(&#39;Frequency (Hz)&#39;)
  12. ax.set_ylabel(&#39;Amplitude (V)&#39;)
  13. 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:

确定