auto_match.py 7.28 KB
Newer Older
natsfr's avatar
natsfr committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# -*- coding: utf-8 -*-
"""
Space Baron Team
nats`

Transistor Auto Matcher

Take S parameters file input
"""

import os
import sys
import math
import cmath

16 17 18 19 20 21 22
def print_complex(name, c):
    print("====")
    print(name)
    print("Rect: %f %fj" % (c.real , c.imag))
    print("Polar: %f %fj" % (cmath.polar(c)[0] , math.degrees(cmath.polar(c)[1])))
    print("====")

23 24 25 26 27 28 29 30 31 32
# needed for test on polar
def polar_add(ar, ai, br, bi):
    a = cmath.rect(ar, math.degrees(ai))
    b = cmath.rect(br, math.degrees(bi))
    print_complex("polar res", a + b)

def pol2rec(ar, ai):
    a = cmath.rect(ar, math.radians(ai))
    return a

natsfr's avatar
merge  
natsfr committed
33
class TransistorBlock:
natsfr's avatar
natsfr committed
34
    
natsfr's avatar
merge  
natsfr committed
35 36 37 38 39
    def __init__(self, file, freq, config):
        self.freq = freq
        self.config = config
        self.spformats = {}
        self.parse_spar(file,freq)
natsfr's avatar
natsfr committed
40
    
natsfr's avatar
merge  
natsfr committed
41 42 43 44 45 46 47 48 49 50 51 52 53
    ''' Open S2P Files and extract data '''
    def parse_spar(self, file, freq):
        f = open(file, "r")
        tmpspars = []
        for line in f:
            if line.startswith("#"):
                sfor = line.strip().split()
                self.spformats["UNIT"] = sfor[1]
                self.spformats["TYPE"] = sfor[2]
                self.spformats["FORMAT"] = sfor[3]
                # Apparently the nominal Impedance is splitted: R 50
                self.spformats["R"] = sfor[5]
            elif not line.startswith("!"):
54 55 56
                tmpline = line.strip().split()
                if len(tmpline) >= 9: #We skip short line often used for noise parameter
                    tmpspars.append(tmpline)
natsfr's avatar
merge  
natsfr committed
57 58 59 60 61 62 63 64 65 66 67 68 69
        
        # Parse options: we mainly need to know unit and complex format
        # http://cp.literature.agilent.com/litweb/pdf/genesys200801/sim/linear_sim/sparams/touchstone_file_format.htms
        
        self.spars = {}# 1: Freq 2: S11 3: S21 4: S12 5: S22
        for l in tmpspars:
            if(l[0] != "!" and l[0] != "#"):
                tmp = []
                if self.spformats["FORMAT"] == "RI" : # Real Imaginary
                    tmp.append(complex(float(l[1]), float(l[2]))) # S11
                    tmp.append(complex(float(l[3]), float(l[4]))) # S21
                    tmp.append(complex(float(l[5]), float(l[6]))) # S12
                    tmp.append(complex(float(l[7]), float(l[8]))) # S22
70 71 72 73 74
                elif self.spformats["FORMAT"] == "MA" : # Magnitude Angle (in degree)
                    tmp.append(cmath.rect(float(l[1]), math.radians(float(l[2])))) # S11
                    tmp.append(cmath.rect(float(l[3]), math.radians(float(l[4])))) # S21
                    tmp.append(cmath.rect(float(l[5]), math.radians(float(l[6])))) # S12
                    tmp.append(cmath.rect(float(l[7]), math.radians(float(l[8])))) # S22
natsfr's avatar
merge  
natsfr committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
                else :
                    print("Unsupported Number Format: %s" % self.spformats["FORMAT"])
                    exit(-1)
                # We always set freq to MHz
                freq = float(l[0])
                if self.spformats["UNIT"] == "GHz":
                    freq = freq * 1000
                elif self.spformats["UNIT"] == "KHz":
                    freq = freq / 1000
                elif self.spformats["UNIT"] == "Hz":
                    freq = freq / 1000000
                self.spars[freq] = tmp

    ''' Calculate John Rollett stability factor '''
    def calc_stab(self):
        S = self.S
        
        self.K = (1 + cmath.polar(self.Ds)[0] ** 2 - cmath.polar(S[11])[0] ** 2 - \
            cmath.polar(S[22])[0] ** 2) / (2 * cmath.polar(S[21])[0] * cmath.polar(S[12])[0])
        
    ''' Caculate Maximum Available Gain '''
    def calc_mag(self):
        S = self.S
        
        self.B1 = 1 + cmath.polar(S[11])[0] ** 2 - cmath.polar(S[22])[0] ** 2 - cmath.polar(self.Ds)[0] ** 2
natsfr's avatar
natsfr committed
100

natsfr's avatar
merge  
natsfr committed
101 102 103 104 105 106
        ksq = 0.0
        if self.B1 < 0 :
            ksq = math.sqrt(self.K**2 - 1)
        else :
            ksq = math.sqrt(self.K**2 - 1) * -1
        
107
        self.MAG = 10 * math.log10(cmath.polar(S[21])[0] / cmath.polar(S[12])[0]) + \
natsfr's avatar
natsfr committed
108
            10 * math.log10(self.K + ksq)
natsfr's avatar
natsfr committed
109
    
natsfr's avatar
merge  
natsfr committed
110 111 112
    ''' Calculate Maximum Unilateral Gain (S12 = 0)'''
    def calc_gum(self):
        S = self.S
natsfr's avatar
natsfr committed
113
        self.GUM = 10 * math.log10(cmath.polar(S[21])[0]**2 / ((1-cmath.polar(S[11])[0]**2)*(1-cmath.polar(S[22])[0]**2)))
natsfr's avatar
natsfr committed
114
    
natsfr's avatar
merge  
natsfr committed
115 116
    def set_freq(self, freq):
        self.freq = freq
natsfr's avatar
natsfr committed
117
    
natsfr's avatar
merge  
natsfr committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    def calc_stability_mag(self):
        self.sparam = self.spars[self.freq]
        self.S = {}    
        self.S[11] = self.sparam[0]
        self.S[21] = self.sparam[1]
        self.S[12] = self.sparam[2]
        self.S[22] = self.sparam[3]
        
        S = self.S
        
        self.Ds = S[11] * S[22] - S[12] * S[21]
        self.calc_stab()
        
        if self.K < 1 :
            print("Warning ! K factor %f < 1.0 , amplifier could be unstable" % self.K)
            print("Undefined Maximum Available Gain")
        else :
            print("Stable amplifier design. K factor %f >= 1" % self.K)
            self.calc_mag()
            print("Maximum Available Gain: %fdB" % self.MAG)
    
    def calc_scm(self):
        S = self.S
        if self.K > 1 :
            self.C1 = S[11] - (self.Ds * complex.conjugate(S[22]))
            self.C2 = S[22] - (self.Ds * complex.conjugate(S[11]))
            # B1 is already calculated for MAG
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
            self.B2 = 1 + cmath.polar(S[22])[0] ** 2 - cmath.polar(S[11])[0] ** 2 - cmath.polar(self.Ds)[0] ** 2
            
            rad = 0
            if self.B2 >= 0:
                rad = self.B2 - math.sqrt(self.B2**2 - 4 * cmath.polar(self.C2)[0]**2)
            elif self.B2 < 0:
                rad = self.B2 + math.sqrt(self.B2**2 - 4 * cmath.polar(self.C2)[0]**2)
            
            # Load Reflection magnitude
            ref_mag = rad / (2 * cmath.polar(self.C2)[0])
            
            self.load_ref = cmath.rect(ref_mag, -cmath.polar(self.C2)[1])
            
            self.source_ref = complex.conjugate(S[11] + ((S[12]*S[21]*self.load_ref) / (1-(self.load_ref * S[22]))))
            
natsfr's avatar
merge  
natsfr committed
160 161
        else :
            print("Can't calculate SCM, K < 1, try lossy match")
natsfr's avatar
natsfr committed
162
    
163 164 165 166 167 168 169
    #Calculate Transducer gain
    def calc_gt(self):
        S = self.S
        a = ((abs(S[21])**2)*(1-abs(self.source_ref)**2)*(1-abs(self.load_ref)**2))
        b = abs((1-S[11]*self.source_ref)*(1-S[22]*self.load_ref)-S[12]*S[21]*self.load_ref*self.source_ref)**2
        self.GT = 10*math.log10(a/b)
    
natsfr's avatar
natsfr committed
170 171 172 173 174 175 176
if __name__ == '__main__':
    if(len(sys.argv) < 4):
        print("Error: usage\nauto_match.py spar_file.s2p frequency configuration\n")
        exit(-1)
    fname = sys.argv[1]
    freq = float(sys.argv[2])
    config = sys.argv[3]
natsfr's avatar
merge  
natsfr committed
177 178 179 180
    q1 = TransistorBlock(fname, freq, config)
    q1.calc_stability_mag()
    q1.calc_gum()
    print("GUM: %fdB" % q1.GUM)
181 182 183
    q1.calc_scm()
    q1.calc_gt()
    print("Result")
184 185 186
    print("Optimum Source Reflection")
    print_complex("Source Reflection", q1.source_ref)
    print("Optimum Load Reflection")
187 188
    print_complex("Load Reflection", q1.load_ref)
    print("Transducer gain: %fdB" % q1.GT)
189 190 191 192 193
    
    # test book error
    a = pol2rec(0.4,162) + ((pol2rec(0.04,60)*pol2rec(5.2,63)*pol2rec(0.487,39)) / (1 - pol2rec(0.487,39)*pol2rec(0.35,-39)))
    print_complex("test", a)
    print_complex("test", complex.conjugate(a))