3
0

FIR-Windows.h 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. // Description : A collection of window functions with Finite Impulse Response FIR
  9. // and some helper window functions with Infinite Impulse Response IIR
  10. #pragma once
  11. #include <math.h>
  12. #include <ImageProcessing_Traits_Platform.h>
  13. #include <AzCore/Math/MathUtils.h>
  14. /* ####################################################################################################################
  15. */
  16. #ifndef M_PI
  17. #define M_PI 3.14159265358979323846 /* pi */
  18. #endif
  19. //the original file was from cry's RC.exe with all the filters. We decided we would only use few of them for users
  20. namespace ImageProcessingAtom
  21. {
  22. template<class F>
  23. F cube(const F& op) { return op * op * op; }
  24. template<class F>
  25. F square(F fOp) { return(fOp * fOp); }
  26. enum EWindowFunction
  27. {
  28. eWindowFunction_Combiner = 0,
  29. /*--------------- unit-area filters for unit-spaced samples ----------------*/
  30. eWindowFunction_Point = 1,
  31. eWindowFunction_Box = 2, // box, pulse, Fourier window, 1st order (constant) b-spline
  32. eWindowFunction_Triangle = 3, // triangle, Bartlett window, 2nd order (linear) b-spline
  33. eWindowFunction_Linear = eWindowFunction_Triangle,
  34. eWindowFunction_Bartlett = eWindowFunction_Triangle,
  35. eWindowFunction_Quadric = 4, // 3rd order (quadratic) b-spline
  36. eWindowFunction_Bilinear = eWindowFunction_Quadric,
  37. eWindowFunction_Welch = eWindowFunction_Quadric,
  38. eWindowFunction_Cubic = 5, // 4th order (cubic) b-spline
  39. eWindowFunction_Hermite = 6, // 4th order (cubic hermite) b-spline
  40. eWindowFunction_Catrom = 7, // Catmull-Rom spline, Overhauser spline
  41. eWindowFunction_Sine = 8, // IIR
  42. eWindowFunction_Sinc = 9, // Sinc, perfect lowpass filter (infinite)
  43. eWindowFunction_Bessel = 10, // Bessel (for circularly symm. 2-d filt, inf)
  44. eWindowFunction_Lanczos = 11, // Lanczos filtering, windowed Sinc
  45. /*------------------ filters for non-unit spaced samples -------------------*/
  46. eWindowFunction_Gaussian = 12, // Gaussian (infinite)
  47. eWindowFunction_Normal = 13, // Normal distribution (infinite)
  48. /*------------------------- parameterized filters --------------------------*/
  49. eWindowFunction_Mitchell = 14, // Mitchell & Netravali's two-param cubic
  50. /*--------------------------- window functions -----------------------------*/
  51. eWindowFunction_Hann = 15, // Hanning window
  52. eWindowFunction_BartlettHann = 16,
  53. eWindowFunction_Hamming = 17, // Hamming window
  54. eWindowFunction_Blackman = 18, // Blackman window
  55. eWindowFunction_BlackmanHarris = 19,
  56. eWindowFunction_BlackmanNuttall = 20,
  57. eWindowFunction_Flattop = 21,
  58. /*------------------------- parameterized windows --------------------------*/
  59. eWindowFunction_Kaiser = 22, // parameterized Kaiser window
  60. /*---------------------------- custom windows ------------------------------*/
  61. eWindowFunction_SigmaSix = 23, // two Normal distributions
  62. eWindowFunction_KaiserSinc = 24, // Kaiser and Sinc
  63. eWindowFunction_Num = eWindowFunction_KaiserSinc + 1,
  64. };
  65. enum EWindowEvaluation
  66. {
  67. eWindowEvaluation_Sum,
  68. eWindowEvaluation_Max,
  69. eWindowEvaluation_Min,
  70. };
  71. /* ####################################################################################################################
  72. */
  73. template<typename T = float>
  74. class IWindowFunction
  75. {
  76. public:
  77. virtual ~IWindowFunction() {}
  78. virtual const char* getName() const = 0;
  79. virtual T getLength() const = 0;
  80. virtual bool isCardinal() const = 0;
  81. virtual bool isInfinite() const = 0;
  82. virtual bool isUnitSpaced() const = 0;
  83. virtual bool isCentered() const = 0;
  84. public:
  85. virtual T operator () (T pos) const = 0;
  86. };
  87. /* ####################################################################################################################
  88. * box, pulse, Fourier window,
  89. * box function also know as rectangle function
  90. * 1st order (constant) b-spline
  91. */
  92. template<typename T = double>
  93. class BoxWindowFunction
  94. : public IWindowFunction<T>
  95. {
  96. public:
  97. virtual const char* getName() const
  98. {
  99. return "Box-window";
  100. }
  101. virtual T getLength() const
  102. {
  103. return 0.5;
  104. }
  105. virtual bool isCardinal() const
  106. {
  107. return true;
  108. }
  109. virtual bool isInfinite() const
  110. {
  111. return false;
  112. }
  113. virtual bool isUnitSpaced() const
  114. {
  115. return true;
  116. }
  117. virtual bool isCentered() const
  118. {
  119. return true;
  120. }
  121. public:
  122. virtual T operator () (T pos) const
  123. {
  124. if (pos < 0.0)
  125. {
  126. pos = -pos;
  127. }
  128. if (pos <= 0.5)
  129. {
  130. return 1.0;
  131. }
  132. return 0.0;
  133. }
  134. };
  135. /* --------------------------------------------------------------------------------------------------------------------
  136. * triangle, Bartlett window,
  137. * triangle function also known as lambda function
  138. * 2nd order (linear) b-spline
  139. */
  140. template<typename T = double>
  141. class TriangleWindowFunction
  142. : public IWindowFunction<T>
  143. {
  144. public:
  145. virtual const char* getName() const
  146. {
  147. return "Triangle-window";
  148. }
  149. virtual T getLength() const
  150. {
  151. return 1.0;
  152. }
  153. virtual bool isCardinal() const
  154. {
  155. return true;
  156. }
  157. virtual bool isInfinite() const
  158. {
  159. return false;
  160. }
  161. virtual bool isUnitSpaced() const
  162. {
  163. return true;
  164. }
  165. virtual bool isCentered() const
  166. {
  167. return true;
  168. }
  169. public:
  170. virtual T operator () (T pos) const
  171. {
  172. if (pos < 0.0)
  173. {
  174. pos = -pos;
  175. }
  176. if (pos < 1.0)
  177. {
  178. return 1.0 - pos;
  179. }
  180. return 0.0;
  181. }
  182. };
  183. /* ####################################################################################################################
  184. * 3rd order (quadratic) b-spline
  185. */
  186. template<typename T = double>
  187. class QuadricWindowFunction
  188. : public IWindowFunction<T>
  189. {
  190. public:
  191. virtual const char* getName() const
  192. {
  193. return "Quadric-window";
  194. }
  195. virtual T getLength() const
  196. {
  197. return 1.5;
  198. }
  199. virtual bool isCardinal() const
  200. {
  201. return false;
  202. }
  203. virtual bool isInfinite() const
  204. {
  205. return false;
  206. }
  207. virtual bool isUnitSpaced() const
  208. {
  209. return true;
  210. }
  211. virtual bool isCentered() const
  212. {
  213. return true;
  214. }
  215. public:
  216. virtual T operator () (T pos) const
  217. {
  218. if (pos < 0.0)
  219. {
  220. pos = -pos;
  221. }
  222. if (pos < 0.5)
  223. {
  224. return 0.75 - square(pos);
  225. }
  226. if (pos < 1.5)
  227. {
  228. return 0.50 * square(pos - 1.5);
  229. }
  230. return 0.0;
  231. }
  232. };
  233. /* --------------------------------------------------------------------------------------------------------------------
  234. * 4th order (cubic) b-spline
  235. */
  236. template<typename T = double>
  237. class CubicWindowFunction
  238. : public IWindowFunction<T>
  239. {
  240. public:
  241. virtual const char* getName() const
  242. {
  243. return "Cubic-window";
  244. }
  245. virtual T getLength() const
  246. {
  247. return 2.0;
  248. }
  249. virtual bool isCardinal() const
  250. {
  251. return false;
  252. }
  253. virtual bool isInfinite() const
  254. {
  255. return false;
  256. }
  257. virtual bool isUnitSpaced() const
  258. {
  259. return true;
  260. }
  261. virtual bool isCentered() const
  262. {
  263. return true;
  264. }
  265. public:
  266. virtual T operator () (T pos) const
  267. {
  268. if (pos < 0.0)
  269. {
  270. pos = -pos;
  271. }
  272. if (pos < 1.0)
  273. {
  274. return 0.5 * cube(pos) - square(pos) + 2.0 / 3.0;
  275. }
  276. if (pos < 2.0)
  277. {
  278. return cube(2.0 - pos) / 6.0;
  279. }
  280. return 0.0;
  281. }
  282. };
  283. /* --------------------------------------------------------------------------------------------------------------------
  284. * Hermite filter
  285. * f(x) = 2|x|^3 - 3|x|^2 + 1, -1 <= x <= 1
  286. */
  287. template<typename T = double>
  288. class HermiteWindowFunction
  289. : public IWindowFunction<T>
  290. {
  291. public:
  292. virtual const char* getName() const
  293. {
  294. return "Hermite-window";
  295. }
  296. virtual T getLength() const
  297. {
  298. return 1.0;
  299. }
  300. virtual bool isCardinal() const
  301. {
  302. return false;
  303. }
  304. virtual bool isInfinite() const
  305. {
  306. return false;
  307. }
  308. virtual bool isUnitSpaced() const
  309. {
  310. return true;
  311. }
  312. virtual bool isCentered() const
  313. {
  314. return true;
  315. }
  316. public:
  317. virtual T operator () (T pos) const
  318. {
  319. if (pos < 0.0)
  320. {
  321. pos = -pos;
  322. }
  323. if (pos < 1.0)
  324. {
  325. return 2.0 * cube(pos) - 3.0 * square(pos) + 1.0;
  326. }
  327. return 0.0;
  328. }
  329. };
  330. /* --------------------------------------------------------------------------------------------------------------------
  331. * Catmull-Rom spline, Overhauser spline
  332. */
  333. template<typename T = double>
  334. class CatromWindowFunction
  335. : public IWindowFunction<T>
  336. {
  337. public:
  338. CatromWindowFunction() { }
  339. public:
  340. virtual const char* getName() const
  341. {
  342. return "Catrom-window";
  343. }
  344. virtual T getLength() const
  345. {
  346. return 2.0;
  347. }
  348. virtual bool isCardinal() const
  349. {
  350. return true;
  351. }
  352. virtual bool isInfinite() const
  353. {
  354. return false;
  355. }
  356. virtual bool isUnitSpaced() const
  357. {
  358. return false;
  359. }
  360. virtual bool isCentered() const
  361. {
  362. return true;
  363. }
  364. public:
  365. virtual T operator () (T pos) const
  366. {
  367. if (pos < 0.0)
  368. {
  369. pos = -pos;
  370. }
  371. if (pos < 1.0)
  372. {
  373. return 1.5 * cube(pos) - 2.5 * square(pos) + 1.0;
  374. }
  375. if (pos < 2.0)
  376. {
  377. return -0.5 * cube(pos) + 2.5 * square(pos) - 4.0 * pos + 2.0;
  378. }
  379. return 0.0;
  380. }
  381. };
  382. /* ####################################################################################################################
  383. */
  384. template<typename T = double>
  385. class SineWindowFunction
  386. : public IWindowFunction<T>
  387. {
  388. public:
  389. virtual const char* getName() const
  390. {
  391. return "Sine-window";
  392. }
  393. virtual T getLength() const
  394. {
  395. return 0.0;
  396. }
  397. virtual bool isCardinal() const
  398. {
  399. return true;
  400. }
  401. virtual bool isInfinite() const
  402. {
  403. return true;
  404. }
  405. virtual bool isUnitSpaced() const
  406. {
  407. return true;
  408. }
  409. virtual bool isCentered() const
  410. {
  411. return true;
  412. }
  413. public:
  414. virtual T operator () (T pos) const
  415. {
  416. return sin(pos);
  417. }
  418. };
  419. /* --------------------------------------------------------------------------------------------------------------------
  420. * sinc, perfect lowpass filter (infinite)
  421. *
  422. * Note: Some people say sinc(x) is sin(x)/x. Others say it's
  423. * sin(PI*x)/(PI*x), a horizontal compression of the former which is
  424. * zero at integer values. We use the latter, whose Fourier transform
  425. * is a canonical rectangle function (edges at -1/2, +1/2, height 1).
  426. */
  427. template<typename T = double>
  428. class SincWindowFunction
  429. : public IWindowFunction<T>
  430. {
  431. public:
  432. virtual const char* getName() const
  433. {
  434. return "Sinc-window";
  435. }
  436. virtual T getLength() const
  437. {
  438. return 4.0;
  439. }
  440. virtual bool isCardinal() const
  441. {
  442. return true;
  443. }
  444. virtual bool isInfinite() const
  445. {
  446. return true;
  447. }
  448. virtual bool isUnitSpaced() const
  449. {
  450. return false;
  451. }
  452. virtual bool isCentered() const
  453. {
  454. return true;
  455. }
  456. public:
  457. virtual T operator () (T pos) const
  458. {
  459. if (pos == 0.0)
  460. {
  461. return 1.0;
  462. }
  463. return sin(M_PI * pos) / (M_PI * pos);
  464. }
  465. };
  466. /* --------------------------------------------------------------------------------------------------------------------
  467. * Bessel (for circularly symm. 2-d filt, infinite)
  468. * See Pratt "Digital Image Processing" p. 97 for Bessel functions
  469. */
  470. template<typename T = double>
  471. class BesselWindowFunction
  472. : public IWindowFunction<T>
  473. {
  474. public:
  475. virtual const char* getName() const
  476. {
  477. return "Bessel-window";
  478. }
  479. virtual T getLength() const
  480. {
  481. return 3.2383;
  482. }
  483. virtual bool isCardinal() const
  484. {
  485. return false;
  486. }
  487. virtual bool isInfinite() const
  488. {
  489. return true;
  490. }
  491. virtual bool isUnitSpaced() const
  492. {
  493. return false;
  494. }
  495. virtual bool isCentered() const
  496. {
  497. return true;
  498. }
  499. public:
  500. virtual T operator () (T pos) const
  501. {
  502. if (pos == 0.0)
  503. {
  504. return M_PI / 4.0;
  505. }
  506. return AZ_TRAIT_IMAGEPROCESSING_BESSEL_FUNCTION_FIRST_ORDER(M_PI * pos) / (2.0 * pos);
  507. }
  508. };
  509. /* --------------------------------------------------------------------------------------------------------------------
  510. * Lanczos filter
  511. */
  512. template<typename T = double>
  513. class LanczosWindowFunction
  514. : public SincWindowFunction<T>
  515. {
  516. public:
  517. LanczosWindowFunction(T tap = 3.0)
  518. {
  519. this->tap = AZ::GetMax(3.0, tap);
  520. }
  521. protected:
  522. T tap;
  523. public:
  524. virtual const char* getName() const
  525. {
  526. return "Lanczos-window";
  527. }
  528. virtual T getLength() const
  529. {
  530. return tap;
  531. }
  532. virtual bool isCardinal() const
  533. {
  534. return false;
  535. }
  536. virtual bool isInfinite() const
  537. {
  538. return false;
  539. }
  540. virtual bool isUnitSpaced() const
  541. {
  542. return false;
  543. }
  544. virtual bool isCentered() const
  545. {
  546. return true;
  547. }
  548. public:
  549. virtual T operator () (T pos) const
  550. {
  551. if (pos < 0.0)
  552. {
  553. pos = -pos;
  554. }
  555. if (pos < tap)
  556. {
  557. return ((SincWindowFunction<T>) * this)(pos) * ((SincWindowFunction<T>) * this)(pos / tap);
  558. }
  559. return 0.0;
  560. }
  561. };
  562. /* --------------------------------------------------------------------------------------------------------------------
  563. * Gaussian filter (infinite)
  564. */
  565. template<typename T = double>
  566. class GaussianWindowFunction
  567. : public IWindowFunction<T>
  568. {
  569. public:
  570. virtual const char* getName() const
  571. {
  572. return "Gaussian-window";
  573. }
  574. virtual T getLength() const
  575. {
  576. return 1.25;
  577. }
  578. virtual bool isCardinal() const
  579. {
  580. return false;
  581. }
  582. virtual bool isInfinite() const
  583. {
  584. return true;
  585. }
  586. virtual bool isUnitSpaced() const
  587. {
  588. return true;
  589. }
  590. virtual bool isCentered() const
  591. {
  592. return true;
  593. }
  594. public:
  595. virtual T operator () (T pos) const
  596. {
  597. return exp(-2.0 * square(pos)) * sqrt(2.0 / M_PI);
  598. }
  599. };
  600. /* --------------------------------------------------------------------------------------------------------------------
  601. * normal distribution (infinite)
  602. * Normal(x) = Gaussian(x/2)/2
  603. */
  604. template<typename T = double>
  605. class NormalWindowFunction
  606. : public IWindowFunction<T>
  607. {
  608. public:
  609. virtual const char* getName() const
  610. {
  611. return "Normal-window";
  612. }
  613. virtual T getLength() const
  614. {
  615. return 2.5;
  616. }
  617. virtual bool isCardinal() const
  618. {
  619. return false;
  620. }
  621. virtual bool isInfinite() const
  622. {
  623. return true;
  624. }
  625. virtual bool isUnitSpaced() const
  626. {
  627. return false;
  628. }
  629. virtual bool isCentered() const
  630. {
  631. return true;
  632. }
  633. public:
  634. virtual T operator () (T pos) const
  635. {
  636. return exp(-square(pos) / 2.0) / sqrt(2.0 * M_PI);
  637. }
  638. };
  639. /* --------------------------------------------------------------------------------------------------------------------
  640. */
  641. template<typename T = double>
  642. class SigmaSixWindowFunction
  643. : public IWindowFunction<T>
  644. {
  645. public:
  646. SigmaSixWindowFunction(T diameter = 1.0, T negative = 0.0)
  647. {
  648. // we aim for 6 * sigma = 99,99996% of all values
  649. const T sigma = 1.0 / 3.0;
  650. s2 = sigma * sigma * 2.0;
  651. d2 = s2 / (diameter * diameter);
  652. d = diameter;
  653. n = negative;
  654. }
  655. protected:
  656. T s2, d2, d, n;
  657. public:
  658. virtual const char* getName() const
  659. {
  660. return "SigmaSix-window";
  661. }
  662. virtual T getLength() const
  663. {
  664. return 1.44;
  665. }
  666. virtual bool isCardinal() const
  667. {
  668. return false;
  669. }
  670. virtual bool isInfinite() const
  671. {
  672. return true;
  673. }
  674. virtual bool isUnitSpaced() const
  675. {
  676. return false;
  677. }
  678. virtual bool isCentered() const
  679. {
  680. return true;
  681. }
  682. public:
  683. virtual T operator () (T pos) const
  684. {
  685. if (pos < 0.0)
  686. {
  687. pos = -pos;
  688. }
  689. T o = exp(-square(pos) / s2) - (1.0 - 0.9999996);
  690. T i = exp(-square(pos) / d2) - (1.0 - 0.9999996);
  691. return (pos >= d ? 0.0 : i) - o * n;
  692. }
  693. };
  694. /* --------------------------------------------------------------------------------------------------------------------
  695. * Mitchell & Netravali's two-param cubic
  696. * see Mitchell & Netravali,
  697. * "Reconstruction Filters in Computer Graphics", SIGGRAPH 88
  698. */
  699. template<typename T = double>
  700. class MitchellWindowFunction
  701. : public IWindowFunction<T>
  702. {
  703. public:
  704. MitchellWindowFunction(T b = 1.0 / 3.0, T c = 1.0 / 3.0)
  705. {
  706. p0 = (6. - 2. * b) / 6.;
  707. p2 = (-18. + 12. * b + 6. * c) / 6.;
  708. p3 = (12. - 9. * b - 6. * c) / 6.;
  709. q0 = (8. * b + 24. * c) / 6.;
  710. q1 = (-12. * b - 48. * c) / 6.;
  711. q2 = (6. * b + 30. * c) / 6.;
  712. q3 = (-b - 6. * c) / 6.;
  713. }
  714. protected:
  715. T p0, p2, p3, q0, q1, q2, q3;
  716. public:
  717. virtual const char* getName() const
  718. {
  719. return "Mitchell-window";
  720. }
  721. virtual T getLength() const
  722. {
  723. return 2.0;
  724. }
  725. virtual bool isCardinal() const
  726. {
  727. return false;
  728. }
  729. virtual bool isInfinite() const
  730. {
  731. return false;
  732. }
  733. virtual bool isUnitSpaced() const
  734. {
  735. return false;
  736. }
  737. virtual bool isCentered() const
  738. {
  739. return true;
  740. }
  741. public:
  742. virtual T operator () (T pos) const
  743. {
  744. if (pos < 0.0)
  745. {
  746. pos = -pos;
  747. }
  748. if (pos < 1.0)
  749. {
  750. return p3 * cube(pos) + p2 * square(pos) + p0;
  751. }
  752. if (pos < 2.0)
  753. {
  754. return q3 * cube(pos) + q2 * square(pos) + q1 * pos + q0;
  755. }
  756. return 0.0;
  757. }
  758. };
  759. /* ####################################################################################################################
  760. * Hanning window (infinite)
  761. */
  762. template<typename T = double>
  763. class HannWindowFunction
  764. : public IWindowFunction<T>
  765. {
  766. public:
  767. virtual const char* getName() const
  768. {
  769. return "Hann-window";
  770. }
  771. virtual T getLength() const
  772. {
  773. return 1.0;
  774. }
  775. virtual bool isCardinal() const
  776. {
  777. return true;
  778. }
  779. virtual bool isInfinite() const
  780. {
  781. return true;
  782. }
  783. virtual bool isUnitSpaced() const
  784. {
  785. return true;
  786. }
  787. virtual bool isCentered() const
  788. {
  789. return true;
  790. }
  791. public:
  792. virtual T operator () (T pos) const
  793. {
  794. return 0.5 + 0.5 * cos(M_PI * pos);
  795. }
  796. };
  797. /* --------------------------------------------------------------------------------------------------------------------
  798. * BartlettHanning window (infinite)
  799. */
  800. template<typename T = double>
  801. class BartlettHannWindowFunction
  802. : public IWindowFunction<T>
  803. {
  804. public:
  805. virtual const char* getName() const
  806. {
  807. return "Bartlett-Hann-window";
  808. }
  809. virtual T getLength() const
  810. {
  811. return 1.0;
  812. }
  813. virtual bool isCardinal() const
  814. {
  815. return true;
  816. }
  817. virtual bool isInfinite() const
  818. {
  819. return true;
  820. }
  821. virtual bool isUnitSpaced() const
  822. {
  823. return true;
  824. }
  825. virtual bool isCentered() const
  826. {
  827. return true;
  828. }
  829. public:
  830. virtual T operator () (T pos) const
  831. {
  832. return 0.62 + 0.48 * (1.0 - pos) + 0.38 * cos(M_PI * pos);
  833. }
  834. };
  835. /* --------------------------------------------------------------------------------------------------------------------
  836. * Hamming window (infinite)
  837. */
  838. template<typename T = double>
  839. class HammingWindowFunction
  840. : public IWindowFunction<T>
  841. {
  842. public:
  843. virtual const char* getName() const
  844. {
  845. return "Hamming-window";
  846. }
  847. virtual T getLength() const
  848. {
  849. return 1.0;
  850. }
  851. virtual bool isCardinal() const
  852. {
  853. return true;
  854. }
  855. virtual bool isInfinite() const
  856. {
  857. return true;
  858. }
  859. virtual bool isUnitSpaced() const
  860. {
  861. return true;
  862. }
  863. virtual bool isCentered() const
  864. {
  865. return true;
  866. }
  867. public:
  868. virtual T operator () (T pos) const
  869. {
  870. return 0.53836 + 0.46164 * cos(M_PI * pos);
  871. }
  872. };
  873. /* --------------------------------------------------------------------------------------------------------------------
  874. * Blackman window (infinite)
  875. */
  876. template<typename T = double>
  877. class BlackmanWindowFunction
  878. : public IWindowFunction<T>
  879. {
  880. public:
  881. virtual const char* getName() const
  882. {
  883. return "Blackman-window";
  884. }
  885. virtual T getLength() const
  886. {
  887. return 1.0;
  888. }
  889. virtual bool isCardinal() const
  890. {
  891. return true;
  892. }
  893. virtual bool isInfinite() const
  894. {
  895. return true;
  896. }
  897. virtual bool isUnitSpaced() const
  898. {
  899. return true;
  900. }
  901. virtual bool isCentered() const
  902. {
  903. return true;
  904. }
  905. public:
  906. virtual T operator () (T pos) const
  907. {
  908. return 0.42659
  909. + 0.49656 * cos(M_PI * pos)
  910. + 0.07685 * cos(2.0 * M_PI * pos);
  911. }
  912. };
  913. /* --------------------------------------------------------------------------------------------------------------------
  914. * BlackmanHarris window (infinite)
  915. */
  916. template<typename T = double>
  917. class BlackmanHarrisWindowFunction
  918. : public IWindowFunction<T>
  919. {
  920. public:
  921. virtual const char* getName() const
  922. {
  923. return "Blackman-Harris-window";
  924. }
  925. virtual T getLength() const
  926. {
  927. return 1.0;
  928. }
  929. virtual bool isCardinal() const
  930. {
  931. return true;
  932. }
  933. virtual bool isInfinite() const
  934. {
  935. return true;
  936. }
  937. virtual bool isUnitSpaced() const
  938. {
  939. return true;
  940. }
  941. virtual bool isCentered() const
  942. {
  943. return true;
  944. }
  945. public:
  946. virtual T operator () (T pos) const
  947. {
  948. return 0.35875
  949. + 0.48829 * cos(M_PI * pos)
  950. + 0.14128 * cos(2.0 * M_PI * pos)
  951. + 0.01168 * cos(3.0 * M_PI * pos);
  952. }
  953. };
  954. /* --------------------------------------------------------------------------------------------------------------------
  955. * BlackmanNuttall window (infinite)
  956. */
  957. template<typename T = double>
  958. class BlackmanNuttallWindowFunction
  959. : public IWindowFunction<T>
  960. {
  961. public:
  962. virtual const char* getName() const
  963. {
  964. return "Blackman-Nuttall-window";
  965. }
  966. virtual T getLength() const
  967. {
  968. return 1.0;
  969. }
  970. virtual bool isCardinal() const
  971. {
  972. return true;
  973. }
  974. virtual bool isInfinite() const
  975. {
  976. return true;
  977. }
  978. virtual bool isUnitSpaced() const
  979. {
  980. return true;
  981. }
  982. virtual bool isCentered() const
  983. {
  984. return true;
  985. }
  986. public:
  987. virtual T operator () (T pos) const
  988. {
  989. return 0.3635819
  990. + 0.4891775 * cos(M_PI * pos)
  991. + 0.1365995 * cos(2.0 * M_PI * pos)
  992. + 0.0106411 * cos(3.0 * M_PI * pos);
  993. }
  994. };
  995. /* --------------------------------------------------------------------------------------------------------------------
  996. * FlatTop window (infinite)
  997. */
  998. template<typename T = double>
  999. class FlatTopWindowFunction
  1000. : public IWindowFunction<T>
  1001. {
  1002. public:
  1003. virtual const char* getName() const
  1004. {
  1005. return "Flat-Top-window";
  1006. }
  1007. virtual T getLength() const
  1008. {
  1009. return 1.0;
  1010. }
  1011. virtual bool isCardinal() const
  1012. {
  1013. return true;
  1014. }
  1015. virtual bool isInfinite() const
  1016. {
  1017. return true;
  1018. }
  1019. virtual bool isUnitSpaced() const
  1020. {
  1021. return true;
  1022. }
  1023. virtual bool isCentered() const
  1024. {
  1025. return true;
  1026. }
  1027. public:
  1028. virtual T operator () (T pos) const
  1029. {
  1030. return 0.215578948
  1031. + 0.416631580 * cos(M_PI * pos)
  1032. + 0.277263158 * cos(2.0 * M_PI * pos)
  1033. + 0.083578947 * cos(3.0 * M_PI * pos)
  1034. + 0.006947368 * cos(4.0 * M_PI * pos);
  1035. }
  1036. };
  1037. /* --------------------------------------------------------------------------------------------------------------------
  1038. * parameterized Kaiser window (infinite)
  1039. * from Oppenheim & Schafer, Hamming
  1040. */
  1041. template<typename T = double>
  1042. class KaiserWindowFunction
  1043. : public IWindowFunction<T>
  1044. {
  1045. public:
  1046. KaiserWindowFunction(T a = 6.5)
  1047. {
  1048. /* typically 4<a<9 */
  1049. /* param a trades off main lobe width (sharpness) */
  1050. /* for side lobe amplitude (ringing) */
  1051. this->a = a;
  1052. this->i0a = 1. / bessel_i0(a);
  1053. }
  1054. protected:
  1055. T a, i0a;
  1056. /* modified zeroth order Bessel function of the first kind. */
  1057. static T bessel_i0(T x)
  1058. {
  1059. #define EPSILON 1e-7
  1060. const T y = square(x) / 4.0;
  1061. T sum = 1.0;
  1062. T t = y;
  1063. for (int i = 2; t > EPSILON; i++)
  1064. {
  1065. sum += t;
  1066. t *= y / square(i);
  1067. }
  1068. return sum;
  1069. #undef EPSILON
  1070. }
  1071. public:
  1072. virtual const char* getName() const
  1073. {
  1074. return "Kaiser-window";
  1075. }
  1076. virtual T getLength() const
  1077. {
  1078. return 1.0;
  1079. }
  1080. virtual bool isCardinal() const
  1081. {
  1082. return true;
  1083. }
  1084. virtual bool isInfinite() const
  1085. {
  1086. return true;
  1087. }
  1088. virtual bool isUnitSpaced() const
  1089. {
  1090. return true;
  1091. }
  1092. virtual bool isCentered() const
  1093. {
  1094. return true;
  1095. }
  1096. public:
  1097. virtual T operator () (T pos) const
  1098. {
  1099. return i0a * bessel_i0(a * sqrt(1.0 - square(pos)));
  1100. }
  1101. };
  1102. /* ####################################################################################################################
  1103. */
  1104. template<typename T = double>
  1105. class CombinerWindowFunction
  1106. : public IWindowFunction<T>
  1107. {
  1108. public:
  1109. CombinerWindowFunction(IWindowFunction<T>* fu, IWindowFunction<T>* wi)
  1110. {
  1111. shaper = fu;
  1112. restrictor = wi;
  1113. }
  1114. protected:
  1115. IWindowFunction<T>* shaper;
  1116. IWindowFunction<T>* restrictor;
  1117. public:
  1118. virtual const char* getName() const
  1119. {
  1120. return "Combiner of two window-generators";
  1121. }
  1122. virtual T getLength() const
  1123. {
  1124. return shaper->getLength();
  1125. }
  1126. virtual bool isCardinal() const
  1127. {
  1128. return shaper->isCardinal() && restrictor->isCardinal();
  1129. }
  1130. virtual bool isInfinite() const
  1131. {
  1132. return shaper->isInfinite() && restrictor->isInfinite();
  1133. }
  1134. virtual bool isUnitSpaced() const
  1135. {
  1136. return shaper->isUnitSpaced() && restrictor->isUnitSpaced();
  1137. }
  1138. virtual bool isCentered() const
  1139. {
  1140. return shaper->isCentered() && restrictor->isCentered();
  1141. }
  1142. public:
  1143. virtual T operator () (T pos) const
  1144. {
  1145. return (*shaper)(pos) * (*restrictor)(pos / shaper->getLength());
  1146. }
  1147. };
  1148. /* --------------------------------------------------------------------------------------------------------------------
  1149. */
  1150. template<typename T = double>
  1151. class PointWindowFunction
  1152. : public BoxWindowFunction<T>
  1153. {
  1154. public:
  1155. PointWindowFunction()
  1156. : BoxWindowFunction<T>() { }
  1157. public:
  1158. virtual const char* getName() const
  1159. {
  1160. return "Point-window";
  1161. }
  1162. virtual T getLength() const
  1163. {
  1164. return 0.0;
  1165. }
  1166. };
  1167. } //end namespace ImageProcessingAtom