Expressions, Functions, and Constants

Ngspice and ngnutmeg store data in the form of vectors: time, voltage, etc. Each vector has a type, and vectors can be operated on and combined algebraically in ways consistent with their types. Vectors are normally created as the output of a simulation, or when a data file (output raw file) is read in again (ngspice, ngnutmeg, see the load command 17.5.40), or when the initial data-file is loaded directly into ngnutmeg. They can also be created with the let command 817.5.37).

An expression is an algebraic formula involving vectors and scalars (a scalar is a vector of length 1) and the following operations:

+ - * / ^ % ,

% is the modulo operator, and the comma operator has two meanings: if it is present in the argument list of a user definable function, it serves to separate the arguments. Otherwise, the term x , y is synonymous with x + j(y). Also available are the logical operations & (and), | (or), ! (not), and the relational operations <, >, >=, <=, =, and <> (not equal). If used in an algebraic expression they work like they would in C, producing values of 0 or 1. The relational operators have the following synonyms:

Operator
Synonym
gt
>
lt
<
ge
>=
le
<=
ne
<>
and
&
or
|
not
!
eq
=

The operators are useful when < and > might be confused with the internal IO redirection (see 17.4, which is almost always happening). It is however safe to use < and > with the define command (17.5.15).

The following functions are available:

Name
Function
mag(vector)
Magnitude of vector (same as abs(vector)).
ph(vector)
Phase of vector.
cph(vector)
Phase of vector. Continuous values, no discontinuity at ±π.
unwrap(vector)
Phase of vector. Continuous values, no discontinuity at ±π. Real phase vector in degrees as input.
j(vector)
i(sqrt(-1)) times vector.
real(vector
The real component of vector.
imag(vector)
The imaginary part of vector.
db(vector)
20 log10(mag(vector)).
log10(vector)
The logarithm (base 10) of vector.
ln(vector)
The natural logarithm (base e) of vector.
exp(vector)
e to the vector power.
abs(vector)
The absolute value of vector (same as mag).
sqrt(vector)
The square root of vector.
sin(vector)
The sine of vector.
cos(vector)
The cosine of vector.
tan(vector)
The tangent of vector.
atan(vector)
The inverse tangent of vector.
sinh(vector)
The hyperbolic sine of vector.
cosh(vector)
The hyperbolic cosine of vector.
tanh(vector)
The hyperbolic tangent of vector.
floor(vector)
Largest integer that is less than or equal to vector.
ceil(vector)
Smallest integer that is greater than or equal to vector.
norm(vector)
The vector normalized to 1 (i.e, the largest magnitude of any component is 1).
mean(vector)
The result is a scalar (a length 1 vector) that is the mean of the elements of vector (elements values added, divided by number of elements).
avg(vector)
The average of a vector.
Returns a vector where each element is the mean of the preceding elements of the input vector (including the actual element).
stddev(vector)
The result is a scalar (a length 1 vector) that is the standard deviation of the elements of vector .
group_delay(vector)
Calculates the group delay dphase[rad]/dω[rad/s]. Input is the complex vector of a system transfer function versus frequency, resembling damping and phase per frequency value. Output is a vector of group delay values (real values of delay times) versus frequency.
vector(number)
The result is a vector of length number, with elements 0, 1, ... number - 1. If number is a vector then just the first element is taken, and if it isn't an integer then the floor of the magnitude is used.
unitvec(number)
The result is a vector of length number, all elements having a value 1.
Name
Function
length(vector)
The length of vector.
interpolate(plot.vector)
The result of interpolating the named vector onto the scale of the current plot. This function uses the variable polydegree to determine the degree of interpolation.
deriv(vector)
Calculates the derivative of the given vector. This uses numeric differentiation by interpolating a polynomial and may not produce satisfactory results (particularly with iterated differentiation). The implementation only calculates the derivative with respect to the real component of that vector's scale.
vecd(vector)
Compute the differential of a vector.
vecmin(vector)
Returns the value of the vector element with minimum value. Same as minimum.
minimum(vector)
Returns the value of the vector element with minimum value. Same as vecmin.
vecmax(vector)
Returns the value of the vector element with maximum value. Same as maximum.
maximum(vector)
Returns the value of the vector element with maximum value. Same as vecmax.
fft(vector)
fast fourier transform (17.5.27)
ifft(vector)
inverse fast fourier transform (17.5.27)
sortorder(vector)
Returns a vector with the positions of the elements in a real vector after they have been sorted into increasing order using a stable method (qsort).
timer(vector)
Returns CPU-time minus the value of the first vector element.
clock(vector)
Returns wall-time minus the value of the first vector element.

Several functions offering statistical procedures are listed in the following table:

Name
Function
rnd(vector)
A vector with each component a random integer between 0 and the absolute value of the input vector's corresponding integer element value.
sgauss(vector)
Returns a vector of random numbers drawn from a Gaussian distribution (real value, mean = 0 , standard deviation = 1). The length of the vector returned is determined by the input vector. The contents of the input vector will not be used. A call to sgauss(0) will return a single value of a random number as a vector of length 1..
sunif(vector)
Returns a vector of random real numbers uniformly distributed in the interval [-1 .. 1[. The length of the vector returned is determined by the input vector. The contents of the input vector will not be used. A call to sunif(0) will return a single value of a random number as a vector of length 1.
poisson(vector)
Returns a vector with its elements being integers drawn from a Poisson distribution. The elements of the input vector (real numbers) are the expected numbers λ. Complex vectors are allowed, real and imaginary values are treated separately.
exponential(vector)
Returns a vector with its elements (real numbers) drawn from an exponential distribution. The elements of the input vector are the respective mean values (real numbers). Complex vectors are allowed, real and imaginary values are treated separately.

An input vector may be either the name of a vector already defined or a floating-point number (a scalar). A scalar will result in an output vector of length 1. A number may be written in any format acceptable to ngspice, such as 14.6Meg or -1.231e-4. Note that you can either use scientific notation or one of the abbreviations like MEG or G, but not both. As with ngspice, a number may have trailing alphabetic characters.

The notation expr [num] denotes the num'th element of expr. For multi-dimensional vectors, a vector of one less dimension is returned. Also for multi-dimensional vectors, the notation expr[m][n] will return the nth element of the mth subvector. To get a subrange of a vector, use the form expr[lower, upper]. To reference vectors in a plot that is not the current plot (see the setplot command, below), the notation plotname.vecname can be used. Either a plotname or a vector name may be the wildcard all. If the plotname is all, matching vectors from all plots are specified, and if the vector name is all, all vectors in the specified plots are referenced. Note that you may not use binary operations on expressions involving wildcards - it is not obvious what all + all should denote, for instance. Thus some (contrived) examples of expressions are:

Expressions examples:

cos(TIME) + db(v(3))
sin(cos(log([1 2 3 4 5 6 7 8 9 10])))
TIME * rnd(v(9)) - 15 * cos(vin#branch) ^ [7.9e5 8]
not ((ac3.FREQ[32] & tran1.TIME[10]) gt 3)
(sunif(0) ge 0) ? 1.0 : 2.0
mag(fft(v(18)))

Vector names in ngspice may look like @dname[param], where dname is either the name of a device instance or of a device model. The vector contains the value of the parameter of the device or model. See Appendix, Chapt. 31 for details of which parameters are available. The returned value is a vector of length 1. Please note that finding the value of device and device model parameters can also be done with the show command (e.g. show v1 : dc).

There are a number of pre-defined constants in ngspice, which you may use by their name. They are stored in plot (17.3) const and are listed in the table below:

Name
Description
Value
pi
π
3.14159...
e
e (the base of natural logarithms)
2.71828...
c
c (the speed of light)
299,792,500 $\frac{m}{sec}$
i
i (the square root of -1)
$\sqrt{- 1}$
kelvin
(absolute zero in centigrade)
-273.15C
echarge
q (the charge of an electron)
1.60219e-19 C
boltz
k (Boltzmann's constant)
1.38062e-23$\frac{J}{K}$
planck
h (Planck's constant)
6.62620e-34
yes
boolean
1
no
boolean
0
TRUE
boolean
1
FALSE
boolean
0

These constants are all given in MKS units. If you define another variable with a name that conflicts with one of these then it takes precedence.

Additional constants may be generated during circuit setup (see .csparam, 2.10).