]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/bodemag.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / bodemag.m
1 ## Copyright (C) 2009, 2010, 2011, 2012   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {[@var{mag}, @var{w}] =} bodemag (@var{sys})
20 ## @deftypefnx {Function File} {[@var{mag}, @var{w}] =} bodemag (@var{sys}, @var{w})
21 ## Bode magnitude diagram of frequency response.  If no output arguments are given,
22 ## the response is printed on the screen.
23 ##
24 ## @strong{Inputs}
25 ## @table @var
26 ## @item sys
27 ## LTI system.  Must be a single-input and single-output (SISO) system.
28 ## @item w
29 ## Optional vector of frequency values.  If @var{w} is not specified,
30 ## it is calculated by the zeros and poles of the system.
31 ## Alternatively, the cell @code{@{wmin, wmax@}} specifies a frequency range,
32 ## where @var{wmin} and @var{wmax} denote minimum and maximum frequencies
33 ## in rad/s.
34 ## @end table
35 ##
36 ## @strong{Outputs}
37 ## @table @var
38 ## @item mag
39 ## Vector of magnitude.  Has length of frequency vector @var{w}.
40 ## @item w
41 ## Vector of frequency values used.
42 ## @end table
43 ##
44 ## @seealso{bode, nichols, nyquist, sigma}
45 ## @end deftypefn
46
47 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
48 ## Created: November 2009
49 ## Version: 0.4
50
51 function [mag_r, w_r] = bodemag (sys, w = [])
52
53   ## TODO: multiplot feature:   bodemag (sys1, "b", sys2, "r", ...)
54
55   if (nargin == 0 || nargin > 2)
56     print_usage ();
57   endif
58
59   [H, w] = __frequency_response__ (sys, w, false, 0, "std");
60
61   H = reshape (H, [], 1);
62   mag = abs (H);
63
64   if (! nargout)
65     mag_db = 20 * log10 (mag);
66
67     if (isct (sys))
68       xl_str = "Frequency [rad/s]";
69     else
70       xl_str = sprintf ("Frequency [rad/s]     w_N = %g", pi / get (sys, "tsam"));
71     endif
72
73     semilogx (w, mag_db)
74     axis ("tight")
75     ylim (__axis_margin__ (ylim))
76     grid ("on")
77     title (["Bode Magnitude Diagram of ", inputname(1)])
78     xlabel (xl_str)
79     ylabel ("Magnitude [dB]")
80   else
81     mag_r = mag;
82     w_r = w;
83   endif
84
85 endfunction