API¶
Lcapy is a Python library for linear circuit analysis. It uses SymPy for symbolic mathematics.
Lcapy can analyse circuits described with netlists using modified nodal analysis. See lcapy.netlist
Alternatively, Lcapy can analyse networks and circuits formed by combining one, two, and three port networks.
Copyright 2014, 2015, 2016 Michael Hayes, UCECE

lcapy.
tf
(numer, denom=1, var=None)¶ Create a transfer function from lists of the coefficient for the numerator and denominator.

lcapy.
sympify
(expr, evaluate=True, **assumptions)¶ Create a sympy expression.

lcapy.
zp2tf
(zeros, poles, K=1, var=None)¶ Create a transfer function from lists of zeros and poles, and from a constant gain.

class
lcapy.
Expr
(arg, **assumptions)¶ Decorator class for sympy classes derived from sympy.Expr

D
¶ Return denominator of rational function.

N
¶ Return numerator of rational function.

abs
¶ Return magnitude

angle
¶ Return phase

conjugate
¶ Return complex conjugate.

dB
¶ Return magnitude in dB.

denominator
¶ Return denominator of rational function.

evaluate
(arg=None)¶ Evaluate expression at arg. arg may be a scalar, or a vector. The result is of type float or complex.
There can be no symbols in the expression except for the variable.

has
(subexpr)¶ Test whether the subexpression is contained.

imag
¶ Return imaginary part.

latex
()¶ Make latex string.

latexans
(name)¶ Print latex string with LHS name.

limit
(var, value, dir='+')¶ Determine limit of expression(var) at var = value.

magnitude
¶ Return magnitude

numerator
¶ Return numerator of rational function.

omega
¶ Return angular frequency.

parallel
(x)¶ Parallel combination

phase
¶ Return phase in radians.

phase_degrees
¶ Return phase in degrees.

pprint
()¶ Pretty print

pprintans
(name)¶ Pretty print string with LHS name.

pretty
()¶ Make pretty string.

prettyans
(name)¶ Make pretty string with LHS name.

rationalize_denominator
()¶ Rationalize denominator by multiplying numerator and denominator by complex conjugate of denominator.

real
¶ Return real part.

real_imag
¶ Rewrite as x + j * y

subs
(*args, **kwargs)¶ Substitute variables in expression, see sympy.subs for usage.

val
¶ Return floating point value of expression if it can be evaluated, otherwise the expression.


class
lcapy.
sExpr
(val, **assumptions)¶ sdomain expression or symbol.

angular_frequency_response
(wvector=None)¶ Convert to angular frequency domain and evaluate response if angular frequency vector specified.

delay
(T)¶ Apply delay of T seconds by multiplying by exp(s T).

differentiate
()¶ Differentiate (multiply by s).

final_value
()¶ Determine value at t = oo.

fourier
()¶ Convert to Fourier domain.

frequency_response
(fvector=None)¶ Convert to frequency domain and evaluate response if frequency vector specified.

impulse_response
(tvector=None)¶ Evaluate transient (impulse) response.

initial_value
()¶ Determine value at t = 0.

integrate
()¶ Integrate (divide by s).

inverse_laplace
(**assumptions)¶ Attempt inverse Laplace transform.
If causal=True the response is zero for t < 0 and the result is multiplied by Heaviside(t) If ac=True or dc=True the result is extrapolated for t < 0. Otherwise the result is only known for t >= 0.

jomega
¶ Return expression with s = j omega.

laplace
()¶ Convert to sdomain.

plot
(t=None, **kwargs)¶ Plot polezero map.

response
(x, t)¶ Evaluate response to input signal x at times t.

step_response
(tvector=None)¶ Evaluate step response.

time
(**assumptions)¶ Convert to time domain.

transient_response
(tvector=None)¶ Evaluate transient (impulse) response.


class
lcapy.
tExpr
(val, **assumptions)¶ tdomain expression or symbol.

fourier
()¶ Attempt Fourier transform.

laplace
()¶ Determine oneside Laplace transform with 0 as the lower limit.


class
lcapy.
fExpr
(val, **assumptions)¶ Fourier domain expression or symbol.

inverse_fourier
()¶ Attempt inverse Fourier transform.

