windows.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. # mypy: allow-untyped-defs
  2. from collections.abc import Iterable
  3. from math import sqrt
  4. from typing import Callable, Optional, TypeVar
  5. import torch
  6. from torch import Tensor
  7. from torch._torch_docs import factory_common_args, merge_dicts, parse_kwargs
  8. __all__ = [
  9. "bartlett",
  10. "blackman",
  11. "cosine",
  12. "exponential",
  13. "gaussian",
  14. "general_cosine",
  15. "general_hamming",
  16. "hamming",
  17. "hann",
  18. "kaiser",
  19. "nuttall",
  20. ]
  21. _T = TypeVar("_T")
  22. window_common_args = merge_dicts(
  23. parse_kwargs(
  24. """
  25. M (int): the length of the window.
  26. In other words, the number of points of the returned window.
  27. sym (bool, optional): If `False`, returns a periodic window suitable for use in spectral analysis.
  28. If `True`, returns a symmetric window suitable for use in filter design. Default: `True`.
  29. """
  30. ),
  31. factory_common_args,
  32. {
  33. "normalization": "The window is normalized to 1 (maximum value is 1). However, the 1 doesn't appear if "
  34. ":attr:`M` is even and :attr:`sym` is `True`.",
  35. },
  36. )
  37. def _add_docstr(*args: str) -> Callable[[_T], _T]:
  38. r"""Adds docstrings to a given decorated function.
  39. Specially useful when then docstrings needs string interpolation, e.g., with
  40. str.format().
  41. REMARK: Do not use this function if the docstring doesn't need string
  42. interpolation, just write a conventional docstring.
  43. Args:
  44. args (str):
  45. """
  46. def decorator(o: _T) -> _T:
  47. o.__doc__ = "".join(args)
  48. return o
  49. return decorator
  50. def _window_function_checks(
  51. function_name: str, M: int, dtype: torch.dtype, layout: torch.layout
  52. ) -> None:
  53. r"""Performs common checks for all the defined windows.
  54. This function should be called before computing any window.
  55. Args:
  56. function_name (str): name of the window function.
  57. M (int): length of the window.
  58. dtype (:class:`torch.dtype`): the desired data type of returned tensor.
  59. layout (:class:`torch.layout`): the desired layout of returned tensor.
  60. """
  61. if M < 0:
  62. raise ValueError(
  63. f"{function_name} requires non-negative window length, got M={M}"
  64. )
  65. if layout is not torch.strided:
  66. raise ValueError(
  67. f"{function_name} is implemented for strided tensors only, got: {layout}"
  68. )
  69. if dtype not in [torch.float32, torch.float64]:
  70. raise ValueError(
  71. f"{function_name} expects float32 or float64 dtypes, got: {dtype}"
  72. )
  73. @_add_docstr(
  74. r"""
  75. Computes a window with an exponential waveform.
  76. Also known as Poisson window.
  77. The exponential window is defined as follows:
  78. .. math::
  79. w_n = \exp{\left(-\frac{|n - c|}{\tau}\right)}
  80. where `c` is the ``center`` of the window.
  81. """,
  82. r"""
  83. {normalization}
  84. Args:
  85. {M}
  86. Keyword args:
  87. center (float, optional): where the center of the window will be located.
  88. Default: `M / 2` if `sym` is `False`, else `(M - 1) / 2`.
  89. tau (float, optional): the decay value.
  90. Tau is generally associated with a percentage, that means, that the value should
  91. vary within the interval (0, 100]. If tau is 100, it is considered the uniform window.
  92. Default: 1.0.
  93. {sym}
  94. {dtype}
  95. {layout}
  96. {device}
  97. {requires_grad}
  98. Examples::
  99. >>> # Generates a symmetric exponential window of size 10 and with a decay value of 1.0.
  100. >>> # The center will be at (M - 1) / 2, where M is 10.
  101. >>> torch.signal.windows.exponential(10)
  102. tensor([0.0111, 0.0302, 0.0821, 0.2231, 0.6065, 0.6065, 0.2231, 0.0821, 0.0302, 0.0111])
  103. >>> # Generates a periodic exponential window and decay factor equal to .5
  104. >>> torch.signal.windows.exponential(10, sym=False,tau=.5)
  105. tensor([4.5400e-05, 3.3546e-04, 2.4788e-03, 1.8316e-02, 1.3534e-01, 1.0000e+00, 1.3534e-01, 1.8316e-02, 2.4788e-03, 3.3546e-04])
  106. """.format(**window_common_args),
  107. )
  108. def exponential(
  109. M: int,
  110. *,
  111. center: Optional[float] = None,
  112. tau: float = 1.0,
  113. sym: bool = True,
  114. dtype: Optional[torch.dtype] = None,
  115. layout: torch.layout = torch.strided,
  116. device: Optional[torch.device] = None,
  117. requires_grad: bool = False,
  118. ) -> Tensor:
  119. if dtype is None:
  120. dtype = torch.get_default_dtype()
  121. _window_function_checks("exponential", M, dtype, layout)
  122. if tau <= 0:
  123. raise ValueError(f"Tau must be positive, got: {tau} instead.")
  124. if sym and center is not None:
  125. raise ValueError("Center must be None for symmetric windows")
  126. if M == 0:
  127. return torch.empty(
  128. (0,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  129. )
  130. if center is None:
  131. center = (M if not sym and M > 1 else M - 1) / 2.0
  132. constant = 1 / tau
  133. k = torch.linspace(
  134. start=-center * constant,
  135. end=(-center + (M - 1)) * constant,
  136. steps=M,
  137. dtype=dtype,
  138. layout=layout,
  139. device=device,
  140. requires_grad=requires_grad,
  141. )
  142. return torch.exp(-torch.abs(k))
  143. @_add_docstr(
  144. r"""
  145. Computes a window with a simple cosine waveform, following the same implementation as SciPy.
  146. This window is also known as the sine window.
  147. The cosine window is defined as follows:
  148. .. math::
  149. w_n = \sin\left(\frac{\pi (n + 0.5)}{M}\right)
  150. This formula differs from the typical cosine window formula by incorporating a 0.5 term in the numerator,
  151. which shifts the sample positions. This adjustment results in a window that starts and ends with non-zero values.
  152. """,
  153. r"""
  154. {normalization}
  155. Args:
  156. {M}
  157. Keyword args:
  158. {sym}
  159. {dtype}
  160. {layout}
  161. {device}
  162. {requires_grad}
  163. Examples::
  164. >>> # Generates a symmetric cosine window.
  165. >>> torch.signal.windows.cosine(10)
  166. tensor([0.1564, 0.4540, 0.7071, 0.8910, 0.9877, 0.9877, 0.8910, 0.7071, 0.4540, 0.1564])
  167. >>> # Generates a periodic cosine window.
  168. >>> torch.signal.windows.cosine(10, sym=False)
  169. tensor([0.1423, 0.4154, 0.6549, 0.8413, 0.9595, 1.0000, 0.9595, 0.8413, 0.6549, 0.4154])
  170. """.format(
  171. **window_common_args,
  172. ),
  173. )
  174. def cosine(
  175. M: int,
  176. *,
  177. sym: bool = True,
  178. dtype: Optional[torch.dtype] = None,
  179. layout: torch.layout = torch.strided,
  180. device: Optional[torch.device] = None,
  181. requires_grad: bool = False,
  182. ) -> Tensor:
  183. if dtype is None:
  184. dtype = torch.get_default_dtype()
  185. _window_function_checks("cosine", M, dtype, layout)
  186. if M == 0:
  187. return torch.empty(
  188. (0,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  189. )
  190. start = 0.5
  191. constant = torch.pi / (M + 1 if not sym and M > 1 else M)
  192. k = torch.linspace(
  193. start=start * constant,
  194. end=(start + (M - 1)) * constant,
  195. steps=M,
  196. dtype=dtype,
  197. layout=layout,
  198. device=device,
  199. requires_grad=requires_grad,
  200. )
  201. return torch.sin(k)
  202. @_add_docstr(
  203. r"""
  204. Computes a window with a gaussian waveform.
  205. The gaussian window is defined as follows:
  206. .. math::
  207. w_n = \exp{\left(-\left(\frac{n}{2\sigma}\right)^2\right)}
  208. """,
  209. r"""
  210. {normalization}
  211. Args:
  212. {M}
  213. Keyword args:
  214. std (float, optional): the standard deviation of the gaussian. It controls how narrow or wide the window is.
  215. Default: 1.0.
  216. {sym}
  217. {dtype}
  218. {layout}
  219. {device}
  220. {requires_grad}
  221. Examples::
  222. >>> # Generates a symmetric gaussian window with a standard deviation of 1.0.
  223. >>> torch.signal.windows.gaussian(10)
  224. tensor([4.0065e-05, 2.1875e-03, 4.3937e-02, 3.2465e-01, 8.8250e-01, 8.8250e-01, 3.2465e-01, 4.3937e-02, 2.1875e-03, 4.0065e-05])
  225. >>> # Generates a periodic gaussian window and standard deviation equal to 0.9.
  226. >>> torch.signal.windows.gaussian(10, sym=False,std=0.9)
  227. tensor([1.9858e-07, 5.1365e-05, 3.8659e-03, 8.4658e-02, 5.3941e-01, 1.0000e+00, 5.3941e-01, 8.4658e-02, 3.8659e-03, 5.1365e-05])
  228. """.format(
  229. **window_common_args,
  230. ),
  231. )
  232. def gaussian(
  233. M: int,
  234. *,
  235. std: float = 1.0,
  236. sym: bool = True,
  237. dtype: Optional[torch.dtype] = None,
  238. layout: torch.layout = torch.strided,
  239. device: Optional[torch.device] = None,
  240. requires_grad: bool = False,
  241. ) -> Tensor:
  242. if dtype is None:
  243. dtype = torch.get_default_dtype()
  244. _window_function_checks("gaussian", M, dtype, layout)
  245. if std <= 0:
  246. raise ValueError(f"Standard deviation must be positive, got: {std} instead.")
  247. if M == 0:
  248. return torch.empty(
  249. (0,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  250. )
  251. start = -(M if not sym and M > 1 else M - 1) / 2.0
  252. constant = 1 / (std * sqrt(2))
  253. k = torch.linspace(
  254. start=start * constant,
  255. end=(start + (M - 1)) * constant,
  256. steps=M,
  257. dtype=dtype,
  258. layout=layout,
  259. device=device,
  260. requires_grad=requires_grad,
  261. )
  262. return torch.exp(-(k**2))
  263. @_add_docstr(
  264. r"""
  265. Computes the Kaiser window.
  266. The Kaiser window is defined as follows:
  267. .. math::
  268. w_n = I_0 \left( \beta \sqrt{1 - \left( {\frac{n - N/2}{N/2}} \right) ^2 } \right) / I_0( \beta )
  269. where ``I_0`` is the zeroth order modified Bessel function of the first kind (see :func:`torch.special.i0`), and
  270. ``N = M - 1 if sym else M``.
  271. """,
  272. r"""
  273. {normalization}
  274. Args:
  275. {M}
  276. Keyword args:
  277. beta (float, optional): shape parameter for the window. Must be non-negative. Default: 12.0
  278. {sym}
  279. {dtype}
  280. {layout}
  281. {device}
  282. {requires_grad}
  283. Examples::
  284. >>> # Generates a symmetric gaussian window with a standard deviation of 1.0.
  285. >>> torch.signal.windows.kaiser(5)
  286. tensor([4.0065e-05, 2.1875e-03, 4.3937e-02, 3.2465e-01, 8.8250e-01, 8.8250e-01, 3.2465e-01, 4.3937e-02, 2.1875e-03, 4.0065e-05])
  287. >>> # Generates a periodic gaussian window and standard deviation equal to 0.9.
  288. >>> torch.signal.windows.kaiser(5, sym=False,std=0.9)
  289. tensor([1.9858e-07, 5.1365e-05, 3.8659e-03, 8.4658e-02, 5.3941e-01, 1.0000e+00, 5.3941e-01, 8.4658e-02, 3.8659e-03, 5.1365e-05])
  290. """.format(
  291. **window_common_args,
  292. ),
  293. )
  294. def kaiser(
  295. M: int,
  296. *,
  297. beta: float = 12.0,
  298. sym: bool = True,
  299. dtype: Optional[torch.dtype] = None,
  300. layout: torch.layout = torch.strided,
  301. device: Optional[torch.device] = None,
  302. requires_grad: bool = False,
  303. ) -> Tensor:
  304. if dtype is None:
  305. dtype = torch.get_default_dtype()
  306. _window_function_checks("kaiser", M, dtype, layout)
  307. if beta < 0:
  308. raise ValueError(f"beta must be non-negative, got: {beta} instead.")
  309. if M == 0:
  310. return torch.empty(
  311. (0,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  312. )
  313. if M == 1:
  314. return torch.ones(
  315. (1,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  316. )
  317. # Avoid NaNs by casting `beta` to the appropriate dtype.
  318. beta = torch.tensor(beta, dtype=dtype, device=device)
  319. start = -beta
  320. constant = 2.0 * beta / (M if not sym else M - 1)
  321. end = torch.minimum(beta, start + (M - 1) * constant)
  322. k = torch.linspace(
  323. start=start,
  324. end=end,
  325. steps=M,
  326. dtype=dtype,
  327. layout=layout,
  328. device=device,
  329. requires_grad=requires_grad,
  330. )
  331. return torch.i0(torch.sqrt(beta * beta - torch.pow(k, 2))) / torch.i0(beta)
  332. @_add_docstr(
  333. r"""
  334. Computes the Hamming window.
  335. The Hamming window is defined as follows:
  336. .. math::
  337. w_n = \alpha - \beta\ \cos \left( \frac{2 \pi n}{M - 1} \right)
  338. """,
  339. r"""
  340. {normalization}
  341. Arguments:
  342. {M}
  343. Keyword args:
  344. {sym}
  345. alpha (float, optional): The coefficient :math:`\alpha` in the equation above.
  346. beta (float, optional): The coefficient :math:`\beta` in the equation above.
  347. {dtype}
  348. {layout}
  349. {device}
  350. {requires_grad}
  351. Examples::
  352. >>> # Generates a symmetric Hamming window.
  353. >>> torch.signal.windows.hamming(10)
  354. tensor([0.0800, 0.1876, 0.4601, 0.7700, 0.9723, 0.9723, 0.7700, 0.4601, 0.1876, 0.0800])
  355. >>> # Generates a periodic Hamming window.
  356. >>> torch.signal.windows.hamming(10, sym=False)
  357. tensor([0.0800, 0.1679, 0.3979, 0.6821, 0.9121, 1.0000, 0.9121, 0.6821, 0.3979, 0.1679])
  358. """.format(**window_common_args),
  359. )
  360. def hamming(
  361. M: int,
  362. *,
  363. sym: bool = True,
  364. dtype: Optional[torch.dtype] = None,
  365. layout: torch.layout = torch.strided,
  366. device: Optional[torch.device] = None,
  367. requires_grad: bool = False,
  368. ) -> Tensor:
  369. return general_hamming(
  370. M,
  371. sym=sym,
  372. dtype=dtype,
  373. layout=layout,
  374. device=device,
  375. requires_grad=requires_grad,
  376. )
  377. @_add_docstr(
  378. r"""
  379. Computes the Hann window.
  380. The Hann window is defined as follows:
  381. .. math::
  382. w_n = \frac{1}{2}\ \left[1 - \cos \left( \frac{2 \pi n}{M - 1} \right)\right] =
  383. \sin^2 \left( \frac{\pi n}{M - 1} \right)
  384. """,
  385. r"""
  386. {normalization}
  387. Arguments:
  388. {M}
  389. Keyword args:
  390. {sym}
  391. {dtype}
  392. {layout}
  393. {device}
  394. {requires_grad}
  395. Examples::
  396. >>> # Generates a symmetric Hann window.
  397. >>> torch.signal.windows.hann(10)
  398. tensor([0.0000, 0.1170, 0.4132, 0.7500, 0.9698, 0.9698, 0.7500, 0.4132, 0.1170, 0.0000])
  399. >>> # Generates a periodic Hann window.
  400. >>> torch.signal.windows.hann(10, sym=False)
  401. tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955])
  402. """.format(**window_common_args),
  403. )
  404. def hann(
  405. M: int,
  406. *,
  407. sym: bool = True,
  408. dtype: Optional[torch.dtype] = None,
  409. layout: torch.layout = torch.strided,
  410. device: Optional[torch.device] = None,
  411. requires_grad: bool = False,
  412. ) -> Tensor:
  413. return general_hamming(
  414. M,
  415. alpha=0.5,
  416. sym=sym,
  417. dtype=dtype,
  418. layout=layout,
  419. device=device,
  420. requires_grad=requires_grad,
  421. )
  422. @_add_docstr(
  423. r"""
  424. Computes the Blackman window.
  425. The Blackman window is defined as follows:
  426. .. math::
  427. w_n = 0.42 - 0.5 \cos \left( \frac{2 \pi n}{M - 1} \right) + 0.08 \cos \left( \frac{4 \pi n}{M - 1} \right)
  428. """,
  429. r"""
  430. {normalization}
  431. Arguments:
  432. {M}
  433. Keyword args:
  434. {sym}
  435. {dtype}
  436. {layout}
  437. {device}
  438. {requires_grad}
  439. Examples::
  440. >>> # Generates a symmetric Blackman window.
  441. >>> torch.signal.windows.blackman(5)
  442. tensor([-1.4901e-08, 3.4000e-01, 1.0000e+00, 3.4000e-01, -1.4901e-08])
  443. >>> # Generates a periodic Blackman window.
  444. >>> torch.signal.windows.blackman(5, sym=False)
  445. tensor([-1.4901e-08, 2.0077e-01, 8.4923e-01, 8.4923e-01, 2.0077e-01])
  446. """.format(**window_common_args),
  447. )
  448. def blackman(
  449. M: int,
  450. *,
  451. sym: bool = True,
  452. dtype: Optional[torch.dtype] = None,
  453. layout: torch.layout = torch.strided,
  454. device: Optional[torch.device] = None,
  455. requires_grad: bool = False,
  456. ) -> Tensor:
  457. if dtype is None:
  458. dtype = torch.get_default_dtype()
  459. _window_function_checks("blackman", M, dtype, layout)
  460. return general_cosine(
  461. M,
  462. a=[0.42, 0.5, 0.08],
  463. sym=sym,
  464. dtype=dtype,
  465. layout=layout,
  466. device=device,
  467. requires_grad=requires_grad,
  468. )
  469. @_add_docstr(
  470. r"""
  471. Computes the Bartlett window.
  472. The Bartlett window is defined as follows:
  473. .. math::
  474. w_n = 1 - \left| \frac{2n}{M - 1} - 1 \right| = \begin{cases}
  475. \frac{2n}{M - 1} & \text{if } 0 \leq n \leq \frac{M - 1}{2} \\
  476. 2 - \frac{2n}{M - 1} & \text{if } \frac{M - 1}{2} < n < M \\ \end{cases}
  477. """,
  478. r"""
  479. {normalization}
  480. Arguments:
  481. {M}
  482. Keyword args:
  483. {sym}
  484. {dtype}
  485. {layout}
  486. {device}
  487. {requires_grad}
  488. Examples::
  489. >>> # Generates a symmetric Bartlett window.
  490. >>> torch.signal.windows.bartlett(10)
  491. tensor([0.0000, 0.2222, 0.4444, 0.6667, 0.8889, 0.8889, 0.6667, 0.4444, 0.2222, 0.0000])
  492. >>> # Generates a periodic Bartlett window.
  493. >>> torch.signal.windows.bartlett(10, sym=False)
  494. tensor([0.0000, 0.2000, 0.4000, 0.6000, 0.8000, 1.0000, 0.8000, 0.6000, 0.4000, 0.2000])
  495. """.format(**window_common_args),
  496. )
  497. def bartlett(
  498. M: int,
  499. *,
  500. sym: bool = True,
  501. dtype: Optional[torch.dtype] = None,
  502. layout: torch.layout = torch.strided,
  503. device: Optional[torch.device] = None,
  504. requires_grad: bool = False,
  505. ) -> Tensor:
  506. if dtype is None:
  507. dtype = torch.get_default_dtype()
  508. _window_function_checks("bartlett", M, dtype, layout)
  509. if M == 0:
  510. return torch.empty(
  511. (0,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  512. )
  513. if M == 1:
  514. return torch.ones(
  515. (1,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  516. )
  517. start = -1
  518. constant = 2 / (M if not sym else M - 1)
  519. k = torch.linspace(
  520. start=start,
  521. end=start + (M - 1) * constant,
  522. steps=M,
  523. dtype=dtype,
  524. layout=layout,
  525. device=device,
  526. requires_grad=requires_grad,
  527. )
  528. return 1 - torch.abs(k)
  529. @_add_docstr(
  530. r"""
  531. Computes the general cosine window.
  532. The general cosine window is defined as follows:
  533. .. math::
  534. w_n = \sum^{M-1}_{i=0} (-1)^i a_i \cos{ \left( \frac{2 \pi i n}{M - 1}\right)}
  535. """,
  536. r"""
  537. {normalization}
  538. Arguments:
  539. {M}
  540. Keyword args:
  541. a (Iterable): the coefficients associated to each of the cosine functions.
  542. {sym}
  543. {dtype}
  544. {layout}
  545. {device}
  546. {requires_grad}
  547. Examples::
  548. >>> # Generates a symmetric general cosine window with 3 coefficients.
  549. >>> torch.signal.windows.general_cosine(10, a=[0.46, 0.23, 0.31], sym=True)
  550. tensor([0.5400, 0.3376, 0.1288, 0.4200, 0.9136, 0.9136, 0.4200, 0.1288, 0.3376, 0.5400])
  551. >>> # Generates a periodic general cosine window with 2 coefficients.
  552. >>> torch.signal.windows.general_cosine(10, a=[0.5, 1 - 0.5], sym=False)
  553. tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955])
  554. """.format(**window_common_args),
  555. )
  556. def general_cosine(
  557. M,
  558. *,
  559. a: Iterable,
  560. sym: bool = True,
  561. dtype: Optional[torch.dtype] = None,
  562. layout: torch.layout = torch.strided,
  563. device: Optional[torch.device] = None,
  564. requires_grad: bool = False,
  565. ) -> Tensor:
  566. if dtype is None:
  567. dtype = torch.get_default_dtype()
  568. _window_function_checks("general_cosine", M, dtype, layout)
  569. if M == 0:
  570. return torch.empty(
  571. (0,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  572. )
  573. if M == 1:
  574. return torch.ones(
  575. (1,), dtype=dtype, layout=layout, device=device, requires_grad=requires_grad
  576. )
  577. if not isinstance(a, Iterable):
  578. raise TypeError("Coefficients must be a list/tuple")
  579. if not a:
  580. raise ValueError("Coefficients cannot be empty")
  581. constant = 2 * torch.pi / (M if not sym else M - 1)
  582. k = torch.linspace(
  583. start=0,
  584. end=(M - 1) * constant,
  585. steps=M,
  586. dtype=dtype,
  587. layout=layout,
  588. device=device,
  589. requires_grad=requires_grad,
  590. )
  591. a_i = torch.tensor(
  592. [(-1) ** i * w for i, w in enumerate(a)],
  593. device=device,
  594. dtype=dtype,
  595. requires_grad=requires_grad,
  596. )
  597. i = torch.arange(
  598. a_i.shape[0],
  599. dtype=a_i.dtype,
  600. device=a_i.device,
  601. requires_grad=a_i.requires_grad,
  602. )
  603. return (a_i.unsqueeze(-1) * torch.cos(i.unsqueeze(-1) * k)).sum(0)
  604. @_add_docstr(
  605. r"""
  606. Computes the general Hamming window.
  607. The general Hamming window is defined as follows:
  608. .. math::
  609. w_n = \alpha - (1 - \alpha) \cos{ \left( \frac{2 \pi n}{M-1} \right)}
  610. """,
  611. r"""
  612. {normalization}
  613. Arguments:
  614. {M}
  615. Keyword args:
  616. alpha (float, optional): the window coefficient. Default: 0.54.
  617. {sym}
  618. {dtype}
  619. {layout}
  620. {device}
  621. {requires_grad}
  622. Examples::
  623. >>> # Generates a symmetric Hamming window with the general Hamming window.
  624. >>> torch.signal.windows.general_hamming(10, sym=True)
  625. tensor([0.0800, 0.1876, 0.4601, 0.7700, 0.9723, 0.9723, 0.7700, 0.4601, 0.1876, 0.0800])
  626. >>> # Generates a periodic Hann window with the general Hamming window.
  627. >>> torch.signal.windows.general_hamming(10, alpha=0.5, sym=False)
  628. tensor([0.0000, 0.0955, 0.3455, 0.6545, 0.9045, 1.0000, 0.9045, 0.6545, 0.3455, 0.0955])
  629. """.format(**window_common_args),
  630. )
  631. def general_hamming(
  632. M,
  633. *,
  634. alpha: float = 0.54,
  635. sym: bool = True,
  636. dtype: Optional[torch.dtype] = None,
  637. layout: torch.layout = torch.strided,
  638. device: Optional[torch.device] = None,
  639. requires_grad: bool = False,
  640. ) -> Tensor:
  641. return general_cosine(
  642. M,
  643. a=[alpha, 1.0 - alpha],
  644. sym=sym,
  645. dtype=dtype,
  646. layout=layout,
  647. device=device,
  648. requires_grad=requires_grad,
  649. )
  650. @_add_docstr(
  651. r"""
  652. Computes the minimum 4-term Blackman-Harris window according to Nuttall.
  653. .. math::
  654. w_n = 1 - 0.36358 \cos{(z_n)} + 0.48917 \cos{(2z_n)} - 0.13659 \cos{(3z_n)} + 0.01064 \cos{(4z_n)}
  655. where :math:`z_n = \frac{2 \pi n}{M}`.
  656. """,
  657. """
  658. {normalization}
  659. Arguments:
  660. {M}
  661. Keyword args:
  662. {sym}
  663. {dtype}
  664. {layout}
  665. {device}
  666. {requires_grad}
  667. References::
  668. - A. Nuttall, "Some windows with very good sidelobe behavior,"
  669. IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 29, no. 1, pp. 84-91,
  670. Feb 1981. https://doi.org/10.1109/TASSP.1981.1163506
  671. - Heinzel G. et al., "Spectrum and spectral density estimation by the Discrete Fourier transform (DFT),
  672. including a comprehensive list of window functions and some new flat-top windows",
  673. February 15, 2002 https://holometer.fnal.gov/GH_FFT.pdf
  674. Examples::
  675. >>> # Generates a symmetric Nutall window.
  676. >>> torch.signal.windows.general_hamming(5, sym=True)
  677. tensor([3.6280e-04, 2.2698e-01, 1.0000e+00, 2.2698e-01, 3.6280e-04])
  678. >>> # Generates a periodic Nuttall window.
  679. >>> torch.signal.windows.general_hamming(5, sym=False)
  680. tensor([3.6280e-04, 1.1052e-01, 7.9826e-01, 7.9826e-01, 1.1052e-01])
  681. """.format(**window_common_args),
  682. )
  683. def nuttall(
  684. M: int,
  685. *,
  686. sym: bool = True,
  687. dtype: Optional[torch.dtype] = None,
  688. layout: torch.layout = torch.strided,
  689. device: Optional[torch.device] = None,
  690. requires_grad: bool = False,
  691. ) -> Tensor:
  692. return general_cosine(
  693. M,
  694. a=[0.3635819, 0.4891775, 0.1365995, 0.0106411],
  695. sym=sym,
  696. dtype=dtype,
  697. layout=layout,
  698. device=device,
  699. requires_grad=requires_grad,
  700. )