光谱行动

译者:ApacheCN

  1. torch.fft(input, signal_ndim, normalized=False) Tensor

复杂到复杂的离散傅立叶变换

该方法计算复数到复数的离散傅立叶变换。忽略批量维度,它计算以下表达式:

光谱行动 - 图1

其中 光谱行动 - 图2 = signal_ndim是信号的维数, 光谱行动 - 图3 是信号维数 光谱行动 - 图4 的大小。

该方法支持1D,2D和3D复杂到复合变换,由signal_ndim表示。 input必须是最后一个尺寸为2的张量,表示复数的实部和虚部,并且至少应具有signal_ndim + 1尺寸和任意数量的前导批量尺寸。如果normalized设置为True,则通过将其除以 光谱行动 - 图5 来将结果标准化,以便操作符是单一的。

将实部和虚部一起作为input的相同形状的一个张量返回。

该函数的反函数是 ifft()

注意

对于CUDA张量,LRU高速缓存用于cuFFT计划,以加速在具有相同配置的相同几何的张量上重复运行FFT方法。

更改torch.backends.cuda.cufft_plan_cache.max_size(CUDA 10及更高版本上的默认值为4096,旧版本的CUDA上为1023)控制此缓存的容量。一些cuFFT计划可能会分配GPU内存。您可以使用torch.backends.cuda.cufft_plan_cache.size查询当前缓存中的计划数量,使用torch.backends.cuda.cufft_plan_cache.clear()清除缓存。

警告

对于CPU张量,此方法目前仅适用于MKL。使用torch.backends.mkl.is_available()检查是否安装了MKL。

参数:

  • 输入 (Tensor) - 至少signal_ndim + 1维度的输入张量
  • signal_ndim (int) - 每个信号中的维数。 signal_ndim只能是1,2或3
  • 归一化 (bool 任选) - 控制是否返回归一化结果。默认值:False
返回: 包含复数到复数傅立叶变换结果的张量
返回类型: Tensor

例:

  1. >>> # unbatched 2D FFT
  2. >>> x = torch.randn(4, 3, 2)
  3. >>> torch.fft(x, 2)
  4. tensor([[[-0.0876, 1.7835],
  5. [-2.0399, -2.9754],
  6. [ 4.4773, -5.0119]],
  7. [[-1.5716, 2.7631],
  8. [-3.8846, 5.2652],
  9. [ 0.2046, -0.7088]],
  10. [[ 1.9938, -0.5901],
  11. [ 6.5637, 6.4556],
  12. [ 2.9865, 4.9318]],
  13. [[ 7.0193, 1.1742],
  14. [-1.3717, -2.1084],
  15. [ 2.0289, 2.9357]]])
  16. >>> # batched 1D FFT
  17. >>> torch.fft(x, 1)
  18. tensor([[[ 1.8385, 1.2827],
  19. [-0.1831, 1.6593],
  20. [ 2.4243, 0.5367]],
  21. [[-0.9176, -1.5543],
  22. [-3.9943, -2.9860],
  23. [ 1.2838, -2.9420]],
  24. [[-0.8854, -0.6860],
  25. [ 2.4450, 0.0808],
  26. [ 1.3076, -0.5768]],
  27. [[-0.1231, 2.7411],
  28. [-0.3075, -1.7295],
  29. [-0.5384, -2.0299]]])
  30. >>> # arbitrary number of batch dimensions, 2D FFT
  31. >>> x = torch.randn(3, 3, 5, 5, 2)
  32. >>> y = torch.fft(x, 2)
  33. >>> y.shape
  34. torch.Size([3, 3, 5, 5, 2])
  1. torch.ifft(input, signal_ndim, normalized=False) Tensor

复数到复数的逆离散傅立叶变换

该方法计算复数到复数的离散傅里叶逆变换。忽略批量维度,它计算以下表达式:

光谱行动 - 图6

where 光谱行动 - 图7 = signal_ndim is number of dimensions for the signal, and 光谱行动 - 图8 is the size of signal dimension 光谱行动 - 图9.

参数规范与 fft() 几乎相同。但是,如果normalized设置为True,则返回结果乘以 光谱行动 - 图10 ,成为单一运算符。因此,要反转 fft()normalized参数应设置为 fft() 相同。

Returns the real and the imaginary parts together as one tensor of the same shape of input.

该函数的反函数是 fft()

Note

For CUDA tensors, an LRU cache is used for cuFFT plans to speed up repeatedly running FFT methods on tensors of same geometry with same same configuration.

