Python中恒定功能的FFT
我想计算恒定信号的DFT(FFT)。这是代码
import matplotlib.pyplot as plt
import numpy as np
from scipy.fftpack import fft, ifft
def constant_function(x):
return 1
t = np.arange(0.0,1,0.1)
print(type(t1))
print( np.full(t.shape, constant_function(t)))
plt.plot(t, np.full(t.shape, constant_function(t)))
freq = 1
X = fft(constant_function(t))
plt.figure(figsize = (12, 6))
plt.subplot(121)
plt.stem(freq, np.abs(X), 'b', \
markerfmt=" ", basefmt="-b")
plt.xlabel('Freq (Hz)')
plt.ylabel('FFT Amplitude |X(freq)|')
plt.xlim(0, 1)
plt.subplot(122)
plt.plot(t, ifft(X), 'r')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.tight_layout()
plt.show()
,但我会收到此错误:
IndexError: tuple index out of range
我无法以某种方式解决此错误! 谢谢
I want to calculate the DFT(FFT) of a constant signal. Here is the code
import matplotlib.pyplot as plt
import numpy as np
from scipy.fftpack import fft, ifft
def constant_function(x):
return 1
t = np.arange(0.0,1,0.1)
print(type(t1))
print( np.full(t.shape, constant_function(t)))
plt.plot(t, np.full(t.shape, constant_function(t)))
freq = 1
X = fft(constant_function(t))
plt.figure(figsize = (12, 6))
plt.subplot(121)
plt.stem(freq, np.abs(X), 'b', \
markerfmt=" ", basefmt="-b")
plt.xlabel('Freq (Hz)')
plt.ylabel('FFT Amplitude |X(freq)|')
plt.xlim(0, 1)
plt.subplot(122)
plt.plot(t, ifft(X), 'r')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.tight_layout()
plt.show()
But I get this error:
IndexError: tuple index out of range
I can't work around this error somehow!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题不是与FFT有关:
无论您将其放入什么(您的linspace范围),它都将返回
1
的常数,并尝试将单个数字的FFT( 1)而不是数字向量。您可能想要:尽管在这些情况下,以不同频率创建正弦波的总和通常是测试FFT的最简单方法。
The issue is not with the FFT but with your function:
No matter what you are putting into it (your linspace range), it's returning a constant of
1
and trying to take the FFT of a single number (1) rather than a vector of numbers. You probably want:Though in these cases, creating a sum of sine waves at different frequencies is usually the easiest way to test the FFT.