Beispiel #1
0
def format_complex_imp(z: complex, allow_negative: bool = False) -> str:
    fmt_re = FMT_COMPLEX
    if allow_negative:
        fmt_re = FMT_COMPLEX_NEG
    re = SITools.Value(z.real, fmt=fmt_re)
    im = SITools.Value(abs(z.imag), fmt=FMT_COMPLEX)
    return f"{re}{'-' if z.imag < 0 else '+'}j{im} ""\N{OHM SIGN}"
Beispiel #2
0
def format_complex_adm(z: complex, allow_negative: bool = False) -> str:
    if z == 0:
        return "- S"
    adm = 1 / z

    fmt_re = FMT_COMPLEX_NEG if allow_negative else FMT_COMPLEX
    re = SITools.Value(adm.real, fmt=fmt_re)
    im = SITools.Value(abs(adm.imag), fmt=FMT_COMPLEX)
    return f"{re}{'-' if adm.imag < 0 else '+'}j{im} S"
Beispiel #3
0
def parse_value(val: str, unit: str = "",
                fmt: SITools.Format = FMT_PARSE_VALUE) -> int:
    try:
        val.replace(',', '.')
        return float(SITools.Value(val, unit, fmt))
    except (ValueError, IndexError):
        return 0.0
Beispiel #4
0
def format_group_delay(val: float) -> str:
    return str(SITools.Value(val, "s", FMT_GROUP_DELAY))
Beispiel #5
0
def format_frequency(freq: float, fmt=FMT_FREQ) -> str:
    return str(SITools.Value(freq, "Hz", fmt))
Beispiel #6
0
def format_frequency_sweep(freq: Number) -> str:
    return str(SITools.Value(freq, "Hz", FMT_FREQ_SWEEP))
Beispiel #7
0
def format_frequency_chart_2(freq: Number) -> str:
    return str(SITools.Value(freq, "", FMT_FREQ))
Beispiel #8
0
def format_frequency_inputs(freq: float) -> str:
    return str(SITools.Value(freq, "Hz", FMT_FREQ_INPUTS))
Beispiel #9
0
def parse_frequency(freq: str) -> int:
    try:
        return int(SITools.Value(freq, "Hz", FMT_PARSE))
    except (ValueError, IndexError):
        return -1
Beispiel #10
0
def format_q_factor(val: float) -> str:
    if 0 > val or val > 10000.0:
        return "\N{INFINITY}"
    return str(SITools.Value(val, fmt=FMT_Q_FACTOR))
Beispiel #11
0
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
import math
from typing import List

from PyQt5 import QtGui, QtWidgets, QtCore
from PyQt5.QtCore import pyqtSignal

from NanoVNASaver import SITools
from NanoVNASaver.RFTools import Datapoint, RFTools, groupDelay

FMT_Q_FACTOR = SITools.Format(max_nr_digits=4, assume_infinity=False,
                              min_offset=0, max_offset=0, allow_strip=True)


def format_q_factor(val: float) -> str:
    if 0 > val or val > 10000.0:
        return "\N{INFINITY}"
    return str(SITools.Value(val, fmt=FMT_Q_FACTOR))


class Marker(QtCore.QObject):
    name = "Marker"
    frequency = 0
    color: QtGui.QColor = QtGui.QColor()
    coloredText = True
    location = -1
Beispiel #12
0
def format_complex_imp(z: complex) -> str:
    re = SITools.Value(z.real, fmt=FMT_COMPLEX)
    im = SITools.Value(abs(z.imag), fmt=FMT_COMPLEX)
    return f"{re}{'-' if z.imag < 0 else '+'}j{im} \N{OHM SIGN}"
Beispiel #13
0
def format_resistance(val: float) -> str:
    if val < 0:
        return "- \N{OHM SIGN}"
    return str(SITools.Value(val, "\N{OHM SIGN}", FMT_REACT))
Beispiel #14
0
def format_wavelength(length: Number) -> str:
    return str(SITools.Value(length, "m", FMT_WAVELENGTH))
