Skip to content

Instantly share code, notes, and snippets.

View astoeckel's full-sized avatar

Andreas Stöckel astoeckel

View GitHub Profile
@astoeckel
astoeckel / C++
Created January 17, 2024 00:01
"Ode an die Freude" in C++ for my Hotplate AVR Firmware...
static const Beeper::Tone ode_an_die_freude_tone_seq[60] = {
{.note = MusicalNotes::B4, .len = 200},
{.note = MusicalNotes::Pause, .len = 50},
{.note = MusicalNotes::B4, .len = 200},
{.note = MusicalNotes::Pause, .len = 50},
{.note = MusicalNotes::C5, .len = 200},
{.note = MusicalNotes::Pause, .len = 50},
@astoeckel
astoeckel / log2.py
Created January 13, 2024 00:13
Integer log2 with 14-bit fractional part in Python
def polyval_fixed(coeffs, x, scale):
res = 0
for c in coeffs:
res = (((x * res) >> scale) + c)
return res
def log2_approx(x):
if x < 1:
return 0
@astoeckel
astoeckel / log2.cpp
Last active January 13, 2024 00:12
Integer log2 with 14-bit fractional part in C/C++
#include <stdint.h>
int32_t log2_f14(int32_t x) {
// We don't support zero or negative values, just return zero in that case
if (x <= 0) {
return 0;
}
// Compute the integer logarithm
int32_t log2_int = 0;
@astoeckel
astoeckel / sandbox.py
Last active December 12, 2023 13:55
Python sandbox
#!/usr/bin/env python3
"""
This module provides an `eval_in_sandbox` function that, on Linux systems,
executes arbitrary Python code in a secure and lightweight sandbox with
memory, I/O, and time limits.
----
Copyright (c) 2023 Andreas Stöckel
@astoeckel
astoeckel / env_guard.py
Created August 11, 2021 16:25
Environment Guard
import os
class EnvGuard:
"""
Class used to temporarily set some environment variables to a certain value.
In particular, this is used to set the variable OMP_NUM_THREADS to "1" for
each of the subprocesses using cvxopt.
"""
def __init__(self, env={}):
self.env = env
@astoeckel
astoeckel / low_firing_rate_dynamics_sweep.py
Created July 23, 2021 21:01
Compute neuron transfer function
def compute_transfer_function(us, xs, dt=dt, smoothing=100.0):
Us = np.fft.fftshift(np.fft.fft(us))
Xs = np.fft.fftshift(np.fft.fft(xs))
fs = np.fft.fftshift(np.fft.fftfreq(len(us), dt))
wnd = np.exp(-np.square(fs) / np.square(smoothing))
wnd /= np.sum(wnd) * dt
UXs = np.fft.fftshift(np.fft.fft(Us * np.conj(Xs)))
@astoeckel
astoeckel / fuzzy.py
Created February 27, 2021 02:16
Fuzzy Jaccard similarity based search
def fuzzy_find(term, lst, threshold=0.75, shingle_len=3):
# Create a set of n-grams of length shingle_len
def shingle(s):
shingles = []
for t in re.split(' ', s):
for i in range(max(1, len(t) - shingle_len)):
shingles.append(t[i:i+shingle_len])
return set(shingles)
# Computes the Jaccard similarity between two sets
@astoeckel
astoeckel / zorder.py
Created January 29, 2021 01:08
Python 2D z-order curve
def zorder(N):
if N == 0:
return np.zeros((0, 0), dtype=np.int)
elif N == 1:
return np.zeros((1, 2), dtype=np.int)
else:
pnts = zorder(N - 1)
l = pnts.shape[0]
offs = 2 ** (N - 2)
res = np.zeros((4 * l, 2), dtype=np.int)
@astoeckel
astoeckel / nbdkit.service
Last active November 16, 2020 07:44
nbdkit systemd socket and service file
# nbdkit.service
[Unit]
Description=nbdkit Service
After=nbdkit.socket
Requires=nbdkit.socket
[Service]
Environment="LD_LIBRARY_PATH=/opt/nbdkit/lib64"
Environment="TMPDIR=/var/lib/nbdkit/cache/"
@astoeckel
astoeckel / compile_and_install_nbdkit_centos_7.sh
Created November 15, 2020 23:22
Compiling and installing nbdkit on CentOS 7
#!/usr/bin/bash
# Downloads NBDKit and builds a minimal version. This minimal version only
# installs basic plugins and filters, and the SSH plugin.
# For compilation to succeed, you'll need to install
# yum install gcc gcc-c++ libssh-devel openssl-devel autoconf automake libtool
# Also, best create a user nbdkit
# adduser --system --user-group --shell /sbin/nologin --home-dir /opt/nbdkit --create-home nbdkit