-
-
Notifications
You must be signed in to change notification settings - Fork 360
Add C# implemenation for 1D Convolutions #846
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
Merged
Trashtalk217
merged 19 commits into
algorithm-archivists:master
from
stormofice:1d-convolutions-csharp-implementation
Sep 14, 2021
Merged
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
745e874
Add C# implemenation for 1D Convolutions
stormofice 47f8541
Fix off by one error and julia line numbers
stormofice a19b1a3
Fix off by one error for linear convolutions
stormofice 5d15a92
Fix trailing zero
stormofice 7e14eb6
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 85b259b
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 78a179a
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 6dd35c7
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice d9a3ad5
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 3afd1b8
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 2b837d5
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 0ab262e
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 56f0ebe
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 306c81e
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 35f4c11
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 99d1bdb
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 52b16ca
Update contents/convolutions/code/csharp/1DConvolution.cs
stormofice 16e75cf
Add trailing new line
stormofice 2dec66f
Merge branch 'master' into 1d-convolutions-csharp-implementation
Trashtalk217 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
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 | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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); | ||
} | ||
|
||
// Julia like modulus function which handles negative values properly | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Assumes that y >= 0 | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
stormofice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
stormofice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
stormofice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var x = CreateSawtooth(200); | ||
var y = CreateSawtooth(200); | ||
|
||
// Normalization is not strictly necessary, but good practice | ||
stormofice marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Normalize(x); | ||
Normalize(y); | ||
|
||
// Full convolution, output will be the size of x + y - 1 | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var fullLinearOutput = ConvolveLinear(x, y, x.Length + y.Length - 1); | ||
// Simple boundaries | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var simpleLinearOutput = ConvolveLinear(x, y, x.Length); | ||
// Cyclic convolution | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var cyclicOutput = ConvolveCyclic(x, y); | ||
|
||
// Output convolutions to different files for plotting | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)); | ||
} | ||
} | ||
} | ||
Trashtalk217 marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.