There are various methods of determining the length of a cable acting as a transmission line. The one I’ve used up until getting the nanoVNA is to inject a fast impulse into the cable and measure the reflection time on an oscilloscope. As usual, Alan W2AEW has a good video about this: #88 Cheap and simple TDR using an oscilloscope and 74AC14 Schmitt Trigger Inverter.

Alan also shows a good method of using the nanoVNA to measure cable length: #316: Use NanoVNA to measure coax length – BONUS Transmission Lines and Smith Charts, SWR and more.

The author of the code I used in my previous post about nanoVNA and MATLAB, Alex, has as part of his code the use of a TDR Step Response. With this you can look at the step response graph and derive a cable’s length by looking at the dips in the response.

It seemed to me that his code could be extended so that the cable length could be worked out in the code with no need to look at the graph other than to check that it was all ok. Here’s the script that does the calculations: ajfCableLength.m. Make sure you read on to the end of the post to understand its limitations.

You connect the cable you’re measuring to the S11 port of the nanoVNA and run the script. Here’s a sample MATLAB session using one of the cables supplied with my nanoVNA:

>> ajfCableLength

connect: Serial-/dev/tty.usbmodem4001

status: open

freq: 101

s11: 101

s21: 101

Measuring cable connected to ch0 ...

Velocity factor of cable? 0.66

The calculated cable length is 0.19775 metres.

Show TDR Response plot (Y/n)

close

>>

The script is perhaps worth explaining. The first part is the difficult bit which Alex provided. The S11 scattering parameters are obtained from the nanoVNA. These s-parameters are converted into a MATLAB rationalfit object which is a mathematical representation of the s-parameter curve. This rationalfit object is then used to derive the TDR step response mathematically. The second part, which I added, looks at the step response to find the last dip in the curve. This gives the return trip time just like in a real TDR. Given the return trip time the cable length is calculated using the velocity factor which the script has already asked the user to provide.

This all works well for short lengths of cable, say up to 2m. But beyond that it all goes haywire and the results are not correct. I fried my brain reading books (see below) on TDR step responses to find out why. I haven’t managed to understand it properly yet. So it’s a matter for further study.

Some of the books I looked at are:

*Electromagnetics*by*Kraus*. An excellent book, Chapter 12 is the pertinent part.*Electromagnetism for Electronic Engineers*by*Carter*.*Signals and Systems*by*Hsu*.*Techniques for Advanced Cable Testing*which is on the Keysight web site.

None of these are easy reading and all assume an understanding of vector arithmetic and calculus.

Easier, is:

*Practical Digital Signal Processing using Microcontrollers*by*Ibrahim*.