Skip to content

Instantly share code, notes, and snippets.

View MarcinKonowalczyk's full-sized avatar
😴
Sleeping

Marcin Konowalczyk MarcinKonowalczyk

😴
Sleeping
View GitHub Profile
@MarcinKonowalczyk
MarcinKonowalczyk / Fireproof_bomber.txt
Last active June 29, 2020 13:51 — forked from anonymous/readme.txt
Fireproof bomber
Play this game by going to http://www.puzzlescript.net/play.html?p=8f791761e397316a88b8ee5c5a931521
And have a play with the code at http://www.puzzlescript.net/editor.html?hack=8f791761e397316a88b8ee5c5a931521
Use the action button [space] to place bombs which will detonate in 3 moves. Destroy all the crates in the level to open the exit.
@MarcinKonowalczyk
MarcinKonowalczyk / pe031.py
Last active June 24, 2020 23:12
Verbose project Euler 31 solution (with recursive call to self and cache)
# Project Euler 31 solution
# https://projecteuler.net/problem=31
from functools import lru_cache
@lru_cache(maxsize=1000) # Cache the results (this can be commented out and the function just works slower)
def ways(target, coins):
# Early escape for simple cases
if target==0: return 1 # This will be the case when reduced_target is 0
@MarcinKonowalczyk
MarcinKonowalczyk / autocorr.m
Last active September 17, 2020 16:05
[Matlab] Calculate autocorrelation of y up to lag k
function acf = autocorr(y,k)
%% acf = autocorr(y,k)
% Calculate autocorrelation of y up to lag k
y = y(:)-mean(y); % Make sure y is column with 0 mean
N = numel(y);
vy = y'*y; % Unscaled variance of y
if nargin<2 || isempty(k)
k = N; % Default to N lags
@MarcinKonowalczyk
MarcinKonowalczyk / noc.m
Last active June 30, 2020 18:10
[Matlab] Name of Caller
function name = noc()
%% name = noc()
% Name of caller. Allows the funciton to get the name of the calling
% funciton. Returns 'base' if there is no caller, and 'command line' if
% caled from the command window.
stack = dbstack;
if numel(stack) == 1 % Only this function in stack
name = 'command line';
elseif numel(dbstack) == 2 % Called by only one other function
@MarcinKonowalczyk
MarcinKonowalczyk / asymmetric_gaussian_lineshape.m
Last active June 30, 2020 18:10
[Matlab] Model functions
function y = asymmetric_gaussian_lineshape(x,center,width_left,width_right,height)
%% y = asymmetric_gaussian_lineshape(x,center,width,height)
% Piecewise asymmetric Gaussian lineshape.
% 'width_left' and 'width_right' correspond to full width
% at half maximum of the respective halves. The derivative of this
% function is discontinuous at x=center.
l42 = 2.772588722239781; % 4.*log(2)
x = x-center;
y1 = exp( -l42.*(x./width_left).^2 ) .* height;
@MarcinKonowalczyk
MarcinKonowalczyk / pid.m
Created July 11, 2020 23:09
Basic PID controller demo
function control_variable = pid(plant_variable,setpoint,K)
%% control_variable = pid(plant_variable,setpoint,K)
% PID controller
persistent h; % History of the *input*
if nargin == 0 % Clear history
h = []; control_variable = NaN; return
end
%% Current error term
@MarcinKonowalczyk
MarcinKonowalczyk / paper.tex
Created July 20, 2020 13:59
[LaTeX] Minimal starting point for a revtex paper layout
\documentclass[aip,jcp,reprint]{revtex4-1}
\usepackage[utf8]{inputenc} % Unicode input encoding
\usepackage[T1]{fontenc} % Font encoding
\usepackage{lmodern} % Use an updated latim modern font (same as latex, just updated)
\usepackage[english]{babel} % English linebrakes, hyphenation etc.
\usepackage{graphicx} % Graphics
\graphicspath{{./figures/}}
@MarcinKonowalczyk
MarcinKonowalczyk / bake_in_colormap.m
Last active July 27, 2020 16:51
[Matab] Image operations
%% Bake-in colormap
Im = frame_stack(:,:,1); % Pick image
Im = Im-min(Im(:)); Im = Im./max(Im(:)); % Normalise
%Im = Im-prctile(Im(:),1); Im = Im./prctile(Im(:),99); % Normalise to percentiles
Im = round(Im*255); % 0-255
cm = viridis(256); % Colormap (ideally uniform)
Im = ind2rgb(Im,cm); % Bake
figure(1); image(Im) % Looks good
@MarcinKonowalczyk
MarcinKonowalczyk / viridis.m
Created September 16, 2020 10:53
[Matlab] Viridis colormap. Simple, without any external toolbox dependencies.
function map = viridis(N)
%% Viridis colormap
% Perceptually uniform colormap
% Based on matlab ports of the matplotlib colormaps.
if nargin<1 || isempty(N), N = size(get(gcf,'colormap'),1); end
raw = [0.267004,0.004874,0.329415; 0.268510,0.009605,0.335427; 0.269944,0.014625,0.341379; 0.271305,0.019942,0.347269; 0.272594,0.025563,0.353093; 0.273809,0.031497,0.358853; 0.274952,0.037752,0.364543; 0.276022,0.044167,0.370164; 0.277018,0.050344,0.375715; 0.277941,0.056324,0.381191; 0.278791,0.062145,0.386592; 0.279566,0.067836,0.391917; 0.280267,0.073417,0.397163; 0.280894,0.078907,0.402329; 0.281446,0.084320,0.407414; 0.281924,0.089666,0.412415; 0.282327,0.094955,0.417331; 0.282656,0.100196,0.422160; 0.282910,0.105393,0.426902; 0.283091,0.110553,0.431554; 0.283197,0.115680,0.436115; 0.283229,0.120777,0.440584; 0.283187,0.125848,0.444960; 0.283072,0.130895,0.449241; 0.282884,0.135920,0.453427; 0.282623,0.140926,0.457517; 0.282290,0.145912,0.461510; 0.281887,0.150881,0.465405; 0.281412,0.155834,0.469201; 0.280868,
@MarcinKonowalczyk
MarcinKonowalczyk / percentile.m
Last active September 16, 2020 16:08
[Matlab] p'th percentile of vector x
function y = percentile(x,p)
%% y = percentile(x,p)
% Get p'th percentiles of vector x
% x must not have any NaNs or Infs
x = x(:); % Make sure x is a collumn
N = numel(x);
x = sort(x,1);
%% Fast median