⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scdraw.m

📁 This is a source code . this is a sourc c odeeg
💻 M
字号:
%scDraw : Draws a blank smith chart
%
%  SYNOPSIS:
%     When called without arguments, the function draws a standard smith chart.
%     Otherwise, one may want to draw only specific curves for r and/or x values.
%     For different calling syntaxes see under SYNTAX.
%
%     See also scRay, scMove, scConCirc, scMatchCirc : available for different tasks
%     
%     
%  SYNTAX:
%     [h] = scDraw(r, x, ChColor)
%           draws only the r and x lines required
%     [h] = scDraw
%           draws a standard smith chart
%
%     And one can suppress the secondary details like scales and labels by calling:
%     [h] = scDraw(0)
%           draws 'only' a standard smith chart without r and m scales etc.
%
%  INPUT ARGUMENTS:
%     r       : A vector consisting of the desired values of r 
%     x       : A vector consisting of the desired values of x
%     ChColor : Color of the smith chart
%     xL      : a vector of x values containing left termination points for r circles
%     xR      : a vector of x values containing right termination points for r circles
%     rL      : a vector of r values containing left termination points for x arcs
%     rR      : a vector of r values containing right termination points for r arcs
%
%  OUTPUT ARGUMENT:
%     h : figure handle. If no output argument is given, the handle is returned in
%         the workspace variable ans. 
%
%  EXAMPLES:
%     (1) scDraw;
%         will draw a blank smith chart
%
%     (2) h = scDraw;
%         will draw a blank smith chart and return the gigure handle h.
%
%     (3) scDraw(0);
%         will draw a blank smith chart without scales etc.
%
%     (4) scDraw([1 2 3 4 5],[.1 2 3 4]);
%         will draw only the r lines specified by [1 2 3 4 5] and x lines [.1 2 3 4]
%
%     Mohammad Ashfaq - (31-05-2000)
%     Mohammad Ashfaq - (13-04-2006) Modified (examples included)

function h = scDraw(r, x, ChColor, xR, xL, rR, rL)

FullMapWithLabels = 1;
if nargin==1
    FullMapWithLabels   = 0;
end
if nargin<=1
    % DEFINE STANDARD DEFAULT FOR r
    r= [0.00, 0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.50, 0.55, 0.60, 0.65,...
            0.70, 0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.10, 1.20, 1.30, 1.40, 1.50, 1.50, 1.60, 1.70,...
            1.80, 1.90, 2.00, 2.20, 2.40, 2.50, 2.60, 2.80, 3.00, 3.50, 4.00, 4.50, 5.00, 6.00, 7.00,...
            8.00, 9.00, 10.0, 15.0, 20.0, 50.0];
    xR=[   0,    1,    2,    1,    4,    1,    2,    1,    4,    1,    2,    5,     1,   4,    1,...
            2,    1,    4,    1,    2,    1,   10,    2,    4,    2,    4,    2,     5,   4,    2,...
            4,    2,   20,    2,    2,    5,    2,    2,   10,    5,   10,    5,    10,  10,   10,...
            10,   10,   20,   20,   50,   0];
    
    xL=[   0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,     4,     0,   0,    0,...
            0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,     0,     4,   0,    0,...
            0,    0,    0,    0,    0,    2,    0,    0,    0,    0,    0,     0,     0,   0,    0,...
            0,    0,    0,    0,    0,    0];
    rPrint = [0.1:0.1:1.0,1.5 2.0 3.0 4.0 5.0 10 20 50];
    
    % DEFINE STANDARD DEFAULT FOR x
    x=[0.05, 0.10, 0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50,  0.50, 0.55, 0.60, 0.65, 0.70,...
            0.75, 0.80, 0.85, 0.90, 0.95, 1.00, 1.10, 1.20, 1.30, 1.40,  1.50, 1.50, 1.60, 1.70, 1.80,...
            1.90, 2.00, 2.20, 2.40, 2.50, 2.60, 2.80, 3.00, 3.20, 3.40,  3.50, 3.60, 3.80, 4.00, 4.50,...
            5.00, 6.00, 7.00, 8.00, 9.00, 10.0, 15.0, 20.0, 50.0];
    rR=  [1,    2,    1,    3,    1,    2,    1,    3,    1,    2,     5,    1,    3,    1,    2,...
            1,    3,    1,    2,    1,   10,    2,    3,    2,    3,     2,    5,    3,    2,    3,...
            2,   10,    2,    2,    5,    2,    2,   10,    2,    2,     5,    2,    2,   10,    5,...
            10,   10,   10,   10,   10,   20,   20,   50,   0];
    
    rL=  [0,    0,    0,    0,    0,    0,    0,    0,    0,    0,     3,    0,     0,   0,    0,...
            0,    0,    0,    0,    0,    0,    0,    0,    0,    0,     0,    3,     0,   0,    0,...
            0,    0,    0,    0,    2,    0,    0,    0,    0,    0,     2,    0,     0,   0,    0,...
            0,    0,    0,    0,    0,    0,    0     0,    0];
    xPrint  = [0.1:0.1:1.0,1.5 2.0 3.0 4.0 5.0 10 20 50];
    ChColor = 'b';
