|
3 | 3 | % c2d_euler Transforms a continuous transfer function to a discrete |
4 | 4 | % transfer function using the forward and backward Euler methods. |
5 | 5 | % |
6 | | -% Hz = c2d_euler(Hs,T,'forward') |
7 | | -% Hz = c2d_euler(Hs,T,'backward') |
| 6 | +% Hz = c2d_euler(Hs,T,type) |
| 7 | +% Hz = c2d_euler(Hs,T,type,output,normalize) |
8 | 8 | % |
9 | 9 | % See also c2d. |
10 | 10 | % |
11 | 11 | % Copyright © 2021 Tamas Kis |
12 | | -% Last Update: 2022-09-26 |
| 12 | +% Last Update: 2023-09-17 |
13 | 13 | % Website: https://tamaskis.github.io |
14 | 14 | % Contact: tamas.a.kis@outlook.com |
15 | 15 | % |
|
25 | 25 | % ------ |
26 | 26 | % INPUT: |
27 | 27 | % ------ |
28 | | -% Hs - (1×1 tf or zpk) continous transfer function |
29 | | -% T - (1×1 double) sampling period |
30 | | -% type - (char) 'forward' or 'backward' |
31 | | -% output - (OPTIONAL) (char) specifies output type ('tf' or 'zpk') |
| 28 | +% Hs - (1×1 tf or zpk) continous transfer function |
| 29 | +% T - (1×1 double) sampling period |
| 30 | +% type - (char array) 'forward' or 'backward' |
| 31 | +% output - (OPTIONAL) (char array) specifies output type ('tf' or |
| 32 | +% 'zpk') (defaults to 'tf') |
| 33 | +% normalize - (OPTIONAL) (1×1 logical) true if transfer function should |
| 34 | +% be normalized, false otherwise (defaults to false) |
32 | 35 | % |
33 | 36 | % ------- |
34 | 37 | % OUTPUT: |
35 | 38 | % ------- |
36 | 39 | % Hz - (1×1 tf or zpk) discrete transfer function |
37 | 40 | % |
38 | 41 | %========================================================================== |
39 | | -function Hz = c2d_euler(Hs,T,type,output) |
| 42 | +function Hz = c2d_euler(Hs,T,type,output,normalize) |
40 | 43 |
|
41 | 44 | % defaults "output" to 'tf' if not input |
42 | 45 | if (nargin < 4) || isempty(output) |
43 | 46 | output = 'tf'; |
44 | 47 | end |
45 | 48 |
|
| 49 | + % defaults "normalize" to false if not input |
| 50 | + if (nargin < 5) || isempty(normalize) |
| 51 | + normalize = false; |
| 52 | + end |
| 53 | + |
46 | 54 | % symbolic variable for z; |
47 | 55 | z = sym('z'); |
48 | 56 |
|
49 | 57 | % specified Euler approximation of s |
50 | 58 | if strcmpi(type,'backward') |
51 | 59 | s = (z-1)/(T*z); |
52 | | - else |
| 60 | + elseif strcmpi(type,'forward') |
53 | 61 | s = (z-1)/T; |
| 62 | + else |
| 63 | + error("'type' must be input as 'backward' or 'forward'") |
54 | 64 | end |
55 | 65 |
|
56 | 66 | % converts transfer function object to symbolic function object |
|
66 | 76 | num = sym2poly(sym_num); |
67 | 77 | den = sym2poly(sym_den); |
68 | 78 |
|
| 79 | + % normalizes coefficients w.r.t. coefficient on largest power of z in |
| 80 | + % denominator |
| 81 | + if normalize |
| 82 | + num = num/den(1); |
| 83 | + den = den/den(1); |
| 84 | + end |
| 85 | + |
69 | 86 | % creates discrete transfer function model |
70 | 87 | Hz = tf(num,den,T); |
71 | 88 |
|
|
0 commit comments