_realtransforms.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. from ._basic import _dispatch
  2. from scipy._lib.uarray import Dispatchable
  3. import numpy as np
  4. __all__ = ['dct', 'idct', 'dst', 'idst', 'dctn', 'idctn', 'dstn', 'idstn']
  5. @_dispatch
  6. def dctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
  7. workers=None, *, orthogonalize=None):
  8. """
  9. Return multidimensional Discrete Cosine Transform along the specified axes.
  10. Parameters
  11. ----------
  12. x : array_like
  13. The input array.
  14. type : {1, 2, 3, 4}, optional
  15. Type of the DCT (see Notes). Default type is 2.
  16. s : int or array_like of ints or None, optional
  17. The shape of the result. If both `s` and `axes` (see below) are None,
  18. `s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is
  19. ``numpy.take(x.shape, axes, axis=0)``.
  20. If ``s[i] > x.shape[i]``, the ith dimension is padded with zeros.
  21. If ``s[i] < x.shape[i]``, the ith dimension is truncated to length
  22. ``s[i]``.
  23. If any element of `s` is -1, the size of the corresponding dimension of
  24. `x` is used.
  25. axes : int or array_like of ints or None, optional
  26. Axes over which the DCT is computed. If not given, the last ``len(s)``
  27. axes are used, or all axes if `s` is also not specified.
  28. norm : {"backward", "ortho", "forward"}, optional
  29. Normalization mode (see Notes). Default is "backward".
  30. overwrite_x : bool, optional
  31. If True, the contents of `x` can be destroyed; the default is False.
  32. workers : int, optional
  33. Maximum number of workers to use for parallel computation. If negative,
  34. the value wraps around from ``os.cpu_count()``.
  35. See :func:`~scipy.fft.fft` for more details.
  36. orthogonalize : bool, optional
  37. Whether to use the orthogonalized DCT variant (see Notes).
  38. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  39. .. versionadded:: 1.8.0
  40. Returns
  41. -------
  42. y : ndarray of real
  43. The transformed input array.
  44. See Also
  45. --------
  46. idctn : Inverse multidimensional DCT
  47. Notes
  48. -----
  49. For full details of the DCT types and normalization modes, as well as
  50. references, see `dct`.
  51. Examples
  52. --------
  53. >>> import numpy as np
  54. >>> from scipy.fft import dctn, idctn
  55. >>> rng = np.random.default_rng()
  56. >>> y = rng.standard_normal((16, 16))
  57. >>> np.allclose(y, idctn(dctn(y)))
  58. True
  59. """
  60. return (Dispatchable(x, np.ndarray),)
  61. @_dispatch
  62. def idctn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
  63. workers=None, orthogonalize=None):
  64. """
  65. Return multidimensional Inverse Discrete Cosine Transform along the specified axes.
  66. Parameters
  67. ----------
  68. x : array_like
  69. The input array.
  70. type : {1, 2, 3, 4}, optional
  71. Type of the DCT (see Notes). Default type is 2.
  72. s : int or array_like of ints or None, optional
  73. The shape of the result. If both `s` and `axes` (see below) are
  74. None, `s` is ``x.shape``; if `s` is None but `axes` is
  75. not None, then `s` is ``numpy.take(x.shape, axes, axis=0)``.
  76. If ``s[i] > x.shape[i]``, the ith dimension is padded with zeros.
  77. If ``s[i] < x.shape[i]``, the ith dimension is truncated to length
  78. ``s[i]``.
  79. If any element of `s` is -1, the size of the corresponding dimension of
  80. `x` is used.
  81. axes : int or array_like of ints or None, optional
  82. Axes over which the IDCT is computed. If not given, the last ``len(s)``
  83. axes are used, or all axes if `s` is also not specified.
  84. norm : {"backward", "ortho", "forward"}, optional
  85. Normalization mode (see Notes). Default is "backward".
  86. overwrite_x : bool, optional
  87. If True, the contents of `x` can be destroyed; the default is False.
  88. workers : int, optional
  89. Maximum number of workers to use for parallel computation. If negative,
  90. the value wraps around from ``os.cpu_count()``.
  91. See :func:`~scipy.fft.fft` for more details.
  92. orthogonalize : bool, optional
  93. Whether to use the orthogonalized IDCT variant (see Notes).
  94. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  95. .. versionadded:: 1.8.0
  96. Returns
  97. -------
  98. y : ndarray of real
  99. The transformed input array.
  100. See Also
  101. --------
  102. dctn : multidimensional DCT
  103. Notes
  104. -----
  105. For full details of the IDCT types and normalization modes, as well as
  106. references, see `idct`.
  107. Examples
  108. --------
  109. >>> import numpy as np
  110. >>> from scipy.fft import dctn, idctn
  111. >>> rng = np.random.default_rng()
  112. >>> y = rng.standard_normal((16, 16))
  113. >>> np.allclose(y, idctn(dctn(y)))
  114. True
  115. """
  116. return (Dispatchable(x, np.ndarray),)
  117. @_dispatch
  118. def dstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
  119. workers=None, orthogonalize=None):
  120. """
  121. Return multidimensional Discrete Sine Transform along the specified axes.
  122. Parameters
  123. ----------
  124. x : array_like
  125. The input array.
  126. type : {1, 2, 3, 4}, optional
  127. Type of the DST (see Notes). Default type is 2.
  128. s : int or array_like of ints or None, optional
  129. The shape of the result. If both `s` and `axes` (see below) are None,
  130. `s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is
  131. ``numpy.take(x.shape, axes, axis=0)``.
  132. If ``s[i] > x.shape[i]``, the ith dimension is padded with zeros.
  133. If ``s[i] < x.shape[i]``, the ith dimension is truncated to length
  134. ``s[i]``.
  135. If any element of `shape` is -1, the size of the corresponding dimension
  136. of `x` is used.
  137. axes : int or array_like of ints or None, optional
  138. Axes over which the DST is computed. If not given, the last ``len(s)``
  139. axes are used, or all axes if `s` is also not specified.
  140. norm : {"backward", "ortho", "forward"}, optional
  141. Normalization mode (see Notes). Default is "backward".
  142. overwrite_x : bool, optional
  143. If True, the contents of `x` can be destroyed; the default is False.
  144. workers : int, optional
  145. Maximum number of workers to use for parallel computation. If negative,
  146. the value wraps around from ``os.cpu_count()``.
  147. See :func:`~scipy.fft.fft` for more details.
  148. orthogonalize : bool, optional
  149. Whether to use the orthogonalized DST variant (see Notes).
  150. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  151. .. versionadded:: 1.8.0
  152. Returns
  153. -------
  154. y : ndarray of real
  155. The transformed input array.
  156. See Also
  157. --------
  158. idstn : Inverse multidimensional DST
  159. Notes
  160. -----
  161. For full details of the DST types and normalization modes, as well as
  162. references, see `dst`.
  163. Examples
  164. --------
  165. >>> import numpy as np
  166. >>> from scipy.fft import dstn, idstn
  167. >>> rng = np.random.default_rng()
  168. >>> y = rng.standard_normal((16, 16))
  169. >>> np.allclose(y, idstn(dstn(y)))
  170. True
  171. """
  172. return (Dispatchable(x, np.ndarray),)
  173. @_dispatch
  174. def idstn(x, type=2, s=None, axes=None, norm=None, overwrite_x=False,
  175. workers=None, orthogonalize=None):
  176. """
  177. Return multidimensional Inverse Discrete Sine Transform along the specified axes.
  178. Parameters
  179. ----------
  180. x : array_like
  181. The input array.
  182. type : {1, 2, 3, 4}, optional
  183. Type of the DST (see Notes). Default type is 2.
  184. s : int or array_like of ints or None, optional
  185. The shape of the result. If both `s` and `axes` (see below) are None,
  186. `s` is ``x.shape``; if `s` is None but `axes` is not None, then `s` is
  187. ``numpy.take(x.shape, axes, axis=0)``.
  188. If ``s[i] > x.shape[i]``, the ith dimension is padded with zeros.
  189. If ``s[i] < x.shape[i]``, the ith dimension is truncated to length
  190. ``s[i]``.
  191. If any element of `s` is -1, the size of the corresponding dimension of
  192. `x` is used.
  193. axes : int or array_like of ints or None, optional
  194. Axes over which the IDST is computed. If not given, the last ``len(s)``
  195. axes are used, or all axes if `s` is also not specified.
  196. norm : {"backward", "ortho", "forward"}, optional
  197. Normalization mode (see Notes). Default is "backward".
  198. overwrite_x : bool, optional
  199. If True, the contents of `x` can be destroyed; the default is False.
  200. workers : int, optional
  201. Maximum number of workers to use for parallel computation. If negative,
  202. the value wraps around from ``os.cpu_count()``.
  203. See :func:`~scipy.fft.fft` for more details.
  204. orthogonalize : bool, optional
  205. Whether to use the orthogonalized IDST variant (see Notes).
  206. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  207. .. versionadded:: 1.8.0
  208. Returns
  209. -------
  210. y : ndarray of real
  211. The transformed input array.
  212. See Also
  213. --------
  214. dstn : multidimensional DST
  215. Notes
  216. -----
  217. For full details of the IDST types and normalization modes, as well as
  218. references, see `idst`.
  219. Examples
  220. --------
  221. >>> import numpy as np
  222. >>> from scipy.fft import dstn, idstn
  223. >>> rng = np.random.default_rng()
  224. >>> y = rng.standard_normal((16, 16))
  225. >>> np.allclose(y, idstn(dstn(y)))
  226. True
  227. """
  228. return (Dispatchable(x, np.ndarray),)
  229. @_dispatch
  230. def dct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None,
  231. orthogonalize=None):
  232. r"""Return the Discrete Cosine Transform of arbitrary type sequence x.
  233. Parameters
  234. ----------
  235. x : array_like
  236. The input array.
  237. type : {1, 2, 3, 4}, optional
  238. Type of the DCT (see Notes). Default type is 2.
  239. n : int, optional
  240. Length of the transform. If ``n < x.shape[axis]``, `x` is
  241. truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
  242. default results in ``n = x.shape[axis]``.
  243. axis : int, optional
  244. Axis along which the dct is computed; the default is over the
  245. last axis (i.e., ``axis=-1``).
  246. norm : {"backward", "ortho", "forward"}, optional
  247. Normalization mode (see Notes). Default is "backward".
  248. overwrite_x : bool, optional
  249. If True, the contents of `x` can be destroyed; the default is False.
  250. workers : int, optional
  251. Maximum number of workers to use for parallel computation. If negative,
  252. the value wraps around from ``os.cpu_count()``.
  253. See :func:`~scipy.fft.fft` for more details.
  254. orthogonalize : bool, optional
  255. Whether to use the orthogonalized DCT variant (see Notes).
  256. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  257. .. versionadded:: 1.8.0
  258. Returns
  259. -------
  260. y : ndarray of real
  261. The transformed input array.
  262. See Also
  263. --------
  264. idct : Inverse DCT
  265. Notes
  266. -----
  267. For a single dimension array ``x``, ``dct(x, norm='ortho')`` is equal to
  268. MATLAB ``dct(x)``.
  269. .. warning:: For ``type in {1, 2, 3}``, ``norm="ortho"`` breaks the direct
  270. correspondence with the direct Fourier transform. To recover
  271. it you must specify ``orthogonalize=False``.
  272. For ``norm="ortho"`` both the `dct` and `idct` are scaled by the same
  273. overall factor in both directions. By default, the transform is also
  274. orthogonalized which for types 1, 2 and 3 means the transform definition is
  275. modified to give orthogonality of the DCT matrix (see below).
  276. For ``norm="backward"``, there is no scaling on `dct` and the `idct` is
  277. scaled by ``1/N`` where ``N`` is the "logical" size of the DCT. For
  278. ``norm="forward"`` the ``1/N`` normalization is applied to the forward
  279. `dct` instead and the `idct` is unnormalized.
  280. There are, theoretically, 8 types of the DCT, only the first 4 types are
  281. implemented in SciPy.'The' DCT generally refers to DCT type 2, and 'the'
  282. Inverse DCT generally refers to DCT type 3.
  283. **Type I**
  284. There are several definitions of the DCT-I; we use the following
  285. (for ``norm="backward"``)
  286. .. math::
  287. y_k = x_0 + (-1)^k x_{N-1} + 2 \sum_{n=1}^{N-2} x_n \cos\left(
  288. \frac{\pi k n}{N-1} \right)
  289. If ``orthogonalize=True``, ``x[0]`` and ``x[N-1]`` are multiplied by a
  290. scaling factor of :math:`\sqrt{2}`, and ``y[0]`` and ``y[N-1]`` are divided
  291. by :math:`\sqrt{2}`. When combined with ``norm="ortho"``, this makes the
  292. corresponding matrix of coefficients orthonormal (``O @ O.T = np.eye(N)``).
  293. .. note::
  294. The DCT-I is only supported for input size > 1.
  295. **Type II**
  296. There are several definitions of the DCT-II; we use the following
  297. (for ``norm="backward"``)
  298. .. math::
  299. y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi k(2n+1)}{2N} \right)
  300. If ``orthogonalize=True``, ``y[0]`` is divided by :math:`\sqrt{2}` which,
  301. when combined with ``norm="ortho"``, makes the corresponding matrix of
  302. coefficients orthonormal (``O @ O.T = np.eye(N)``).
  303. **Type III**
  304. There are several definitions, we use the following (for
  305. ``norm="backward"``)
  306. .. math::
  307. y_k = x_0 + 2 \sum_{n=1}^{N-1} x_n \cos\left(\frac{\pi(2k+1)n}{2N}\right)
  308. If ``orthogonalize=True``, ``x[0]`` terms are multiplied by
  309. :math:`\sqrt{2}` which, when combined with ``norm="ortho"``, makes the
  310. corresponding matrix of coefficients orthonormal (``O @ O.T = np.eye(N)``).
  311. The (unnormalized) DCT-III is the inverse of the (unnormalized) DCT-II, up
  312. to a factor `2N`. The orthonormalized DCT-III is exactly the inverse of
  313. the orthonormalized DCT-II.
  314. **Type IV**
  315. There are several definitions of the DCT-IV; we use the following
  316. (for ``norm="backward"``)
  317. .. math::
  318. y_k = 2 \sum_{n=0}^{N-1} x_n \cos\left(\frac{\pi(2k+1)(2n+1)}{4N} \right)
  319. ``orthogonalize`` has no effect here, as the DCT-IV matrix is already
  320. orthogonal up to a scale factor of ``2N``.
  321. References
  322. ----------
  323. .. [1] 'A Fast Cosine Transform in One and Two Dimensions', by J.
  324. Makhoul, `IEEE Transactions on acoustics, speech and signal
  325. processing` vol. 28(1), pp. 27-34,
  326. :doi:`10.1109/TASSP.1980.1163351` (1980).
  327. .. [2] Wikipedia, "Discrete cosine transform",
  328. https://en.wikipedia.org/wiki/Discrete_cosine_transform
  329. Examples
  330. --------
  331. The Type 1 DCT is equivalent to the FFT (though faster) for real,
  332. even-symmetrical inputs. The output is also real and even-symmetrical.
  333. Half of the FFT input is used to generate half of the FFT output:
  334. >>> from scipy.fft import fft, dct
  335. >>> import numpy as np
  336. >>> fft(np.array([4., 3., 5., 10., 5., 3.])).real
  337. array([ 30., -8., 6., -2., 6., -8.])
  338. >>> dct(np.array([4., 3., 5., 10.]), 1)
  339. array([ 30., -8., 6., -2.])
  340. """
  341. return (Dispatchable(x, np.ndarray),)
  342. @_dispatch
  343. def idct(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False,
  344. workers=None, orthogonalize=None):
  345. """
  346. Return the Inverse Discrete Cosine Transform of an arbitrary type sequence.
  347. Parameters
  348. ----------
  349. x : array_like
  350. The input array.
  351. type : {1, 2, 3, 4}, optional
  352. Type of the DCT (see Notes). Default type is 2.
  353. n : int, optional
  354. Length of the transform. If ``n < x.shape[axis]``, `x` is
  355. truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
  356. default results in ``n = x.shape[axis]``.
  357. axis : int, optional
  358. Axis along which the idct is computed; the default is over the
  359. last axis (i.e., ``axis=-1``).
  360. norm : {"backward", "ortho", "forward"}, optional
  361. Normalization mode (see Notes). Default is "backward".
  362. overwrite_x : bool, optional
  363. If True, the contents of `x` can be destroyed; the default is False.
  364. workers : int, optional
  365. Maximum number of workers to use for parallel computation. If negative,
  366. the value wraps around from ``os.cpu_count()``.
  367. See :func:`~scipy.fft.fft` for more details.
  368. orthogonalize : bool, optional
  369. Whether to use the orthogonalized IDCT variant (see Notes).
  370. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  371. .. versionadded:: 1.8.0
  372. Returns
  373. -------
  374. idct : ndarray of real
  375. The transformed input array.
  376. See Also
  377. --------
  378. dct : Forward DCT
  379. Notes
  380. -----
  381. For a single dimension array `x`, ``idct(x, norm='ortho')`` is equal to
  382. MATLAB ``idct(x)``.
  383. .. warning:: For ``type in {1, 2, 3}``, ``norm="ortho"`` breaks the direct
  384. correspondence with the inverse direct Fourier transform. To
  385. recover it you must specify ``orthogonalize=False``.
  386. For ``norm="ortho"`` both the `dct` and `idct` are scaled by the same
  387. overall factor in both directions. By default, the transform is also
  388. orthogonalized which for types 1, 2 and 3 means the transform definition is
  389. modified to give orthogonality of the IDCT matrix (see `dct` for the full
  390. definitions).
  391. 'The' IDCT is the IDCT-II, which is the same as the normalized DCT-III.
  392. The IDCT is equivalent to a normal DCT except for the normalization and
  393. type. DCT type 1 and 4 are their own inverse and DCTs 2 and 3 are each
  394. other's inverses.
  395. Examples
  396. --------
  397. The Type 1 DCT is equivalent to the DFT for real, even-symmetrical
  398. inputs. The output is also real and even-symmetrical. Half of the IFFT
  399. input is used to generate half of the IFFT output:
  400. >>> from scipy.fft import ifft, idct
  401. >>> import numpy as np
  402. >>> ifft(np.array([ 30., -8., 6., -2., 6., -8.])).real
  403. array([ 4., 3., 5., 10., 5., 3.])
  404. >>> idct(np.array([ 30., -8., 6., -2.]), 1)
  405. array([ 4., 3., 5., 10.])
  406. """
  407. return (Dispatchable(x, np.ndarray),)
  408. @_dispatch
  409. def dst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False, workers=None,
  410. orthogonalize=None):
  411. r"""
  412. Return the Discrete Sine Transform of arbitrary type sequence x.
  413. Parameters
  414. ----------
  415. x : array_like
  416. The input array.
  417. type : {1, 2, 3, 4}, optional
  418. Type of the DST (see Notes). Default type is 2.
  419. n : int, optional
  420. Length of the transform. If ``n < x.shape[axis]``, `x` is
  421. truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
  422. default results in ``n = x.shape[axis]``.
  423. axis : int, optional
  424. Axis along which the dst is computed; the default is over the
  425. last axis (i.e., ``axis=-1``).
  426. norm : {"backward", "ortho", "forward"}, optional
  427. Normalization mode (see Notes). Default is "backward".
  428. overwrite_x : bool, optional
  429. If True, the contents of `x` can be destroyed; the default is False.
  430. workers : int, optional
  431. Maximum number of workers to use for parallel computation. If negative,
  432. the value wraps around from ``os.cpu_count()``.
  433. See :func:`~scipy.fft.fft` for more details.
  434. orthogonalize : bool, optional
  435. Whether to use the orthogonalized DST variant (see Notes).
  436. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  437. .. versionadded:: 1.8.0
  438. Returns
  439. -------
  440. dst : ndarray of reals
  441. The transformed input array.
  442. See Also
  443. --------
  444. idst : Inverse DST
  445. Notes
  446. -----
  447. .. warning:: For ``type in {2, 3}``, ``norm="ortho"`` breaks the direct
  448. correspondence with the direct Fourier transform. To recover
  449. it you must specify ``orthogonalize=False``.
  450. For ``norm="ortho"`` both the `dst` and `idst` are scaled by the same
  451. overall factor in both directions. By default, the transform is also
  452. orthogonalized which for types 2 and 3 means the transform definition is
  453. modified to give orthogonality of the DST matrix (see below).
  454. For ``norm="backward"``, there is no scaling on the `dst` and the `idst` is
  455. scaled by ``1/N`` where ``N`` is the "logical" size of the DST.
  456. There are, theoretically, 8 types of the DST for different combinations of
  457. even/odd boundary conditions and boundary off sets [1]_, only the first
  458. 4 types are implemented in SciPy.
  459. **Type I**
  460. There are several definitions of the DST-I; we use the following for
  461. ``norm="backward"``. DST-I assumes the input is odd around :math:`n=-1` and
  462. :math:`n=N`.
  463. .. math::
  464. y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(n+1)}{N+1}\right)
  465. Note that the DST-I is only supported for input size > 1.
  466. The (unnormalized) DST-I is its own inverse, up to a factor :math:`2(N+1)`.
  467. The orthonormalized DST-I is exactly its own inverse.
  468. ``orthogonalize`` has no effect here, as the DST-I matrix is already
  469. orthogonal up to a scale factor of ``2N``.
  470. **Type II**
  471. There are several definitions of the DST-II; we use the following for
  472. ``norm="backward"``. DST-II assumes the input is odd around :math:`n=-1/2` and
  473. :math:`n=N-1/2`; the output is odd around :math:`k=-1` and even around :math:`k=N-1`
  474. .. math::
  475. y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(k+1)(2n+1)}{2N}\right)
  476. If ``orthogonalize=True``, ``y[0]`` is divided :math:`\sqrt{2}` which, when
  477. combined with ``norm="ortho"``, makes the corresponding matrix of
  478. coefficients orthonormal (``O @ O.T = np.eye(N)``).
  479. **Type III**
  480. There are several definitions of the DST-III, we use the following (for
  481. ``norm="backward"``). DST-III assumes the input is odd around :math:`n=-1` and
  482. even around :math:`n=N-1`
  483. .. math::
  484. y_k = (-1)^k x_{N-1} + 2 \sum_{n=0}^{N-2} x_n \sin\left(
  485. \frac{\pi(2k+1)(n+1)}{2N}\right)
  486. If ``orthogonalize=True``, ``x[0]`` is multiplied by :math:`\sqrt{2}`
  487. which, when combined with ``norm="ortho"``, makes the corresponding matrix
  488. of coefficients orthonormal (``O @ O.T = np.eye(N)``).
  489. The (unnormalized) DST-III is the inverse of the (unnormalized) DST-II, up
  490. to a factor :math:`2N`. The orthonormalized DST-III is exactly the inverse of the
  491. orthonormalized DST-II.
  492. **Type IV**
  493. There are several definitions of the DST-IV, we use the following (for
  494. ``norm="backward"``). DST-IV assumes the input is odd around :math:`n=-0.5` and
  495. even around :math:`n=N-0.5`
  496. .. math::
  497. y_k = 2 \sum_{n=0}^{N-1} x_n \sin\left(\frac{\pi(2k+1)(2n+1)}{4N}\right)
  498. ``orthogonalize`` has no effect here, as the DST-IV matrix is already
  499. orthogonal up to a scale factor of ``2N``.
  500. The (unnormalized) DST-IV is its own inverse, up to a factor :math:`2N`. The
  501. orthonormalized DST-IV is exactly its own inverse.
  502. References
  503. ----------
  504. .. [1] Wikipedia, "Discrete sine transform",
  505. https://en.wikipedia.org/wiki/Discrete_sine_transform
  506. """
  507. return (Dispatchable(x, np.ndarray),)
  508. @_dispatch
  509. def idst(x, type=2, n=None, axis=-1, norm=None, overwrite_x=False,
  510. workers=None, orthogonalize=None):
  511. """
  512. Return the Inverse Discrete Sine Transform of an arbitrary type sequence.
  513. Parameters
  514. ----------
  515. x : array_like
  516. The input array.
  517. type : {1, 2, 3, 4}, optional
  518. Type of the DST (see Notes). Default type is 2.
  519. n : int, optional
  520. Length of the transform. If ``n < x.shape[axis]``, `x` is
  521. truncated. If ``n > x.shape[axis]``, `x` is zero-padded. The
  522. default results in ``n = x.shape[axis]``.
  523. axis : int, optional
  524. Axis along which the idst is computed; the default is over the
  525. last axis (i.e., ``axis=-1``).
  526. norm : {"backward", "ortho", "forward"}, optional
  527. Normalization mode (see Notes). Default is "backward".
  528. overwrite_x : bool, optional
  529. If True, the contents of `x` can be destroyed; the default is False.
  530. workers : int, optional
  531. Maximum number of workers to use for parallel computation. If negative,
  532. the value wraps around from ``os.cpu_count()``.
  533. See :func:`~scipy.fft.fft` for more details.
  534. orthogonalize : bool, optional
  535. Whether to use the orthogonalized IDST variant (see Notes).
  536. Defaults to ``True`` when ``norm="ortho"`` and ``False`` otherwise.
  537. .. versionadded:: 1.8.0
  538. Returns
  539. -------
  540. idst : ndarray of real
  541. The transformed input array.
  542. See Also
  543. --------
  544. dst : Forward DST
  545. Notes
  546. -----
  547. .. warning:: For ``type in {2, 3}``, ``norm="ortho"`` breaks the direct
  548. correspondence with the inverse direct Fourier transform.
  549. For ``norm="ortho"`` both the `dst` and `idst` are scaled by the same
  550. overall factor in both directions. By default, the transform is also
  551. orthogonalized which for types 2 and 3 means the transform definition is
  552. modified to give orthogonality of the DST matrix (see `dst` for the full
  553. definitions).
  554. 'The' IDST is the IDST-II, which is the same as the normalized DST-III.
  555. The IDST is equivalent to a normal DST except for the normalization and
  556. type. DST type 1 and 4 are their own inverse and DSTs 2 and 3 are each
  557. other's inverses.
  558. """
  559. return (Dispatchable(x, np.ndarray),)