Skip to content

Instantly share code, notes, and snippets.

View going-digital's full-sized avatar

Going Digital (Peter Knight) going-digital

View GitHub Profile
@going-digital
going-digital / rbf-bunny.ipynb
Created August 30, 2021 17:56
RBF Bunny.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@going-digital
going-digital / calc.py
Last active July 20, 2021 19:46
Calc Lanczos polynomials
# -*- coding: utf-8 -*-
#%%
from sympy import symbols, cos, pi, factor_list
from sympy.core.numbers import One
from sympy.utilities.lambdify import lambdify
from math import sin, pi
import numpy as np
def chebyshev(func, interval, degree=7, precision=20):
@going-digital
going-digital / math.wasm
Last active December 26, 2023 07:44
WebAssembly native sin, log and exp functions optimised for code size.
;; Native implementations of sin, log and exp functions.
;; sintau: 41 bytes code, 34 bytes shared code, 24 bytes data
;; exp2: 25 bytes code, 34 bytes shared code, 20 bytes data
;; log2: 37 bytes code, 34 bytes shared code, 24 bytes data
;; Total 137 bytes code, 68 bytes data
;; Wasm-opt -Oz tries to optimise out $half by converting to f32.consts, but that actually takes up more space, not less.
;; Polynomial coefficients calculated by accompanying python script.
;; call $evalpoly parameters will need to be manually changed for different length polynomials.
@going-digital
going-digital / log2.wat
Last active June 2, 2021 11:55
log2(x) in WebAssembly
;; Calculate log2(x) = ln(x)/ln(2) natively in WebAssembly.
;; Compiles to 75 bytes of WebAssembly code
(module
(export "log2" (func $log2))
(func $log2 (param $x f32) (result f32)
(local $x2 f32)
(f32.add
;; Extract exponent of $x
(f32.convert_s/i32
(i32.sub
@going-digital
going-digital / rand.wat
Last active June 1, 2021 19:40
Pseudorandom numbers in WebAssembly
;; From https://github.com/vsariola/sointu
;; Compiles to 19 bytes of WebAssembly code
;;
;; Returns pseudorandom numbers in the range 0 to 1
(module
(global $randseed (mut i32) (i32.const 1))
(export "rand" (func $rand))
(func $rand (result f32)
(set_global $randseed (i32.mul (get_global $randseed) (i32.const 16007)))
(f32.div (f32.convert_s/i32 (get_global $randseed)) (f32.const -2147483648))
@going-digital
going-digital / sintau.wat
Last active June 2, 2021 12:15
sin(tau x) in WebAssembly
(module
;; Calculate sin(tau x) natively in WebAssembly.
;; Compiles to 57 bytes of WebAssembly code
;; Uses angle units of 0 to 1 for a circle (useful for synths). Pre-divide by 2*pi to use radians.
;; Using Bhaskara I's approximation
;; https://en.wikipedia.org/wiki/Bhaskara_I%27s_sine_approximation_formula
;; Accurate to about 0.2%
(export "sintau" (func $sintau))
(func $sintau (param $x f32) (result f32)
(local $s f32)
@going-digital
going-digital / exp2.wat
Last active June 1, 2021 15:39
exp2 in WebAssembly
;; Calculate exp2(x) = 2**x natively in WebAssembly.
;; Compiles to 49 bytes of WebAssembly code
(module
(func $exp2 (param $x f32) (result f32)
(local $xf f32)
;; Parse result as a float bitfield
(f32.reinterpret_i32
(i32.add
;; Compute mantissa of result
;; Compute polynomial to cover f(x) = 2**23 * ( 2^(x) - 1 ) where 0 <= x < 1
@going-digital
going-digital / config.txt
Last active January 28, 2018 19:39
Raspberry Pi Eleduino 1024x600 LCD
# Improved support for Eleduino 1024x600 LCD panel
hdmi_group=2
hdmi_mode=87
#Safer option at 50.4MHz pixel clock
#hdmi_timings=1024 0 48 32 240 600 0 3 32 13 0 0 0 60 0 50400000 6
#Safer option at data sheet max clock of 65.2
#hdmi_timings=1024 0 48 32 240 600 0 3 32 13 0 0 0 60 0 65200000 6
hdmi_timings=1024 0 48 32 216 600 0 2 14 0 0 0 0 60 0 65200000 6
max_usb_current=1
@going-digital
going-digital / fast_trig.frag
Created December 17, 2012 17:21
OpenGL ES Fast sin/cos functions. Double the speed of native on Raspberry Pi / Videocore IV
// Language is OpenGL ES, but gist.github.com doesn't know that language
//
// Approximation is based on multiply/accumulate, because thats the only
// thing Videocore can do quickly!
precision highp float;
float sinf(float x)
{
x*=0.159155;