plot
(fvector=None, **kwargs)¶ Plot frequency response at values specified by fvector.
There are many plotting options, see matplotlib.pyplot.plot.
 For example:
 V.plot(fvector, log_scale=True) V.real.plot(fvector, color=’black’) V.phase.plot(fvector, color=’black’, linestyle=’–’)
By default complex data is plotted as separate plots of magnitude (dB) and phase.


class
lcapy.
cExpr
(val, **assumptions)¶ Constant real expression or symbol.
If symbols in the expression are known to be negative, use cExpr(expr, positive=False)

laplace
()¶ Convert to Laplace domain representation.


class
lcapy.
omegaExpr
(val, **assumptions)¶ Fourier domain expression or symbol (angular frequency).

inverse_fourier
()¶ Attempt inverse Fourier transform.

plot
(wvector=None, **kwargs)¶ Plot angular frequency response at values specified by wvector.
There are many plotting options, see matplotlib.pyplot.plot.
 For example:
 V.plot(fvector, log_scale=True) V.real.plot(fvector, color=’black’) V.phase.plot(fvector, color=’black’, linestyle=’–’)
By default complex data is plotted as separate plots of magnitude (dB) and phase.

time
()¶ Alias for inverse_fourier.


lcapy.
H
(expr)¶ Heaviside’s unit step.

lcapy.
Heaviside
(expr)¶ Heaviside’s unit step.

lcapy.
DiracDelta
(*args)¶ Dirac delta (impulse).

lcapy.
u
(expr)¶ Heaviside’s unit step.

lcapy.
delta
(expr, *args)¶ Dirac delta (impulse).

class
lcapy.
Hs
(val, **assumptions)¶ sdomain ratio

class
lcapy.
Is
(val, **assumptions)¶ sdomain current (units A s / radian).

class
lcapy.
Vs
(val, **assumptions)¶ sdomain voltage (units V s / radian).

class
lcapy.
Ys
(val, **assumptions)¶ sdomain admittance value.

class
lcapy.
Zs
(val, **assumptions)¶ sdomain impedance value.

class
lcapy.
Ht
(val, **assumptions)¶ impulse response

class
lcapy.
It
(val, **assumptions)¶ tdomain current (units A).

class
lcapy.
Vt
(val, **assumptions)¶ tdomain voltage (units V).

class
lcapy.
Yt
(val, **assumptions)¶ tdomain ‘admittance’ value.

class
lcapy.
Zt
(val, **assumptions)¶ tdomain ‘impedance’ value.

class
lcapy.
Hf
(val, **assumptions)¶ fdomain transfer function response.

class
lcapy.
If
(val, **assumptions)¶ fdomain current (units A/Hz).

class
lcapy.
Vf
(val, **assumptions)¶ fdomain voltage (units V/Hz).

class
lcapy.
Yf
(val, **assumptions)¶ fdomain admittance

class
lcapy.
Zf
(val, **assumptions)¶ fdomain impedance

class
lcapy.
In
(val, **assumptions)¶ fdomain noise current (units A/rtHz).

class
lcapy.
Vn
(val, **assumptions)¶ fdomain noise voltage (units V/rtHz).

class
lcapy.
Homega
(val, **assumptions)¶ omegadomain transfer function response.

class
lcapy.
Iomega
(val, **assumptions)¶ omegadomain current (units A/rad/s).

class
lcapy.
Vomega
(val, **assumptions)¶ omegadomain voltage (units V/rad/s).

class
lcapy.
Yomega
(val, **assumptions)¶ omegadomain admittance.

class
lcapy.
Zomega
(val, **assumptions)¶ omegadomain impedance.

class
lcapy.
V
(Vval)¶ Voltage source. If the expression contains s treat as sdomain voltage otherwise time domain. A constant V is considered DC with an sdomain voltage V / s.

class
lcapy.
I
(Ival)¶ Current source. If the expression contains s treat as sdomain current otherwise time domain. A constant I is considered DC with an sdomain current I / s.

class
lcapy.
v
(vval)¶ Arbitrary tdomain voltage source

class
lcapy.
i
(ival)¶ Arbitrary tdomain current source

class
lcapy.
R
(Rval)¶ Resistor

class
lcapy.
L
(Lval, i0=None)¶ Inductor
Inductance Lval, initial current i0

