Convert digital filter state-space parameters to second-order sections form
Second-Order Section Form of Filter
Design a fifth-order Butterworth lowpass filter, specifying a cutoff frequency of rad/sample and expressing the output in state-space form. Convert the state-space result to second-order sections. Visualize the frequency response of the filter.
[A,B,C,D] = butter(5,0.2); sos = ss2sos(A,B,C,D)
sos = 3×6 0.0013 0.0013 0 1.0000 -0.5095 0 1.0000 1.9997 0.9997 1.0000 -1.0966 0.3554 1.0000 2.0000 1.0000 1.0000 -1.3693 0.6926
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 measures the acceleration, , of the mass.
The system is sampled at Hz. Generate 50 time samples. Define the sampling interval .
Fs = 5; dt = 1/Fs; N = 50; t = dt*(0:N-1);
The oscillator can be described by the state-space equations
where is the state vector, and are respectively the position and velocity of the mass, and the matrices
A = [cos(dt) sin(dt);-sin(dt) cos(dt)]; B = [1-cos(dt);sin(dt)]; C = [-1 0]; D = 1;
The system is excited with a unit impulse in the positive direction. Use the state-space model to compute the time evolution of the system starting from an all-zero initial state.
u = [1 zeros(1,N-1)]; x = [0;0]; for k = 1:N y(k) = C*x + D*u(k); x = A*x + B*u(k); end
Plot the acceleration of the mass as a function of time.
Compute the time-dependent acceleration using the transfer function to filter the input. Express the transfer function as second-order sections. Plot the result.
sos = ss2sos(A,B,C,D); yt = sosfilt(sos,u); stem(t,yt,'filled')
The result is the same in both cases.
A — State matrix
State matrix, specified as a matrix. If the system has p inputs
and q outputs and is described by n state
A is of size
B — Input-to-state matrix
Input-to-state matrix, specified as a matrix. If the system has p
inputs and q outputs and is described by n state
B is of size
C — Output-to-state matrix
Output-to-state matrix, specified as a matrix. If the system has
p inputs and q outputs and is described by
n state variables, then
C is of size
D — Feedthrough matrix
Feedthrough matrix, specified as a matrix. If the system has p
inputs and q outputs and is described by n state
D is of size
iu — Index
1 (default) | integer
Index, specified as an integer.
order — Row order
'up' (default) |
Row order in
sos, specified as one of these values:
'down'— Order the sections so that the first row of
soscontains the poles that are closest to the unit circle.
'up'— Order the sections so that the first row of
soscontains the poles that are farthest from the unit circle.
The zeros are paired with the poles that are closest to them.
scale — Scaling of gain and numerator coefficients
'none' (default) |
Scaling of the gain and numerator coefficients, specified as one of these values:
'none'— Apply no scaling.
'inf'— Apply infinity-norm scaling.
'two'— Apply 2-norm scaling.
Using infinity-norm scaling in conjunction with
minimizes the probability of overflow in the realization. Using 2-norm scaling in
down-ordering minimizes the peak round-off
Infinity-norm and 2-norm scaling are appropriate for only direct-form II implementations.
sos — Second-order section representation
Second-order section representation, returned as a matrix.
is an L-by-6 matrix of the form
whose rows contain the numerator and denominator coefficients bik and aik of the second-order sections of H(z), which is given by
g — Overall system gain
Overall system gain, returned as a real-valued scalar.
If you call the function with one output argument, the function embeds the 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
the function with two outputs:
ss2sos function uses this four-step algorithm to determine the
second-order section representation for an input state-space system.
Find the poles and zeros of the system given by
Use 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 these rules:
Match the poles that are closest to the unit circle with the zeros that are closest to those poles.
Match the poles that are next closest to the unit circle with the zeros that are closest to those poles.
Continue this process until all of the poles and zeros are matched.
ss2sosfunction groups real poles into sections with the real poles that are closest to them in absolute value. The same rule holds for real zeros.
Order the sections according to the proximity of the pole pairs to the unit circle. The
ss2sosfunction normally orders the sections with poles that are closest to the unit circle last in the cascade. You can specify for
ss2sosto order the sections in the reverse order by setting the order input to
Scale the sections by the norm specified by the
scaleinput. For arbitrary H(ω), the scaling is defined by
where p can be either ∞ or 2. For details, see the references. This scaling is an attempt to minimize overflow or peak round-off noise in fixed-point filter implementations.
 Jackson, Leland B. Digital Filters and Signal Processing. Boston: Kluwer Academic Publishers, 1996.
 Mitra, Sanjit Kumar. 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.