Exemplo n.º 1
0
 def __init__(self, f=None, resp=None):
     """Constructor
     @param f (optional) instance of class FrequencyList
     @param resp (optional) list of complex frequency content or response
     """
     self.m_f = FrequencyList(f)
     if not resp is None:
         list.__init__(self, resp)
Exemplo n.º 2
0
 def ReadFromFile(self, fileName):
     """reads in frequency domain content from the file specified.
     @param fileName string file name to read
     @return self
     """
     with open(fileName, 'rU' if sys.version_info.major < 3 else 'r') as f:
         data = f.readlines()
     if data[0].strip('\n') != 'UnevenlySpaced':
         N = int(str(data[0]))
         Fe = float(str(data[1]))
         frl = [line.split(' ') for line in data[2:]]
         resp = [float(fr[0]) + 1j * float(fr[1]) for fr in frl]
         self.m_f = EvenlySpacedFrequencyList(Fe, N)
         list.__init__(self, resp)
     else:
         frl = [line.split(' ') for line in data[1:]]
         f = [float(fr[0]) for fr in frl]
         resp = [float(fr[1]) + 1j * float(fr[2]) for fr in frl]
         self.m_f = GenericFrequencyList(f)
         list.__init__(self, resp)
     return self
Exemplo n.º 3
0
class FrequencyDomain(list):
    """base class for frequency domain elements.  This class handles all kinds of utility things
    common to all frequency-domain classes.
    """
    def __init__(self, f=None, resp=None):
        """Constructor
        @param f (optional) instance of class FrequencyList
        @param resp (optional) list of complex frequency content or response
        """
        self.m_f = FrequencyList(f)
        if not resp is None:
            list.__init__(self, resp)

    def FrequencyList(self):
        """FrequencyList
        @return the frequency list in m_f
        """
        return self.m_f

    def Frequencies(self, unit=None):
        """Frequencies
        @param unit (optional) string containing the unit for the frequencies
        @see FrequencyList for information on valid unit strings.
        """
        return self.m_f.Frequencies(unit)

    def Values(self, unit=None):
        """Values
        @param unit (optional) string containing the unit for the frequencies
        @return list of complex values corresponding to the frequency-domain elements in the
        units specified.
        @remark
        Valid unit strings are:
        - 'dB' - values in decibels.
        - 'mag' - values in absolute magnitude.
        - 'rad' - the argument or phase in radians.
        - 'deg' - the argument or phase in degrees.
        - 'real' - the real part of the values.
        - 'imag' - the imaginary part of the values.

        Returns the list of complex values if no unit specified.
        
        Returns None if the unit is invalid.
        """
        if unit == None:
            return list(self)
        elif unit == 'dB':
            return [
                -3000. if
                (abs(self[n]) < 1e-15) else 20. * math.log10(abs(self[n]))
                for n in range(len(self.m_f))
            ]
        elif unit == 'mag':
            return [abs(self[n]) for n in range(len(self.m_f))]
        elif unit == 'rad':
            return [cmath.phase(self[n]) for n in range(len(self.m_f))]
        elif unit == 'deg':
            return [
                cmath.phase(self[n]) * 180. / math.pi
                for n in range(len(self.m_f))
            ]
        elif unit == 'real':
            return [self[n].real for n in range(len(self.m_f))]
        elif unit == 'imag':
            return [self[n].imag for n in range(len(self.m_f))]

    def ReadFromFile(self, fileName):
        """reads in frequency domain content from the file specified.
        @param fileName string file name to read
        @return self
        """
        with open(fileName, 'rU' if sys.version_info.major < 3 else 'r') as f:
            data = f.readlines()
        if data[0].strip('\n') != 'UnevenlySpaced':
            N = int(str(data[0]))
            Fe = float(str(data[1]))
            frl = [line.split(' ') for line in data[2:]]
            resp = [float(fr[0]) + 1j * float(fr[1]) for fr in frl]
            self.m_f = EvenlySpacedFrequencyList(Fe, N)
            list.__init__(self, resp)
        else:
            frl = [line.split(' ') for line in data[1:]]
            f = [float(fr[0]) for fr in frl]
            resp = [float(fr[1]) + 1j * float(fr[2]) for fr in frl]
            self.m_f = GenericFrequencyList(f)
            list.__init__(self, resp)
        return self

    def WriteToFile(self, fileName):
        """write the frequency domain content to the file specified.
        @param fileName string file name to write
        @return self
        """
        fl = self.FrequencyList()
        with open(fileName, "w") as f:
            if fl.CheckEvenlySpaced():
                f.write(str(fl.N) + '\n')
                f.write(str(fl.Fe) + '\n')
                for v in self.Values():
                    f.write(str(v.real) + ' ' + str(v.imag) + '\n')
            else:
                f.write('UnevenlySpaced\n')
                for n in range(len(fl)):
                    f.write(
                        str(fl[n]) + ' ' + str(self.Values()[n].real) + ' ' +
                        str(self.Values()[n].imag) + '\n')
        return self

    def __eq__(self, other):
        """overloads ==
        @param other an instance of a class derived from FrequencyDomain.
        @return whether self == other
        """
        if self.FrequencyList() != other.FrequencyList():
            return False  # pragma: no cover
        if len(self) != len(other):
            return False  # pragma: no cover
        for k in range(len(self)):
            if abs(self[k] - other[k]) > 1e-5:
                return False  # pragma: no cover
        return True

    def __ne__(self, other):
        """overloads !=
        @param other an instance of a class derived from FrequencyDomain.
        @return whether self != other
        """
        return not self == other
