Skip to content

Instantly share code, notes, and snippets.

View WillMatthews's full-sized avatar
🎓
I am *legally* a doctor. Hooray

William Matthews WillMatthews

🎓
I am *legally* a doctor. Hooray
View GitHub Profile
@WillMatthews
WillMatthews / party-cam.sh
Created July 30, 2024 13:31
Create a virtual webcam of a gif. In this case - it points at a party parrot.
#!/bin/bash
# Creates a fake webcam pointing at a party parrot gif.
# Requires you to have a a party parrot somewhere - just make sure you set the path correctly
PARROT=~/party-parrot.gif
# Reload v4l2loopback
sudo modprobe -r v4l2loopback
@WillMatthews
WillMatthews / ulid.m
Created April 6, 2024 02:23
ULID ported to MATLAB
% MATLAB port of https://github.com/alizain/ulid
% inspiration from https://github.com/mdipierro/ulid
% License MIT
function data = ulid(varargin)
if nargin == 0
data = generate();
elseif nargin == 1
%data = string(1,varargin(1));
function [] = sendemail(emailMessage)
setpref('Internet','SMTP_Server','smtp.gmail.com');
setpref('Internet','E_mail','your-email@gmail.com');
setpref('Internet','SMTP_Username','smtp-username-here');
setpref('Internet','SMTP_Password','p@$$w0rd-here');
props = java.lang.System.getProperties;
props.setProperty('mail.smtp.auth','true');
props.setProperty('mail.smtp.socketFactory.class', 'javax.net.ssl.SSLSocketFactory');
props.setProperty('mail.smtp.socketFactory.port','465');
@WillMatthews
WillMatthews / fourier-space-webcam.py
Last active March 17, 2024 23:17
Have a good stare into Fourier space, by doing a 2D FFT on your webcam feed. Quick and very dirty script.
import cv2
import numpy as np
def fft_frame(frame):
# b, g, r
im = [np.array([[col[i] for col in row] for row in frame]) for i in range(3)]
# shift to u,v space
F = [np.fft.fft2(img) for img in im]
# amplitude
@WillMatthews
WillMatthews / vis_psf.m
Created March 17, 2024 22:58
visusalise a point spread function arising from a pupil in matlab.
clear all; close all; clc;
xp = linspace(-2,2,1000);
yp = xp';
d = (xp.^2 + yp.^2).^0.5;
d2 = (abs(xp) + abs(yp));
% defines pupil
p = (d<0.1); % circle
@WillMatthews
WillMatthews / sine_fit.m
Created March 17, 2024 22:50
Fit a sine wave with zero DC component in MATLAB.
clear all; close all; clc;
% the below code fits a sine wave which has a zero DC component.
% the output is 'bestx' which is a 1x3 matrix with components
% [amplitude, omega, phase 0-2*pi]
% we also have a fitting function @fitfun which can be used y =
% fitfun(bestx,time_arry) to generate our fitted sine wave for some generic
% time array.
@WillMatthews
WillMatthews / serial-test.py
Created March 17, 2024 22:40
A brutally quick and dirty serial interaction tool for the command line, written in Python.
import serial # ONLY USE PYSERIAL!! pip install pyserial
import time
import sys
import glob
def serial_ports():
""" Lists serial port names
:raises EnvironmentError:
On unsupported or unknown platforms
@WillMatthews
WillMatthews / flips.py
Last active March 17, 2024 22:32
This is a simple Markov Chain solution to the following problem. Calculate the expectation of the score in a game where: Each turn you flip a coin. Heads increases your score by 1 and you may flip again, Tails ends the game and you are left with your score.
import matplotlib.pyplot as plt
flips = [0 for _ in range(100)]
flips[0] = 1
for i in range(1, len(flips)):
flips[i] = flips[i-1]/2
total_mass = sum(flips)
prob = [f/total_mass for f in flips]
@WillMatthews
WillMatthews / playground.m
Last active March 17, 2024 22:32
Oxford Engineering Science, 3rd Year Tutorials, Communications. B13 Q1 Tute Sheet 3 Communications Sandbox. This is for students to experiment with signals and their FFTs in a communications environment.
clear all; close all; clc;
% 3B13c Q1 Tutorial Exploration Sandbox
% Written by William Matthews 25/02/2021
% Q2, Q3, Q4, Q6 can be applied here,
% but is left as an exercise for the interested reader.
% For the interested reader, Q5 asks for the instantaneous frequency.
% This can be found with a signal processing technique,
% research 'Analytic signal' which is covered in 4th year's
@WillMatthews
WillMatthews / multi-detector-dot-prod.py
Created March 17, 2024 22:25
Calculate the radiant flux on a pyramid of detectors as a function of angle. Vary number of faces and pyramid angle and plot the irrad-theta curves for them.
import matplotlib.pyplot as plt
import numpy as np
import math as m
def Rx(theta):
return np.matrix([[ 1, 0 , 0 ],
[ 0, m.cos(theta),-m.sin(theta)],
[ 0, m.sin(theta), m.cos(theta)]])
def Ry(theta):