Skip to content

Commit

Permalink
Merge pull request #2 from tamaskis/normalization
Browse files Browse the repository at this point in the history
added normalization + better input checking, updated examples
  • Loading branch information
tamaskis authored Sep 17, 2023
2 parents b7c89ce + 173da42 commit c85c50f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
.DS_Store
local/
.DS_Store
Binary file not shown.
Binary file modified EXAMPLES.mlx
Binary file not shown.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
Transforms a continuous transfer function to a discrete transfer function using the forward and backward Euler methods.



## Syntax

`Hz = c2d_euler(Hs,T,'forward')` \
`Hz = c2d_euler(Hs,T,'backward')`
`Hz = c2d_euler(Hs,T,type)` \
`Hz = c2d_euler(Hs,T,type,output,normalize)`


## Inputs

## Description
- `Hs` (1×1 `'tf'` or `'zpk'`): continuous transfer function
- `T` (1×1 double): sampling period
- `type` (`char` array): `'forward'` or `'backward`
- `output` (OPTIONAL) (`char` array): specifies output type (`'tf'` or `'zpk'`) (defaults to `'tf'`)
- `normalize` (OPTIONAL) (1×1 `logical`) `true` if transfer function should be normalized, `false` otherwise (defaults to `false`)

`Hz = c2d_euler(Hs,T,'forward')` returns the discrete transfer function `Hz` obtained by applying the forward Euler (i.e. forward difference) transformation to a continuous transfer function `Hs`, where `T` is the sampling period.
## Outputs

`Hz = c2d_euler(Hs,T,'backward')` returns the discrete transfer function `Hz` obtained by applying the backward Euler (i.e. backward difference) transformation to a continuous transfer function `Hs`, where `T` is the sampling period.
- `Hz` (1×1 `tf` or `zpk`): discrete transfer function


## Examples and Additional Documentation
Expand Down
35 changes: 26 additions & 9 deletions c2d_euler.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
% c2d_euler Transforms a continuous transfer function to a discrete
% transfer function using the forward and backward Euler methods.
%
% Hz = c2d_euler(Hs,T,'forward')
% Hz = c2d_euler(Hs,T,'backward')
% Hz = c2d_euler(Hs,T,type)
% Hz = c2d_euler(Hs,T,type,output,normalize)
%
% See also c2d.
%
% Copyright © 2021 Tamas Kis
% Last Update: 2022-09-26
% Last Update: 2023-09-17
% Website: https://tamaskis.github.io
% Contact: tamas.a.kis@outlook.com
%
Expand All @@ -25,32 +25,42 @@
% ------
% INPUT:
% ------
% Hs - (1×1 tf or zpk) continous transfer function
% T - (1×1 double) sampling period
% type - (char) 'forward' or 'backward'
% output - (OPTIONAL) (char) specifies output type ('tf' or 'zpk')
% Hs - (1×1 tf or zpk) continous transfer function
% T - (1×1 double) sampling period
% type - (char array) 'forward' or 'backward'
% output - (OPTIONAL) (char array) specifies output type ('tf' or
% 'zpk') (defaults to 'tf')
% normalize - (OPTIONAL) (1×1 logical) true if transfer function should
% be normalized, false otherwise (defaults to false)
%
% -------
% OUTPUT:
% -------
% Hz - (1×1 tf or zpk) discrete transfer function
%
%==========================================================================
function Hz = c2d_euler(Hs,T,type,output)
function Hz = c2d_euler(Hs,T,type,output,normalize)

% defaults "output" to 'tf' if not input
if (nargin < 4) || isempty(output)
output = 'tf';
end

% defaults "normalize" to false if not input
if (nargin < 5) || isempty(normalize)
normalize = false;
end

% symbolic variable for z;
z = sym('z');

% specified Euler approximation of s
if strcmpi(type,'backward')
s = (z-1)/(T*z);
else
elseif strcmpi(type,'forward')
s = (z-1)/T;
else
error("'type' must be input as 'backward' or 'forward'")
end

% converts transfer function object to symbolic function object
Expand All @@ -66,6 +76,13 @@
num = sym2poly(sym_num);
den = sym2poly(sym_den);

% normalizes coefficients w.r.t. coefficient on largest power of z in
% denominator
if normalize
num = num/den(1);
den = den/den(1);
end

% creates discrete transfer function model
Hz = tf(num,den,T);

Expand Down

0 comments on commit c85c50f

Please sign in to comment.