-
-
Notifications
You must be signed in to change notification settings - Fork 360
Convolutions in 1d in rust #1007
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
Open
eengamer2007
wants to merge
17
commits into
algorithm-archivists:main
Choose a base branch
from
eengamer2007:convolutions_in_1d_in_rust
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
1d35153
wrote the 1d convolutions algorithm in rust
eengamer2007 fb21997
modified 1d convolutions page to use the rust code
eengamer2007 e8573e2
replace for-loop with iterator
eengamer2007 067c929
replaced reference to vec with a slice
eengamer2007 fc70dc0
replace write! with writeln!
eengamer2007 9316119
replace write! with writeln!
eengamer2007 9054525
replace write! with writeln!
eengamer2007 7fb386f
use iterator instead of indexing
eengamer2007 6c52d0a
add forgotten ;
eengamer2007 9151463
add missing ;
eengamer2007 ad0bd32
remove a magic number
eengamer2007 d1c591f
add name to contributors list
eengamer2007 fb90b49
reduce amount of type casts by turning modulus from Fn(isize, isize) …
eengamer2007 1e761b3
inline arguments at the writeln because clippy says so
eengamer2007 eb76ce5
fix line numbers
297f0ec
Merge branch 'main' into convolutions_in_1d_in_rust
eengamer2007 9062658
Merge branch 'main' into convolutions_in_1d_in_rust
eengamer2007 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,99 @@ | ||
use std::fs::File; | ||
use std::io::Write; | ||
use std::cmp::max; | ||
|
||
fn main() { | ||
let x = normalize(create_sawtooth(200)); | ||
let y = normalize(create_sawtooth(200)); | ||
|
||
let len_x = x.len(); | ||
let len_y = y.len(); | ||
|
||
let full_linear_output = convolve_linear(&x, &y, len_x + len_y - 1); | ||
let simple_linear_output = convolve_linear(&x, &y, len_x); | ||
let cyclic_output = convolve_cyclic(&x, &y); | ||
|
||
// Save the convolutions to plot them. | ||
// The way I do it is a little weird but it is to store the data the same way as the other programs | ||
let mut full_file = File::create("full_linear.dat").unwrap(); | ||
for i in full_linear_output { | ||
writeln!(full_file, "{}", i).unwrap(); | ||
} | ||
full_file.sync_all().unwrap(); | ||
|
||
let mut simple_file = File::create("simple_linear.dat").unwrap(); | ||
for i in simple_linear_output { | ||
writeln!(simple_file, "{}", i).unwrap(); | ||
} | ||
simple_file.sync_all().unwrap(); | ||
|
||
let mut cyclic_file = File::create("cyclic.dat").unwrap(); | ||
for i in cyclic_output { | ||
writeln!(cyclic_file, "{}", i).unwrap(); | ||
} | ||
cyclic_file.sync_all().unwrap(); | ||
} | ||
|
||
// Generates a sawtooth function with a given length. | ||
fn create_sawtooth(length: usize) -> Vec<f64> { | ||
let mut array: Vec<f64> = Vec::with_capacity(length); | ||
for i in 0..length { | ||
array.push((i+1) as f64 / 200.); | ||
} | ||
array | ||
} | ||
|
||
// Normalizes the given array. | ||
fn normalize(array: Vec<f64>) -> Vec<f64> { | ||
let norm = norm(&array); | ||
let mut output: Vec<f64> = Vec::with_capacity(array.len()); | ||
|
||
for value in array { | ||
output.push(value / norm); | ||
} | ||
|
||
output | ||
} | ||
|
||
// Calculates the norm of an array. | ||
fn norm(array: &[f64]) -> f64 { | ||
let sum = array.iter().map(|i| i * i).sum::<f64>() | ||
eengamer2007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sum.sqrt() | ||
} | ||
|
||
|
||
// Modulus function that handles negative values correctly. | ||
// Assumes that y >= 0. | ||
fn modulus(x: isize, y: isize) -> isize {((x%y) + y) % y} | ||
|
||
fn convolve_linear(signal: &Vec<f64>, filter: &Vec<f64>, output_size: usize) -> Vec<f64> { | ||
let mut output = Vec::with_capacity(output_size); | ||
|
||
for i in 0..(output_size as isize) { | ||
let mut sum: f64 = 0.; | ||
for j in max(0, i as isize - filter.len() as isize)..=i as isize { | ||
if j < signal.len() as isize && (i - j) < filter.len() as isize { | ||
sum += signal[j as usize] * filter[(i-j) as usize]; | ||
} | ||
} | ||
output.push(sum); | ||
} | ||
|
||
output | ||
} | ||
|
||
fn convolve_cyclic(signal: &Vec<f64>, filter: &Vec<f64>) -> Vec<f64> { | ||
let output_size = max(signal.len(), filter.len()) as isize; | ||
|
||
let mut output: Vec<f64> = Vec::with_capacity(output_size as usize); | ||
for i in 0..output_size { | ||
let mut sum: f64 = 0.; | ||
for j in 0..output_size { | ||
if modulus(i - j, output_size) < filter.len() as isize { | ||
sum += signal[modulus(j - 1, output_size) as usize] * filter[modulus(i - j, output_size) as usize]; | ||
} | ||
} | ||
output.push(sum); | ||
} | ||
output | ||
} |
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,12 @@ | ||
[package] | ||
name = "rust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
|
||
[[bin]] | ||
path = "./1d_convolution.rs" | ||
name = "main" |
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.