Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add C# implemenation for 1D Convolutions #846

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
745e874
Add C# implemenation for 1D Convolutions
stormofice Aug 25, 2021
47f8541
Fix off by one error and julia line numbers
stormofice Sep 3, 2021
a19b1a3
Fix off by one error for linear convolutions
stormofice Sep 5, 2021
5d15a92
Fix trailing zero
stormofice Sep 6, 2021
7e14eb6
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
85b259b
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
78a179a
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
6dd35c7
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
d9a3ad5
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
3afd1b8
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
2b837d5
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
0ab262e
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
56f0ebe
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
306c81e
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
35f4c11
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
99d1bdb
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
52b16ca
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice Sep 14, 2021
16e75cf
Add trailing new line
stormofice Sep 14, 2021
2dec66f
Merge branch 'master' into 1d-convolutions-csharp-implementation
Trashtalk217 Sep 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions contents/convolutions/1d/1d.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ With this in mind, we can almost directly transcribe the discrete equation into

{% method %}
{% sample lang="jl" %}
[import:27-46, lang:"julia"](code/julia/1d_convolution.jl)
[import:27-46, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import:63-84, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

The easiest way to reason about this code is to read it as you might read a textbook.
Expand Down Expand Up @@ -184,30 +186,38 @@ Here it is again for clarity:

{% method %}
{% sample lang="jl" %}
[import:27-46, lang:"julia"](code/julia/1d_convolution.jl)
[import:27-46, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import:63-84, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

Here, the main difference between the bounded and unbounded versions is that the output array size is smaller in the bounded case.
For an unbounded convolution, the function would be called with a the output array size specified to be the size of both signals put together:

{% method %}
{% sample lang="jl" %}
[import:58-59, lang:"julia"](code/julia/1d_convolution.jl)
[import:58-59, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import:96-97, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

On the other hand, the bounded call would set the output array size to simply be the length of the signal

{% method %}
{% sample lang="jl" %}
[import:61-62, lang:"julia"](code/julia/1d_convolution.jl)
[import:61-62, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import:98-99, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

Finally, as we mentioned before, it is possible to center bounded convolutions by changing the location where we calculate the each point along the filter.
This can be done by modifying the following line:

{% method %}
{% sample lang="jl" %}
[import:35-35, lang:"julia"](code/julia/1d_convolution.jl)
[import:35-35, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import:71-71, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

Here, `j` counts from `i-length(filter)` to `i`.
Expand Down Expand Up @@ -239,7 +249,9 @@ In code, this typically amounts to using some form of modulus operation, as show

{% method %}
{% sample lang="jl" %}
[import:4-25, lang:"julia"](code/julia/1d_convolution.jl)
[import:4-25, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import:38-61, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

This is essentially the same as before, except for the modulus operations, which allow us to work on a periodic domain.
Expand All @@ -254,7 +266,9 @@ For the code associated with this chapter, we have used the convolution to gener

{% method %}
{% sample lang="jl" %}
[import, lang:"julia"](code/julia/1d_convolution.jl)
[import, lang:"julia"](../code/julia/1d_convolution.jl)
{% sample lang="cs" %}
[import, lang:"csharp"](../code/csharp/1DConvolution.cs)
{% endmethod %}

At a test case, we have chosen to use two sawtooth functions, which should produce the following images:
Expand Down
110 changes: 110 additions & 0 deletions contents/convolutions/code/csharp/1DConvolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.IO;

namespace Convolution1D
{
public class Convolution1D
{
// Creates a sawtooth function with the given length.
static double[] CreateSawtooth(int length)
{
var array = new double[length];
for (var i = 0; i < length; i++)
array[i] = (i + 1) / 200f;
return array;
}

// Normalizes the given array.
static void Normalize(double[] array)
{
var norm = Norm(array);
for (var i = 0; i < array.Length; i++)
array[i] /= norm;
}

// Calculates the norm of the array.
static double Norm(double[] array)
{
var sum = 0.0;
for (var i = 0; i < array.Length; i++)
sum += Math.Pow(array[i], 2);
return Math.Sqrt(sum);
}

// Modulus function which handles negative values properly.
// Assumes that y >= 0.
static int Mod(int x, int y) => ((x % y) + y) % y;

static double[] ConvolveCyclic(double[] signal, double[] filter)
{
var outputSize = Math.Max(signal.Length, filter.Length);

// Convolutional output.
var output = new double[outputSize];
var sum = 0.0;

for (var i = 0; i < outputSize; i++)
{
for (var j = 0; j < outputSize; j++)
{
if (Mod(i - j, outputSize) < filter.Length)
{
sum += signal[Mod(j - 1, outputSize)] * filter[Mod(i - j, outputSize)];
}
}

output[i] = sum;
sum = 0.0;
}

return output;
}

static double[] ConvolveLinear(double[] signal, double[] filter, int outputSize)
{
// Convolutional output.
var output = new double[outputSize];
var sum = 0.0;

for (var i = 0; i < outputSize; i++)
{
for (var j = Math.Max(0, i - filter.Length); j <= i; j++)
{
if (j < signal.Length && (i - j) < filter.Length)
{
sum += signal[j] * filter[i - j];
}
}

output[i] = sum;
sum = 0.0;
}

return output;
}

static void Main()
{
// Create sawtooth functions for x and y.
var x = CreateSawtooth(200);
var y = CreateSawtooth(200);

// Normalization is not strictly necessary, but good practice.
Normalize(x);
Normalize(y);

// Full convolution, output will be the size of x + y - 1.
var fullLinearOutput = ConvolveLinear(x, y, x.Length + y.Length - 1);
// Simple boundaries.
var simpleLinearOutput = ConvolveLinear(x, y, x.Length);
// Cyclic convolution.
var cyclicOutput = ConvolveCyclic(x, y);

// Output convolutions to different files for plotting.
File.WriteAllText("full_linear.dat", String.Join(Environment.NewLine, fullLinearOutput));
File.WriteAllText("simple_linear.dat", String.Join(Environment.NewLine, simpleLinearOutput));
File.WriteAllText("cyclic.dat", String.Join(Environment.NewLine, cyclicOutput));
}
}
}