end

if (nargin == 2)||(nargin ==3)
    rR = zeros(size(x));
    rL = zeros(size(x));
    xR = zeros(size(r));
    xL = zeros(size(r));
    rPrint = r;
    xPrint = x;
end
if nargin ==2 
    ChColor = 'b';
end

if isempty(get(gcf, 'name')) || nargin ==0
    h = gcf;
    set(gcf,'MenuBar','none','numbertitle','off','name','Smith Chart');
    set(gca,'position',[0.01 0.01 0.98 0.98]);
    scPresent = 0;
else
    scPresent = 1;
end


% DRAW r CIRCLES
scResCirc(r, ChColor, xR, xL);
axis equal;
axis off;


% DRAW x ARCS
scReacArc(x, 1, ChColor, rR, rL);
scReacArc(x, -1, ChColor, rR, rL);

if ~scPresent
    
    % DRAW THE OUTER CIRCLE AND THE X-AXIS
    plot([-1 1],[0 0], ChColor);
    plot(-1:.001:1, sqrt(1-(-1:.001:1).^2), ChColor);
    plot(-1:.001:1,-sqrt(1-(-1:.001:1).^2), ChColor);
    
    % PRINT r VALUES
    scPrnVal(rPrint, 'r');
    
    % PRINT x VALUES
    scPrnVal(xPrint, 'x');
    
    % DRAW THE ANGLE BOUNDARY
    scConCirc(1.01,'r');
    scConCirc(1.08,'r');
    scAngles(1.01, 1.08, 'r');
    
    % DRAW THE LENGTH BOUNDARY
    scConCirc(1.085,'m');
    scConCirc(1.16,'m');
    scLength(1.085, 1.16, 'm');
    
    if FullMapWithLabels
        scScales;
        scLabels;
    end
end

function scResCirc(r, LinCol, xR, xL)
% scResCirc: draws r circles in the smith chart
%
%  SYNOPSIS:
%     This fuction draws r circles which have their centers on the real axis and
%     the radius is always such that they pass through the point (1,0).
%     The function is called by scDraw to do so. 
%     
%  SYNTAX:
%     scResCirc(r, LinCol, xR, xL)
%
%  INPUT ARGUMENTS:
%     r      : a vector consisting of the desired values of r 
%     LinCol : desired color of the circle(s)
%     xR     : a vector containing right termination points for r circles
%     xL     : a vector containing left termination points for r circles
%
%  OUTPUT ARGUMENT:
%     none

% PLOT CIRCLES
for ii = 1:length(r)
    rc = r(ii);
    if xR(ii)~=0
        xc1 = xR(ii);
        [xco1, yco1] = scPOI(rc, xc1);
    else
        xco1 = 1;
    end
    
    if xL(ii)~=0
        [xco2, yco2] = scPOI(rc, xL(ii));
    else
        xco2 = 20;
    end
    if xco1 < xco2
        u      = linspace((rc-1)/(rc+1), xco1, 500);
        vplus  = sqrt((1/(1+rc))^2-(u-rc/(1+rc)).^2);
        vminus = (-sqrt((1/(1+rc))^2-(u-rc/(1+rc)).^2));
        plot(real(u),real(vplus),LinCol);
        hold on;
        plot(real(u),real(vminus),LinCol);
    end
    if xco2 ~= 20
        u      = linspace(xco2, xco1, 200);
        vplus  = sqrt((1/(1+rc))^2-(u-rc/(1+rc)).^2);
        vminus = (-sqrt((1/(1+rc))^2-(u-rc/(1+rc)).^2));
        plot(real(u),real(vplus),LinCol);
        hold on;
        plot(real(u),real(vminus),LinCol);
    end
end

function scReacArc(x,updown,LinCol,rR, rL)
%scReacArc: draws x arcs in the smith chart
%
%  SYNOPSIS:
%     This fuction draws x arcs which have their centers on the the line parallel
%     to the imaginary axis and passing through the point (1,0).
%
%     The function is called by scDraw to do so. 
%     
%  SYNTAX:
%     scReacArc(x, updown, LinCol, rR, rL)
%
%  INPUT ARGUMENTS:
%     x     : a vector consisting of the desired values of x
%     updown: draw arc in the upper(lower) half plane if positive(negative)
%     LinCol: desired color of the arc
%     rR    : a vector containing left termination points for x arcs
%     rL    : a vector containing right termination points for x arcs
%
%  OUTPUT ARGUMENT:
%     none

for jj=1:length(x)
    xc     = x(jj);
    if rR(jj)~=0
        rc1 = rR(jj);
        [xco1, yco1] = scPOI(rR(jj), xc);
    else
        xco1 = 1;
        yco1 = 0;
    end
    
    if rL(jj)~=0
        [xco2, yco2] = scPOI(rL(jj), xc);
    else
        xco2 = 20;
        yco2 = 0;
    end
    if xco1 < xco2 && xc~=0
        % FOR THE ARCS STARTING FROM THE OUTERMOST CIRCLE
        if xc <= 1
            % IF THE 9'O CLOCK POS. OF THE X ARC DOES NOT LIE WITHIN THE SMITH CHART
            uup = [];
            vup = [];
            udn = linspace((xc^2-1)/(xc^2+1), xco1, 500);
            vdn = sign(updown)*(1/xc-sqrt((1/xc)^2-(udn-1).^2));
            
        elseif (1/xc-sqrt((1/xc)^2-(1-1/xc-1).^2)) < yco1
            % IF THE 9'O CLOCK POS. OF THE X ARC  IS LOWER THAN POINT OF INTERSECTION
            uup = linspace(xco1,(xc^2-1)/(xc^2+1),100);
            vup = sign(updown)*(1/xc+sqrt(1/xc^2-(uup-1).^2));
            udn = [];
            vdn = [];
        else
            % IF THE 9'O CLOCK POS. OF THE X ARC  IS HIGHER THAN POINT OF INTERSECTION
            uup = linspace(1-1/xc,(xc^2-1)/(xc^2+1),100);
            vup = sign(updown)*(1/xc+sqrt(1/xc^2-(uup-1).^2));
            udn = linspace(1-1/xc,xco1,500);
            vdn = sign(updown)*(1/xc-sqrt((1/xc)^2-(udn-1).^2));
        end
    end
    if xco2~=20 && xc~=0
        if (1/xc-sqrt((1/xc)^2-(1-1/xc-1).^2)) > yco2
            uup = [];
            vup = [];
            udn = linspace(xco2, xco1, 500);
            vdn = sign(updown)*(1/xc-sqrt((1/xc)^2-(udn-1).^2));
        else
            % IF THE 9'O CLOCK POS. OF THE X ARC  IS LOWER THAN POINT OF INTERSECTION
            uup = linspace(1-1/xc,xco2,100);
            vup = sign(updown)*(1/xc+sqrt(1/xc^2-(uup-1).^2));
            udn = linspace(1-1/xc,xco1,500);
            vdn = sign(updown)*(1/xc-sqrt((1/xc)^2-(udn-1).^2));
        end
    end
    if xc~=0
        plot(real(uup),real(vup),LinCol);
        plot(real(udn),real(vdn),LinCol);
    end
end

function scPrnVal(vPrn, rORx)
%scPrnVal: Prints x and r values on the smith chart
%
%  SYNOPSIS:
%     This fuction prints x and r values on the smith chart.
%
%     THIS function is called internally from the function scDraw.
%     
%  SYNTAX:
%     scPrnVal(vPrn, rORx)
%
%  INPUT ARGUMENTS:
%     vPrn  : value to be printed
%     rORx  : whether an 'r' or an 'x' value is to be printed.
%
%  OUTPUT ARGUMENT:
%     none

