【问题标题】:Calculating amplitude from np.fft从 np.fft 计算振幅
【发布时间】:2019-10-11 07:31:05
【问题描述】:

我似乎正在使用 np.fft.fft 为原始波计算不正确的幅度。

显示了 fft 的图,您可以看到显示的幅度在 3 和 1.5 左右,但是如果您查看代码,我正在使用幅度 7 和 3 来生成信号。该图应该有两个尖峰,在 x=13 处达到 y=3,在 x=15 处达到 y=7

我需要做什么才能在图表中看到正确的幅度(3 和 7)?

我可以通过实验看到我需要将振幅乘以 2.3 的常数,但我如何准确计算这个数字?

import numpy as np
import matplotlib.pyplot as plt

t0 = 0
t1 = 20
n_samples = 1000

xs = np.linspace(t0, t1, n_samples)
# Generate signal with amplitudes 7 and 3
ys = 7*np.sin(15 * 2 * np.pi * xs) + 3*np.sin(13 * 2 * np.pi * xs)

np_fft = np.fft.fft(ys)
amplitudes = 1/n_samples * np.abs(np_fft) #This gives wrong results

frequencies = np.fft.fftfreq(n_samples) * n_samples * 1/(t1-t0)

plt.plot(frequencies[:len(frequencies)//2], amplitudes[:len(np_fft)//2])
plt.show()

【问题讨论】:

标签: python numpy


【解决方案1】:

我认为你错误地计算了幅度。你应该改变

amplitudes = 1/n_samples * np.abs(np_fft)

 amplitudes = 2 / n_samples * np.abs(np_fft)

结果:

import numpy as np
import matplotlib.pyplot as plt

t0 = 0
t1 = 1
n_samples = 10000

xs = np.linspace(t0, t1, n_samples)
ys = 7 * np.sin(15 * 2 * np.pi * xs) + 3 * np.sin(13 * 2 * np.pi * xs)

plt.subplot(2, 1, 1)
plt.plot(xs, ys)

np_fft = np.fft.fft(ys)
amplitudes = 2 / n_samples * np.abs(np_fft) 
frequencies = np.fft.fftfreq(n_samples) * n_samples * 1 / (t1 - t0)

plt.subplot(2, 1, 2)
plt.semilogx(frequencies[:len(frequencies) // 2], amplitudes[:len(np_fft) // 2])

plt.show()

amplitudes 的峰值不完全是72,但如果增加n_samples,它们会变得更准确。

【讨论】:

    猜你喜欢
    • 2022-07-11
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 2020-07-25
    • 2012-06-14
    • 1970-01-01
    相关资源
    最近更新 更多