{"id":7554,"date":"2022-04-25T00:00:00","date_gmt":"2022-04-25T00:00:00","guid":{"rendered":"https:\/\/academy.deepsim.caplot-the-filter-bank-of-a-disctete-wavelet-in-python-42fbb6eb418d\/"},"modified":"2022-11-25T04:03:58","modified_gmt":"2022-11-25T04:03:58","slug":"plot-the-filter-bank-of-a-disctete-wavelet-in-python-42fbb6eb418d","status":"publish","type":"post","link":"https:\/\/academy.deepsim.ca\/zh\/plot-the-filter-bank-of-a-disctete-wavelet-in-python-42fbb6eb418d\/","title":{"rendered":"\u4f7f\u7528Python\u7ed8\u5236\u79bb\u6563\u5c0f\u6ce2\u6ee4\u6ce2\u5668\u7ec4\u7684\u56fe\u50cf"},"content":{"rendered":"<p id=\"4a15\" class=\"graf graf--h3 graf--leading graf--title\">Filters method is an efficient way to implement Discrete Wavelet Transform<\/p>\n<figure><img decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/08\/1kQutn9dHAgdAegFxZPj48A.png\" alt=\"\" \/><\/figure>\n<h3 id=\"1165\" class=\"graf graf--h3 graf-after--figure\">1. What is a Filter Bank<\/h3>\n<p>Filter banks play an important role in modern signal and image processing. As its name suggests, a Filter Bank is a bank of filters, meaning a collection of filters, or many filters in simple words, and each of the filter is applied to the original signal. So, one signals can be decomposed into 2 or more signals out. Then what is a Filter bank?<\/p>\n<p>Simply speaking, a Filter Bank can be defined as a system (an array) of bandpass filters\u00a0\u00a0that split the input signal (<em><i>S<\/i><\/em><em><sub><i>t<\/i><\/sub><\/em>) into a set of analysis signals (<em><i>x<\/i><\/em><sub>1t<\/sub>, <em><i>x<\/i><\/em><sub>2t<\/sub>, \u2026, <em><i>x<\/i><\/em><sub>nt<\/sub> ) and each one carrying a single frequency sub-band of the original signal.<\/p>\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/08\/filter_bank-1024x513.png\" alt=\"\" class=\"wp-image-8328\" width=\"814\" height=\"408\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/filter_bank-1024x513.png 1024w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/filter_bank-600x301.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/filter_bank-300x150.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/filter_bank-768x385.png 768w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/filter_bank.png 1092w\" sizes=\"(max-width: 814px) 100vw, 814px\" \/><figcaption class=\"wp-element-caption\"><strong>Fig. 1. Structure of a Filter Bank<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p>In wavelet analysis, filter banks usually refers to a class of systems that generates scaling and wavelet function. The below figure shows the simple structure of a filter bank can be illustrated by the figure below.<\/p>\n\n\n\n<p>In practice, an efficient way to implement Discrete Wavelet Transform (DWT) is to use filters method, which was developed in 1988 by Mallat. This very practical filtering algorithm yields a Fast Discrete Wavelet Transform (FWT).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"aca6\">2. Wavelet filters coefficients<\/h3>\n\n\n\n<p id=\"ed44\">Wavelet filters coefficients are the most important information used in Discrete Wavelet Transforms (DWT). In the PyWavelets library of Python, filters coefficients can be obtained via the <strong>dec_lo<\/strong>, <strong>dec_hi<\/strong>, <strong>rec_lo<\/strong> and <strong>rec_hi<\/strong> attributes, which correspond to <em>lowpass<\/em> and <em>highpass<\/em> <strong>decomposition filters<\/strong> and <em>lowpass<\/em> and <em>highpass<\/em> <strong>reconstruction filters<\/strong> respectively.<\/p>\n\n\n\n<p id=\"2387\">In<a href=\"https:\/\/medium.com\/@shouke.wei\/plot-approximations-of-wavelet-and-scaling-functions-in-python-dabd31452bdf\" target=\"_blank\" rel=\"noreferrer noopener\"> a previous article<\/a>, we have already learned what PyWavelets is, how to install it, and how to display its built-in Wavelets families and their members in PyWavelets, and so on. In the PyWavelets, the <code>filter_bank<\/code>method returns a list for the current wavelet in the following order: <code>[dec_lo, dec_hi, rec_lo, rec_hi].<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>dec_lo<\/code>: Decomposition low-pass filter<\/li>\n\n\n\n<li><code>dec_hi<\/code>: Decomposition high-pass filter<\/li>\n\n\n\n<li><code>rec_lo<\/code>: Reconstruction low-pass filter<\/li>\n\n\n\n<li><code>rec_hi<\/code>: Reconstruction high-pass filter<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"a772\">3. Print Filter Bank of a Discrete&nbsp;Wavelet<\/h3>\n\n\n\n<p id=\"c583\">First, let\u2019s import the required packages: one is PyWavelets and another is matplotlib for visualization.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># import required packages\nimport pywt\nimport matplotlib.pyplot as plt<\/code><\/pre>\n\n\n\n<p id=\"96f3\">Next, we define a discrete wavelt, for example, \u201c<strong>db5<\/strong>\u201d&nbsp;, and then display its filter bank as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wavelet = pywt.Wavelet('db5')\nwavelet.filter_bank<\/code><\/pre>\n\n\n\n<p id=\"2af0\">The output is as follows:<\/p>\n\n\n\n<pre id=\"4c7a\" class=\"wp-block-preformatted\">([0.0033357252854737712,\n  -0.012580751999081999,\n  -0.006241490212798274,\n  0.07757149384004572,\n  -0.032244869584638375,\n  -0.24229488706638203,\n  0.13842814590132074,\n  0.7243085284377729,\n  0.6038292697971896,\n  0.16010239797419293],\n [-0.16010239797419293,\n  0.6038292697971896,\n  -0.7243085284377729,\n  0.13842814590132074,\n  0.24229488706638203,\n  -0.032244869584638375,\n  -0.07757149384004572,\n  -0.006241490212798274,\n  0.012580751999081999,\n  0.0033357252854737712],\n [0.16010239797419293,\n  0.6038292697971896,\n  0.7243085284377729,\n  0.13842814590132074,\n  -0.24229488706638203,\n  -0.032244869584638375,\n  0.07757149384004572,\n  -0.006241490212798274,\n  -0.012580751999081999,\n  0.0033357252854737712],\n [0.0033357252854737712,\n  0.012580751999081999,\n  -0.006241490212798274,\n  -0.07757149384004572,\n  -0.032244869584638375,\n  0.24229488706638203,\n  0.13842814590132074,\n  -0.7243085284377729,\n  0.6038292697971896,\n  -0.16010239797419293])<\/pre>\n\n\n\n<p id=\"2799\">We can just print one filter, for example, the decomposition low-pass filter using the following code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>wavelet.dec_lo<\/code><\/pre>\n\n\n\n<p id=\"e7aa\">We get the following results:<\/p>\n\n\n\n<pre id=\"3526\" class=\"wp-block-preformatted\">[0.0033357252854737712,\n -0.012580751999081999,\n -0.006241490212798274,\n 0.07757149384004572,\n -0.032244869584638375,\n -0.24229488706638203,\n 0.13842814590132074,\n 0.7243085284377729,\n 0.6038292697971896,\n 0.16010239797419293]<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"b81c\">5. Visualization of Wavelet&nbsp;Bank<\/h3>\n\n\n\n<p id=\"87a9\">First, let\u2019s obtain the list of the four filters using the following methods:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;dec_lo, dec_hi, rec_lo, rec_hi] = wavelet.filter_bank<\/code><\/pre>\n\n\n\n<p id=\"9605\">Then, we can use Matplotlib to make a stem plot for each filter.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(15,7))\n\nax1.stem(dec_lo) \nax1.set_title('Decomposition low-pass filter')\nax2.stem(dec_hi)\nax2.set_title('Decomposition high-pass filter')\nax3.stem(rec_lo)\nax3.set_title('Reconstruction low-pass filter')\nax4.stem(rec_hi)\nax4.set_title('Reconstruction high-pass filter')\n\nplt.savefig('.\/results\/filter_bank.png') # save it in any path, here I save it into the results folder in the working directory\nplt.show()<\/code><\/pre>\n\n\n\n<p id=\"0e8f\">The output looks as follows:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" width=\"1024\" height=\"491\" src=\"https:\/\/academy.deepsim.cawp-content\/uploads\/2022\/08\/dwt_filters-1024x491.png\" alt=\"\" class=\"wp-image-8330\" srcset=\"https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/dwt_filters-1024x491.png 1024w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/dwt_filters-600x287.png 600w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/dwt_filters-300x144.png 300w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/dwt_filters-768x368.png 768w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/dwt_filters-1536x736.png 1536w, https:\/\/academy.deepsim.ca\/wp-content\/uploads\/2022\/08\/dwt_filters-2048x981.png 2048w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\"><strong>Fig. 2. Plots of Filter Banks of db5<\/strong><\/figcaption><\/figure>\n<\/div>\n\n\n<p id=\"272b\"><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9d31\">6. Online&nbsp;Course<\/h3>\n\n\n\n<p id=\"c0b3\">If you are interested in learning Practical Wavelet Transforms from very beginning, welcome to the courses via the following linage:<\/p>\n\n\n\n<p id=\"c0b3\"><a href=\"https:\/\/academy.deepsim.cacourses\/python-wavelet-fundamentals\/\">Practical Python Wavelet Transforms (I): Fundamentals<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/academy.deepsim.cacourses\/practical-python-wavelet-transforms-ii-1d-dwt\/\">Practical Python Wavelet Transforms (II): 1D DWT<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Filters method is an efficient way to implement Discrete Wavelet Transform 1. What is a Filter Bank Filter banks play<\/p>","protected":false},"author":2,"featured_media":7634,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65,67,69],"tags":[],"class_list":["post-7554","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-signal-processing","category-wavelet-transforms"],"_links":{"self":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7554","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=7554"}],"version-history":[{"count":0,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/posts\/7554\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media\/7634"}],"wp:attachment":[{"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/media?parent=7554"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/categories?post=7554"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/academy.deepsim.ca\/zh\/wp-json\/wp\/v2\/tags?post=7554"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}