OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why does my numpy convolution appear off-centred?

  • Thread starter Thread starter Jacob Ayre
  • Start date Start date
J

Jacob Ayre

Guest
I'm trying to convolve two probability maps in Python with numpy.convolve, and decided to use mode='same' so that I could plot the convolution against the original distributions. However, my convolution doesn't seem to be in the correct place, as I would expect its peak to lie between distributions A and B (see figure), or at the very least at a higher value of x than it is now. When I try using mode='full', the convolution occurs in a different place on the x-axis, but still does not occur where I would expect it. The location of the convolution in relation to the original arrays is very important to what I'm doing, so needs to occur between the other two distributions (provided by understanding of maths is right). I don't know why this isn't happening, so does anyone know how to fix it? Any help would be greatly appreciated!

A plot of probability distributions A and B, and their convolution with mode='same' A plot of the convolution of A and B with mode='full'

This is the code I'm using (my two input arrays are very long so I've left them out can I can probably find a way to supply them if it would help):

Code:
import numpy as np
import matplotlib.pyplot as plt

x1=np.arange(len(GRB_240612A))
x2=np.arange(len(GRB_240612B))
if len(x1)==len(x2):
  x=x1

convolution=np.convolve(GRB_240612A, GRB_240612B, mode='same')

plt.plot(x, GRB_240612A, label='A')
plt.plot(x, GRB_240612B, label='B')
plt.plot(x, convolution, label='Convolution of A and B')

plt.legend()
plt.xlim(0)
plt.ylim(0)
plt.show()
<p>I'm trying to convolve two probability maps in Python with numpy.convolve, and decided to use mode='same' so that I could plot the convolution against the original distributions. However, my convolution doesn't seem to be in the correct place, as I would expect its peak to lie between distributions A and B (see figure), or at the very least at a higher value of x than it is now. When I try using mode='full', the convolution occurs in a different place on the x-axis, but still does not occur where I would expect it. The location of the convolution in relation to the original arrays is very important to what I'm doing, so needs to occur between the other two distributions (provided by understanding of maths is right). I don't know why this isn't happening, so does anyone know how to fix it? Any help would be greatly appreciated!</p>
<p><a href="https://i.sstatic.net/9QsZJc6K.png" rel="nofollow noreferrer">A plot of probability distributions A and B, and their convolution with mode='same'</a>
<a href="https://i.sstatic.net/TM4aU4AJ.png" rel="nofollow noreferrer">A plot of the convolution of A and B with mode='full'</a></p>
<p>This is the code I'm using (my two input arrays are very long so I've left them out can I can probably find a way to supply them if it would help):</p>
<pre><code>import numpy as np
import matplotlib.pyplot as plt

x1=np.arange(len(GRB_240612A))
x2=np.arange(len(GRB_240612B))
if len(x1)==len(x2):
x=x1

convolution=np.convolve(GRB_240612A, GRB_240612B, mode='same')

plt.plot(x, GRB_240612A, label='A')
plt.plot(x, GRB_240612B, label='B')
plt.plot(x, convolution, label='Convolution of A and B')

plt.legend()
plt.xlim(0)
plt.ylim(0)
plt.show()
</code></pre>
 
Top