Skip to content

Instantly share code, notes, and snippets.

View astoeckel's full-sized avatar

Andreas Stöckel astoeckel

View GitHub Profile
@astoeckel
astoeckel / pdf_to_html_presentation.sh
Last active February 26, 2024 01:36
Script for the generation of an HTML presentation from a (LaTeX beamer) PDF
#!/bin/bash -e
# Fetch the number of pages in the pdf
NP=`pdfinfo "$1" | grep "Pages:" | sed 's/Pages:[ \t]*\(0-9\)*[ \t]*/\1/'`
# Fetch the title
TITLE=`pdfinfo "$1" | grep "Title:" | sed 's/Title:[ \t]*\(.*\)/\1/'`
# Create a target folder
NAME=`basename "$1" .pdf`
@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 / stream_local_audio.sh
Last active October 12, 2021 06:05
Stream audio from pulse audio as opus stream via UDP/RTP
#!/bin/sh
pacat \
--device=alsa_output.pci-0000_00_1b.0.analog-stereo.monitor \
--rate=48000 \
--record | \
opusenc \
--expect-loss=25 \
--max-delay=0 \
--framesize=2.5 \
@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)