Changing torch.backends.cuda.cufft_plan_cache.max_size (default is 4096 on CUDA 10 and newer, and 1023 on older CUDA versions) controls the capacity of this cache. Some cuFFT plans may allocate GPU memory. You can use torch.backends.cuda.cufft_plan_cache.size to query the number of plans currently in cache, and torch.backends.cuda.cufft_plan_cache.clear() to clear the cache.

Warning

For CPU tensors, this method is currently only available with MKL. Use torch.backends.mkl.is_available() to check if MKL is installed.

Parameters:

  • 输入 (Tensor) - 至少signal_ndim + 1维度的输入张量
  • signal_ndim (int) - 每个信号中的维数。 signal_ndim只能是1,2或3
  • 归一化 (bool 任选) - 控制是否返回归一化结果。默认值:False
Returns: 包含复数到复数逆傅立叶变换结果的张量
Return type: Tensor

Example:

  1. >>> x = torch.randn(3, 3, 2)
  2. >>> x
  3. tensor([[[ 1.2766, 1.3680],
  4. [-0.8337, 2.0251],
  5. [ 0.9465, -1.4390]],
  6. [[-0.1890, 1.6010],
  7. [ 1.1034, -1.9230],
  8. [-0.9482, 1.0775]],
  9. [[-0.7708, -0.8176],
  10. [-0.1843, -0.2287],
  11. [-1.9034, -0.2196]]])
  12. >>> y = torch.fft(x, 2)
  13. >>> torch.ifft(y, 2) # recover x
  14. tensor([[[ 1.2766, 1.3680],
  15. [-0.8337, 2.0251],
  16. [ 0.9465, -1.4390]],
  17. [[-0.1890, 1.6010],
  18. [ 1.1034, -1.9230],
  19. [-0.9482, 1.0775]],
  20. [[-0.7708, -0.8176],
  21. [-0.1843, -0.2287],
  22. [-1.9034, -0.2196]]])
  1. torch.rfft(input, signal_ndim, normalized=False, onesided=True) Tensor

实对复离散傅立叶变换

该方法计算实数到复数的离散傅立叶变换。它在数学上等同于 fft() ,仅在输入和输出的格式上有所不同。

该方法支持1D,2D和3D实对复变换,由signal_ndim表示。 input必须是具有至少signal_ndim尺寸的张量,可选择任意数量的前导批量。如果normalized设置为True,则通过将其除以 光谱行动 - 图11 来将结果标准化,以便操作符是单一的,其中 光谱行动 - 图12 是信号的大小维 光谱行动 - 图13

实对复傅里叶变换结果遵循共轭对称:

光谱行动 - 图14

计算指数算术的模数是相应维数的大小, 光谱行动 - 图15 是共轭算子, 光谱行动 - 图16 = signal_ndimonesided标志控制是否避免输出结果中的冗余。如果设置为True(默认),输出将不是形状 光谱行动 - 图17 的完整复杂结果,其中 光谱行动 - 图18input的形状,而是最后一个尺寸将是大小 光谱行动 - 图19 的一半。

该函数的反函数是 irfft()

Note

For CUDA tensors, an LRU cache is used for cuFFT plans to speed up repeatedly running FFT methods on tensors of same geometry with same same configuration.

Changing torch.backends.cuda.cufft_plan_cache.max_size (default is 4096 on CUDA 10 and newer, and 1023 on older CUDA versions) controls the capacity of this cache. Some cuFFT plans may allocate GPU memory. You can use torch.backends.cuda.cufft_plan_cache.size to query the number of plans currently in cache, and torch.backends.cuda.cufft_plan_cache.clear() to clear the cache.

Warning

For CPU tensors, this method is currently only available with MKL. Use torch.backends.mkl.is_available() to check if MKL is installed.

Parameters:

  • 输入 (Tensor) - 至少signal_ndim维度的输入张量
  • signal_ndim (int) - 每个信号中的维数。 signal_ndim只能是1,2或3
  • 归一化 (bool 任选) - 控制是否返回归一化结果。默认值:False
  • 单独 (bool 可选) - 控制是否返回一半结果以避免冗余。默认值:True
Returns: 包含实数到复数傅立叶变换结果的张量
Return type: Tensor

Example:

  1. >>> x = torch.randn(5, 5)
  2. >>> torch.rfft(x, 2).shape
  3. torch.Size([5, 3, 2])
  4. >>> torch.rfft(x, 2, onesided=False).shape
  5. torch.Size([5, 5, 2])
  1. torch.irfft(input, signal_ndim, normalized=False, onesided=True, signal_sizes=None) Tensor

复数到实数的逆离散傅立叶变换

该方法计算复数到实数的逆离散傅里叶变换。它在数学上等同于 ifft() ,仅在输入和输出的格式上有所不同。

