Python中恒定功能的FFT

发布于 2025-02-06 21:30:54 字数 785 浏览 2 评论 0原文

我想计算恒定信号的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

套路撩心 2025-02-13 21:30:54

问题不是与FFT有关:

def constant_function(x):
   return 1

无论您将其放入什么(您的linspace范围),它都将返回1的常数,并尝试将单个数字的FFT( 1)而不是数字向量。您可能想要:

def constant_function(x):
    return np.ones(len(x))

尽管在这些情况下,以不同频率创建正弦波的总和通常是测试FFT的最简单方法。

The issue is not with the FFT but with your function:

def constant_function(x):
   return 1

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:

def constant_function(x):
    return np.ones(len(x))

Though in these cases, creating a sum of sine waves at different frequencies is usually the easiest way to test the FFT.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文