test_windows.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. from __future__ import division, print_function, absolute_import
  2. import pickle
  3. import numpy as np
  4. from numpy import array
  5. from numpy.testing import (assert_array_almost_equal, assert_array_equal,
  6. assert_allclose,
  7. assert_equal, assert_, assert_array_less)
  8. from pytest import raises as assert_raises
  9. from scipy._lib._numpy_compat import suppress_warnings
  10. from scipy import fftpack
  11. from scipy.signal import windows, get_window, resample, hann as dep_hann
  12. window_funcs = [
  13. ('boxcar', ()),
  14. ('triang', ()),
  15. ('parzen', ()),
  16. ('bohman', ()),
  17. ('blackman', ()),
  18. ('nuttall', ()),
  19. ('blackmanharris', ()),
  20. ('flattop', ()),
  21. ('bartlett', ()),
  22. ('hanning', ()),
  23. ('barthann', ()),
  24. ('hamming', ()),
  25. ('kaiser', (1,)),
  26. ('dpss', (2,)),
  27. ('gaussian', (0.5,)),
  28. ('general_gaussian', (1.5, 2)),
  29. ('chebwin', (1,)),
  30. ('slepian', (2,)),
  31. ('cosine', ()),
  32. ('hann', ()),
  33. ('exponential', ()),
  34. ('tukey', (0.5,)),
  35. ]
  36. class TestBartHann(object):
  37. def test_basic(self):
  38. assert_allclose(windows.barthann(6, sym=True),
  39. [0, 0.35857354213752, 0.8794264578624801,
  40. 0.8794264578624801, 0.3585735421375199, 0])
  41. assert_allclose(windows.barthann(7),
  42. [0, 0.27, 0.73, 1.0, 0.73, 0.27, 0])
  43. assert_allclose(windows.barthann(6, False),
  44. [0, 0.27, 0.73, 1.0, 0.73, 0.27])
  45. class TestBartlett(object):
  46. def test_basic(self):
  47. assert_allclose(windows.bartlett(6), [0, 0.4, 0.8, 0.8, 0.4, 0])
  48. assert_allclose(windows.bartlett(7), [0, 1/3, 2/3, 1.0, 2/3, 1/3, 0])
  49. assert_allclose(windows.bartlett(6, False),
  50. [0, 1/3, 2/3, 1.0, 2/3, 1/3])
  51. class TestBlackman(object):
  52. def test_basic(self):
  53. assert_allclose(windows.blackman(6, sym=False),
  54. [0, 0.13, 0.63, 1.0, 0.63, 0.13], atol=1e-14)
  55. assert_allclose(windows.blackman(7, sym=False),
  56. [0, 0.09045342435412804, 0.4591829575459636,
  57. 0.9203636180999081, 0.9203636180999081,
  58. 0.4591829575459636, 0.09045342435412804], atol=1e-8)
  59. assert_allclose(windows.blackman(6),
  60. [0, 0.2007701432625305, 0.8492298567374694,
  61. 0.8492298567374694, 0.2007701432625305, 0],
  62. atol=1e-14)
  63. assert_allclose(windows.blackman(7, True),
  64. [0, 0.13, 0.63, 1.0, 0.63, 0.13, 0], atol=1e-14)
  65. class TestBlackmanHarris(object):
  66. def test_basic(self):
  67. assert_allclose(windows.blackmanharris(6, False),
  68. [6.0e-05, 0.055645, 0.520575, 1.0, 0.520575, 0.055645])
  69. assert_allclose(windows.blackmanharris(7, sym=False),
  70. [6.0e-05, 0.03339172347815117, 0.332833504298565,
  71. 0.8893697722232837, 0.8893697722232838,
  72. 0.3328335042985652, 0.03339172347815122])
  73. assert_allclose(windows.blackmanharris(6),
  74. [6.0e-05, 0.1030114893456638, 0.7938335106543362,
  75. 0.7938335106543364, 0.1030114893456638, 6.0e-05])
  76. assert_allclose(windows.blackmanharris(7, sym=True),
  77. [6.0e-05, 0.055645, 0.520575, 1.0, 0.520575, 0.055645,
  78. 6.0e-05])
  79. class TestBohman(object):
  80. def test_basic(self):
  81. assert_allclose(windows.bohman(6),
  82. [0, 0.1791238937062839, 0.8343114522576858,
  83. 0.8343114522576858, 0.1791238937062838, 0])
  84. assert_allclose(windows.bohman(7, sym=True),
  85. [0, 0.1089977810442293, 0.6089977810442293, 1.0,
  86. 0.6089977810442295, 0.1089977810442293, 0])
  87. assert_allclose(windows.bohman(6, False),
  88. [0, 0.1089977810442293, 0.6089977810442293, 1.0,
  89. 0.6089977810442295, 0.1089977810442293])
  90. class TestBoxcar(object):
  91. def test_basic(self):
  92. assert_allclose(windows.boxcar(6), [1, 1, 1, 1, 1, 1])
  93. assert_allclose(windows.boxcar(7), [1, 1, 1, 1, 1, 1, 1])
  94. assert_allclose(windows.boxcar(6, False), [1, 1, 1, 1, 1, 1])
  95. cheb_odd_true = array([0.200938, 0.107729, 0.134941, 0.165348,
  96. 0.198891, 0.235450, 0.274846, 0.316836,
  97. 0.361119, 0.407338, 0.455079, 0.503883,
  98. 0.553248, 0.602637, 0.651489, 0.699227,
  99. 0.745266, 0.789028, 0.829947, 0.867485,
  100. 0.901138, 0.930448, 0.955010, 0.974482,
  101. 0.988591, 0.997138, 1.000000, 0.997138,
  102. 0.988591, 0.974482, 0.955010, 0.930448,
  103. 0.901138, 0.867485, 0.829947, 0.789028,
  104. 0.745266, 0.699227, 0.651489, 0.602637,
  105. 0.553248, 0.503883, 0.455079, 0.407338,
  106. 0.361119, 0.316836, 0.274846, 0.235450,
  107. 0.198891, 0.165348, 0.134941, 0.107729,
  108. 0.200938])
  109. cheb_even_true = array([0.203894, 0.107279, 0.133904,
  110. 0.163608, 0.196338, 0.231986,
  111. 0.270385, 0.311313, 0.354493,
  112. 0.399594, 0.446233, 0.493983,
  113. 0.542378, 0.590916, 0.639071,
  114. 0.686302, 0.732055, 0.775783,
  115. 0.816944, 0.855021, 0.889525,
  116. 0.920006, 0.946060, 0.967339,
  117. 0.983557, 0.994494, 1.000000,
  118. 1.000000, 0.994494, 0.983557,
  119. 0.967339, 0.946060, 0.920006,
  120. 0.889525, 0.855021, 0.816944,
  121. 0.775783, 0.732055, 0.686302,
  122. 0.639071, 0.590916, 0.542378,
  123. 0.493983, 0.446233, 0.399594,
  124. 0.354493, 0.311313, 0.270385,
  125. 0.231986, 0.196338, 0.163608,
  126. 0.133904, 0.107279, 0.203894])
  127. class TestChebWin(object):
  128. def test_basic(self):
  129. with suppress_warnings() as sup:
  130. sup.filter(UserWarning, "This window is not suitable")
  131. assert_allclose(windows.chebwin(6, 100),
  132. [0.1046401879356917, 0.5075781475823447, 1.0, 1.0,
  133. 0.5075781475823447, 0.1046401879356917])
  134. assert_allclose(windows.chebwin(7, 100),
  135. [0.05650405062850233, 0.316608530648474,
  136. 0.7601208123539079, 1.0, 0.7601208123539079,
  137. 0.316608530648474, 0.05650405062850233])
  138. assert_allclose(windows.chebwin(6, 10),
  139. [1.0, 0.6071201674458373, 0.6808391469897297,
  140. 0.6808391469897297, 0.6071201674458373, 1.0])
  141. assert_allclose(windows.chebwin(7, 10),
  142. [1.0, 0.5190521247588651, 0.5864059018130382,
  143. 0.6101519801307441, 0.5864059018130382,
  144. 0.5190521247588651, 1.0])
  145. assert_allclose(windows.chebwin(6, 10, False),
  146. [1.0, 0.5190521247588651, 0.5864059018130382,
  147. 0.6101519801307441, 0.5864059018130382,
  148. 0.5190521247588651])
  149. def test_cheb_odd_high_attenuation(self):
  150. with suppress_warnings() as sup:
  151. sup.filter(UserWarning, "This window is not suitable")
  152. cheb_odd = windows.chebwin(53, at=-40)
  153. assert_array_almost_equal(cheb_odd, cheb_odd_true, decimal=4)
  154. def test_cheb_even_high_attenuation(self):
  155. with suppress_warnings() as sup:
  156. sup.filter(UserWarning, "This window is not suitable")
  157. cheb_even = windows.chebwin(54, at=40)
  158. assert_array_almost_equal(cheb_even, cheb_even_true, decimal=4)
  159. def test_cheb_odd_low_attenuation(self):
  160. cheb_odd_low_at_true = array([1.000000, 0.519052, 0.586405,
  161. 0.610151, 0.586405, 0.519052,
  162. 1.000000])
  163. with suppress_warnings() as sup:
  164. sup.filter(UserWarning, "This window is not suitable")
  165. cheb_odd = windows.chebwin(7, at=10)
  166. assert_array_almost_equal(cheb_odd, cheb_odd_low_at_true, decimal=4)
  167. def test_cheb_even_low_attenuation(self):
  168. cheb_even_low_at_true = array([1.000000, 0.451924, 0.51027,
  169. 0.541338, 0.541338, 0.51027,
  170. 0.451924, 1.000000])
  171. with suppress_warnings() as sup:
  172. sup.filter(UserWarning, "This window is not suitable")
  173. cheb_even = windows.chebwin(8, at=-10)
  174. assert_array_almost_equal(cheb_even, cheb_even_low_at_true, decimal=4)
  175. exponential_data = {
  176. (4, None, 0.2, False):
  177. array([4.53999297624848542e-05,
  178. 6.73794699908546700e-03, 1.00000000000000000e+00,
  179. 6.73794699908546700e-03]),
  180. (4, None, 0.2, True): array([0.00055308437014783, 0.0820849986238988,
  181. 0.0820849986238988, 0.00055308437014783]),
  182. (4, None, 1.0, False): array([0.1353352832366127, 0.36787944117144233, 1.,
  183. 0.36787944117144233]),
  184. (4, None, 1.0, True): array([0.22313016014842982, 0.60653065971263342,
  185. 0.60653065971263342, 0.22313016014842982]),
  186. (4, 2, 0.2, False):
  187. array([4.53999297624848542e-05, 6.73794699908546700e-03,
  188. 1.00000000000000000e+00, 6.73794699908546700e-03]),
  189. (4, 2, 0.2, True): None,
  190. (4, 2, 1.0, False): array([0.1353352832366127, 0.36787944117144233, 1.,
  191. 0.36787944117144233]),
  192. (4, 2, 1.0, True): None,
  193. (5, None, 0.2, True):
  194. array([4.53999297624848542e-05,
  195. 6.73794699908546700e-03, 1.00000000000000000e+00,
  196. 6.73794699908546700e-03, 4.53999297624848542e-05]),
  197. (5, None, 1.0, True): array([0.1353352832366127, 0.36787944117144233, 1.,
  198. 0.36787944117144233, 0.1353352832366127]),
  199. (5, 2, 0.2, True): None,
  200. (5, 2, 1.0, True): None
  201. }
  202. def test_exponential():
  203. for k, v in exponential_data.items():
  204. if v is None:
  205. assert_raises(ValueError, windows.exponential, *k)
  206. else:
  207. win = windows.exponential(*k)
  208. assert_allclose(win, v, rtol=1e-14)
  209. class TestFlatTop(object):
  210. def test_basic(self):
  211. assert_allclose(windows.flattop(6, sym=False),
  212. [-0.000421051, -0.051263156, 0.19821053, 1.0,
  213. 0.19821053, -0.051263156])
  214. assert_allclose(windows.flattop(7, sym=False),
  215. [-0.000421051, -0.03684078115492348,
  216. 0.01070371671615342, 0.7808739149387698,
  217. 0.7808739149387698, 0.01070371671615342,
  218. -0.03684078115492348])
  219. assert_allclose(windows.flattop(6),
  220. [-0.000421051, -0.0677142520762119, 0.6068721525762117,
  221. 0.6068721525762117, -0.0677142520762119,
  222. -0.000421051])
  223. assert_allclose(windows.flattop(7, True),
  224. [-0.000421051, -0.051263156, 0.19821053, 1.0,
  225. 0.19821053, -0.051263156, -0.000421051])
  226. class TestGaussian(object):
  227. def test_basic(self):
  228. assert_allclose(windows.gaussian(6, 1.0),
  229. [0.04393693362340742, 0.3246524673583497,
  230. 0.8824969025845955, 0.8824969025845955,
  231. 0.3246524673583497, 0.04393693362340742])
  232. assert_allclose(windows.gaussian(7, 1.2),
  233. [0.04393693362340742, 0.2493522087772962,
  234. 0.7066482778577162, 1.0, 0.7066482778577162,
  235. 0.2493522087772962, 0.04393693362340742])
  236. assert_allclose(windows.gaussian(7, 3),
  237. [0.6065306597126334, 0.8007374029168081,
  238. 0.9459594689067654, 1.0, 0.9459594689067654,
  239. 0.8007374029168081, 0.6065306597126334])
  240. assert_allclose(windows.gaussian(6, 3, False),
  241. [0.6065306597126334, 0.8007374029168081,
  242. 0.9459594689067654, 1.0, 0.9459594689067654,
  243. 0.8007374029168081])
  244. class TestGeneralCosine(object):
  245. def test_basic(self):
  246. assert_allclose(windows.general_cosine(5, [0.5, 0.3, 0.2]),
  247. [0.4, 0.3, 1, 0.3, 0.4])
  248. assert_allclose(windows.general_cosine(4, [0.5, 0.3, 0.2], sym=False),
  249. [0.4, 0.3, 1, 0.3])
  250. class TestGeneralHamming(object):
  251. def test_basic(self):
  252. assert_allclose(windows.general_hamming(5, 0.7),
  253. [0.4, 0.7, 1.0, 0.7, 0.4])
  254. assert_allclose(windows.general_hamming(5, 0.75, sym=False),
  255. [0.5, 0.6727457514, 0.9522542486,
  256. 0.9522542486, 0.6727457514])
  257. assert_allclose(windows.general_hamming(6, 0.75, sym=True),
  258. [0.5, 0.6727457514, 0.9522542486,
  259. 0.9522542486, 0.6727457514, 0.5])
  260. class TestHamming(object):
  261. def test_basic(self):
  262. assert_allclose(windows.hamming(6, False),
  263. [0.08, 0.31, 0.77, 1.0, 0.77, 0.31])
  264. assert_allclose(windows.hamming(7, sym=False),
  265. [0.08, 0.2531946911449826, 0.6423596296199047,
  266. 0.9544456792351128, 0.9544456792351128,
  267. 0.6423596296199047, 0.2531946911449826])
  268. assert_allclose(windows.hamming(6),
  269. [0.08, 0.3978521825875242, 0.9121478174124757,
  270. 0.9121478174124757, 0.3978521825875242, 0.08])
  271. assert_allclose(windows.hamming(7, sym=True),
  272. [0.08, 0.31, 0.77, 1.0, 0.77, 0.31, 0.08])
  273. class TestHann(object):
  274. def test_basic(self):
  275. assert_allclose(windows.hann(6, sym=False),
  276. [0, 0.25, 0.75, 1.0, 0.75, 0.25])
  277. assert_allclose(windows.hann(7, sym=False),
  278. [0, 0.1882550990706332, 0.6112604669781572,
  279. 0.9504844339512095, 0.9504844339512095,
  280. 0.6112604669781572, 0.1882550990706332])
  281. assert_allclose(windows.hann(6, True),
  282. [0, 0.3454915028125263, 0.9045084971874737,
  283. 0.9045084971874737, 0.3454915028125263, 0])
  284. assert_allclose(windows.hann(7),
  285. [0, 0.25, 0.75, 1.0, 0.75, 0.25, 0])
  286. class TestKaiser(object):
  287. def test_basic(self):
  288. assert_allclose(windows.kaiser(6, 0.5),
  289. [0.9403061933191572, 0.9782962393705389,
  290. 0.9975765035372042, 0.9975765035372042,
  291. 0.9782962393705389, 0.9403061933191572])
  292. assert_allclose(windows.kaiser(7, 0.5),
  293. [0.9403061933191572, 0.9732402256999829,
  294. 0.9932754654413773, 1.0, 0.9932754654413773,
  295. 0.9732402256999829, 0.9403061933191572])
  296. assert_allclose(windows.kaiser(6, 2.7),
  297. [0.2603047507678832, 0.6648106293528054,
  298. 0.9582099802511439, 0.9582099802511439,
  299. 0.6648106293528054, 0.2603047507678832])
  300. assert_allclose(windows.kaiser(7, 2.7),
  301. [0.2603047507678832, 0.5985765418119844,
  302. 0.8868495172060835, 1.0, 0.8868495172060835,
  303. 0.5985765418119844, 0.2603047507678832])
  304. assert_allclose(windows.kaiser(6, 2.7, False),
  305. [0.2603047507678832, 0.5985765418119844,
  306. 0.8868495172060835, 1.0, 0.8868495172060835,
  307. 0.5985765418119844])
  308. class TestNuttall(object):
  309. def test_basic(self):
  310. assert_allclose(windows.nuttall(6, sym=False),
  311. [0.0003628, 0.0613345, 0.5292298, 1.0, 0.5292298,
  312. 0.0613345])
  313. assert_allclose(windows.nuttall(7, sym=False),
  314. [0.0003628, 0.03777576895352025, 0.3427276199688195,
  315. 0.8918518610776603, 0.8918518610776603,
  316. 0.3427276199688196, 0.0377757689535203])
  317. assert_allclose(windows.nuttall(6),
  318. [0.0003628, 0.1105152530498718, 0.7982580969501282,
  319. 0.7982580969501283, 0.1105152530498719, 0.0003628])
  320. assert_allclose(windows.nuttall(7, True),
  321. [0.0003628, 0.0613345, 0.5292298, 1.0, 0.5292298,
  322. 0.0613345, 0.0003628])
  323. class TestParzen(object):
  324. def test_basic(self):
  325. assert_allclose(windows.parzen(6),
  326. [0.009259259259259254, 0.25, 0.8611111111111112,
  327. 0.8611111111111112, 0.25, 0.009259259259259254])
  328. assert_allclose(windows.parzen(7, sym=True),
  329. [0.00583090379008747, 0.1574344023323616,
  330. 0.6501457725947521, 1.0, 0.6501457725947521,
  331. 0.1574344023323616, 0.00583090379008747])
  332. assert_allclose(windows.parzen(6, False),
  333. [0.00583090379008747, 0.1574344023323616,
  334. 0.6501457725947521, 1.0, 0.6501457725947521,
  335. 0.1574344023323616])
  336. class TestTriang(object):
  337. def test_basic(self):
  338. assert_allclose(windows.triang(6, True),
  339. [1/6, 1/2, 5/6, 5/6, 1/2, 1/6])
  340. assert_allclose(windows.triang(7),
  341. [1/4, 1/2, 3/4, 1, 3/4, 1/2, 1/4])
  342. assert_allclose(windows.triang(6, sym=False),
  343. [1/4, 1/2, 3/4, 1, 3/4, 1/2])
  344. tukey_data = {
  345. (4, 0.5, True): array([0.0, 1.0, 1.0, 0.0]),
  346. (4, 0.9, True): array([0.0, 0.84312081893436686,
  347. 0.84312081893436686, 0.0]),
  348. (4, 1.0, True): array([0.0, 0.75, 0.75, 0.0]),
  349. (4, 0.5, False): array([0.0, 1.0, 1.0, 1.0]),
  350. (4, 0.9, False): array([0.0, 0.58682408883346526,
  351. 1.0, 0.58682408883346526]),
  352. (4, 1.0, False): array([0.0, 0.5, 1.0, 0.5]),
  353. (5, 0.0, True): array([1.0, 1.0, 1.0, 1.0, 1.0]),
  354. (5, 0.8, True): array([0.0, 0.69134171618254492,
  355. 1.0, 0.69134171618254492, 0.0]),
  356. (5, 1.0, True): array([0.0, 0.5, 1.0, 0.5, 0.0]),
  357. (6, 0): [1, 1, 1, 1, 1, 1],
  358. (7, 0): [1, 1, 1, 1, 1, 1, 1],
  359. (6, .25): [0, 1, 1, 1, 1, 0],
  360. (7, .25): [0, 1, 1, 1, 1, 1, 0],
  361. (6,): [0, 0.9045084971874737, 1.0, 1.0, 0.9045084971874735, 0],
  362. (7,): [0, 0.75, 1.0, 1.0, 1.0, 0.75, 0],
  363. (6, .75): [0, 0.5522642316338269, 1.0, 1.0, 0.5522642316338267, 0],
  364. (7, .75): [0, 0.4131759111665348, 0.9698463103929542, 1.0,
  365. 0.9698463103929542, 0.4131759111665347, 0],
  366. (6, 1): [0, 0.3454915028125263, 0.9045084971874737, 0.9045084971874737,
  367. 0.3454915028125263, 0],
  368. (7, 1): [0, 0.25, 0.75, 1.0, 0.75, 0.25, 0],
  369. }
  370. class TestTukey(object):
  371. def test_basic(self):
  372. # Test against hardcoded data
  373. for k, v in tukey_data.items():
  374. if v is None:
  375. assert_raises(ValueError, windows.tukey, *k)
  376. else:
  377. win = windows.tukey(*k)
  378. assert_allclose(win, v, rtol=1e-14)
  379. def test_extremes(self):
  380. # Test extremes of alpha correspond to boxcar and hann
  381. tuk0 = windows.tukey(100, 0)
  382. box0 = windows.boxcar(100)
  383. assert_array_almost_equal(tuk0, box0)
  384. tuk1 = windows.tukey(100, 1)
  385. han1 = windows.hann(100)
  386. assert_array_almost_equal(tuk1, han1)
  387. dpss_data = {
  388. # All values from MATLAB:
  389. # * taper[1] of (3, 1.4, 3) sign-flipped
  390. # * taper[3] of (5, 1.5, 5) sign-flipped
  391. (4, 0.1, 2): ([[0.497943898, 0.502047681, 0.502047681, 0.497943898], [0.670487993, 0.224601537, -0.224601537, -0.670487993]], [0.197961815, 0.002035474]), # noqa
  392. (3, 1.4, 3): ([[0.410233151, 0.814504464, 0.410233151], [0.707106781, 0.0, -0.707106781], [0.575941629, -0.580157287, 0.575941629]], [0.999998093, 0.998067480, 0.801934426]), # noqa
  393. (5, 1.5, 5): ([[0.1745071052, 0.4956749177, 0.669109327, 0.495674917, 0.174507105], [0.4399493348, 0.553574369, 0.0, -0.553574369, -0.439949334], [0.631452756, 0.073280238, -0.437943884, 0.073280238, 0.631452756], [0.553574369, -0.439949334, 0.0, 0.439949334, -0.553574369], [0.266110290, -0.498935248, 0.600414741, -0.498935248, 0.266110290147157]], [0.999728571, 0.983706916, 0.768457889, 0.234159338, 0.013947282907567]), # noqa: E501
  394. (100, 2, 4): ([[0.0030914414, 0.0041266922, 0.005315076, 0.006665149, 0.008184854, 0.0098814158, 0.011761239, 0.013829809, 0.016091597, 0.018549973, 0.02120712, 0.02406396, 0.027120092, 0.030373728, 0.033821651, 0.037459181, 0.041280145, 0.045276872, 0.049440192, 0.053759447, 0.058222524, 0.062815894, 0.067524661, 0.072332638, 0.077222418, 0.082175473, 0.087172252, 0.092192299, 0.097214376, 0.1022166, 0.10717657, 0.11207154, 0.11687856, 0.12157463, 0.12613686, 0.13054266, 0.13476986, 0.13879691, 0.14260302, 0.14616832, 0.14947401, 0.1525025, 0.15523755, 0.15766438, 0.15976981, 0.16154233, 0.16297223, 0.16405162, 0.16477455, 0.16513702, 0.16513702, 0.16477455, 0.16405162, 0.16297223, 0.16154233, 0.15976981, 0.15766438, 0.15523755, 0.1525025, 0.14947401, 0.14616832, 0.14260302, 0.13879691, 0.13476986, 0.13054266, 0.12613686, 0.12157463, 0.11687856, 0.11207154, 0.10717657, 0.1022166, 0.097214376, 0.092192299, 0.087172252, 0.082175473, 0.077222418, 0.072332638, 0.067524661, 0.062815894, 0.058222524, 0.053759447, 0.049440192, 0.045276872, 0.041280145, 0.037459181, 0.033821651, 0.030373728, 0.027120092, 0.02406396, 0.02120712, 0.018549973, 0.016091597, 0.013829809, 0.011761239, 0.0098814158, 0.008184854, 0.006665149, 0.005315076, 0.0041266922, 0.0030914414], [0.018064449, 0.022040342, 0.026325013, 0.030905288, 0.035764398, 0.040881982, 0.046234148, 0.051793558, 0.057529559, 0.063408356, 0.069393216, 0.075444716, 0.081521022, 0.087578202, 0.093570567, 0.099451049, 0.10517159, 0.11068356, 0.11593818, 0.12088699, 0.12548227, 0.12967752, 0.1334279, 0.13669069, 0.13942569, 0.1415957, 0.14316686, 0.14410905, 0.14439626, 0.14400686, 0.14292389, 0.1411353, 0.13863416, 0.13541876, 0.13149274, 0.12686516, 0.12155045, 0.1155684, 0.10894403, 0.10170748, 0.093893752, 0.08554251, 0.076697768, 0.067407559, 0.057723559, 0.04770068, 0.037396627, 0.026871428, 0.016186944, 0.0054063557, -0.0054063557, -0.016186944, -0.026871428, -0.037396627, -0.04770068, -0.057723559, -0.067407559, -0.076697768, -0.08554251, -0.093893752, -0.10170748, -0.10894403, -0.1155684, -0.12155045, -0.12686516, -0.13149274, -0.13541876, -0.13863416, -0.1411353, -0.14292389, -0.14400686, -0.14439626, -0.14410905, -0.14316686, -0.1415957, -0.13942569, -0.13669069, -0.1334279, -0.12967752, -0.12548227, -0.12088699, -0.11593818, -0.11068356, -0.10517159, -0.099451049, -0.093570567, -0.087578202, -0.081521022, -0.075444716, -0.069393216, -0.063408356, -0.057529559, -0.051793558, -0.046234148, -0.040881982, -0.035764398, -0.030905288, -0.026325013, -0.022040342, -0.018064449], [0.064817553, 0.072567801, 0.080292992, 0.087918235, 0.095367076, 0.10256232, 0.10942687, 0.1158846, 0.12186124, 0.12728523, 0.13208858, 0.13620771, 0.13958427, 0.14216587, 0.14390678, 0.14476863, 0.1447209, 0.14374148, 0.14181704, 0.13894336, 0.13512554, 0.13037812, 0.1247251, 0.11819984, 0.11084487, 0.10271159, 0.093859853, 0.084357497, 0.074279719, 0.063708406, 0.052731374, 0.041441525, 0.029935953, 0.018314987, 0.0066811877, -0.0048616765, -0.016209689, -0.027259848, -0.037911124, -0.048065512, -0.05762905, -0.066512804, -0.0746338, -0.081915903, -0.088290621, -0.09369783, -0.098086416, -0.10141482, -0.10365146, -0.10477512, -0.10477512, -0.10365146, -0.10141482, -0.098086416, -0.09369783, -0.088290621, -0.081915903, -0.0746338, -0.066512804, -0.05762905, -0.048065512, -0.037911124, -0.027259848, -0.016209689, -0.0048616765, 0.0066811877, 0.018314987, 0.029935953, 0.041441525, 0.052731374, 0.063708406, 0.074279719, 0.084357497, 0.093859853, 0.10271159, 0.11084487, 0.11819984, 0.1247251, 0.13037812, 0.13512554, 0.13894336, 0.14181704, 0.14374148, 0.1447209, 0.14476863, 0.14390678, 0.14216587, 0.13958427, 0.13620771, 0.13208858, 0.12728523, 0.12186124, 0.1158846, 0.10942687, 0.10256232, 0.095367076, 0.087918235, 0.080292992, 0.072567801, 0.064817553], [0.14985551, 0.15512305, 0.15931467, 0.16236806, 0.16423291, 0.16487165, 0.16426009, 0.1623879, 0.1592589, 0.15489114, 0.14931693, 0.14258255, 0.13474785, 0.1258857, 0.11608124, 0.10543095, 0.094041635, 0.082029213, 0.069517411, 0.056636348, 0.043521028, 0.030309756, 0.017142511, 0.0041592774, -0.0085016282, -0.020705223, -0.032321494, -0.043226982, -0.053306291, -0.062453515, -0.070573544, -0.077583253, -0.083412547, -0.088005244, -0.091319802, -0.093329861, -0.094024602, -0.093408915, -0.091503383, -0.08834406, -0.08398207, -0.078483012, -0.071926192, -0.064403681, -0.056019215, -0.046886954, -0.037130106, -0.026879442, -0.016271713, -0.005448, 0.005448, 0.016271713, 0.026879442, 0.037130106, 0.046886954, 0.056019215, 0.064403681, 0.071926192, 0.078483012, 0.08398207, 0.08834406, 0.091503383, 0.093408915, 0.094024602, 0.093329861, 0.091319802, 0.088005244, 0.083412547, 0.077583253, 0.070573544, 0.062453515, 0.053306291, 0.043226982, 0.032321494, 0.020705223, 0.0085016282, -0.0041592774, -0.017142511, -0.030309756, -0.043521028, -0.056636348, -0.069517411, -0.082029213, -0.094041635, -0.10543095, -0.11608124, -0.1258857, -0.13474785, -0.14258255, -0.14931693, -0.15489114, -0.1592589, -0.1623879, -0.16426009, -0.16487165, -0.16423291, -0.16236806, -0.15931467, -0.15512305, -0.14985551]], [0.999943140, 0.997571533, 0.959465463, 0.721862496]), # noqa: E501
  395. }
  396. class TestDPSS(object):
  397. def test_basic(self):
  398. # Test against hardcoded data
  399. for k, v in dpss_data.items():
  400. win, ratios = windows.dpss(*k, return_ratios=True)
  401. assert_allclose(win, v[0], atol=1e-7, err_msg=k)
  402. assert_allclose(ratios, v[1], rtol=1e-5, atol=1e-7, err_msg=k)
  403. def test_unity(self):
  404. # Test unity value handling (gh-2221)
  405. for M in range(1, 21):
  406. # corrected w/approximation (default)
  407. win = windows.dpss(M, M / 2.1)
  408. expected = M % 2 # one for odd, none for even
  409. assert_equal(np.isclose(win, 1.).sum(), expected,
  410. err_msg='%s' % (win,))
  411. # corrected w/subsample delay (slower)
  412. win_sub = windows.dpss(M, M / 2.1, norm='subsample')
  413. if M > 2:
  414. # @M=2 the subsample doesn't do anything
  415. assert_equal(np.isclose(win_sub, 1.).sum(), expected,
  416. err_msg='%s' % (win_sub,))
  417. assert_allclose(win, win_sub, rtol=0.03) # within 3%
  418. # not the same, l2-norm
  419. win_2 = windows.dpss(M, M / 2.1, norm=2)
  420. expected = 1 if M == 1 else 0
  421. assert_equal(np.isclose(win_2, 1.).sum(), expected,
  422. err_msg='%s' % (win_2,))
  423. def test_extremes(self):
  424. # Test extremes of alpha
  425. lam = windows.dpss(31, 6, 4, return_ratios=True)[1]
  426. assert_array_almost_equal(lam, 1.)
  427. lam = windows.dpss(31, 7, 4, return_ratios=True)[1]
  428. assert_array_almost_equal(lam, 1.)
  429. lam = windows.dpss(31, 8, 4, return_ratios=True)[1]
  430. assert_array_almost_equal(lam, 1.)
  431. def test_degenerate(self):
  432. # Test failures
  433. assert_raises(ValueError, windows.dpss, 4, 1.5, -1) # Bad Kmax
  434. assert_raises(ValueError, windows.dpss, 4, 1.5, -5)
  435. assert_raises(TypeError, windows.dpss, 4, 1.5, 1.1)
  436. assert_raises(ValueError, windows.dpss, 3, 1.5, 3) # NW must be < N/2.
  437. assert_raises(ValueError, windows.dpss, 3, -1, 3) # NW must be pos
  438. assert_raises(ValueError, windows.dpss, 3, 0, 3)
  439. assert_raises(ValueError, windows.dpss, -1, 1, 3) # negative M
  440. class TestGetWindow(object):
  441. def test_boxcar(self):
  442. w = windows.get_window('boxcar', 12)
  443. assert_array_equal(w, np.ones_like(w))
  444. # window is a tuple of len 1
  445. w = windows.get_window(('boxcar',), 16)
  446. assert_array_equal(w, np.ones_like(w))
  447. def test_cheb_odd(self):
  448. with suppress_warnings() as sup:
  449. sup.filter(UserWarning, "This window is not suitable")
  450. w = windows.get_window(('chebwin', -40), 53, fftbins=False)
  451. assert_array_almost_equal(w, cheb_odd_true, decimal=4)
  452. def test_cheb_even(self):
  453. with suppress_warnings() as sup:
  454. sup.filter(UserWarning, "This window is not suitable")
  455. w = windows.get_window(('chebwin', 40), 54, fftbins=False)
  456. assert_array_almost_equal(w, cheb_even_true, decimal=4)
  457. def test_kaiser_float(self):
  458. win1 = windows.get_window(7.2, 64)
  459. win2 = windows.kaiser(64, 7.2, False)
  460. assert_allclose(win1, win2)
  461. def test_invalid_inputs(self):
  462. # Window is not a float, tuple, or string
  463. assert_raises(ValueError, windows.get_window, set('hann'), 8)
  464. # Unknown window type error
  465. assert_raises(ValueError, windows.get_window, 'broken', 4)
  466. def test_array_as_window(self):
  467. # github issue 3603
  468. osfactor = 128
  469. sig = np.arange(128)
  470. win = windows.get_window(('kaiser', 8.0), osfactor // 2)
  471. assert_raises(ValueError, resample,
  472. (sig, len(sig) * osfactor), {'window': win})
  473. def test_windowfunc_basics():
  474. for window_name, params in window_funcs:
  475. window = getattr(windows, window_name)
  476. with suppress_warnings() as sup:
  477. sup.filter(UserWarning, "This window is not suitable")
  478. if window_name in ('slepian', 'hanning'):
  479. sup.filter(DeprecationWarning)
  480. # Check symmetry for odd and even lengths
  481. w1 = window(8, *params, sym=True)
  482. w2 = window(7, *params, sym=False)
  483. assert_array_almost_equal(w1[:-1], w2)
  484. w1 = window(9, *params, sym=True)
  485. w2 = window(8, *params, sym=False)
  486. assert_array_almost_equal(w1[:-1], w2)
  487. # Check that functions run and output lengths are correct
  488. assert_equal(len(window(6, *params, sym=True)), 6)
  489. assert_equal(len(window(6, *params, sym=False)), 6)
  490. assert_equal(len(window(7, *params, sym=True)), 7)
  491. assert_equal(len(window(7, *params, sym=False)), 7)
  492. # Check invalid lengths
  493. assert_raises(ValueError, window, 5.5, *params)
  494. assert_raises(ValueError, window, -7, *params)
  495. # Check degenerate cases
  496. assert_array_equal(window(0, *params, sym=True), [])
  497. assert_array_equal(window(0, *params, sym=False), [])
  498. assert_array_equal(window(1, *params, sym=True), [1])
  499. assert_array_equal(window(1, *params, sym=False), [1])
  500. # Check dtype
  501. assert_(window(0, *params, sym=True).dtype == 'float')
  502. assert_(window(0, *params, sym=False).dtype == 'float')
  503. assert_(window(1, *params, sym=True).dtype == 'float')
  504. assert_(window(1, *params, sym=False).dtype == 'float')
  505. assert_(window(6, *params, sym=True).dtype == 'float')
  506. assert_(window(6, *params, sym=False).dtype == 'float')
  507. # Check normalization
  508. assert_array_less(window(10, *params, sym=True), 1.01)
  509. assert_array_less(window(10, *params, sym=False), 1.01)
  510. assert_array_less(window(9, *params, sym=True), 1.01)
  511. assert_array_less(window(9, *params, sym=False), 1.01)
  512. # Check that DFT-even spectrum is purely real for odd and even
  513. assert_allclose(fftpack.fft(window(10, *params, sym=False)).imag,
  514. 0, atol=1e-14)
  515. assert_allclose(fftpack.fft(window(11, *params, sym=False)).imag,
  516. 0, atol=1e-14)
  517. def test_needs_params():
  518. for winstr in ['kaiser', 'ksr', 'gaussian', 'gauss', 'gss',
  519. 'general gaussian', 'general_gaussian',
  520. 'general gauss', 'general_gauss', 'ggs',
  521. 'slepian', 'optimal', 'slep', 'dss', 'dpss',
  522. 'chebwin', 'cheb', 'exponential', 'poisson', 'tukey',
  523. 'tuk', 'dpss']:
  524. assert_raises(ValueError, get_window, winstr, 7)
  525. def test_deprecation():
  526. if dep_hann.__doc__ is not None: # can be None with `-OO` mode
  527. assert_('signal.hann is deprecated' in dep_hann.__doc__)
  528. assert_('deprecated' not in windows.hann.__doc__)
  529. def test_deprecated_pickleable():
  530. dep_hann2 = pickle.loads(pickle.dumps(dep_hann))
  531. assert_(dep_hann2 is dep_hann)