python integer怎么用_Python core.integer方法代码示例

这篇博客介绍了如何在Python中使用numpy.core.integer进行离散傅里叶变换(DFT)样本频率计算。内容包括从numpy导入integer,定义rfftfreq函数,该函数返回DFT频率样本,特别是在rfft和irfft操作中的应用。函数参数n表示窗口长度,d表示样本间距,默认为1.0。返回的频率数组包含正向的Nyquist频率。博客还提供了示例代码,展示如何计算信号的频谱,并对比了与fftfreq函数的区别。
摘要由CSDN通过智能技术生成

# 需要导入模块: from numpy import core [as 别名]

# 或者: from numpy.core import integer [as 别名]

def rfftfreq(n, d=1.0):

"""

Return the Discrete Fourier Transform sample frequencies

(for usage with rfft, irfft).

The returned float array `f` contains the frequency bin centers in cycles

per unit of the sample spacing (with zero at the start). For instance, if

the sample spacing is in seconds, then the frequency unit is cycles/second.

Given a window length `n` and a sample spacing `d`::

f = [0, 1, ..., n/2-1, n/2] / (d*n) if n is even

f = [0, 1, ..., (n-1)/2-1, (n-1)/2] / (d*n) if n is odd

Unlike `fftfreq` (but like `scipy.fftpack.rfftfreq`)

the Nyquist frequency component is considered to be positive.

Parameters

----------

n : int

Window length.

d : scalar, optional

Sample spacing (inverse of the sampling rate). Defaults to 1.

Returns

-------

f : ndarray

Array of length ``n//2 + 1`` containing the sample frequencies.

Examples

--------

>>> signal = np.array([-2, 8, 6, 4, 1, 0, 3, 5, -3, 4], dtype=float)

>>> fourier = np.fft.rfft(signal)

>>> n = signal.size

>>> sample_rate = 100

>>> freq = np.fft.fftfreq(n, d=1./sample_rate)

>>> freq

array([ 0., 10., 20., 30., 40., -50., -40., -30., -20., -10.])

>>> freq = np.fft.rfftfreq(n, d=1./sample_rate)

>>> freq

array([ 0., 10., 20., 30., 40., 50.])

"""

if not isinstance(n, integer_types):

raise ValueError("n should be an integer")

val = 1.0/(n*d)

N = n//2 + 1

results = arange(0, N, dtype=int)

return results * val

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Traceback (most recent call last): File "D:\anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 3460, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-2-1430dfa068df>", line 1, in <module> runfile('D:\\Users\\Admin\\PycharmProjects\\pythonProject2\\线性分析预测.py', wdir='D:\\Users\\Admin\\PycharmProjects\\pythonProject2') File "D:\PyCharm 2023.1.1\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile pydev_imports.execfile(filename, global_vars, local_vars) # execute the script File "D:\PyCharm 2023.1.1\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "D:\Users\Admin\PycharmProjects\pythonProject2\线性分析预测.py", line 9, in <module> y = data.iloc[:, 4].values File "D:\anaconda\lib\site-packages\pandas\core\indexing.py", line 1067, in __getitem__ return self._getitem_tuple(key) File "D:\anaconda\lib\site-packages\pandas\core\indexing.py", line 1563, in _getitem_tuple tup = self._validate_tuple_indexer(tup) File "D:\anaconda\lib\site-packages\pandas\core\indexing.py", line 873, in _validate_tuple_indexer self._validate_key(k, i) File "D:\anaconda\lib\site-packages\pandas\core\indexing.py", line 1466, in _validate_key self._validate_integer(key, axis) File "D:\anaconda\lib\site-packages\pandas\core\indexing.py", line 1557, in _validate_integer raise IndexError("single positional indexer is out-of-bounds") IndexError: single positional indexer is out-of-bounds
05-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值