Skip to content

Commit

Permalink
Update TDR.py
Browse files Browse the repository at this point in the history
Allow user to select |Z|, VSWR, or S11
  • Loading branch information
EnPassant123 authored and zarath committed Oct 18, 2024
1 parent 2ec8981 commit f43a869
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/NanoVNASaver/Windows/TDR.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,28 @@ def __init__(self, app: QtWidgets.QWidget):
layout = QtWidgets.QFormLayout()
make_scrollable(self, layout)

dropdown_layout = QtWidgets.QHBoxLayout()

self.tdr_velocity_dropdown = QtWidgets.QComboBox()
for cable_name, velocity in CABLE_PARAMETERS:
self.tdr_velocity_dropdown.addItem(cable_name, velocity)
self.tdr_velocity_dropdown.insertSeparator(
self.tdr_velocity_dropdown.count()
)
self.tdr_velocity_dropdown.insertSeparator(self.tdr_velocity_dropdown.count())
self.tdr_velocity_dropdown.addItem("Custom", -1)
self.tdr_velocity_dropdown.setCurrentIndex(1) # Default to PE (0.66)
self.tdr_velocity_dropdown.currentIndexChanged.connect(self.updateTDR)
layout.addRow(self.tdr_velocity_dropdown)

dropdown_layout.addWidget(self.tdr_velocity_dropdown)

self.format_dropdown = QtWidgets.QComboBox()
self.format_dropdown.addItem("|Z|")
self.format_dropdown.addItem("S11")
self.format_dropdown.addItem("VSWR")

self.format_dropdown.currentIndexChanged.connect(self.updateFormat)

dropdown_layout.addWidget(self.format_dropdown)

layout.addRow(dropdown_layout)

self.tdr_velocity_input = QtWidgets.QLineEdit()
self.tdr_velocity_input.setDisabled(True)
Expand All @@ -111,6 +123,10 @@ def __init__(self, app: QtWidgets.QWidget):

layout.addRow(self.app.tdr_chart)

def updateFormat(self):
self.app.tdr_chart.resetDisplayLimits()
self.updateTDR()

def updateTDR(self):
# TODO: Let the user select whether to use high or low resolution TDR?
FFT_POINTS = 2**14
Expand Down Expand Up @@ -138,24 +154,31 @@ def updateTDR(self):
return

s11 = [complex(d.re, d.im) for d in self.app.data.s11]

s11 = np.array(s11)
s11 = np.concatenate([s11, np.conj(s11[-1:0:-1])]) # Include negative frequencies
s11 = np.fft.fftshift(s11)

window = np.blackman(len(s11))
windowed_s11 = window * s11 # Now windowing eliminates higher frequencies while leaving low frequencies untouched
windowed_s11 = window * s11 #Now windowing eliminates higher frequencies while leaving low frequencies untouched

pad_points = (FFT_POINTS - len(windowed_s11)) // 2
windowed_s11 = np.pad(windowed_s11, [pad_points + 1, pad_points]) # Pad array to length FFT_POINTS
windowed_s11 = np.fft.ifftshift(windowed_s11)

td = np.fft.ifft(windowed_s11)

step = np.ones(FFT_POINTS)
step_response = convolve(td, step)

self.step_response_Z = np.abs(50 * (1 + step_response) / (1 - step_response)) #Can plot impedance in terms of real and imaginary too

# calculate step response based on the format that the user selected
TDR_format = self.format_dropdown.currentText();
step_Z = 50 * (1 + step_response) / (1 - step_response)
step_refl_coefficient = np.abs((step_Z - 50)/(step_Z + 50))
if TDR_format == "|Z|":
self.step_response_Z = np.abs(step_Z)
elif TDR_format == "S11":
self.step_response_Z = 20 * np.log10(step_refl_coefficient)
elif TDR_format == "VSWR":
self.step_response_Z = np.abs((1 + step_refl_coefficient)/(1 - step_refl_coefficient))
time_axis = np.linspace(0, 1 / step_size, FFT_POINTS)
self.distance_axis = time_axis * v * speed_of_light
# peak = np.max(td)
Expand Down

0 comments on commit f43a869

Please sign in to comment.