{"id":7545,"date":"2022-04-06T00:00:00","date_gmt":"2022-04-06T00:00:00","guid":{"rendered":"https:\/\/academy.deepsim.caplot-approximations-of-wavelet-and-scaling-functions-in-python-dabd31452bdf\/"},"modified":"2022-11-25T04:06:39","modified_gmt":"2022-11-25T04:06:39","slug":"plot-approximations-of-wavelet-and-scaling-functions-in-python-dabd31452bdf","status":"publish","type":"post","link":"https:\/\/academy.deepsim.ca\/zh\/plot-approximations-of-wavelet-and-scaling-functions-in-python-dabd31452bdf\/","title":{"rendered":"Python\u4e2d\u7ed8\u5236\u5c0f\u6ce2\u548c\u5c3a\u5ea6\u51fd\u6570\u7684\u56fe\u50cf"},"content":{"rendered":"<p id=\"4b60\" class=\"graf graf--p graf-after--h3\" style=\"text-align: center;\"><em><span style=\"font-size: 24px;\">PyWavelets is probably the most Powerful Wavelet Transform Library of Python Today<\/span><\/em><\/p>\n<p id=\"b458\" class=\"graf graf--p graf-after--figure\"><strong class=\"markup--strong markup--p-strong\">Wavelet Transforms (WT)<\/strong> or wavelet analysis is probably the most recent solution to overcome the shortcomings of the <strong class=\"markup--strong markup--p-strong\">Fourier Transform (FT)<\/strong>. WT transforms a signal in period (or frequency) without losing time resolution. In the signal processing context, WT provides a method to decompose an input signal of interest into a set of elementary waveforms, i.e. \u201cwavelets\u201d., and then analyze the signal by examining the coefficients (or weights) of these wavelets.<\/p>\n<h3 id=\"5ec4\" class=\"graf graf--h3 graf-after--p\">1. Applications of Wavelet Transform<\/h3>\n<p id=\"add0\" class=\"graf graf--p graf-after--h3\">Wavelets transform can be used for stationary and nonstationary signals, including but not limited to the following:<\/p>\n<ul class=\"postList\">\n<li id=\"c1bc\" class=\"graf graf--li graf-after--p\">noise removal from the signals<\/li>\n<li id=\"6457\" class=\"graf graf--li graf-after--li\">trend analysis and forecasting<\/li>\n<li id=\"f29b\" class=\"graf graf--li graf-after--li\">detection of abrupt discontinuities, change, or abnormal behavior, etc. and<\/li>\n<li id=\"0aa1\" class=\"graf graf--li graf-after--li\">compression of large amounts of data<\/li>\n<li id=\"65d6\" class=\"graf graf--li graf-after--li\">the new image compression standard called JPEG2000 is fully based on wavelets<\/li>\n<li id=\"730b\" class=\"graf graf--li graf-after--li\">data encryption\uff0ci.e. secure the data<\/li>\n<li id=\"4437\" class=\"graf graf--li graf-after--li\">Combine it with machine learning to improve the modelling accuracy<\/li>\n<\/ul>\n<h3 id=\"9081\" class=\"graf graf--h3 graf-after--li\"><strong class=\"markup--strong markup--h3-strong\">2. PyWavelets<\/strong><\/h3>\n<p id=\"6081\" class=\"graf graf--p graf-after--h3\"><a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/github.com\/PyWavelets\/pywt\" target=\"_blank\" rel=\"noopener\" data-href=\"https:\/\/github.com\/PyWavelets\/pywt\" data->PyWavelets<\/a>\u00a0is a free Open Source library for wavelet transforms in Python, which is released under the MIT license.<\/p>\n<p id=\"e247\" class=\"graf graf--p graf-after--p\">The main features of PyWavelets are that it can make wavelet transforms on 1D, 2D and nD signals, which include the following main transforms:<\/p>\n<ul class=\"postList\">\n<li id=\"da23\" class=\"graf graf--li graf-after--p\">Forward and Inverse Discrete Wavelet Transform (DWT and IDWT)<\/li>\n<li id=\"baad\" class=\"graf graf--li graf-after--li\">Multilevel DWT and IDWT<\/li>\n<li id=\"275c\" class=\"graf graf--li graf-after--li\">Stationary Wavelet Transform (Undecimated Wavelet Transform)<\/li>\n<li id=\"cd53\" class=\"graf graf--li graf-after--li\">Wavelet Packet decomposition and reconstruction<\/li>\n<li id=\"08fd\" class=\"graf graf--li graf-after--li\">Continuous Wavelet Transform<\/li>\n<li id=\"54b3\" class=\"graf graf--li graf-after--li\">Multiresolutiom Analysis (MRA)<\/li>\n<\/ul>\n<h3 id=\"b888\" class=\"graf graf--h3 graf-after--li\">3. Install PyWavelets and Required\u00a0Packages<\/h3>\n<p id=\"36da\" class=\"graf graf--p graf-after--h3\">PyWavelets has dropped support for Python 3.5 and 3.6 and now supports Python 3.7\u20133.10. PyWavelets is only dependent on NumPy (supported versions are currently &gt;= 1.14.6). Besides, Matplotlib are required for visualization.<\/p>\n<p id=\"0224\" class=\"graf graf--p graf-after--p\">From PyPI:<\/p>\n<pre id=\"6629\" class=\"graf graf--pre graf-after--p\"><code>pip install PyWavelets matplotlib<\/code><\/pre>\n<p id=\"2a21\" class=\"graf graf--p graf-after--pre\">For Anaconda Users, Anaconda has these packages installed. If you have not installed it on your PC, you can install by:<\/p>\n<pre id=\"f285\" class=\"graf graf--pre graf-after--p\"><code>conda install -c conda-forge pywavelets matplotlib<\/code><\/pre>\n<h3 id=\"d216\" class=\"graf graf--h3 graf-after--pre\">4. Built-in Wavelet Families and Family Members<\/h3>\n<p id=\"36de\" class=\"graf graf--p graf-after--h3\">One can simply check and display all PyWavelets Built-in Wavelet Families and their members using the following command:<\/p>\n<pre id=\"49de\" class=\"graf graf--pre graf-after--p\"><code><strong class=\"markup--strong markup--pre-strong\">import pywt<\/strong><\/code><\/pre>\n<pre id=\"b1e4\" class=\"graf graf--pre graf-after--pre\"><code><strong class=\"markup--strong markup--pre-strong\">for <\/strong>family <strong class=\"markup--strong markup--pre-strong\">in <\/strong>pywt.families():<\/code> <code>\n    print(f'{family} family: {pywt.wavelist(family)}')<\/code><\/pre>\n<p id=\"7193\" class=\"graf graf--p graf-after--pre\">Then it returns a list as follows:<\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"alignnone wp-image-8397 \" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/04\/wavelet_family.png\" alt=\"\" width=\"757\" height=\"567\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/wavelet_family.png 1148w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/wavelet_family-600x449.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/wavelet_family-300x225.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/wavelet_family-1024x767.png 1024w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/wavelet_family-768x575.png 768w\" sizes=\"(max-width: 757px) 100vw, 757px\" \/><\/p>\n<h3 id=\"1f9a\" class=\"graf graf--h3 graf-after--figure\">5. Plot Approximations of Wavelet and Scaling Functions<\/h3>\n<h4 id=\"778b\" class=\"graf graf--h4 graf-after--h3\">5.1. Import Required\u00a0Packages<\/h4>\n<pre id=\"de8b\" class=\"graf graf--pre graf-after--h4\"><code># import the required packages\nimport pywt\nimport matplotlib.pyplot as plt<\/code><\/pre>\n<h4 id=\"175f\" class=\"graf graf--h4 graf-after--pre\">5.2. Plot Discrete <strong class=\"markup--strong markup--h4-strong\">Wavelet and Scaling Functions<\/strong><\/h4>\n<p id=\"e385\" class=\"graf graf--p graf-after--h4\">(1) Orthogonal Wavelets<\/p>\n<p id=\"ec1d\" class=\"graf graf--p graf-after--p\">First, let\u2019s get the approximation wavelet and scaling functions of a given wavelet and choose a level. Take \u2018db5\u2019 and level=1 for instance.<\/p>\n<pre id=\"5041\" class=\"graf graf--pre graf-after--p\"><code>wavelet <strong class=\"markup--strong markup--pre-strong\">= <\/strong>pywt.Wavelet('db5')<\/code><\/pre>\n<pre id=\"3cff\" class=\"graf graf--pre graf-after--pre\"><code>[phi, psi, x] <strong class=\"markup--strong markup--pre-strong\">= <\/strong>wavelet.wavefun(level<strong class=\"markup--strong markup--pre-strong\">=<\/strong>1)<\/code><\/pre>\n<p id=\"1702\" class=\"graf graf--p graf-after--pre\">Plot wavelet function:<\/p>\n<pre id=\"b351\" class=\"graf graf--pre graf-after--p\"><code>plt.plot(x,psi)<\/code><\/pre>\n<pre id=\"9579\" class=\"graf graf--pre graf-after--pre\"><code>plt.show()<\/code><\/pre>\n<p id=\"bb2d\" class=\"graf graf--p graf-after--pre\">It generates the following plot:<\/p>\n<figure><img decoding=\"async\" class=\"wp-image-8398  alignleft\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/04\/db5-l1-function.png\" alt=\"\" width=\"701\" height=\"444\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-function.png 861w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-function-600x380.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-function-300x190.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-function-768x486.png 768w\" sizes=\"(max-width: 701px) 100vw, 701px\" \/><figcaption>Fig.1. Wavelet Function of \u2018db5\u2019 at level 1<\/p>\n<\/figcaption><\/figure>\n<p id=\"0066\" class=\"graf graf--p graf-after--figure\">Plot scaling function:<\/p>\n<pre id=\"404c\" class=\"graf graf--pre graf-after--p\"><code>plt.plot(x,phi)\nplt.show()<\/code><\/pre>\n<p id=\"e84e\" class=\"graf graf--p graf-after--pre\">The result looks as follows:<\/p>\n<p><img decoding=\"async\" class=\"alignnone wp-image-8399 \" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/04\/db5-l1-scal-function.png\" alt=\"\" width=\"747\" height=\"488\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-scal-function.png 842w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-scal-function-600x392.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-scal-function-300x196.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/db5-l1-scal-function-768x502.png 768w\" sizes=\"(max-width: 747px) 100vw, 747px\" \/><\/p>\n<figure><figcaption>Fig.2. Scaling Function of \u2018db5\u2019 at level 1<\/figcaption><\/figure>\n<h4 id=\"ac0e\" class=\"graf graf--h4 graf-after--figure\">(2) Other\u00a0Wavelets<\/h4>\n<p id=\"a327\" class=\"graf graf--p graf-after--h4\">For other (biorthogonal but not orthogonal) wavelets, it returns approximations of scaling function and wavelet function both for decomposition and reconstruction and corresponding x-grid coordinates.<\/p>\n<p id=\"688a\" class=\"graf graf--p graf-after--p\">For example, let\u2019s see wavelet \u2018bior3.5\u2019 and level=5.<\/p>\n<pre id=\"ca7d\" class=\"graf graf--pre graf-after--p\"><code>biwavelet = pywt.Wavelet('bior3.5')\n[phi_d,psi_d,phi_r,psi_r,x] = biwavelet.wavefun(level=5)<\/code><\/pre>\n<p id=\"9895\" class=\"graf graf--p graf-after--pre\">Visualize their functions as follows:<\/p>\n<pre id=\"349d\" class=\"graf graf--pre graf-after--p\"><code># Wavelet Function of decomposition\nplt.plot(x,psi_d)\nplt.show()<\/code><\/pre>\n<figure><img decoding=\"async\" class=\"alignleft\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/08\/04hbkMmrsn65yfN5H.png\" alt=\"\" \/><figcaption>\n<p>Fig.3. Wavelet Function of Decomposition of \u2018bior3.5\u2019 at level 5<\/p>\n<\/figcaption><\/figure>\n<pre id=\"73e3\" class=\"graf graf--pre graf-after--figure\"><code># wavelet function of reconstruction\nplt.plot(x,psi_r)\nplt.show()\n<\/code><\/pre>\n<figure><figcaption><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8400 \" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/04\/bior3.5-reconstr-wave-fun.png\" alt=\"\" width=\"726\" height=\"462\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-reconstr-wave-fun.png 863w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-reconstr-wave-fun-600x382.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-reconstr-wave-fun-300x191.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-reconstr-wave-fun-768x489.png 768w\" sizes=\"(max-width: 726px) 100vw, 726px\" \/><br \/>\nFig.4. Wavelet Function of Reconstruction of \u2018bior3.5\u2019 at level 5<\/figcaption><\/figure>\n<pre id=\"6aa7\" class=\"graf graf--pre graf-after--figure\"><code># scaling functions of decomposition\nplt.plot(x,phi_d)\nplt.show()<\/code><\/pre>\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-8401 \" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/04\/bior3.5-dec-scal-fun.png\" alt=\"\" width=\"728\" height=\"466\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-dec-scal-fun.png 857w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-dec-scal-fun-600x384.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-dec-scal-fun-300x192.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/04\/bior3.5-dec-scal-fun-768x492.png 768w\" sizes=\"(max-width: 728px) 100vw, 728px\" \/><figcaption>Fig.5. Scaling Function of decomposition of \u2018bior3.5\u2019 at level 5<\/figcaption><\/figure>\n<pre id=\"55aa\" class=\"graf graf--pre graf-after--figure\"><code># Scaling function of reconstruction\nplt.plot(x,phi_r)\nplt.show()<\/code><\/pre>\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/miro.medium.com\/max\/875\/1*mMBP2nuIl98i2ZBHKk5Z_g.png\" width=\"738\" height=\"484\" \/><figcaption>Fig.6. Scaling Function of reconstruction of \u2018bior3.5\u2019 at level 5<\/figcaption><\/figure>\n<h4 id=\"be9a\" class=\"graf graf--h4 graf-after--figure\">5.3. Continuous Wavelet Functions<\/h4>\n<p id=\"0514\" class=\"graf graf--p graf-after--h4\">Let\u2019s take \u2018gaus2\u2019 at level 5 for example as follows:<\/p>\n<p id=\"d189\" class=\"graf graf--p graf-after--p\">(1) Obtain the approximation function<\/p>\n<pre id=\"3e34\" class=\"graf graf--pre graf-after--p\"><code>W = pywt.ContinuousWavelet(\u2018gaus2\u2019)\n[psi,x] = Wavelet.wavefun(level=5)<\/code><\/pre>\n<p id=\"365e\" class=\"graf graf--p graf-after--pre\">(2) Plot the function<\/p>\n<pre id=\"3462\" class=\"graf graf--pre graf-after--p\"><code>plt.plot(x,psi)\nplt.show()<\/code><\/pre>\n<figure><img loading=\"lazy\" decoding=\"async\" class=\"\" src=\"https:\/\/miro.medium.com\/max\/875\/1*if0wlH--9YReTmSdjJr9Mw.png\" width=\"761\" height=\"496\" \/><figcaption>Fig.7. Wavelet Function of \u2018gaus2\u2019 at level 5<\/figcaption><\/figure>\n<h4 id=\"7fd7\" class=\"graf graf--h4 graf-after--figure\">5.4. Online\u00a0Course<\/h4>\n<p id=\"b195\" class=\"graf graf--p graf-after--h4\">If you are interested in learning Practical Wavelet Transforms from the very beginning, welcome to my online course via the following linage:<\/p>\n<p id=\"e832\" class=\"graf graf--p graf-after--p\"><a href=\"https:\/\/academy.deepsim.cacourses\/python-wavelet-fundamentals\/\"><em class=\"markup--em markup--p-em\">Practical Python Wavelet Transforms (I): Fudamentals DWT<\/em><\/a><\/p>\n<p id=\"b417\" class=\"graf graf--p graf-after--p\"><a href=\"https:\/\/academy.deepsim.cacourses\/practical-python-wavelet-transforms-ii-1d-dwt\/\"><em class=\"markup--em markup--p-em\">Practical Python Wavelet Transforms (II): 1D DWT<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyWavelets is probably the most Powerful Wavelet Transform Library of Python Today Wavelet Transforms (WT) or wavelet analysis is probably<\/p>","protected":false},"author":2,"featured_media":8404,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[66,65,67,69],"tags":[],"class_list":["post-7545","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jupyter-notebook","category-python","category-signal-processing","category-wavelet-transforms"],"_links":{"self":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7545","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/comments?post=7545"}],"version-history":[{"count":0,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7545\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media\/8404"}],"wp:attachment":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media?parent=7545"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/categories?post=7545"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/tags?post=7545"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}