Skip to content

Instantly share code, notes, and snippets.

View 0xm00n's full-sized avatar
👁️

Royce 0xm00n

👁️
View GitHub Profile
@0xm00n
0xm00n / mlp.rs
Created October 5, 2023 05:48
Multi-layer perceptron with an input layer, 1 hidden layer, and an output layer written from scratch in rust. The sigmoid activation function is used for both the hidden and output layers.
use std::f64;
// sigmoid activation function
fn sigmoid(x: f64) -> f64 {
1.0 / (1.0 + (-x).exp())
}
// derivative of the sigmoid function
fn sigmoid_prime(x: f64) -> f64 {
sigmoid(x) * (1.0 - sigmoid(x))
@0xm00n
0xm00n / fft.ml
Created October 4, 2023 03:17
Simple OCaml implementation of Fast Fourier Transform.
type complex = {re: float; im: float}
let complex_add a b = {re = a.re +. b.re; im = a.im +. b.im}
let complex_sub a b = {re = a.re -. b.re; im = a.im -. b.im}
let complex_mul a b = {re = a.re *. b.re -. a.im *. b.im; im = a.re *. b.im +. a.im *. b.re}
let complex_exp theta = {re = cos theta; im = sin theta}
let rec fft x =
let n = Array.length x in
if n <= 1 then x
@0xm00n
0xm00n / idft.ml
Created October 4, 2023 03:16
Simple OCaml implementation of Inverse Discrete Fourier Transform.
type complex = {re: float; im: float}
let complex_add a b = {re = a.re +. b.re; im = a.im +. b.im}
let complex_mul a b = {re = a.re *. b.re -. a.im *. b.im; im = a.re *. b.im +. a.im *. b.re}
let complex_exp theta = {re = cos theta; im = sin theta}
let idft x =
let n_total = Array.length x in
let x_n n =
let sum = ref {re = 0.0; im = 0.0} in
@0xm00n
0xm00n / dft.ml
Last active October 4, 2023 03:10
Simple OCaml implementation of the Discrete Fourier Transform for complex-valued signals.
(* complex num ops*)
type complex = {re: float; im: float}
let complex_add a b = {re = a.re +. b.re; im = a.im +. b.im}
let complex_sub a b = {re = a.re -. b.re; im = a.im -. b.im}
let complex_mul a b = {re = a.re *. b.re -. a.im *. b.im; im = a.re *. b.im +. a.im *. b.re}
(* exponential of a complex number given angle in radians*)
let complex_exp theta = {re = cos theta; im = sin theta}
(* discrete fourier transform*)