参数规范与 ifft() 几乎相同。类似于 ifft() ,如果normalized设置为True,则通过将其与 光谱行动 - 图20 相乘来使结果归一化,以便运算符是单一的,其中 [] ](/uploads/projects/Pytorch-document-turtorial/docs/1.0/uploads/projects/Pytorch-document-turtorial/docs/1.0/uploads/projects/Pytorch-document-turtorial/docs/1.0/uploads/projects/Pytorch-document-turtorial/docs/1.0/apachecn/pytorch-doc-zh/raw/master/docs/1.0/img/4236d8cccece7d17f3a004865adbf94d.jpg) 是信号维 光谱行动 - 图22 的大小。

由于共轭对称性,input不需要包含完整的复频率值。大约一半的值就足够了, rfft() rfft(signal, onesided=True)给出input的情况就足够了。在这种情况下,将此方法的onesided参数设置为True。此外,原始信号形状信息有时会丢失,可选地将signal_sizes设置为原始信号的大小(如果处于批处理模式,则没有批量维度)以正确的形状恢复它。

因此,要反转 rfft()normalizedonesided参数应设置为 irfft() 相同,并且最好给出signal_sizes以避免大小不匹配。有关尺寸不匹配的情况,请参阅下面的示例。

有关共轭对称性的详细信息,请参见 rfft()

该函数的反函数是 rfft()

Warning

一般而言,此函数的输入应包含共轭对称后的值。请注意,即使onesidedTrue,仍然需要对某些部分进行对称。当不满足此要求时, irfft() 的行为未定义。由于 torch.autograd.gradcheck() 估计具有点扰动的数值雅可比行列式, irfft() 几乎肯定会失败。

Note

For CUDA tensors, an LRU cache is used for cuFFT plans to speed up repeatedly running FFT methods on tensors of same geometry with same same configuration.

Changing torch.backends.cuda.cufft_plan_cache.max_size (default is 4096 on CUDA 10 and newer, and 1023 on older CUDA versions) controls the capacity of this cache. Some cuFFT plans may allocate GPU memory. You can use torch.backends.cuda.cufft_plan_cache.size to query the number of plans currently in cache, and torch.backends.cuda.cufft_plan_cache.clear() to clear the cache.

Warning

For CPU tensors, this method is currently only available with MKL. Use torch.backends.mkl.is_available() to check if MKL is installed.

Parameters:

  • 输入 (Tensor) - 至少signal_ndim + 1维度的输入张量
  • signal_ndim (int) - 每个信号中的维数。 signal_ndim只能是1,2或3
  • 归一化 (bool 任选) - 控制是否返回归一化结果。默认值:False
  • 单独 (bool 任选) - 控制input是否为半数以避免冗余,例如, rfft() 。默认值:True
  • signal_sizes (列表或torch.Size,可选) - 原始信号的大小(无批量维度)。默认值:None
Returns: 包含复数到实数逆傅立叶变换结果的张量
Return type: Tensor

Example:

  1. >>> x = torch.randn(4, 4)
  2. >>> torch.rfft(x, 2, onesided=True).shape
  3. torch.Size([4, 3, 2])
  4. >>>
  5. >>> # notice that with onesided=True, output size does not determine the original signal size
  6. >>> x = torch.randn(4, 5)
  7. >>> torch.rfft(x, 2, onesided=True).shape
  8. torch.Size([4, 3, 2])
  9. >>>
  10. >>> # now we use the original shape to recover x
  11. >>> x
  12. tensor([[-0.8992, 0.6117, -1.6091, -0.4155, -0.8346],
  13. [-2.1596, -0.0853, 0.7232, 0.1941, -0.0789],
  14. [-2.0329, 1.1031, 0.6869, -0.5042, 0.9895],
  15. [-0.1884, 0.2858, -1.5831, 0.9917, -0.8356]])
  16. >>> y = torch.rfft(x, 2, onesided=True)
  17. >>> torch.irfft(y, 2, onesided=True, signal_sizes=x.shape) # recover x
  18. tensor([[-0.8992, 0.6117, -1.6091, -0.4155, -0.8346],
  19. [-2.1596, -0.0853, 0.7232, 0.1941, -0.0789],
  20. [-2.0329, 1.1031, 0.6869, -0.5042, 0.9895],
  21. [-0.1884, 0.2858, -1.5831, 0.9917, -0.8356]])
  1. torch.stft(input, n_fft, hop_length=None, win_length=None, window=None, center=True, pad_mode='reflect', normalized=False, onesided=True)

短时傅立叶变换(STFT)。