Beispiel #15
0
    def updateLabels(self, s11data: List[Datapoint], s21data: List[Datapoint]):
        if self.location == -1:
            return
        s11 = s11data[self.location]
        if s21data:
            s21 = s21data[self.location]
        imp = s11.impedance()
        re50, im50 = imp.real, imp.imag
        vswr = s11.vswr
        if re50 > 0:
            rp = (re50 ** 2 + im50 ** 2) / re50
            rp = round(rp, 3 - max(0, math.floor(math.log10(abs(rp)))))
            if rp > 10000:
                rpstr = str(round(rp/1000, 2)) + "k"
            elif rp > 1000:
                rpstr = str(round(rp))
            else:
                rpstr = str(rp)

            re50 = round(re50, 3 - max(0, math.floor(math.log10(abs(re50)))))
            if re50 > 10000:
                re50str = str(round(re50/1000, 2)) + "k"
            elif re50 > 1000:
                re50str = str(round(re50))  # Remove the ".0"
            else:
                re50str = str(re50)
        else:
            rpstr = "-"
            re50 = 0
            re50str = "-"

        if im50 != 0:
            xp = (re50 ** 2 + im50 ** 2) / im50
            xp = round(xp, 3 - max(0, math.floor(math.log10(abs(xp)))))
            xpcstr = RFTools.capacitanceEquivalent(xp, s11data[self.location].freq)
            xplstr = RFTools.inductanceEquivalent(xp, s11data[self.location].freq)
            if xp < 0:
                xpstr = xpcstr
                xp50str = " -j" + str(-1 * xp)
            else:
                xpstr = xplstr
                xp50str = " +j" + str(xp)
            xp50str += " \N{OHM SIGN}"
        else:
            xp50str = " +j ? \N{OHM SIGN}"
            xpstr = xpcstr = xplstr = "-"

        if im50 != 0:
            im50 = round(im50, 3 - max(0, math.floor(math.log10(abs(im50)))))

        if im50 < 0:
            im50str = " -j" + str(-1 * im50)
        else:
            im50str = " +j" + str(im50)
        im50str += " \N{OHM SIGN}"

        self.frequency_label.setText(RFTools.formatFrequency(s11data[self.location].freq))
        self.impedance_label.setText(re50str + im50str)
        self.admittance_label.setText(rpstr + xp50str)
        self.series_r_label.setText(re50str + " \N{OHM SIGN}")
        self.parallel_r_label.setText(rpstr + " \N{OHM SIGN}")
        self.parallel_x_label.setText(xpstr)
        if self.returnloss_is_positive:
            returnloss = -round(s11data[self.location].gain, 3)
        else:
            returnloss = round(s11data[self.location].gain, 3)
        self.returnloss_label.setText(str(returnloss) + " dB")
        capacitance = RFTools.capacitanceEquivalent(im50, s11data[self.location].freq)
        inductance = RFTools.inductanceEquivalent(im50, s11data[self.location].freq)
        self.inductance_label.setText(inductance)
        self.capacitance_label.setText(capacitance)
        self.parallel_c_label.setText(xpcstr)
        self.parallel_l_label.setText(xplstr)
        if im50 > 0:
            self.series_lc_label.setText(inductance)
        else:
            self.series_lc_label.setText(capacitance)
        vswr = round(vswr, 3)
        if vswr < 0:
            vswr = "-"
        self.vswr_label.setText(str(vswr))
        q = s11data[self.location].qFactor()
        self.quality_factor_label.setText(format_q_factor(q))
        self.s11_phase_label.setText(
            str(round(math.degrees(s11data[self.location].phase), 2)) + "\N{DEGREE SIGN}")
        fmt = SITools.Format(max_nr_digits=5, space_str=" ")
        self.s11_group_delay_label.setText(str(SITools.Value(groupDelay(s11data, self.location), "s", fmt)))

        if len(s21data) == len(s11data):
            self.gain_label.setText(str(round(s21data[self.location].gain, 3)) + " dB")
            self.s21_phase_label.setText(
                str(round(math.degrees(s21data[self.location].phase), 2)) + "\N{DEGREE SIGN}")
            self.s21_group_delay_label.setText(str(SITools.Value(groupDelay(s21data, self.location) / 2,
                                                                    "s", fmt)))
Beispiel #16
0
def format_y_axis(val: float, unit: str = "") -> str:
    return str(SITools.Value(val, unit, FMT_SHORT))
Beispiel #17
0
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
import math
from numbers import Number

from NanoVNASaver import SITools

FMT_FREQ = SITools.Format()
FMT_FREQ_SHORT = SITools.Format(max_nr_digits=4)
FMT_FREQ_SPACE = SITools.Format(space_str=" ")
FMT_FREQ_SWEEP = SITools.Format(max_nr_digits=9, allow_strip=True)
FMT_FREQ_INPUTS = SITools.Format(max_nr_digits=10,
                                 allow_strip=True,
                                 printable_min=0,
                                 unprintable_under="- ")
FMT_Q_FACTOR = SITools.Format(max_nr_digits=4,
                              assume_infinity=False,
                              min_offset=0,
                              max_offset=0,
                              allow_strip=True)
FMT_GROUP_DELAY = SITools.Format(max_nr_digits=5, space_str=" ")
FMT_REACT = SITools.Format(max_nr_digits=5, space_str=" ", allow_strip=True)
FMT_COMPLEX = SITools.Format(max_nr_digits=3,
Beispiel #18
0
def format_frequency(freq: Number) -> str:
    return str(SITools.Value(freq, "Hz", FMT_FREQ))
Beispiel #19
0
def format_resistance(val: float, allow_negative: bool = False) -> str:
    if not allow_negative and val < 0:
        return "- \N{OHM SIGN}"
    return str(SITools.Value(val, "\N{OHM SIGN}", FMT_REACT))
Beispiel #20
0
def format_frequency_short(freq: Number) -> str:
    return str(SITools.Value(freq, "Hz", FMT_FREQ_SHORT))
Beispiel #21
0
def format_capacitance(val: float, allow_negative: bool = True) -> str:
    if not allow_negative and val < 0:
        return "- pF"
    return str(SITools.Value(val, "F", FMT_REACT))
Beispiel #22
0
def format_frequency_space(freq: float, fmt=FMT_FREQ_SPACE) -> str:
    return str(SITools.Value(freq, "Hz", fmt))
Beispiel #23
0
def format_inductance(val: float, allow_negative: bool = True) -> str:
    if not allow_negative and val < 0:
        return "- nH"
    return str(SITools.Value(val, "H", FMT_REACT))
Beispiel #24
0
def format_q_factor(val: float, allow_negative: bool = False) -> str:
    if (not allow_negative and val < 0) or abs(val > 10000.0):
        return "\N{INFINITY}"
    return str(SITools.Value(val, fmt=FMT_Q_FACTOR))
Beispiel #25
0
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <https://www.gnu.org/licenses/>.
import math

from NanoVNASaver import SITools

FMT_FREQ = SITools.Format(space_str=" ")
FMT_FREQ_INPUTS = SITools.Format(
    max_nr_digits=10, allow_strip=True, printable_min=0, unprintable_under="- ")
FMT_Q_FACTOR = SITools.Format(
    max_nr_digits=4, assume_infinity=False, min_offset=0, max_offset=0, allow_strip=True)
FMT_GROUP_DELAY = SITools.Format(max_nr_digits=5, space_str=" ")
FMT_REACT = SITools.Format(max_nr_digits=5, space_str=" ", allow_strip=True)
FMT_COMPLEX = SITools.Format(max_nr_digits=3, allow_strip=True,
                             printable_min=0, unprintable_under="- ")


def format_frequency(freq: float, fmt=FMT_FREQ) -> str:
    return str(SITools.Value(freq, "Hz", fmt))


def format_frequency_inputs(freq: float) -> str: