1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
use super::error::LtiError;
use super::sos::Sos;
use super::transfer_function::TransferFunction;
use super::util::{
CompositionDomain, cast_real_scalar, real_poly_from_roots, validate_sample_time,
};
use super::{ContinuousStateSpace, ContinuousTime, DiscreteStateSpace, DiscreteTime};
use alloc::vec::Vec;
use faer::complex::Complex;
use faer_traits::RealField;
use num_traits::{Float, NumCast};
/// Real-gain SISO zero/pole/gain representation.
#[derive(Clone, Debug, PartialEq)]
pub struct Zpk<R, Domain> {
zeros: Vec<Complex<R>>,
poles: Vec<Complex<R>>,
gain: R,
domain: Domain,
}
/// Continuous-time SISO zero/pole/gain representation.
pub type ContinuousZpk<R> = Zpk<R, ContinuousTime>;
/// Discrete-time SISO zero/pole/gain representation.
pub type DiscreteZpk<R> = Zpk<R, DiscreteTime<R>>;
impl<R, Domain> Zpk<R, Domain>
where
R: Float + RealField,
Domain: Clone,
{
/// Creates a zero/pole/gain representation.
pub fn new(
zeros: impl Into<Vec<Complex<R>>>,
poles: impl Into<Vec<Complex<R>>>,
gain: R,
domain: Domain,
) -> Result<Self, LtiError> {
Ok(Self {
zeros: zeros.into(),
poles: poles.into(),
gain,
domain,
})
}
/// Zeros of the transfer function.
#[must_use]
pub fn zeros(&self) -> &[Complex<R>] {
&self.zeros
}
/// Poles of the transfer function.
#[must_use]
pub fn poles(&self) -> &[Complex<R>] {
&self.poles
}
/// Overall scalar gain.
#[must_use]
pub fn gain(&self) -> R {
self.gain
}
/// Domain metadata carried by the representation.
#[must_use]
pub fn domain(&self) -> &Domain {
&self.domain
}
/// Evaluates the transfer function at the supplied complex point.
#[must_use]
pub fn evaluate(&self, point: Complex<R>) -> Complex<R> {
let num = self
.zeros
.iter()
.fold(Complex::new(self.gain, R::zero()), |acc, &zero| {
acc * (point - zero)
});
let den = self
.poles
.iter()
.fold(Complex::new(R::one(), R::zero()), |acc, &pole| {
acc * (point - pole)
});
num / den
}
/// Converts zero/pole/gain form back into coefficient form.
///
/// This is the central reverse path from root data into the rest of the
/// representation graph. The state-space conversion methods intentionally
/// chain through this coefficient form instead of reimplementing a second
/// realization path here.
pub fn to_transfer_function(&self) -> Result<TransferFunction<R, Domain>, LtiError> {
let mut numerator = real_poly_from_roots(&self.zeros, "zeros")?;
for coeff in &mut numerator {
*coeff *= self.gain;
}
let denominator = real_poly_from_roots(&self.poles, "poles")?;
TransferFunction::new(numerator, denominator, self.domain.clone())
}
/// Converts zero/pole/gain form into a second-order-section cascade.
///
/// The SOS path is implemented through the existing real root-section
/// builder so the pairing and padding logic stays centralized.
pub fn to_sos(&self) -> Result<Sos<R, Domain>, LtiError> {
Sos::from_zpk(self)
}
}
impl<R, Domain> Zpk<R, Domain>
where
R: Float + RealField,
Domain: CompositionDomain<R>,
{
/// Forms the parallel composition `self + rhs`.
///
/// Composition is routed through `TransferFunction`, which is the
/// arithmetic hub of the current SISO representation layer.
pub fn add(&self, rhs: &Self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.add(&rhs.to_transfer_function()?)?
.to_zpk()
}
/// Forms the parallel difference `self - rhs`.
pub fn sub(&self, rhs: &Self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.sub(&rhs.to_transfer_function()?)?
.to_zpk()
}
/// Forms the series composition `self * rhs`.
pub fn mul(&self, rhs: &Self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.mul(&rhs.to_transfer_function()?)?
.to_zpk()
}
/// Forms the quotient `self / rhs`.
pub fn div(&self, rhs: &Self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.div(&rhs.to_transfer_function()?)?
.to_zpk()
}
/// Returns the inverse `1 / self`.
pub fn inv(&self) -> Result<Self, LtiError> {
self.to_transfer_function()?.inv()?.to_zpk()
}
/// Forms the standard negative-feedback closure `self / (1 + self * rhs)`.
pub fn feedback(&self, rhs: &Self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.feedback(&rhs.to_transfer_function()?)?
.to_zpk()
}
/// Forms the positive-feedback closure `self / (1 - self * rhs)`.
pub fn positive_feedback(&self, rhs: &Self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.positive_feedback(&rhs.to_transfer_function()?)?
.to_zpk()
}
/// Forms the standard unity negative-feedback closure `self / (1 + self)`.
pub fn unity_feedback(&self) -> Result<Self, LtiError> {
self.to_transfer_function()?.unity_feedback()?.to_zpk()
}
/// Forms the unity positive-feedback closure `self / (1 - self)`.
pub fn positive_unity_feedback(&self) -> Result<Self, LtiError> {
self.to_transfer_function()?
.positive_unity_feedback()?
.to_zpk()
}
}
impl<R> ContinuousZpk<R>
where
R: Float + RealField,
{
/// Creates a continuous-time zero/pole/gain representation.
pub fn continuous(
zeros: impl Into<Vec<Complex<R>>>,
poles: impl Into<Vec<Complex<R>>>,
gain: R,
) -> Result<Self, LtiError> {
Self::new(zeros, poles, gain, ContinuousTime)
}
/// Returns the steady-state gain `G(0)`.
///
/// This evaluates the factored transfer map directly at `s = 0` and
/// rejects poles at the origin through `NonFiniteResult`.
pub fn dc_gain(&self) -> Result<Complex<R>, LtiError> {
let gain = self.evaluate(Complex::new(R::zero(), R::zero()));
if gain.re.is_finite() && gain.im.is_finite() {
Ok(gain)
} else {
Err(LtiError::NonFiniteResult { which: "dc_gain" })
}
}
/// Converts zero/pole/gain form to continuous-time state space through
/// `TransferFunction`.
///
/// This is intentionally a chained conversion. `TransferFunction` is the
/// hub of the current SISO conversion graph.
pub fn to_state_space(&self) -> Result<ContinuousStateSpace<R>, LtiError> {
self.to_transfer_function()?.to_state_space()
}
/// Casts the continuous-time zero/pole/gain representation to another
/// real scalar dtype.
///
/// Real and imaginary parts of each root are converted independently.
pub fn try_cast<S>(&self) -> Result<ContinuousZpk<S>, LtiError>
where
S: Float + RealField + NumCast,
{
ContinuousZpk::continuous(
self.zeros()
.iter()
.copied()
.map(|value| {
Ok(Complex::new(
cast_real_scalar(value.re, "zpk.zeros")?,
cast_real_scalar(value.im, "zpk.zeros")?,
))
})
.collect::<Result<Vec<_>, LtiError>>()?,
self.poles()
.iter()
.copied()
.map(|value| {
Ok(Complex::new(
cast_real_scalar(value.re, "zpk.poles")?,
cast_real_scalar(value.im, "zpk.poles")?,
))
})
.collect::<Result<Vec<_>, LtiError>>()?,
cast_real_scalar(self.gain(), "zpk.gain")?,
)
}
}
impl<R> DiscreteZpk<R>
where
R: Float + RealField,
{
/// Creates a discrete-time zero/pole/gain representation.
pub fn discrete(
zeros: impl Into<Vec<Complex<R>>>,
poles: impl Into<Vec<Complex<R>>>,
gain: R,
sample_time: R,
) -> Result<Self, LtiError> {
validate_sample_time(sample_time)?;
Self::new(zeros, poles, gain, DiscreteTime::new(sample_time))
}
/// Sample interval carried by the discrete-time representation.
#[must_use]
pub fn sample_time(&self) -> R {
self.domain.sample_time()
}
/// Creates the exact `samples`-step pure delay `z^-samples`.
///
/// In zero/pole/gain form this is represented by `samples` repeated poles
/// at the origin, no zeros, and unit gain.
pub fn delay(samples: usize, sample_time: R) -> Result<Self, LtiError> {
let zero = Complex::new(R::zero(), R::zero());
Self::discrete(Vec::new(), vec![zero; samples], R::one(), sample_time)
}
/// Returns the steady-state gain `G(1)`.
///
/// This evaluates the factored transfer map directly at the discrete
/// steady-state point `z = 1`.
pub fn dc_gain(&self) -> Result<Complex<R>, LtiError> {
let gain = self.evaluate(Complex::new(R::one(), R::zero()));
if gain.re.is_finite() && gain.im.is_finite() {
Ok(gain)
} else {
Err(LtiError::NonFiniteResult { which: "dc_gain" })
}
}
/// Converts zero/pole/gain form to discrete-time state space through
/// `TransferFunction`.
///
/// This keeps the domain-preserving realization logic centralized in the
/// transfer-function layer.
pub fn to_state_space(&self) -> Result<DiscreteStateSpace<R>, LtiError> {
self.to_transfer_function()?.to_state_space()
}
/// Casts the discrete-time zero/pole/gain representation and sample time
/// to another real scalar dtype.
///
/// This preserves the exact factored structure and only changes the scalar
/// storage type.
pub fn try_cast<S>(&self) -> Result<DiscreteZpk<S>, LtiError>
where
S: Float + RealField + NumCast,
{
DiscreteZpk::discrete(
self.zeros()
.iter()
.copied()
.map(|value| {
Ok(Complex::new(
cast_real_scalar(value.re, "zpk.zeros")?,
cast_real_scalar(value.im, "zpk.zeros")?,
))
})
.collect::<Result<Vec<_>, LtiError>>()?,
self.poles()
.iter()
.copied()
.map(|value| {
Ok(Complex::new(
cast_real_scalar(value.re, "zpk.poles")?,
cast_real_scalar(value.im, "zpk.poles")?,
))
})
.collect::<Result<Vec<_>, LtiError>>()?,
cast_real_scalar(self.gain(), "zpk.gain")?,
cast_real_scalar(self.sample_time(), "zpk.sample_time")?,
)
}
}
#[cfg(test)]
mod tests {
use super::ContinuousZpk;
use alloc::vec::Vec;
use faer::complex::Complex;
fn assert_close(lhs: f64, rhs: f64, tol: f64) {
let err = (lhs - rhs).abs();
assert!(err <= tol, "lhs={lhs}, rhs={rhs}, err={err}, tol={tol}");
}
fn assert_complex_close(lhs: Complex<f64>, rhs: Complex<f64>, tol: f64) {
assert_close(lhs.re, rhs.re, tol);
assert_close(lhs.im, rhs.im, tol);
}
#[test]
fn zpk_to_transfer_function_preserves_gain_across_full_numerator() {
let zpk = ContinuousZpk::continuous(vec![Complex::new(1.0, 0.0)], Vec::new(), 2.0).unwrap();
let tf = zpk.to_transfer_function().unwrap();
assert_eq!(tf.numerator(), &[2.0, -2.0]);
assert_complex_close(
tf.evaluate(Complex::new(3.0, 0.0)),
zpk.evaluate(Complex::new(3.0, 0.0)),
1.0e-12,
);
assert_complex_close(
tf.evaluate(Complex::new(-2.0, 0.0)),
zpk.evaluate(Complex::new(-2.0, 0.0)),
1.0e-12,
);
}
}