Monday, March 07, 2016

MATLAB Smith Charts

Last time, I posted a procedure for creating Smith charts by hand.


This time, I’d like to continue the process by showing my procedure for using Matlab® or GNU Octave to create these charts.


I should note that Matlab® already has a “smithchart” function, that does a pretty good job. But if you’d like to customize the chart or do it in GNU Octave, this process might be better. Matlab®’s function will plot a reflection coefficient for you, while the script presented here does not.


Matlab® or Octave, the smithchart function or script doesn’t do the \(Z\) to \(\Gamma\) conversion for you. The function simply draws the circles on in Cartesian coordinates. You still have to convert an impedance to a reflection coefficient.


$$\Gamma = \frac{Z - Z_0}{Z + Z_0}$$


where is the basis impedance for the chart.

The real and imaginary part of the reflection coefficient will be the and coordinates that to plot.

Zero reactance line

The zero reactance line is the axis. We just draw a line from -1 to 1.


x = linspace (-1, 1, 256);
y = zeros (1, length (x));

plot (x, y,'k');

% We don't show the axis, but we need to scale it.
axis ([-1.15, 1.15, -1.15, 1.15]);
axis off;

hold on


Zero Reactance Line




Resistance Circles



We will not plot complete circles for all resistances. This will lead to a very cluttered chart on the right hand side. Instead we will start and stop them at strategic locations on the chart.


The complex impedance can be broken down into


$$Z=A+jB$


Where is the resistance and is the reactance. In a Smith Chart, all impedances are normalized to one. The reflection coefficients for the circles become



A = [.2 .5 1 2]; Blim = [1 2 5 2];

for idx = 1:length(A);
    B = linspace (-Blim(idx), Blim(idx), 256);
    rho = (A(idx)^2 + B.^2 - 1 + 2i * B) ./ ...
      ((A(idx) + 1).^2 + B.^2);

    plot (rho, 'k');
end


Resistance Circles


Some resistances can have full circles, namely, the -, -, and circles. However, we will never be able to complete the circles using the technique above. The right hand side of the chart is infinity.

Instead, we just make Cartesian circles at those points. It’s easier and saves memory.


theta = linspace (0, 2 * pi, 256); A = [0 5 10];

for idx = 1:length (A)
    cx = A(idx) ./ (A(idx) + 1);
    rx = 1 ./ (A(idx) + 1);

    plot (rx * cos (theta) + cx, rx * sin (theta), 'k');
end

Full resistance circles at 0, 5, and 10 ohms.


Constant Reactance

Like resistance, we don’t draw complete circles for the reactances. They start at and end at various resistance circles in the chart. The technique is the same, but we don’t have any reactances that go all the way to the side of the chart.


Alim = [.5 .5 1 1 2 2 5 5 10 10]; % Resistance endpoints
                                  % for each reactance
                                  % line
B = [-.2 .2 -.5 .5 -1 1 -2 2 -5 5];

for idx = 1:length (B)
    A = linspace (0, Alim(idx), 256);
    rho = (A.^2 + B(idx)^2 - 1 + 2i * B(idx)) ./ ...
      ((A + 1).^2 + B(idx)^2);
    plot (rho, 'k');
end
Reactance Arcs


Resistance and Reactance Markings

% Resistance markings are on the zero-reactance line.
A = [0.2 0.5 1 2 5 10]; B = zeros (1, length (A));
rho = (A.^2 + B.^2 - 1 + 2i * B) ./ ((A + 1).^2 + B.^2);

% Offset the text slightly
xoffset = [0.1 0.1 0.05 0.05 0.05 0.075]; 
yoffset = -0.03;

for idx = 1:length (A)
    text (real (rho(idx)) - xoffset(idx), ...
        imag (rho(idx)) - yoffset, num2str (A(idx)));
end

% Let's put the reactance markings outside of the
% zero-resistance circle.
A = [-0.075 -0.075 -0.075 -.1 -0.125];
rho = (A.^2 + B.^2 - 1 + 2i * B) ./ ((A + 1).^2 + B.^2);

for idx = 1:length (B)
    text (real (rho(idx)), imag (rho(idx)), ...
        strcat ('j', num2str (B(idx))));
    text (real (rho(idx)), -imag (rho(idx)), ...
        strcat ('-j', num2str (B(idx)))); end

% Zero for resistance and reactance. 
rho = (-0.05.^2 + 0.^2 - 1) ./ ((-0.05 + 1).^2 + 0.^2);

text (real (rho), imag (rho), '0');

hold off;
Resistance and Reactance Markings


Plotting on the Chart

To plot a value on the chart, calculate the reflection coefficient for the point and use Matlab’s plot function after issuing the hold on command.