忽略可选批处理维度,此方法计算以下表达式:

光谱行动 - 图23

其中 光谱行动 - 图24 是滑动窗口的索引, 光谱行动 - 图25光谱行动 - 图26 的频率。当onesided是默认值True时,

  • input必须是1-D时间序列或2-D批时间序列。
  • 如果hop_lengthNone(默认值),则视为等于floor(n_fft / 4)
  • 如果win_lengthNone(默认值),则视为等于n_fft
  • window可以是尺寸win_length的1-D张量,例如来自 torch.hann_window() 。如果windowNone(默认值),则视为在窗口中的任何地方都有 光谱行动 - 图27 。如果 光谱行动 - 图28window将在施加之前在长度n_fft的两侧填充。
  • 如果centerTrue(默认值),则input将在两侧填充,以便 光谱行动 - 图29 帧在 光谱行动 - 图30 时间居中。否则, 光谱行动 - 图31 - 帧在时间 光谱行动 - 图32 开始。
  • pad_mode确定centerTrueinput上使用的填充方法。有关所有可用选项,请参阅 torch.nn.functional.pad() 。默认为"reflect"
  • 如果onesidedTrue(默认值),则仅返回 光谱行动 - 图33光谱行动 - 图34 的值,因为实数到复数的傅里叶变换满足共轭对称性,即, 光谱行动 - 图35
  • 如果normalizedTrue(默认为False),则该函数返回标准化的STFT结果,即乘以 光谱行动 - 图36

将实部和虚部一起作为一个尺寸 光谱行动 - 图37 返回,其中 光谱行动 - 图38input光谱行动 - 图39的可选批量大小是应用STFT的频率的数量, 光谱行动 - 图40 是使用的帧的总数,并且最后维度中的每对表示作为实部和虚部的复数。

Warning

此功能在0.4.1版本上更改了签名。使用先前的签名调用可能会导致错误或返回错误的结果。

Parameters:

  • 输入 (Tensor) - 输入张量
  • n_fft (int) - 傅立叶变换的大小
  • hop_length (int 可选) - 相邻滑动窗口帧之间的距离。默认值:None(视为等于floor(n_fft / 4)
  • win_length (int 任选) - 窗口框架和STFT过滤器的大小。默认值:None(视为等于n_fft
  • 窗口 (Tensor 可选) - 可选窗函数。默认值:None(被视为所有 光谱行动 - 图41 s的窗口)
  • 中心 (bool 任选) - 是否在两侧垫input使 光谱行动 - 图42 第一帧以时间 光谱行动 - 图43 为中心。默认值:True
  • pad_mode (string 可选) - 控制centerTrue时使用的填充方法。默认值:"reflect"
  • 归一化 (bool 任选) - 控制是否返回归一化STFT结果默认值:False
  • 单独 (bool 可选) - 控制是否返回一半结果以避免冗余默认:True
Returns: 包含具有上述形状的STFT结果的张量
Return type: Tensor
  1. torch.bartlett_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

巴特利特的窗口功能。

光谱行动 - 图44

其中 光谱行动 - 图45 是完整的窗口大小。

输入window_length是控制返回窗口大小的正整数。 periodic标志确定返回的窗口是否从对称窗口中删除最后一个重复值,并准备用作具有 torch.stft() 等功能的周期窗口。因此,如果periodic为真,则上式中的 光谱行动 - 图46 实际上是 光谱行动 - 图47 。此外,我们总是torch.bartlett_window(L, periodic=True)等于torch.bartlett_window(L + 1, periodic=False)[:-1])

Note

如果window_length 光谱行动 - 图48 ,则返回的窗口包含单个值1。

Parameters:

  • window_length (int) - 返回窗口的大小
  • 周期性 (bool 可选) - 如果为True,则返回一个窗口作为周期函数。如果为False,则返回对称窗口。
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。默认值:if None,使用全局默认值(参见 torch.set_default_tensor_type())。仅支持浮点类型。
  • 布局 (torch.layout ,可选) - 返回窗口张量的理想布局。仅支持torch.strided(密集布局)。
  • 设备 (torch.device ,可选) - 返回张量的所需设备。默认值:如果None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。 device将是CPU张量类型的CPU和CUDA张量类型的当前CUDA设备。
  • requires_grad (bool 可选) - 如果autograd应该记录对返回张量的操作。默认值:False
Returns: 含有窗口的1-D张量 光谱行动 - 图49
Return type: Tensor
  1. torch.blackman_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

布莱克曼窗口功能。

光谱行动 - 图50

where 光谱行动 - 图51 is the full window size.

输入window_length是控制返回窗口大小的正整数。 periodic标志确定返回的窗口是否从对称窗口中删除最后一个重复值,并准备用作具有 torch.stft() 等功能的周期窗口。因此,如果periodic为真,则上式中的 光谱行动 - 图52 实际上是 光谱行动 - 图53 。此外,我们总是torch.blackman_window(L, periodic=True)等于torch.blackman_window(L + 1, periodic=False)[:-1])