if rORx == 'x'
    % PRINT x VALUES
    for jj = 1 : length(vPrn)
        xc   = vPrn(jj);
        [xco1, yco1] = scPOI(0, xc);
        [xco2, yco2] = scPOI(1, xc);
        
        if floor(xc)==xc && length(num2str(floor(xc)))>=2
            PrStr='%0.0f';
        else
            PrStr='%0.1f';
        end
        
        h = text (min(xco1), max(yco1), num2str(xc,PrStr));
        set(h,'color','r', 'rotation', 180*atan2(1-min(xco1), max(yco1)-abs(1/xc))/pi, 'fontsize', 4.5, 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
        
        h = text (min(xco1), -max(yco1), num2str(xc, PrStr));
        set(h,'color','r', 'rotation', 180+180*atan2(-1+min(xco1), max(yco1)-abs(1/xc))/pi, 'fontsize', 4.5, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');
        if xc <=1
            h = text (min(xco2), -max(yco2), num2str(xc, PrStr));
            set(h,'color','r', 'rotation', 180+180*atan2(-1+min(xco2), max(yco2-abs(1/xc)))/pi, 'fontsize', 4.5, 'HorizontalAlignment', 'left', 'VerticalAlignment', 'bottom');
            
            h = text (min(xco2), max(yco2), num2str(xc, PrStr));
            set(h,'color','r', 'rotation', 180*atan2(1-min(xco2), max(yco2)-abs(1/xc))/pi, 'fontsize', 4.5, 'HorizontalAlignment', 'right', 'VerticalAlignment', 'bottom');
        end
    end
elseif rORx == 'r'
    % PRINT r VALUES
    for jj = 1 : length(vPrn)
        rc   = vPrn(jj);
        xco1 = (rc-1)/(rc+1);
        yco1 = 0.01;
        [xco2, yco2] = scPOI(rc, 1);
        if floor(rc)==rc && length(num2str(floor(rc)))>=2
            PrStr='%0.0f';
        else
            PrStr='%0.1f';
        end
        h = text (xco1, yco1, num2str(rc,PrStr));
        set(h,'color','r','rotation',90,'fontsize',4.5,'HorizontalAlignment','left', 'VerticalAlignment', 'bottom');
        if rc <= 1
            h = text (xco2, yco2, num2str(rc,PrStr));
            set(h,'color','r','rotation',180*atan2(rc/(1+rc)-xco2, yco2)/pi, 'fontsize',4.5,'HorizontalAlignment','left', 'VerticalAlignment', 'bottom');
            h = text (xco2, -yco2, num2str(rc,PrStr));
            set(h,'color','r','rotation', 180+180*atan2(rc/(1+rc)-xco2, -yco2)/pi, 'fontsize',4.5,'HorizontalAlignment','left', 'VerticalAlignment', 'bottom');
        end
    end
else
    error('scPrnVal.m: The second argument must either be r or x');
end

function scAngles(inr, outr, color)
%scAngles: subdivides and shows angles on smith chart boundary
%
%  SYNOPSIS:
%     Marks angles on the outer periphery of the smith chart. The function is called
%     by scDraw to do so. May not produce desired results if called from the matlab
%     command prompt.
%
%     see also scDraw, scLength, sc
%     
%  SYNTAX:
%     scAngles(inr, outr, color)
%
%  INPUT ARGUMENTS:
%     inr   : radiur of the circle
%     outr  : radiur of the circle
%     color : desired color of the circle
%
%  OUTPUT ARGUMENT:
%     none

for ii=0:2:360
    if floor(ii/10) ~= ii/10
        plot([inr*cos(pi*ii/180), inr*1.015*cos(pi*ii/180)], [inr*sin(pi*ii/180), inr*1.015*sin(pi*ii/180)], 'k');
    else
        plot([inr*cos(pi*ii/180), inr*1.025*cos(pi*ii/180)], [inr*sin(pi*ii/180), inr*1.025*sin(pi*ii/180)], color);
        if ii~=360
            degstr = [num2str(ii),'

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -