nanovna-saver/src/NanoVNASaver/Charts/RIMu.py

202 wiersze
6.8 KiB
Python
Czysty Zwykły widok Historia

# NanoVNASaver
#
# A python program to view and export Touchstone data from a NanoVNA
# Copyright (C) 2019, 2020 Rune B. Broberg
# Copyright (C) 2020,2021 NanoVNA-Saver Authors
#
# This program is free software: you can redistribute it and/or modify
# 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
import numpy as np
import logging
from scipy.constants import mu_0
2023-03-12 07:02:58 +00:00
from PyQt6 import QtWidgets, QtGui
from NanoVNASaver.Formatting import format_frequency_chart
from NanoVNASaver.RFTools import Datapoint
from NanoVNASaver.Charts.Chart import Chart
2023-02-19 08:06:23 +00:00
from NanoVNASaver.Charts.RI import RealImaginaryChart
logger = logging.getLogger(__name__)
MU = "\N{GREEK SMALL LETTER MU}"
2023-02-19 08:06:23 +00:00
class RealImaginaryMuChart(RealImaginaryChart):
def __init__(self, name=""):
super().__init__(name)
self.y_menu.addSeparator()
2023-03-12 07:02:58 +00:00
self.action_set_fixed_maximum_real = QtGui.QAction(
2023-03-08 08:40:39 +00:00
f"Maximum {MU}' ({self.maxDisplayReal})"
)
self.action_set_fixed_maximum_real.triggered.connect(
2023-03-08 08:40:39 +00:00
self.setMaximumRealValue
)
2023-03-12 07:02:58 +00:00
self.action_set_fixed_minimum_real = QtGui.QAction(
2023-03-08 08:40:39 +00:00
f"Minimum {MU}' ({self.minDisplayReal})"
)
self.action_set_fixed_minimum_real.triggered.connect(
2023-03-08 08:40:39 +00:00
self.setMinimumRealValue
)
2023-03-12 07:02:58 +00:00
self.action_set_fixed_maximum_imag = QtGui.QAction(
2023-03-08 08:40:39 +00:00
f"Maximum {MU}'' ({self.maxDisplayImag})"
)
self.action_set_fixed_maximum_imag.triggered.connect(
2023-03-08 08:40:39 +00:00
self.setMaximumImagValue
)
2023-03-12 07:02:58 +00:00
self.action_set_fixed_minimum_imag = QtGui.QAction(
2023-03-08 08:40:39 +00:00
f"Minimum {MU}'' ({self.minDisplayImag})"
)
self.action_set_fixed_minimum_imag.triggered.connect(
2023-03-08 08:40:39 +00:00
self.setMinimumImagValue
)
2023-02-19 08:06:23 +00:00
self.y_menu.addAction(self.action_set_fixed_maximum_real)
self.y_menu.addAction(self.action_set_fixed_minimum_real)
self.y_menu.addSeparator()
self.y_menu.addAction(self.action_set_fixed_maximum_imag)
self.y_menu.addAction(self.action_set_fixed_minimum_imag)
2023-02-19 08:06:23 +00:00
# Manage core parameters
# TODO pick some sane default values?
2023-03-08 08:40:39 +00:00
self.coreLength = 1.0
self.coreArea = 1.0
self.coreWindings = 1
self.menu.addSeparator()
2023-03-12 07:02:58 +00:00
self.action_set_core_length = QtGui.QAction("Core effective length")
2023-03-08 08:40:39 +00:00
self.action_set_core_length.triggered.connect(self.setCoreLength)
2023-03-12 07:02:58 +00:00
self.action_set_core_area = QtGui.QAction("Core area")
2023-03-08 08:40:39 +00:00
self.action_set_core_area.triggered.connect(self.setCoreArea)
2023-03-12 07:02:58 +00:00
self.action_set_core_windings = QtGui.QAction("Core number of windings")
2023-03-08 08:40:39 +00:00
self.action_set_core_windings.triggered.connect(self.setCoreWindings)
self.menu.addAction(self.action_set_core_length)
self.menu.addAction(self.action_set_core_area)
self.menu.addAction(self.action_set_core_windings)
def copy(self):
new_chart: RealImaginaryMuChart = super().copy()
new_chart.coreLength = self.coreLength
new_chart.coreArea = self.coreArea
new_chart.coreWindings = self.coreWindings
return new_chart
def drawChart(self, qp: QtGui.QPainter):
qp.setPen(QtGui.QPen(Chart.color.text))
2023-03-08 08:40:39 +00:00
qp.drawText(self.leftMargin + 5, 15, f"{self.name}")
qp.drawText(5, 15, f"{MU}'")
qp.drawText(self.leftMargin + self.dim.width + 10, 15, f"{MU}''")
qp.setPen(QtGui.QPen(Chart.color.foreground))
2023-03-08 08:40:39 +00:00
qp.drawLine(
self.leftMargin,
self.topMargin - 5,
self.leftMargin,
self.topMargin + self.dim.height + 5,
)
qp.drawLine(
self.leftMargin - 5,
self.topMargin + self.dim.height,
self.leftMargin + self.dim.width + 5,
self.topMargin + self.dim.height,
)
self.drawTitle(qp)
def contextMenuEvent(self, event):
self.action_set_fixed_start.setText(
2023-03-08 08:40:39 +00:00
f"Start ({format_frequency_chart(self.minFrequency)})"
)
self.action_set_fixed_stop.setText(
2023-03-08 08:40:39 +00:00
f"Stop ({format_frequency_chart(self.maxFrequency)})"
)
self.action_set_fixed_minimum_real.setText(
2023-03-08 08:40:39 +00:00
f"Minimum {MU}' ({self.minDisplayReal})"
)
self.action_set_fixed_maximum_real.setText(
2023-03-08 08:40:39 +00:00
f"Maximum {MU}' ({self.maxDisplayReal})"
)
self.action_set_fixed_minimum_imag.setText(
2023-03-08 08:40:39 +00:00
f"Minimum {MU}'' ({self.minDisplayImag})"
)
self.action_set_fixed_maximum_imag.setText(
2023-03-08 08:40:39 +00:00
f"Maximum {MU}'' ({self.maxDisplayImag})"
)
self.menu.exec(event.globalPos())
def setCoreLength(self):
val, selected = QtWidgets.QInputDialog.getDouble(
2023-03-08 08:40:39 +00:00
self,
"Core effective length",
"Set core effective length in mm",
value=self.coreLength,
decimals=2,
)
if not selected:
return
if not (self.fixedValues and val >= 0):
self.coreLength = val
if self.fixedValues:
self.update()
def setCoreArea(self):
val, selected = QtWidgets.QInputDialog.getDouble(
2023-03-08 08:40:39 +00:00
self,
"Core effective area",
2023-03-05 12:33:05 +00:00
"Set core cross section area length in mm\N{SUPERSCRIPT TWO}",
2023-03-08 08:40:39 +00:00
value=self.coreArea,
decimals=2,
)
if not selected:
return
if not (self.fixedValues and val >= 0):
self.coreArea = val
if self.fixedValues:
self.update()
def setCoreWindings(self):
val, selected = QtWidgets.QInputDialog.getInt(
2023-03-08 08:40:39 +00:00
self,
"Core number of windings",
"Set core number of windings",
value=self.coreWindings,
)
if not selected:
return
if not (self.fixedValues and val >= 0):
self.coreWindings = val
if self.fixedValues:
self.update()
def value(self, p: Datapoint) -> complex:
return self.mu_r(p)
def mu_r(self, p: Datapoint) -> complex:
2023-02-19 08:06:23 +00:00
inductance = p.impedance() / (2j * math.pi * p.freq)
# Core length and core area are in mm and mm2 respectively
# note: mu_r = mu' - j * mu ''
2023-02-19 08:06:23 +00:00
return np.conj(
2023-03-08 08:40:39 +00:00
inductance
* (self.coreLength / 1e3)
/ (mu_0 * self.coreWindings**2 * (self.coreArea / 1e6))
2023-02-19 08:06:23 +00:00
)