Note

If window_length 光谱行动 - 图54, the returned window contains a single value 1.

Parameters:

  • window_length (int) - 返回窗口的大小
  • 周期性 (bool 可选) - 如果为True,则返回一个窗口作为周期函数。如果为False,则返回对称窗口。
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。默认值:if None,使用全局默认值(参见 torch.set_default_tensor_type())。仅支持浮点类型。
  • 布局 (torch.layout ,可选) - 返回窗口张量的理想布局。仅支持torch.strided(密集布局)。
  • 设备 (torch.device ,可选) - 返回张量的所需设备。默认值:如果None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。 device将是CPU张量类型的CPU和CUDA张量类型的当前CUDA设备。
  • requires_grad (bool 可选) - 如果autograd应该记录对返回张量的操作。默认值:False
Returns: A 1-D tensor of size 光谱行动 - 图55 containing the window
Return type: Tensor
  1. torch.hamming_window(window_length, periodic=True, alpha=0.54, beta=0.46, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

汉明窗功能。

光谱行动 - 图56

where 光谱行动 - 图57 is the full window size.

输入window_length是控制返回窗口大小的正整数。 periodic标志确定返回的窗口是否从对称窗口中删除最后一个重复值,并准备用作具有 torch.stft() 等功能的周期窗口。因此,如果periodic为真,则上式中的 光谱行动 - 图58 实际上是 光谱行动 - 图59 。此外,我们总是torch.hamming_window(L, periodic=True)等于torch.hamming_window(L + 1, periodic=False)[:-1])

Note

If window_length 光谱行动 - 图60, the returned window contains a single value 1.

Note

这是 torch.hann_window() 的通用版本。

Parameters:

  • window_length (int) - 返回窗口的大小
  • 周期性 (bool 可选) - 如果为True,则返回一个窗口作为周期函数。如果为False,则返回对称窗口。
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。默认值:if None,使用全局默认值(参见 torch.set_default_tensor_type())。仅支持浮点类型。
  • 布局 (torch.layout ,可选) - 返回窗口张量的理想布局。仅支持torch.strided(密集布局)。
  • 设备 (torch.device ,可选) - 返回张量的所需设备。默认值:如果None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。 device将是CPU张量类型的CPU和CUDA张量类型的当前CUDA设备。
  • requires_grad (bool 可选) - 如果autograd应该记录对返回张量的操作。默认值:False
Returns: A 1-D tensor of size 光谱行动 - 图61 containing the window
Return type: Tensor
  1. torch.hann_window(window_length, periodic=True, dtype=None, layout=torch.strided, device=None, requires_grad=False) Tensor

汉恩窗功能。

光谱行动 - 图62

where 光谱行动 - 图63 is the full window size.

输入window_length是控制返回窗口大小的正整数。 periodic标志确定返回的窗口是否从对称窗口中删除最后一个重复值,并准备用作具有 torch.stft() 等功能的周期窗口。因此,如果periodic为真,则上式中的 光谱行动 - 图64 实际上是 光谱行动 - 图65 。此外,我们总是torch.hann_window(L, periodic=True)等于torch.hann_window(L + 1, periodic=False)[:-1])

Note

If window_length 光谱行动 - 图66, the returned window contains a single value 1.

Parameters:

  • window_length (int) - 返回窗口的大小
  • 周期性 (bool 可选) - 如果为True,则返回一个窗口作为周期函数。如果为False,则返回对称窗口。
  • dtype (torch.dtype ,可选) - 返回张量的所需数据类型。默认值:if None,使用全局默认值(参见 torch.set_default_tensor_type())。仅支持浮点类型。
  • 布局 (torch.layout ,可选) - 返回窗口张量的理想布局。仅支持torch.strided(密集布局)。
  • 设备 (torch.device ,可选) - 返回张量的所需设备。默认值:如果None,则使用当前设备作为默认张量类型(参见 torch.set_default_tensor_type())。 device将是CPU张量类型的CPU和CUDA张量类型的当前CUDA设备。
  • requires_grad (bool 可选) - 如果autograd应该记录对返回张量的操作。默认值:False
Returns: A 1-D tensor of size 光谱行动 - 图67 containing the window
Return type: Tensor