{"id":40170,"date":"2023-01-30T15:29:05","date_gmt":"2023-01-30T15:29:05","guid":{"rendered":"https:\/\/www.askpython.com\/?p=40170"},"modified":"2023-01-30T15:29:07","modified_gmt":"2023-01-30T15:29:07","slug":"numpy-convolve","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-convolve","title":{"rendered":"How to use Numpy Convolve in Python?"},"content":{"rendered":"\n<p>Digital electronics always rely on processing signals for their routine functioning. The support from Python extends to this part of the spectrum too! <\/p>\n\n\n\n<p>The operation of combining signals is known as <em>convolution <\/em>and Python has an exclusive function to carry it out. This function lies within the <em>numpy <\/em>library. So, let us start by importing it using the code below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport numpy as np\n<\/pre><\/div>\n\n\n<p>Thereafter, we shall explore further the&nbsp;<em>convolve( )&nbsp;<\/em>function through each of the following sections.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>The <em>convolve( )&nbsp;<\/em>function &#8211; explained<\/strong><\/li>\n\n\n\n<li><strong>Syntax of&nbsp;the <\/strong><strong><em>convolve( )&nbsp;<\/em>function<\/strong><\/li>\n\n\n\n<li><strong>Use cases for the <\/strong><strong><em>convolve( ) <\/em>function<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-interp\" data-type=\"post\" data-id=\"39343\">Numpy interp \u2013 One-dimensional linear interpolation for monotonically increasing sample points<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>The <em>convolve( )<\/em> function \u2013 explained<\/strong><\/h2>\n\n\n\n<p>The mathematical technique by which two signals are combined together to form a third signal is known as <em>convolution. <\/em>The probability theory states that the sum of two independent random variables could be distributed only in accordance with the convolution of their individual distributions. <\/p>\n\n\n\n<p><em>Convolution<\/em> is the most critical know-how for someone who is into digital signal processing. The <em>convolve( ) <\/em>function from the <em>numpy <\/em>library deploys two distinct methods to carry out this technique.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Linear convolution<\/li>\n\n\n\n<li>Discrete convolution<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Syntax of\u00a0the <em>convolve( )<\/em>\u00a0function<\/strong><\/h2>\n\n\n\n<p>Given below is the syntax of the <em>convolve( ) <\/em>function with its different components required for its functioning. It is to be noted that the function can be executed only if the inputs are given as one-dimensional arrays.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnumpy.convolve(a, v, mode=\u2019full\u2019)\n<\/pre><\/div>\n\n\n<p>where,<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><em>a \u2013 <\/em><\/strong>First one-dimensional array of length N<\/li>\n\n\n\n<li><strong><em>v \u2013 <\/em><\/strong>Second one-dimensional array of length M<\/li>\n\n\n\n<li><strong><em>mode \u2013 <\/em><\/strong>An option set to <em>full <\/em>by default which is used to state the type of convolution that is to be carried out. It provides three categories to choose from viz.\n<ul class=\"wp-block-list\">\n<li><strong><em>full \u2013<\/em><\/strong> used to return the convolution at each point of overlap. The shape of the output is given by (N+M-1)<\/li>\n\n\n\n<li><strong><em>same \u2013<\/em><\/strong> used to return the output as given by <em>max(M, N)<\/em><\/li>\n\n\n\n<li><strong><em>valid &#8211;<\/em><\/strong> used to return the output as given by <em>max(M, N) \u2013 min(M,N) + 1<\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>It is to be noted that at <em>full &#038; same <\/em>modes, the points on the signals do not overlap completely. But in the case of <em>valid <\/em>mode, the convolution product is only returned for the points where the signals overlap completely. <\/p>\n\n\n\n<p>Also one has to bear in mind that if the array at position<em> \u2018v\u2019<\/em> is longer than that at position <em>\u2018a\u2019<\/em>, then Python takes the privilege of swapping their positions before the code is run.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-positive\" data-type=\"post\" data-id=\"39715\">How to Use Numpy Positive in Python?<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Use cases for the <em>convolve( ) <\/em>function<\/strong><\/h2>\n\n\n\n<p>Let us construct a pair of one-dimensional arrays. Once done, assign them to variables <em>a <\/em>&amp; <em>v <\/em>respectively.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = np.array(&#x5B;1, -1, 9, 0])\nv = np.array(&#x5B;5, 7, -6, 4])\n<\/pre><\/div>\n\n\n<p>Now let us deduce the convolution product with the default setting using the following code.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.convolve(a, v)\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"408\" height=\"102\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Results-with-default-mode-setting.jpg\" alt=\"Results With Default Mode Setting\" class=\"wp-image-40243\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Results-with-default-mode-setting.jpg 408w, https:\/\/www.askpython.com\/wp-content\/uploads\/2023\/01\/Results-with-default-mode-setting-300x75.jpg 300w\" sizes=\"auto, (max-width: 408px) 100vw, 408px\" \/><figcaption class=\"wp-element-caption\">Results With Default Mode Setting<\/figcaption><\/figure>\n\n\n\n<p>Let us now include the <em>mode <\/em>option for the same input arrays to change its default setting into the<em> same<\/em> mode using the code given below.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.convolve(a, v, mode = \u2018same\u2019)\n<\/pre><\/div>\n\n\n<p>Following is the output array returned.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOutput: array(&#x5B;  2,  32,  73, -58])\n<\/pre><\/div>\n\n\n<p>It is time to demonstrate the <em>valid <\/em>mode for the same input arrays by changing the above code as follows.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nnp.convolve(a, v, mode = \u2018valid\u2019)\n<\/pre><\/div>\n\n\n<p>Following is the output array returned.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code aligncenter\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nOutput: array(&#x5B;73])\n<\/pre><\/div>\n\n\n<p>One can observe the change in the length of the output when the same input arrays are run with each of the modes. This bears evidence to the logic of determining the length of the outputs as stated in the syntax section earlier.<\/p>\n\n\n\n<p><strong><em>Also read: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-qr\" data-type=\"post\" data-id=\"40031\">How to find the QR Factorization of Matrix using Numpy?<\/a><\/em><\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Now that we have reached the end of this article, hope it has elaborated on how to use the\u00a0<em>convolve( )\u00a0<\/em>function from the\u00a0<em>numpy\u00a0<\/em>library. Here\u2019s another article that details the <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/numpy-linalg-qr\" target=\"_blank\" data-type=\"post\" data-id=\"40031\" rel=\"noreferrer noopener\">usage of the <em>linalg.qr( ) <\/em>function from the\u00a0<em>numpy\u00a0<\/em>library<\/a> in Python. There are numerous other enjoyable and equally informative articles in <a href=\"https:\/\/www.askpython.com\/\" data-type=\"URL\" data-id=\"https:\/\/www.askpython.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">AskPython<\/a> that might be of great help to those who are looking to level up in Python.\u00a0<em>Carpe diem<\/em>!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/generated\/numpy.convolve.html\" target=\"_blank\" rel=\"noopener\">Official documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Digital electronics always rely on processing signals for their routine functioning. The support from Python extends to this part of the spectrum too! The operation of combining signals is known as convolution and Python has an exclusive function to carry it out. This function lies within the numpy library. So, let us start by importing [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":40172,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[93],"tags":[],"class_list":["post-40170","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-numpy"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/40170","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=40170"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/40170\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/40172"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=40170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=40170"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=40170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}