Exemplo n.º 4
0
    def __init__(self, name, Z0=None):
        """Constructor
        @param name string file name of s-parameter file to read.
        @param Z0 (optional) real or complex reference impedance desired (defaults to 50 ohms).
        Reads the s-parameter file and produces an instance of its base class SParameters.

        If the reference impedance of the Touchstone 1.0 file read is not the reference
        impedance specified, then the reference impedance of the s-parameters are converted
        to the reference impedance specified."""
        self.m_sToken = 'S'
        self.m_Z0 = Z0
        # pragma: silent exclude
        ext = str.lower(name).split('.')[-1]
        if ext == 'si':
            from SignalIntegrity.App.SignalIntegrityAppHeadless import ProjectSParameters
            sp = ProjectSParameters(name)
            if not sp is None:
                SParameters.__init__(self, sp.m_f, sp.m_d, sp.m_Z0)
                self.SetReferenceImpedance(Z0)
                return
            else:
                raise SignalIntegrityExceptionSParameterFile(
                    's-parameters could not be produced by ' + name)
        else:
            try:
                # pragma: include outdent outdent
                self.m_P = int(
                    str.lower(name).split('.')[-1].split('s')[1].split('p')[0])
            # pragma: silent exclude indent indent
            except:
                raise SignalIntegrityExceptionSParameterFile(
                    'incorrect extension in s-parameter file name in ' + name)
        # pragma: include
        freqMul = 1e6
        complexType = 'MA'
        Z0 = 50.
        sp = True
        f = []
        self.m_f = []
        numbersList = []
        # pragma: silent exclude
        try:
            # pragma: include outdent
            spfile = open(name, 'rU' if sys.version_info.major < 3 else 'r')
        # pragma: silent exclude indent
        except IOError:
            raise SignalIntegrityExceptionSParameterFile(name + ' not found')
        # pragma: include
        for line in spfile:
            lineList = str.lower(line).split('!')[0].split()
            if len(lineList) > 0:
                if lineList[0] == '#':
                    if 'hz' in lineList: freqMul = 1.0
                    if 'khz' in lineList: freqMul = 1e3
                    if 'mhz' in lineList: freqMul = 1e6
                    if 'ghz' in lineList: freqMul = 1e9
                    if 'ma' in lineList: complexType = 'MA'
                    if 'ri' in lineList: complexType = 'RI'
                    if 'db' in lineList: complexType = 'DB'
                    if 'r' in lineList:
                        Z0 = float(lineList[lineList.index('r') + 1])
                    if not self.m_sToken.lower() in lineList:
                        sp = False
                else:
                    numbersList.extend(lineList)
        spfile.close()
        if not sp: return
        if self.m_Z0 == None: self.m_Z0 = Z0
        frequencies = len(numbersList) // (1 + self.m_P * self.m_P * 2)
        P = self.m_P
        self.m_d = [empty([P, P]).tolist() for fi in range(frequencies)]
        for fi in range(frequencies):
            f.append(float(numbersList[(1 + P * P * 2) * fi]) * freqMul)
            for r in range(P):
                for c in range(P):
                    n1 = float(numbersList[(1 + P * P * 2) * fi + 1 +
                                           (r * P + c) * 2])
                    n2 = float(numbersList[(1 + P * P * 2) * fi + 1 +
                                           (r * P + c) * 2 + 1])
                    if complexType == 'RI':
                        self.m_d[fi][r][c] = n1 + 1j * n2
                    else:
                        expangle = cmath.exp(1j * math.pi / 180. * n2)
                        if complexType == 'MA':
                            self.m_d[fi][r][c] = n1 * expangle
                        elif complexType == 'DB':
                            self.m_d[fi][r][c] = math.pow(10.,
                                                          n1 / 20) * expangle
            if P == 2:
                self.m_d[fi] = array(self.m_d[fi]).transpose().tolist()
            if Z0 != self.m_Z0:
                self.m_d[fi] = ReferenceImpedance(self.m_d[fi], self.m_Z0, Z0)
        self.m_f = GenericFrequencyList(f)