class
lcapy.
C
(Cval, v0=None)¶ Capacitor
Capacitance Cval, initial voltage v0

class
lcapy.
G
(Gval)¶ Conductance

class
lcapy.
Y
(Yval)¶ General admittance.

class
lcapy.
Z
(Zval)¶ General impedance.

class
lcapy.
Vdc
(v)¶ DC voltage source (note a DC voltage source of voltage V has an s domain voltage of V / s).

class
lcapy.
Vstep
(v)¶ Step voltage source (s domain voltage of v / s).

class
lcapy.
Idc
(i)¶ DC current source (note a DC current source of current i has an s domain current of i / s).

class
lcapy.
Istep
(i)¶ Step current source (s domain current of i / s).

class
lcapy.
Vac
(V, phi=0)¶ AC voltage source.

class
lcapy.
sV
(Vval)¶ Arbitrary sdomain voltage source

class
lcapy.
sI
(Ival)¶ Arbitrary sdomain current source

class
lcapy.
Iac
(I, phi=0)¶ AC current source.

class
lcapy.
Vnoise
(V)¶ Noise voltage source.

class
lcapy.
Inoise
(I)¶ Noise current source.

class
lcapy.
Par
(*args)¶ Parallel class

class
lcapy.
Ser
(*args)¶ Series class

class
lcapy.
Xtal
(C0, R1, L1, C1)¶ Crystal
This is modelled as a series R, L, C circuit in parallel with C0 (a Butterworth van Dyke model). Note, harmonic resonances are not modelled.

class
lcapy.
FerriteBead
(Rs, Rp, Cp, Lp)¶ Ferrite bead (lossy inductor)
This is modelled as a series resistor (Rs) connected to a parallel R, L, C network (Rp, Lp, Cp).

class
lcapy.
Chain
(*args)¶ Connect twoport networks in a chain (aka cascade)

class
lcapy.
Par2
(*args)¶ Connect twoport networks in parallel

class
lcapy.
Ser2
(*args)¶ Connect twoport networks in series (note this is unusual and can break the port condition)

class
lcapy.
Hybrid2
(*args)¶ Connect twoport networks in hybrid configuration (inputs in series, outputs in parallel)

class
lcapy.
InverseHybrid2
(*args)¶ Connect twoport networks in inverse hybrid configuration (outputs in series, inputs in parallel)

class
lcapy.
Series
(OP)¶ Twoport comprising a single oneport in series configuration
++ + OP + ++ 
Note, this has a singular Y matrix.

class
lcapy.
Shunt
(OP)¶ Twoport comprising a single oneport in shunt configuration
+  +++   OP    +++  +
Note, this has a singular Z matrix.

class
lcapy.
IdealTransformer
(alpha=1)¶ Ideal transformer voltage gain alpha, current gain 1 / alpha

class
lcapy.
IdealGyrator
(R=1)¶ Ideal gyrator with gyration resistance R.
A gyrator converts a voltage to current and a current to voltage. Cascaded gyrators act like a transformer

class
lcapy.
VoltageFollower
¶ Voltage follower

class
lcapy.
VoltageAmplifier
(Av=1, Af=0, Yin=0, Zout=0)¶ Voltage amplifier

class
lcapy.
IdealVoltageAmplifier
(Av=1)¶ Ideal voltage amplifier

class
lcapy.
IdealDelay
(delay=0)¶ Ideal buffered delay

class
lcapy.
IdealVoltageDifferentiator
(Av=1)¶ Voltage differentiator

class
lcapy.
IdealVoltageIntegrator
(Av=1)¶ Ideal voltage integrator

class
lcapy.
CurrentFollower
¶ Current follower

class
lcapy.
IdealCurrentAmplifier
(Ai=1)¶ Ideal Current amplifier

class
lcapy.
IdealCurrentDifferentiator
(Ai=1)¶ Ideal Current differentiator

class
lcapy.
IdealCurrentIntegrator
(Ai=1)¶ Ideal Current integrator

class
lcapy.
OpampInverter
(R1, R2)¶ Opamp inverter

class
lcapy.
OpampIntegrator
(R1, C1)¶ Inverting opamp integrator

class
lcapy.
OpampDifferentiator
(R1, C1)¶ Inverting opamp differentiator

