Convert digital filter transfer function data to second-order sections form
overall system gain in the first section.
sos = tf2sos(___)
Second-Order Section Implementation of a Butterworth Filter
Design a Butterworth 4th-order lowpass filter using the function
butter. Specify the cutoff frequency as half the Nyquist frequency. Implement the filter as second-order sections. Verify that the two representations are identical by comparing their numerators and denominators.
[nm,dn] = butter(4,0.5); [ss,gn] = tf2sos(nm,dn); numers = [conv(ss(1,1:3),ss(2,1:3))*gn;nm]
numers = 2×5 0.0940 0.3759 0.5639 0.3759 0.0940 0.0940 0.3759 0.5639 0.3759 0.0940
denoms = [conv(ss(1,4:6),ss(2,4:6));dn]
denoms = 2×5 1.0000 0.0000 0.4860 -0.0000 0.0177 1.0000 0.0000 0.4860 -0.0000 0.0177
A one-dimensional discrete-time oscillating system consists of a unit mass, , attached to a wall by a spring of unit elastic constant. A sensor samples the acceleration, , of the mass at Hz.
Generate 50 time samples. Define the sampling interval .
Fs = 5; dt = 1/Fs; N = 50; t = dt*(0:N-1); u = [1 zeros(1,N-1)];
The transfer function of the system has an analytic expression:
The system is excited with a unit impulse in the positive direction. Compute the time evolution of the system using the transfer function. Plot the response.
bf = [1 -(1+cos(dt)) cos(dt)]; af = [1 -2*cos(dt) 1]; yf = filter(bf,af,u); stem(t,yf,'o') xlabel('t')
Compute the time-dependent acceleration using the second-order sections representation of the transfer function to filter the input. Plot the result. The result is the same in both cases.
sos = tf2sos(bf,af); yt = sosfilt(sos,u); stem(t,yt,'filled')
a — Transfer function coefficients
Transfer function coefficients, specified as vectors. Express the transfer function
in terms of
b = [1 3 3 1]/6 and
a = [3 0 1 0]/3
specify a third-order Butterworth filter with normalized 3 dB frequency 0.5π
order — Row order
'up' (default) |
Row order, specified as one of the following:
'up'— Order the sections so the first row of
soscontains the poles farthest from the unit circle.
'down'— Order the sections so the first row of
soscontains the poles closest to the unit circle.
scale — Scaling of gain and numerator coefficients
'none' (default) |
Scaling of gain and numerator coefficients, specified as one of the following:
'none'— Apply no scaling.
'inf'— Apply infinity-norm scaling.
'two'— Apply 2-norm scaling.
Using infinity-norm scaling with
'up'-ordering minimizes the
probability of overflow in the realization. Using 2-norm scaling with
'down'-ordering minimizes the peak round-off noise.
Infinity-norm and 2-norm scaling are appropriate only for direct-form II implementations.
sos — Second-order section representation
Second-order section representation, returned as a matrix.
is an L-by-6 matrix
whose rows contain the numerator and denominator coefficients bik and aik of the second-order sections of H(z):
g — Overall system gain
Overall system gain, returned as a real scalar.
If you call
tf2sos with one output argument, the function
embeds the overall system gain in the first section,
H1(z), so that
Embedding the gain in the first section when scaling a direct-form II structure is
not recommended and can result in erratic scaling. To avoid embedding the gain, use
tf2sos with two outputs.
tf2sos uses a four-step algorithm to determine the second-order
section representation for an input transfer function system:
It uses the function
zp2sos, which first groups the zeros and poles into complex conjugate pairs using the
zp2sosthen forms the second-order sections by matching the pole and zero pairs according to the following rules:
Match the poles closest to the unit circle with the zeros closest to those poles.
Match the poles next closest to the unit circle with the zeros closest to those poles.
Continue until all of the poles and zeros are matched.
tf2sosgroups real poles into sections with the real poles closest to them in absolute value. The same rule holds for real zeros.
It orders the sections according to the proximity of the pole pairs to the unit circle.
tf2sosnormally orders the sections with poles closest to the unit circle last in the cascade. You can tell
tf2sosto order the sections in the reverse order by specifying
tf2sosscales the sections by the norm specified in
scale. For arbitrary H(ω), the scaling is defined by
where p can be either ∞ or 2. See the references for details on the scaling. The algorithm follows this scaling in an attempt to minimize overflow or peak round-off noise in fixed-point filter implementations.
 Jackson, L. B. Digital Filters and Signal Processing. 3rd ed. Boston: Kluwer Academic Publishers, 1996.
 Mitra, S. K. Digital Signal Processing: A Computer-Based Approach. New York: McGraw-Hill, 1998.
 Vaidyanathan, P. P. “Robust Digital Filter Structures.” Handbook for Digital Signal Processing (S. K. Mitra and J. F. Kaiser, eds.). New York: John Wiley & Sons, 1993.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Any character or string input must be a constant at compile time.
Introduced before R2006a