1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) 2011 - 2016 - INRIA - Serge Steer
3// Copyright (C) 2012 - 2016 - Scilab Enterprises
4//
5// This file is hereby licensed under the terms of the GNU GPL v2.0,
6// pursuant to article 5.3.4 of the CeCILL v.2.1.
7// This file was originally licensed under the terms of the CeCILL v2.1,
8// and continues to be available under such terms.
9// For more information, see the COPYING file which you should have received
10// along with this program.
11
12function []=phaseplot(varargin)
13    rhs=size(varargin)
14    if type(varargin($))==10 then
15        comments=varargin($);
16        rhs=rhs-1;
17    else
18        comments=[];
19    end
20    fname="phaseplot";//for error messages
21
22    fmax=[];
23    if or(typeof(varargin(1))==["state-space" "rational" "zpk"]) then
24        //sys,fmin,fmax [,pas] or sys,frq
25        refdim=1 //for error message
26        if rhs==1 then
27            [frq,repf]=repfreq(varargin(1),1d-3,1d3);
28        elseif rhs==2 then //sys,frq
29            if size(varargin(2),2)<2 then
30                error(msprintf(_("%s: Wrong size for input argument #%d: A row vector with length>%d expected.\n"),..
31                fname,2,1));
32            end
33            [frq,repf]=repfreq(varargin(1:rhs));
34        elseif or(rhs==(3:4)) then //sys,fmin,fmax [,pas]
35            [frq,repf]=repfreq(varargin(1:rhs));
36        else
37            error(msprintf(_("%s: Wrong number of input arguments: %d to %d expected.\n"),fname,1,5))
38        end
39        [phi,d]=phasemag(repf);
40    elseif  type(varargin(1))==1 then
41        //frq,db,phi [,comments] or frq, repf [,comments]
42        refdim=2
43        select rhs
44        case 2 then //frq,repf
45            frq=varargin(1);
46            if size(frq,2)<2 then
47                error(msprintf(_("%s: Wrong size for input argument #%d: A row vector with length>%d expected.\n"),..
48                fname,1,1))
49            end
50            if size(frq,2)<>size(varargin(2),2) then
51                error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same column dimensions expected.\n"),..
52                fname,1,2))
53            end
54
55            [phi,d]=phasemag(varargin(2))
56        case 3 then  //frq,db,phi
57            [frq,d,phi]=varargin(1:rhs)
58            if size(frq,2)<>size(d,2) then
59                error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same column dimensions expected.\n"),..
60                fname,1,2))
61            end
62        else
63            error(msprintf(_("%s: Wrong number of input arguments: %d to %d expected.\n"),fname,2,4))
64        end
65    else
66        ierr=execstr("%"+typeof(varargin(1),"overload")+"_phaseplot(varargin(:))","errcatch")
67        if ierr<>0 then
68            error(msprintf(_("%s: Wrong type for input argument #%d: Linear dynamical system or row vector of floats expected.\n"),fname,1))
69        end
70        return
71    end;
72
73    frq=frq';
74    phi=phi';
75    [n,mn]=size(phi);
76    if and(size(comments,"*")<>[0 mn]) then
77        error(msprintf(_("%s: Incompatible input arguments #%d and #%d: Same number of elements expected.\n"),...
78        fname,refdim,rhs+1))
79    end
80
81    //
82    fig=gcf();
83    id=fig.immediate_drawing;
84    fig.immediate_drawing="off";
85
86    axes = gca() ;
87    if size(axes.children,"*")==0 then
88        axes.data_bounds=[min(frq),min(phi);max(frq),max(phi)]
89        axes.x_label.text=_("Frequency (Hz)")
90        axes.y_label.text=_("Phase (Deg)")
91
92    else
93        axes.data_bounds=[min([min(frq),min(phi)],axes.data_bounds(1,:));
94        max([max(frq),max(phi)],axes.data_bounds(2,:))];
95    end
96    axes.axes_visible="on";
97    axes.log_flags = "lnn" ;
98    axes.grid=color("lightgrey")*ones(1,3);
99
100    if size(phi,2)>1&size(frq,2)==1 then
101        xpolys(frq(:,ones(1,mn)),phi,1:mn)
102        e=gce();
103    else
104        xpolys(frq,phi,1:mn)
105        e=gce();
106    end
107    for i=1:size(e.children,"*")
108        e.children(i).display_function = "formatPhaseplotTip";
109    end
110    if comments<>[] then
111        legend(comments)
112    end
113    fig.immediate_drawing=id;
114endfunction
115