Friday, May 8, 2015

Smith Chart and MATLAB Code to Generate

To help me grasp the Smith chart (see Antenna-Theory.com's explanation) I coded up the generation of one in MATLAB. See chart below for illustration of the chart with some explanation and some example moves. 

Code is after the break - it hasn't been cleaned up so apologies for the sparse comments. I used a convenient MATLAB library by Adam Danz for labeling points (labelpoints)

Note that where I say "Constant Reflectance Coefficient" it's technically only the magnitude of the complex value that's constant along the line.






% Draw a Custom Smith Chart
polar(0,0);
hold on;
% Hide axis and labels
l_dotted=findall(gcf,'Type','line','LineStyle',':');
text=findall(gcf,'Type','text');
set(l_dotted,'Visible','off');
set(text,'Visible','off');

numPoints=1000;

% For Legend
legendHandles=[0,0,0];

% Plot Reflectance Coefficient Family (Magnitudes)
gToPlot=[.25,.5,.75];

for i=gToPlot
    G=i;
    phases=linspace(0,2*pi,numPoints);
    magnitudes=ones(1,numPoints).*G;
   
    % Remove magnitudes outside normalized region
    idxFilter=find(magnitudes<=1);
    magnitudes=magnitudes(idxFilter);
    phases=phases(idxFilter);
   
    % Plot
    legendHandles(1)=polar(phases,magnitudes,'--g');
   
    % Label Constant Reflectance Coefficient Line
    xpos=0;
    ypos=i;
    label=strcat('\fontsize{8}',num2str(i));
    position='center';
    labelpoints(xpos,ypos,label,position,0.1);
end

% Constant Resistance Impedances
rToPlot=[.1, .2, .3, .4, .5, .6, .7, .8, .9, 1, 2, 3, 4, 5, 10, 20];

for i=rToPlot
    resistance=i;
    reals=ones(1,numPoints).*resistance;
    imags=linspace(-2*pi,2*pi,numPoints);
    Zl=complex(reals,imags);
    G=(Zl-1)./(Zl+1);
    magnitudes=abs(G);
    phases=angle(G);
   
    % Remove magnitudes outside normalized region
    idxFilter=find(magnitudes<=1);
    magnitudes=magnitudes(idxFilter);
    phases=phases(idxFilter);
   
    % Plot
    legendHandles(2)=polar(phases,magnitudes);
   
    % Label Constant Resistance Line
    xpos=(i-1)/(i+1);
    ypos=0i;
    label=strcat('\fontsize{8}',num2str(i));
    position='center';
    labelpoints(xpos,ypos,label,position,0.1);
end

% Constant Reactance Impedences
xToPlot=-2:.1:2;

for i=xToPlot
    reactance=i;
    imags=ones(1,numPoints).*reactance;
    reals=linspace(-20,20,numPoints);
    Zl=complex(reals,imags);
    G=(Zl-1)./(Zl+1);
    magnitudes=abs(G);
    phases=angle(G);
   
    % Remove magnitudes outside normalized region
    idxFilter=find(magnitudes<=1);
    magnitudes=magnitudes(idxFilter);
    phases=phases(idxFilter);
   
    % Plot
    legendHandles(3)=polar(phases,magnitudes,':r');
   
    % Label Constant Reactance Line
    maxMagIndex=find(magnitudes==max(magnitudes));
    [xpos,ypos]=pol2cart(phases(maxMagIndex),magnitudes(maxMagIndex));
    label=strcat('\fontsize{8}',num2str(i),'i');
    position='center';
    labelpoints(xpos,ypos,label,position,0.1);
end

% Label Short Circuit Condition
xpos=-1;
ypos=0i;
label='\fontsize{8}Short Circuit \newline Condition';
position='W';
labelpoints(xpos,ypos,label,position,0.1);
label='\fontsize{14}•';
position='center';
labelpoints(xpos,ypos,label,position);

% Label Open Circuit Condition
xpos=1;
ypos=0i;
label='\fontsize{8}Open Circuit \newline Condition';
position='E';
labelpoints(xpos,ypos,label,position,0.1);
label='\fontsize{14}•';
position='center';
labelpoints(xpos,ypos,label,position);

% Label Matched Impedance Condition
xpos=0;
ypos=0i;
label='\fontsize{8}Matched \newline Impedance';
position='N';
labelpoints(xpos,ypos,label,position,0.1);
label='\fontsize{14}•';
position='center';
labelpoints(xpos,ypos,label,position);

% Label Legend
% Get one handle for each type
legendLabels={'\fontsize{8}Constant Reflectance Coefficient Along Line','\fontsize{8}Constant Resistance Along Line','\fontsize{8}Constant Reactance Along Line'};
legend(legendHandles,legendLabels','Location','SouthEastOutside');

% Create an anchor point to be this textbox's location
explaAnchor = uipanel('Units','Normalized','Position',[0 1 1 1]);

% Create a uicontrol textbox with anchor as parent for some explanation
expla = uicontrol('style','text','Parent',explaAnchor);
explaText= {' Reflectance Coefficient is the ratio of reflected electric field strength to incident field strength',...
' Reflectance Coefficient G, Load Impedance ZL, Transmission Line Characteristic Impedance Z0 are related as follows:',...
'   G=(ZL-Z0)/(ZL+Z0) -> G/Z0=(ZL-1)/(ZL+1)',...
' All values are therefore normalized to a characteristic impedance Z0',...
'   actual impedance (both resistance and reactance) should be scaled before/after Smith chart analysis',...
' Note: Moving a point around the smith chart may only apply for one given frequency',...
};
set(expla,'String',explaText);

% Set XY location (relative to parent) (Move Text Box) and autosize to fit using extent
explaExtent = get(expla,'Extent');
explaPosition = [0 -explaExtent(4) explaExtent(3) explaExtent(4)]; % [x y length height]
set(expla,'Position',explaPosition);
set(expla,'Units','pixels');
set(expla,'HorizontalAlignment','left');

% Create a uicontrol textbox for instructions on analysis
textBox = uicontrol('style','text');
textBoxText= {' How to Move Around the Smith Chart:',...
' • Add a Resistance -> Shift along Constant Reactance Line',...
' • Add a Reactance -> Shift along Constant Resistance Line',...
' • Move Along Transmission Line -> Shift(Rotate) along Constant Reflectance Coefficient Line',...
'    - 1/2 wavelength along transmission line is 360° rotation',...
'    - Movement towards the load is counter-clockwise',...
'    - Movement away from the load is clockwise',...
' • Convert an Impedance to an Admittance -> Reflect to same Constant Reflectance Coefficient line across origin',...
' • Convert an Admittance to an Impedance -> Reflect to same Constant Reflectance Coefficient line across origin',...
};
set(textBox,'String',textBoxText);

% Autosize to fit using extent and set location
textBoxExtent = get(textBox,'Extent');
textBoxPosition = [0 0 textBoxExtent(3) textBoxExtent(4)]; % [x y length height]
set(textBox,'Position',textBoxPosition);
set(textBox,'HorizontalAlignment','left');

hold off;


No comments:

Post a Comment