Skip to content

NWaves for MATLAB and sciPy users

Tim Sharii edited this page Aug 26, 2021 · 11 revisions
scipy.signal NWaves

LTI representations

z, p, k = tf2zpk(num, den)
var tf = new TransferFunction(num, den);
var z = tf.Zeros;
var p = tf.Poles;
var k = tf.Gain;
num, den = zpk2tf(z, p, k)
Complex[] z = new [] { ... }; // zeros
Complex[] p = new [] { ... }; // poles

var tf = new TransferFunction(z, p, k);
var num = tf.Numerator;
var den = tf.Denominator;
sos = tf2sos(num, den)
var tf = new TransferFunction(num, den);
var sos = DesignFilter.TfToSos(tf);
A, B, C, D = tf2ss(num, den)
var tf = new TransferFunction(num, den);
var ss = tf.StateSpace;
// then get ss.A, ss.B, ss.C, ss.D
sos = zpk2sos(z, p, k)
var tf = new TransferFunction(z, p, k);
var sos = DesignFilter.TfToSos(tf);
num, den = ss2tf(A, B, C, D)
var ss = new StateSpace { A=a, B=b, C=c, D=d };
var tf = new TransferFunction(ss);
var num = tf.Numerator;
var den = tf.Denominator;
num, den = sos2tf(sos)
TransferFunction[] sos; // fill array
var tf = DesignFilter.SosToTf(sos);
var num = tf.Numerator;
var den = tf.Denominator;

Filtering

y = lfilter(num, den, x)
var filter = new IirFilter(num, den);
// or if den == [1]:
// var filter = new FirFilter(num);
var y = filter.ApplyTo(x);
y = lfilter(num, den, x, zi=zi)
var filter = new ZiFilter(num, den);
filter.Init(zi);
var y = filter.ApplyTo(x);
zi = lfilter_zi(num, den)
var tf = new TransferFunction(num, den);
var zi = tf.Zi;
p = 3*(max(len(num), len(den))-1)
y = filtfilt(num, den, x, padlen=p)
var filter = new ZiFilter(num, den);
var y = filter.ZeroPhase(x);
y = sosfilt(sos, x)
var tf = new TransferFunction(num, den);
var sos = DesignFilter.TfToSos(tf);
var filter = new FilterChain(sos);
var y = filter.ApplyTo(x);

Filter design

# cutoff freqs are in range [0..1]:
# fc = 2*f / fs

h1 = firwin(order, 0.2,
            window='blackman')
h2 = firwin(order, 0.2,
            pass_zero=False)
h3 = firwin(order, [0.2, 0.6])
h4 = firwin(order, [0.2, 0.6],
            pass_zero=False)
// cutoff freqs are in range [0, 0.5]:
// fc = f / fs

var h1 = DesignFilter.FirWinLp(order, 0.1);
var h2 = DesignFilter.FirWinHp(order, 0.1,
                      WindowType.Hamming);
var h3 = DesignFilter.FirWinBs(order, 0.1, 0.3,
                      WindowType.Hamming);
var h4 = DesignFilter.FirWinBp(order, 0.1, 0.3,
                      WindowType.Hamming);
w, h = freqz(num, den, 1024)
var tf = new TransferFunction(num, den);
var h = tf.FrequencyResponse(1024).Magnitude;
Clone this wiki locally