Created
December 16, 2017 12:17
-
-
Save bencholmes/088a7a47ec61ea9a55e1d606db07c9eb to your computer and use it in GitHub Desktop.
A first order high/low-shelf filter expressed as a continuous time transfer function in MATLAB.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
% - C: Capacitor value | |
% - f: Frequencies at which to evaluate the transfer function | |
% Outputs: | |
% - transferFunction: complex valued continuous time transfer function of | |
% the filter. | |
% Parameterisation: Gain and cutoff frequency can be used instead of | |
% circuit values. | |
% Let G be gain and fc be cutoff frequency | |
% R2 = R1*G/(1-G) | |
% For high-shelf: C = 1/(2*pi*(R1 + R2)*fc) | |
% For low-shelf: C = 1/(2*pi*(R1*R2/(R1+R2))*fc); | |
% Find number of samples and pre-allocate output for speed. | |
Ns = length(f); | |
transferFunction = zeros(1,Ns); | |
% Select between high/low topology | |
if isHighShelf | |
rIncidence = [1 -1 0;0 1 -1]; | |
cIncidence = [0 0 1]; | |
vIncidence = [1 0 0]; | |
X = [0; 0; 0; 1]; | |
else | |
rIncidence = [1 -1;0 1]; | |
cIncidence = [1 -1]; | |
vIncidence = [1 0]; | |
X = [0; 0; 1]; | |
end | |
% Diagonalise the resistors into a 2x2 matrix | |
resistors = diag(1./R); | |
% Evaluate the transfer function at specified points | |
for nn=1:Ns | |
capacitors = 2*pi*sqrt(-1)*f(nn)*C; | |
S = [rIncidence.'*resistors*rIncidence+... | |
cIncidence.'*capacitors*cIncidence ... | |
vIncidence.';... | |
vIncidence 0]; | |
transferFunctions = S\X; | |
transferFunction(nn) = transferFunctions(2); | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment