Skip to content

Instantly share code, notes, and snippets.

@shabaz123
Created January 31, 2024 19:05
Show Gist options
  • Save shabaz123/3749c1e481f7f23989f890e2d37d125a to your computer and use it in GitHub Desktop.
Save shabaz123/3749c1e481f7f23989f890e2d37d125a to your computer and use it in GitHub Desktop.
Charts inductance from .s1p Touchstone file (from S11 Reflection Coefficients)
# plot_ind.py - Reads a .s1p file and plots the inductance vs frequency on a graph
# uses DraggableMarker class from https://stackoverflow.com/questions/43982250/draggable-markers-in-matplotlib
# rev 1 - shabaz - January 2024
import skrf as rf
import numpy as np
import matplotlib.pyplot as plt
import sys
# set the filename
dut_fname = 'corrected_coil.s1p'
# read the file
dut_ntwk = rf.Network(dut_fname)
# convert the reflection coefficients to impedance
Z = dut_ntwk.z
# convert the impedance to inductance
L = np.reshape(Z.imag,-1) / (2 * np.pi * dut_ntwk.frequency.f)
# graph the results
fig, ax = plt.subplots()
ax.plot(dut_ntwk.frequency.f/1e6, L*1e6)
plt.grid()
plt.xlabel('Frequency (MHz)')
plt.ylabel('Inductance (uH)')
plt.title('Inductance vs Frequency')
# draggable marker
class DraggableMarker():
def __init__(self,ax=None, lines=None):
if ax == None:
self.ax = plt.gca()
else:
self.ax=ax
if lines==None:
self.lines=self.ax.lines
else:
self.lines=lines
self.lines = self.lines[:]
self.tx = [self.ax.text(0,0,"") for l in self.lines]
self.marker = [self.ax.plot([0],[0], marker="o", color="red")[0] for l in self.lines]
self.draggable=False
self.c1 = self.ax.figure.canvas.mpl_connect("button_press_event", self.click)
self.c2 = self.ax.figure.canvas.mpl_connect("button_release_event", self.release)
self.c3 = self.ax.figure.canvas.mpl_connect("motion_notify_event", self.drag)
def click(self,event):
if event.button==1:
#leftclick
self.draggable=True
self.update(event)
elif event.button==3:
self.draggable=False
[tx.set_visible(self.draggable) for tx in self.tx]
[m.set_visible(self.draggable) for m in self.marker]
ax.figure.canvas.draw_idle()
def drag(self, event):
if self.draggable:
self.update(event)
ax.figure.canvas.draw_idle()
def release(self,event):
self.draggable=False
def update(self, event):
for i, line in enumerate(self.lines):
x,y = self.get_closest(line, event.xdata)
self.tx[i].set_position((x,y))
self.tx[i].set_text("f:{}MHz\nL:{}uH".format(round(x,2),round(y,3)))
self.marker[i].set_data([x],[y])
def get_closest(self,line, mx):
x,y = line.get_data()
mini = np.argmin(np.abs(x-mx))
return x[mini], y[mini]
dm = DraggableMarker()
# display the graph
plt.show()
print('Done, exiting..')
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment