Skip to content

Instantly share code, notes, and snippets.

View bencholmes's full-sized avatar

Ben Holmes bencholmes

View GitHub Profile
@bencholmes
bencholmes / count_colour_pages.sh
Created January 21, 2019 16:54
A bash script to count the number of pages in a PDF and how many are colour/black&white
FILENAME="$1"
# https://stackoverflow.com/questions/1672580/get-number-of-pages-in-a-pdf-using-a-cmd-batch-file
# Uses pdfinfo to find general information, focusing on the number of pages from which only the number is extracted.
NPAGES=$(pdfinfo "$FILENAME" | grep Pages | sed 's/[^0-9]*//')
# https://root42.blogspot.com/2012/10/counting-color-pages-in-pdf-files.html
# Uses ghostscript to measure any deviation from black in the ink colour used on pages.
GHOSTOUT=$(gs -o - -sDEVICE=inkcov $FILENAME | grep -v "^ 0.00000 0.00000 0.00000" | grep "^ " | wc -l)
@bencholmes
bencholmes / LowPassFilter.m
Created January 1, 2019 14:09
A MATLAB script that derives a computable low pass filter from a circuit prototype. Derivation and discretisation performed with symbolic toolbox.
%%% Low Pass Circuit
% A computable RC circuit model using a recurrence relation derived from
% MNA, through to a transfer function which is then discretised using the
% symbolic math toolbox.
% Author: Ben Holmes
% Date: 2019/01/01
% License: GPL v3
clear;
@bencholmes
bencholmes / crop2ff.html
Last active September 20, 2018 16:25
A javascript calculator to compare full frame to crop sensor equivalents for focal length, aperture, and ISO.
Calculate full frame to cropped equivalent. First order approximations only. Designed as described by <a href="https://northrup.photo/gear-basics/camera-body-features/sensor-size-crop-factor/">Chelsea and Tony Northrup</a>.
<form name="cropCalculator" action="" method="get">
<table>
<tr>
<td>Crop factor:</td>
<td><input type="text" name="cropFactor" value="1.53"></td>
</tr>
<tr>
<td>Direction:</td>
@bencholmes
bencholmes / forceFFTSymmetry.m
Created August 30, 2018 13:30
A MATLAB function to force symmetry on an FFT, thus producing a real signal after an IFFT.
function X = forceFFTSymmetry(X)
% forceFFTSymmetry A function to force conjugate symmetry on an FFT such that when an
% IFFT is performed the result is a real signal.
% The function has been written to replace MATLAB's ifft(X,'symmetric'), as this function
% is not compatible with MATLAB Coder.
% Licensed under Creative Commons Zero (CC0) so use freely.
XStartFlipped = fliplr(X(2:floor(end/2)));
@bencholmes
bencholmes / pdf-colour2grayscale.sh
Created July 6, 2018 13:14
Bash script to convert a PDF from colour to gray scale.
fileName=$(basename $1)
directory=$(dirname $1)
gs \
-sOutputFile="$directory/${fileName%.*}-grayscale.pdf" \
-sDEVICE=pdfwrite \
-sColorConversionStrategy=Gray \
-dProcessColorModel=/DeviceGray \
-dCompatibilityLevel=1.4 \
-dAutoRotatePages=/None \
-dNOPAUSE \
@bencholmes
bencholmes / fftdBV.m
Created May 20, 2018 15:59
Returns the FFT of a voltage signal (in volts) with the amplitude response units dBV.
function [Xmag, Xarg] = fftdBV(x)
%FFTDBV Returns the FFT of a voltage signal (in volts) with the amplitude response units dBV.
% Number of samples
N = length(x);
% Scaling factor is sqrt(2) except for at DC when it is 1.
k = [1, ones(1,N-1)*sqrt(2)];
% Calculate the scaled DFT.
@bencholmes
bencholmes / cascadeSSM.m
Last active June 17, 2019 02:40
A function to cascade linear state-space matrices for the purpose of cascading filters etc.
function [Ac,Bc,Cc,Dc] = cascadeSSM(A,B,C,D)
% CASCADESSM A function to cascade linear state-space matrices for the
% purpose of cascading filters etc.
%
% Author: Ben Holmes
% Date: 2018/05/20
% License: GPL V3
%
% This function is derived from the answer
% https://math.stackexchange.com/questions/2201067/cascade-of-state-space-models-for-linear-systems
@bencholmes
bencholmes / engFormat.m
Created April 4, 2018 12:58
A MATLAB function to convert from component value to formatted string with engineering suffix.
function string = engFormat(value, type)
%ENGFORMAT a function to take a passive electronic component value and
% return a string with the properly formatted suffix.
aVal = abs(value);
nn = 0;
if strcmp(type,'R')
type = char(937);
end
@bencholmes
bencholmes / circuitShelf.m
Created December 16, 2017 12:17
A first order high/low-shelf filter expressed as a continuous time transfer function in MATLAB.
function [transferFunction] = circuitShelf(isHighShelf,R,C,f)
%%% CircuitShelf: A circuit informed shelf filter written as a continuous
%%% time transfer function.
% Author: Ben Holmes
% Date: 2017/12/16
% License: GPL v3
% Arguments:
% - isHighShelf: Boolean variable to switch between highpass and lowpass
% - R: 2x1 vector of resistor values
@bencholmes
bencholmes / bjt.m
Created November 28, 2017 14:32
A general BJT model in MATLAB
function [I, J] = bjt(v, Is, N, Bf, Br)
% BJT: a general model for the currents through a BJT and their
% derivatives.
% Ben Holmes, 2017/11/28, GPL 3.0
% Arguments:
% - v: [vce; vbe], collector-emitter voltage and base-emitter voltage.
% - Is: saturation current
% - N: ideality factor
% - Bf: forward current gain
% - Br: reverse current gain