I recently bought a NanoVNA of amazon following the article on RTL-SDR.

Yes, I got the bad model, no shielding, no QC, I had to fix the short to GND on the USB-C header, but after getting it working, its a very nifty tool for the RF shack.

 

My friend ZS1ARB told me about this article written by Salil aka nuclearrambo.

It goes into detail on how to calculate the length of a cable using the NanoVNA as a Time-domain reflectometer

This animation explains it rather elegantly.

 
Signal transmitted through and reflected from a discontinuity

I highly recommend you start there.

 

This blog would just to show how I used this technique to measure the length of coax cable rather accurately.

 

Here he provides us with a Python script for calculating the distance of a given coax cable. I've modified his original a bit.

import skrf as rf
import matplotlib.pyplot as plt
from scipy import constants
import numpy as np
import sys
 
raw_points = 101
NFFT = 16384
PROPAGATION_SPEED = 78.6 #For RG58U
plt_filename = sys.argv[1]
_prop_speed = PROPAGATION_SPEED/100
cable = rf.Network(plt_filename)
print(plt_data)
 #Read skrf docs
print(cable)
s11 = cable.s[:, 0, 0]
print(type(s11))
window = np.blackman(raw_points)
s11 = window * s11
td = np.abs(np.fft.ifft(s11, NFFT))
#print(s11)
 
#Calculate maximum time axis
t_axis = np.linspace(0, 1/cable.frequency.step, NFFT)
d_axis = constants.speed_of_light * _prop_speed * t_axis
 
#find the peak and distance
pk = np.max(td)
idx_pk = np.where(td == pk)[0]
cable_len  = d_axis[idx_pk[0]]/2
#print(cable_len)
#print(d_axis) 
# Plot time response

plt.plot(d_axis, td)
plt.xlabel("Distance (m) Length of cable(%.5fm)" % cable_len)
plt.ylabel("Magnitude")
plt.title("Return loss Time domain")
plt.show()

Instead of saving a file, running a python script and then only seeing this script's results, I decided to glue these two together for the time being.

 

I modified some parameters for my use. Here I just took the filename and sent that path to the python script.

plt_filename = sys.argv[1]
cable = rf.Network(plt_filename)

I also happen to find that Roger Clark had also conveniently reverse engineered the windows C# app for NanoVNASharp that's on the email groups

So I cloned his repo, added some C# code to make the Python script run.

 

If you'd like to make this run.

  • Have python installed, and Visual Studio 2017 or greater.
  • Please clone the project from my GitHub repo. 
  • Open the solution, then navigate to line 249 in the MainForm.cs file.

 

Here you would have to change the python path in the below code

...
psi.FileName = @"YOUR FULL PATH HERE";
...
var script = @"CLONED DIR PATH\tdr.py";
...

and the script location to the cloned repository directory path it should look something like this.

 public void RunPython(string filename)
 {
            var psi = new ProcessStartInfo();
            psi.FileName = @"C:\Users\SomeUser\AppData\Local\Programs\Python\Python36\python.exe";
            var script = @"C:\NanoVNASharp\tdr.py";
            psi.Arguments = $"\"{script}\" \"{filename}\"";
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            var errors = "";
            using (var process = Process.Start(psi))
            {
                errors = process.StandardError.ReadToEnd();

            }
            Console.WriteLine("ERRORS");
            Console.WriteLine(errors);
}

and voila now when you save the s1p file in NanoVNASharp it will open the python script after the save dialogbox has been closed.

So my first course of action for getting the Velocity factor of the cable I was measuring was to physically measure the cable first.

It came out to 4645 mm, excellent now let's try some various velocities on line 8 the Python script and see what we get. 

 

Velocity: 75% and Length calculated: 4432 mm, not quite what we want.

Velocity: 79% and Length calculated: 4669 mm, mmmh to much hey...

 

Eventually, it came to a velocity factor of 78.6% with a calculated distance of 4645.71mm

So I cut off a piece of wire about 10cm in length.

I then pressed the Get Data button clicked Save s1p, gave it a file name and then was presented with a pretty cool finding XD

4545.03mm XD that is pretty neat!

 

I will see if how far I'd take this reverse-engineered project, for now, this was just an informatory post.

 

Might have to build some other interface to add a few things.

 

This is me sharing my discovery.

 

ZS1SCI

Comments

Salil Tembe

Amazing work ZS1SCI. I might clone your repo and try it out here on my end. Thanks for adding this functionality. Perhaps, someone should take this further and integrate into the firmware.

Salil Tembe

ZS1SCI

I agree This should be added to the firmware. ZS1SCI

ZS1SCI

Vic Zou

Can you email me the S11 file (.s1p) ? Thank you, I want to verfiy.

Vic Zou

Comments are closed