class
lcapy.
TSection
(OP1, OP2, OP3)¶ T (Y) section
++ ++ + OP1 +++ OP3 + ++  ++ +++   OP2   +++  + The Z matrix for a resistive T section is [ R1 + R2, R2 ] [ R2, R2 + R3]

class
lcapy.
TwinTSection
(OP1a, OP2a, OP3a, OP1b, OP2b, OP3b)¶ Twin T section
++ ++ ++ OP1a +++ OP3a ++  ++  ++   +++       OP2a       +++      v   ++ ++  ++ OP1b +++ OP3b ++ ++  ++ +++   OP2b   +++  +

class
lcapy.
BridgedTSection
(OP1, OP2, OP3, OP4)¶ Bridged T section
++ ++ OP4 ++  ++     ++ ++  ++ OP1b +++ OP3b ++ ++  ++ +++   OP2b   +++  +

class
lcapy.
PiSection
(OP1, OP2, OP3)¶ Pi (delta) section
++ ++ OP2 ++  ++  +++ +++     OP1 OP3     +++ +++   ++

class
lcapy.
LSection
(OP1, OP2)¶ L Section
++ + OP1 ++ ++  +++   OP2   +++  +

class
lcapy.
Ladder
(OP1, *args)¶ (Unbalanced) ladder network with alternating Series and Shunt networks chained
++ ++ + OP1 +++ args[1] + ++  ++ +++     args[0]   +++  +

class
lcapy.
GeneralTxLine
(Z0, gamma, l)¶ General transmission line
Z0 is the (real) characteristic impedance (ohms) gamma is the propagation constant (1/m) l is the transmission line length (m)

class
lcapy.
LosslessTxLine
(Z0, c=150000000.0, l=1)¶ Losslees transmission line Z0 is the (real) characteristic impedance (ohms) c is the propagation speed (m/s) l is the transmission line length (m)

class
lcapy.
TxLine
(R, L, G, C, l=1)¶ Transmission line
R series resistance/metre L series inductance/metre G shunt conductance/metre C shunt capacitance/metre l is the transmission line length

class
lcapy.
Circuit
(filename=None)¶ Here’s an example of using the Circuit class:
cct = Circuit() cct.add(‘V1 1 0 V; down’) cct.add(‘R1 1 2 R; right’) cct.add(‘C1 2 0_2 C; down’) cct.add(‘W 0 0_2; right’)
The directions after the semicolon are hints for drawing the schematic and are ignored for the circuit analysis. The last net is a wire to make the schematic look nice; it is not needed for circuit analysis. Indeed the capacitor could be connected directly to nodes 2 and 0.
The nodes are usually numbers but can be any alphanumeric name including underscores. By default, nodes with underscores are not drawn.
The circuit components are also usually numbered but again they can be any alphanumeric name. They can also have anonymous names, as for the wire in the example. Internally they are enumerated sequentially for each component type: W#1, W#2, etc.
The circuit can be displayed using: cct.draw()
The schematic can be saved to a file using: cct.draw(‘schematic.pdf’)
The sdomain voltage across a component can be found using: cct.V1.V
This is found using modified nodal analysis. Once this is performed, the results are cached until the network is modified.
The sdomain voltage through a component can be found using: cct.R1.I
The sdomain nodal voltages with respect to the ground node (0) can be found using: cct[2].V
The time domain voltages and currents are displayed using lowercase attributes v and i. For example, cct.C1.v
Note that the answer assumes that all the dependent sources are zero for t < 0 and that all the inductors and capacitors have no initial currents and voltages. Thus the Heaviside(t) factor should be ignored and replaced with the condition t >= 0.
The impedance between nodes 2 and 0 can be found using: Z = cct.impedance(2, 0)
The opencircuit voltage between nodes 2 and 0 can be found using: Z = cct.Voc(2, 0)
The Thevenin equivalent circuit between nodes 2 and 0 can be found using: thevenin = cct.Thevenin(2, 0)
The sdomain modelcan be drawn using: cct.s_model().draw()

Y
(Np, Nm)¶ Return admittance between nodes Np and Nm with independent sources killed.

Z
(Np, Nm)¶ Return impedance between nodes Np and Nm with independent sources killed.

netfile_add
(filename)¶ Add the nets from file with specified filename
