Skip to content

Commit e456986

Browse files
committed
adding some smallscale changes before adding in visualizations
1 parent 83144a4 commit e456986

File tree

2 files changed

+56
-33
lines changed

2 files changed

+56
-33
lines changed

contents/convolutions/1d/1d.md

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,83 @@
11
# Convolutions in 1D
2-
For now, let's focus on one-dimensional convolutions, which are defined as:
2+
Though the act of applying a filter to an image is the most well-known application of convolutions, it is important to start with the funcamentals, so let us focus on one-dimensional convolutions for now.
3+
These are defined as:
34

45
$$(f*g)(x) = \int_{-\infty}^{\infty}f(\xi)g(x-\xi)d\xi = \int_{-\infty}^{\infty}f(x-\xi)g(\xi)d\xi$$
56

67
Note that in this case, $$x$$ is not necessarily a spatial element.
7-
Often times, it is time or something else entirely!
8-
The easiest way to think about this is that the function $$g(x)$$ is being shifted across all of space by the variable $$\xi$$.
8+
The easiest way to think about this is that the function $$g(x)$$ is reversed and then shifted across all of space by the variable $$\xi$$.
99
At every point $$x$$, we multiply $$f(x)$$ and $$g(x)$$ and integrate the multiplied output to find the convolutional output for that spatial step, $$(f*g)(x)$$.
1010
Note that in code, this is often discretized to look like:
1111

1212
$$(f*g)[n] = \sum_{m = -\infty}^{\infty}f[m]g[n-m] = \sum_{m = -\infty}^{\infty}f[n-m]g[m]$$
1313

1414
Where `f[n]` and `g[n]` are arrays of some form.
15-
This means we basically just need to keep one array steady, flip the second array around, and move it through the first array one step at a time, performing a simple element-wise multiplication each step.
15+
Similar to the interpretation in continuous space, this means that we basically need to keep one array steady, reverse the second array, and move it through the first array one step at a time, performing a simple element-wise multiplication each step.
1616

17-
This can be seen in the following animation:
17+
At this stage, the math and code might still be a little opaque, and it might help to think of the second signal as an array of weights, signalling how much of the convolutional output relies on any particular element within signal one.
18+
In this way a second signal is a filter, and will be called as such for the remainder of this chapter.
19+
For example, let's say that signal one is a square wave, and signal two is a filter composed of a three element triangle wave (`[1 2 1]`), as shown here:
20+
21+
The simplest interpretation for this is that at every point $$x$$ along signal one, the convolutional output will be composed of one part $$x-1$$, two parts $$x$$, and one part $$x+1$$.
22+
If we perform a convolution with signal one and this filter, we will find that the square wave is smeared a little at the edges, like so:
23+
24+
ADD ANIMATION
25+
26+
In a sense, the convolution uses the filter as a guide to figure out how much of the signal to stir together at different points.
27+
Now let's extend this example a bit further with a square wave convolved with a relatively sharp Gaussian, which can be seen in the following animation:
28+
29+
ADD ANIMATION
30+
31+
Note that the final convolved image lookes a lot like the square, except that it's boundaries have been smoothed out or "blurred."
32+
In practice whenever a Gaussian filter is used, it will always blur the other convolved signal, which is why a convolution with a Gaussian is also called a *blurring operation*.
33+
This operation is used very often when dealing with two-dimensional images, and we will discuss common kernels found in the wild in the next section on [convolutions of images](../2d/2d.md).
34+
35+
Finally, let's extend this concept to one final example of a square wave convolved with a triangular, saw-tooth function.
1836

1937
<div style="text-align:center">
2038
<video style="width:90%" controls loop>
21-
<source src="../res/1d_gaussian_animation.mp4" type="video/mp4">
39+
<source src="../res/1d_triangle_animation.mp4" type="video/mp4">
2240
Your browser does not support the video tag.
2341
</video>
2442
</div>
2543

26-
Note that in this case, the output array will be the size of `f[n]` and `g[n]` put together.
27-
Sometimes, though, we have an large size for `f[n]` and a small size for `g[n]`.
28-
In this case `g[n]` is often called a *filter*, and often times when we are using a filter on an array (that might represent an image or some form of data), we want the output array to be the same size as the input.
29-
In this case, rather than outputting a larger array, we often do something special at the borders of the array.
30-
Depending on the situation, this may be necessary.
44+
Here, the output is similar to both the triangle and the square in different ways.
45+
All of these examples show why people often call a convolution a blending operation, as it mixes two signals together.
46+
47+
In all of these case, the output array is the size of `f[n]` and `g[n]` put together, and because the Gaussian had a large number of zeros on either side of it, the convolved output was twice as large as either input signal.
48+
If we were to trim the zeros from the Gaussian function, we would have a large size for `f[n]` and a small size for `g[n]`.
49+
50+
Often times when we are using a filter on an array (that might represent an image or some form of data), we want the output of the convolution to be the same size as the input.
51+
Because of this, we may need to enforce specific conditions at the boundary of the array.
3152
Note that there are different methods to deal with the edges in this case, so it's best to do whatever seems right when the situation arises.
53+
In the following subsection, we will talk about two common boundaries: the simple bound, and periodic boundary conditions.
3254

33-
At this stage, the math and code might still be a little opaque, and it might help to think of the second signal or filter as an array of weights, signalling how much of the convolutional output relies on any particular element within signal one.
34-
For example, let's say that signal one is a square wave, and signal two is a filter composed of a three element triangle wave (`[1 2 1]`), as shown here:
55+
## Simple boundaries
3556

