python 卷积函数
What is a convolution? OK, that’s not such a simple question. Instead, I am will give you a very basic example and then I will show you how to do this in Python with actual functions.
什么是卷积? 好吧,这不是一个简单的问题。 相反,我将给您一个非常基本的示例,然后向您展示如何在Python中使用实际功能执行此操作。
So, suppose you have an object and a camera. The object is the Sun — which produces different frequencies of light at different intensities. You could consider this frequency distribution of intensities a function (I’m going to call this function f). Next, you have a camera. The camera isn’t perfect. It is more sensitive to different frequencies of light. I’ll call this camera frequency sensitivity the function g.
因此,假设您有一个对象和一个照相机。 对象是太阳,它以不同的强度产生不同频率的光。 您可以将这种强度的频率分布视为一个函数(我将其称为f )。 接下来,您有一个摄像头。 相机并不完美。 它对不同频率的光更敏感。 我将此相机频率灵敏度称为功能g 。
When you capture an image of the Sun with this camera, you get some data. That data shows the intensity of different frequencies of light — but this data depends on both the Sun (function f) AND the camera (function g). The data you get would be the convolution of f and g.
当使用此相机捕获太阳图像时,您将获得一些数据。 该数据显示了不同频率的光的强度,但是该数据取决于太阳(函数f )和相机(函数g )。 您获得的数据将是f和g的卷积。
OK, I’m not sure if that’s the best example of a convolution — but I’m going to move forward anyway. Actually, I’ve seen this awesome animation of a convolution and I think it helps explain the thing. I’ll go through the steps to make this down below.
好的,我不确定这是否是卷积的最佳示例-但我还是要继续前进。 实际上,我已经看过这种令人惊叹的卷积动画,并且我认为这有助于解释问题。 我将按照以下步骤进行操作。
In this case, the red curve is one function and the blue is the other one. You can think of the convolution and as the area of amount the two functions overlap. So, as the two functions start to overlap the area in common increases up to the point where they are exactly on top of each other. Then the convolution starts to decrease as they are no longer completely overlapping.
在这种情况下,红色曲线是一种功能,而蓝色是另一种功能。 您可以想到卷积,并且随着数量的增加,两个函数重叠。 因此,当这两个功能开始重叠时,共同的面积会增加,直到它们恰好彼此重叠。 然后,由于卷积不再完全重叠,卷积开始减小。
Fine. But how do you calculate this stuff? One way to define the convolution is with the following integral.
精细。 但是,您如何计算这些东西呢? 定义卷积的一种方法是使用以下积分。
Note that this is an integral over the variable t, but you get the convolution as a function of x. You can think of the t parameter as the part that moves the two functions over each other.
请注意,这是变量t的积分,但是您可以将卷积作为x的函数。 您可以将t参数视为使两个函数相互移动的部分。
But since this is just an integral, it should be fairly straightforward to calculate this as a numerical sum. The basic idea is to take tiny steps along the axis (it would technically be t-axis) and calculate the size of the rectangle that as a width of dt (the step size) and a height of the product of functions. Here is a post with ALL of the details of a numerical integration.
但是,由于这只是一个整数,因此将其作为数字总和进行计算应该相当简单。 基本思想是沿轴(在技术上为t轴)采取微小的步长,并计算矩形的大小,以dt的宽度(步长)和函数乘积的高度表示。 这是有关数值积分所有细节的文章。
Numerical Calculation of a Convolution
卷积的数值计算
Let’s start with the convolution of the two square functions show above in the animation. Here’s the plan.
让我们从动画中上面显示的两个平方函数的卷积开始。 这是计划。
Define the two functions. I’m going to do this by making the function a python function. 定义两个功能。 我将通过使函数成为python函数来实现此目的。 Define a function that determines the integral of the product of these two functions for a particular value of x. This will just be a normal numerical integral and return just one number — but this will be the convolution value. 定义一个函数,该函数确定特定x值这两个函数的乘积的积分。 这将是一个普通的数值积分,并且仅返回一个数字-但这将是卷积值。 Finally, just move to a new x-value and calculate the convolution number again. All of these convolution numbers will be the convolution function (which I will plot). 最后,只需移至新的x值并再次计算卷积数即可。 所有这些卷积数都将是卷积函数(我将对其进行绘制)。
Oh, I guess I should also put this python graphing tutorial here — just in case.
哦,我想我也应该在这里放这个python图形教程 -以防万一。
OK, but here is the code for the first function — f(x). Full program is here.
好的,但是这里是第一个函数的代码-f(x)。 完整程序在这里。
Basically, this gives back a value of 0.5 if the value of t is in between -0.7 and 0.7 — I just picked these values for no particular reason. Otherwise, it returns a value of zero. The function g(x) would look exactly the same.
基本上,如果t的值介于-0.7和0.7之间,则返回的值为0.5-我只是出于特殊原因选择了这些值。 否则,它将返回零值。 函数g(x)看起来完全一样。
Now for the integration for a particular value of x. Here is the code for that part.
现在,对于x的特定值进行积分。 这是该部分的代码。
Yes, I know. This probably a bad idea to use the two functions in another function. There’s a better way to do it, but I wanted to make the code as simple as possible. Here are some comments on this chunk of code.
是的我知道。 在另一个函数中使用这两个函数可能是一个坏主意。 有一种更好的方法,但是我想使代码尽可能简单。 这是关于这段代码的一些注释。
The fog term is the cumulative sum of the integral to find the convolution of f and g. 雾项是找到f和g的卷积的积分的累积和。 You need to add up stuff from negative infinity to positive infinity. You can’t do that with a numerical calculation, so I’m going from -5 to 5 instead. It’s close enough to infinity for this case. 您需要将负无穷大到正无穷大。 您无法通过数值计算来做到这一点,因此我将其从-5改为5。 在这种情况下,它足够接近无穷大。 Notice I have two variables in this function. There is xt (temp x) that changes during the numerical integration and then there is x (plain x). The plain x is just a number passed to the function to get the convolution number at that point. 注意,此函数中有两个变量。 有xt(温度x)在数值积分期间发生变化,然后有x(纯正x)。 普通x只是传递给函数的数字,以在该点获得卷积数。 Line 31 in the code above is the actual part that I am summing. It’s basically the integration. 上面代码中的第31行是我正在总结的实际部分。 基本上就是整合。
Now for the last part. I just need to find the convolution number at a bunch of different x-values. Here’s that code.
现在到最后一部分。 我只需要找到一堆不同x值的卷积数。 这是代码。
I’m using “t” just so I don’t confuse myself with all the x’s in there. You can see that I start at negative infinity (which I call t = -5) and go up to positive infinity ( while t<5:). Other than that, it’s just calling the functions and plotting stuff. Here’s the output. Notice that f(x) and g(x) are RIGHT on top of each other.
我使用的是“ t”,所以我不会把里面的所有x都弄混了。 您可以看到我从负无穷大开始(我称t = -5 ),然后上升到正无穷大( while t<5: :)。 除此之外,它只是调用函数并绘制内容。 这是输出。 请注意,f(x)和g(x)彼此对置。
What if I want to do a convolution with different functions? Simple — just change the functions in the python code. Boom, that’s it. Just for fun, here is what it looks like when I change one of the square functions to a Gaussian.
如果我想用不同的函数进行卷积怎么办? 简单-只需更改python代码中的功能即可。 繁荣,就是这样。 只是为了好玩,这是将平方函数之一更改为高斯函数时的样子。
So, we are cool now?
那么,我们现在很酷吗?
Animated Convolutions
动画卷积
I didn’t forget. I still need to show you how to make an animated convolution — because it looks cool.
我没有忘记。 我仍然需要向您展示如何制作动画卷积-因为它看起来很酷。
Of course step 1 is going to be to look at my graphing tutorial. In particular, we are going to need to animated a graph. The basic idea is to calculate all the stuff you need to plot the graph and put it in a list (a python list). Then you can change that list and re-plot it to make it look animated.
当然,第一步将是看我的制图教程 。 特别是,我们将需要对图表进行动画处理。 基本思想是计算绘制图表并将其放入列表(python列表)所需的所有内容。 然后,您可以更改该列表并重新打印以使其看起来生动。
Let me show you the code and then make some comments. First, setting up the graphs.
让我向您展示代码,然后进行一些注释。 首先,设置图表。
Line 3 makes the stuff to hold the plots. If you don’t set the min and max values for x, the graph is going to look jumpy. I don’t know why. The rest of the lines are just for 4 graphs. Line 5 and 6 are the two functions. Line 7 and 8 show the stuff for the convolution. I’m actually plotting it twice (to make it look nice). One of the convolution plots is a normal line and the other is a bar graph so that it looks like a “filled in” graph.
第3行制作东西来保存情节。 如果您未设置x的最小值和最大值,则该图形将显得跳动。 我不知道为什么 其余各行仅用于4个图形。 第5和6行是两个功能。 第7和8行显示了卷积的内容。 我实际上在绘制两次(以使其看起来不错)。 卷积图之一是法线,另一个是条形图,因此看起来像“填充”图。
I’m skipping over the functions and the convolution stuff (since I already covered that). Also, there is a small bit of code to plot g(x) — but that’s just a normal plot. Now for the fun stuff.
我跳过了函数和卷积的内容(因为我已经讲过了)。 另外,有少量代码可以绘制g(x)-但这只是正常的绘制。 现在来看看有趣的东西。
Notice that I changed infinity from -3 to 3 (instead of -5 to 5). I don’t know why I do things sometimes. But check out line 46: rate(300). This tells the code how fast to run. It says: “don’t do any more than 300 loops per second”. It basically puts an upper speed limit to have fast things happen. If you want it to run faster, change 300 to 500 or something.
请注意,我将无穷大从-3更改为3(而不是从-5更改为5)。 我不知道为什么有时候我会做事。 但是请查看第46行: rate(300) 。 这告诉代码运行速度。 它说:“每秒不超过300个循环”。 基本上,它设置了上限以使快速的事情发生。 如果您希望它运行得更快,请将300更改为500左右。
I’m only animating one function — f(x). That’s the function that I need to re-plot. To do this, I first make an empty list in line 47: data=[]. In the loop (line 49), I go through and recalculate the values for f(x) shifted over by an amount t. I put all these new values into the list until everything from -3 to 3 is calculated.
我只是给一个函数设置动画-f(x)。 那就是我需要重新绘制的功能。 为此,我首先在第47行中创建一个空列表: data=[] 。 在循环(第49行)中,我经历并重新计算了偏移量t的f(x)的值。 我将所有这些新值放入列表中,直到计算出从-3到3的所有值为止。
Then in line 53, I plot all these values at once. This trick makes it look like the curve is moving. Finally, I calculate the convolution at that value of x and add a data point to the convolution plot (along with the bar graph) in line 54 and 55.
然后在第53行中,一次绘制所有这些值。 这个技巧使曲线看起来像在移动。 最后,我在x的值处计算卷积,并在第54和55行中将数据点添加到卷积图中(连同条形图)。
That’s it. Here’s what it looks like in the end.
而已。 这是最后的样子。
Come on, that looks nice. Actually, I’m sort of impressed myself. Oh, quick tip — I just turned the graph into a gif by capturing it with some software (I used GIPHY Capture).
来吧,看起来不错。 实际上,我给自己留下了深刻的印象。 哦,快速提示–我只是通过使用某些软件捕获图形(我使用GIPHY Capture )将图形转换为gif。
翻译自: https://medium.com/swlh/calculating-the-convolution-of-two-functions-with-python-8944e56f5664
python 卷积函数
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。