36-
ADD IMAGES
57+
Since we already know that a naive convolution will result in an array the length of both signals put together, we might now be tasked at providing a simple boundary for the case where the output convolution *must* be a defined size -- usually the size of the input signal.
58+
In this case, the simplest boundary would be to assume that whenever the filter hits the end of the image, it simply disappears.
59+
Another way to think about it is that the signal only exists for the domain we specify it over, and is all 0s outside of this domain; therefore, the filter does not sum any signal from elements beyond its scope
3760

38-
The simplest interpretation for this is that at every point $$x$$ along signal one, the convolutional output will be composed of one part $$x-1$$, two parts $$x$$, and one part $$x+1$$.
39-
If we perform a convolution with signal one and this filter, we will find that the square wave is smeared a little at the edges, like so:
61+
62+
## Periodic boundary conditions
63+
64+
Another relatively simple boundary condition is the Periodic boundary condition.
65+
With the condition, the filter will wrap itself around to the other end of the signal whenever it hits a boundary.
66+
In this way, the signal is periodic, extending to infinity and beyond in both directions, and when the filter leaves one edge of the domain, it simply appears on the other, opposite edge.
67+
In code, this typically amounts to using some form of modulus operation...
68+
69+
This particular convolution is known as a *cyclic* convolution and is also the most common output of convolutions that work via the [convolutional theorem](../convolutional_theorem/convolutional_theorem.md), which will be discussed in another section.
4070

4171
<div style="text-align:center">
4272
<video style="width:90%" controls loop>
43-
<source src="../res/1d_triangle_animation.mp4" type="video/mp4">
73+
<source src="../res/1d_gaussian_animation.mp4" type="video/mp4">
4474
Your browser does not support the video tag.
4575
</video>
4676
</div>
4777

48-
This specific case is similar to a Gaussian, which is a common kernel used for blurring images in two-dimensions.
49-
For this reason, we will discuss common kernels found in the wild in the next section on [convolutions of images](../2d/2d.md).
78+
As a final note before continuing: dealing with boundaries is tricky business and can dramatically change the behaviour of the output convolution.
79+
For this reason, it's important to think about what types of boundaries will work best for what you, the programmer, actually needs.
80+
The selection of correct boundary conditions will be a common trope for a large portion of computer graphics and physica algorithms where researchers often need to present and simulate data on an array of some sort.
5081

5182
In code, the one-dimensional convolution might look something like this::
5283

contents/convolutions/convolutions.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
# TODO
2-
3-
1. Add appropriate graphics that explain the situation
4-
2. Boundary conditions
5-
3. Convolutional Theorem -- How does blending in frequency space result in convolution
6-
4. Correlation of same filters on images. Correlation. Take noisy signal, and correlate it with lean function to see if it's similar
7-
81
# Convolutions
92
To put it bluntly, convolutions can be confusing.
10-
Not only are they hard to describe, but if you do not see them in practice, it's hard to understand why you would ever want to use them.
3+
Not only are they hard to describe, but if they are not used in practice, it's hard to understand why they woudl ever be needed.
114
I'm going to do what I can to describe them in an intuitive way; however, I may need to come back to this in the future.
125
Let me know if there is anything here that is unclear, and I'll do what I can to clear it up.
136

7+
As always, we should start at the start.
148
If you take two functions $$f(x)$$ and $$g(x)$$, there are a number of ways you can combine them.
159
All basic operations can do this (addition, subtraction, multiplication, and division), but there are also special operations that only work with functions and do not work on standard variables or numbers.
1610
For example, $$f \circ g$$ is a *composition* of the two functions, where you plug $$g(x)$$ into $$f(x)$$.
1711
A convolution is another function-related operation, and is often notated with a star ($$*$$) operator, where
1812

1913
$$f(x)*g(x)=c(x)$$
2014

21-
provides a third function $$c(x)$$ that blends $$f(x)$$ and $$g(x)$$.
15+
provides a third function $$c(x)$$ that is a blended version of $$f(x)$$ and $$g(x)$$.
2216
This concept is known as a *convolution*, and as a rather important side-note: there is an incredibly similar operator known as a *correlation* which will be discussed in the near future.
2317
Now we are left with a rather vague question: How do we *blend* functions?
2418

2519
To answer this question, we will need to show off a few simple graphics or animations in the [Convolutions in 1D](1d/1d.md) section before discussing the mathematical definition.
26-
We will then move on to the application of convolutions to images in the [Convolutions of images](2d/2d.md) section.
27-
As a note: convolutions can be extended to $n$-dimensions, but after seeing how it is extended to two dimensions, it should be possible for the reader to extend it to three dimensions and beyond if that is needed.
28-
In addition, we will be touching on a rather difficult but powerful topic with the [Convolutional Theorem](convolutional_theorem/convolutional_therem.md) section where convolutions can be computed by using [Fourier transforms](../Cooley_tukey/cooley_tukey.md)
29-
30-
Finally, all of the sections related to convolutions will be updated after the Fourier transform and fast Fourier transform chapters have been updated in the near future.
20+
We will then move on to the typical application of convolutions to images in the [Convolutions of images](2d/2d.md) section, where we will discuss two important filters: the Gaussian kernel and the Sobel operator.
21+
As a note: convolutions can be extended to $n$-dimensions, but after seeing how it is extended to two dimensions, it should be possible for the reader to extend it to three dimensions and beyond if that is needed, so we will not be covering that in great detail here unless is is useful for another algorithm.
22+
In addition, we will be touching on a rather difficult but powerful topic with the [Convolutional Theorem](convolutional_theorem/convolutional_therem.md) section where convolutions can be computed by using [Fourier transforms](../Cooley_tukey/cooley_tukey.md).
3123

3224
<script>
3325
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);

0 commit comments

Comments
 (0)