Python Resources for RF Engineers
Automate your work with programming, make better data visualizations, leverage measurement automations and software APIs to accelerate your productivity.
Hey, I’m Vikram 👋 and thanks for reading! Reply to this email with your feedback. I love hearing from you. I also recommend joining other readers on Substack chat.
If you like these articles, please let others know. Word of mouth helps grow this newsletter! 🙏
Learning to code has paid itself back 100x for the time I have spent learning it.
It has helped me automate hours of my routine work and minimize mistakes. I can instead focus on learning and new ideas. It is also what helps me write this newsletter with a full-time job.
I strongly recommend that every RF engineer learns to code well.
Why Python?
It does not have to be Python at all. Use a Matlab license if you have one available. If not, another option that is often consistent with Matlab syntax is Octave. Another well-liked tool for data analysis is R, particularly if you do a lot of statistical work. Learning Julia is a challenge, but if you're up for it, it's lightning quick for technical tasks. With the speed of C and the simplicity of Python, Mojo 🔥by Modular is rather popular right now. Go study Fortran for the ultimate bragging rights. It works incredibly well for some types of technical tasks.
We'll stick with Python here. Only because I believe it is simple to get started quickly, adequate for most RF work, and there is a wealth of infrastructure available to help you do your tasks.
Python Setup
If you're not sure where to begin, simply install the Python Anaconda distribution. If you install the full version (as opposed to the minimal miniconda version), it includes the majority of the packages you'll ever use. It runs on Linux, Mac, and Windows and is cross-platform compatible. To start coding right away if you're not familiar with Python syntax, there are lots of tutorials available on YouTube and online learning platforms.
The Anaconda package includes the user-friendly Spyder integrated development environment (IDE). Although I usually use VScode as my IDE, PyCharm is another great alternative. It comes down to personal taste. Make sure you are aware of the licensing if you are utilizing it for business purposes.
If you're scripting for data analysis, I recommend Jupyter notebooks. They run in your browser and let you build code chunks. You can use one command per block or many commands in logical groupings, depending on your taste. It allows for a combination of code and text annotations using markdown syntax. This makes it highly handy to generate reports that include text explanations and code blocks for processing results.
Virtual Environments and Git
Another topic to understand when first starting out is the utilization of virtual environments. They are isolated sandboxes in which you can store compatible versions of the packages you use. They will remain unchanged if you upgrade your main Anaconda installation. They are especially important if you want your code to work in different computing environments. A coworker can reproduce your virtual environment and run your code on their PC.
Finally, I propose that you learn how Git works. Git is a distributed version control system that allows you to manage code modifications. This is useful if you need to revert to a previous version of the code or have multiple individuals working on the same codebase. Git can be installed locally and used to version control your code. If you want to accomplish it on a company-wide scale, you may need to contact IT.
Git is popular among software engineers, but I've seen few RF engineers utilize it effectively. If you want to learn how to get started, take this course. If you want to take a fun approach, Ohmygit gamifies the git learning process.
Let's explore some useful Python packages.
Scikit-RF
This is the absolute workhorse python package to do RF engineering work. As an RF engineer, your immediate needs are usually to parse a Touchstone s-parameter file and plot the results in a Smith Chart. Scikit-RF does this in three lines of code:
import skrf as rf
ntwk = rf.Network('ring slot.s2p')
ntwk.plot_s_smith()
Apart from simply plotting, reading and writing Touchstone data, it is also capable of the following:
Microwave Network Operations: Cascading and deembedding networks, frequency slicing, conversion to Y/Z/H/ABCD parameters, sets of networks.
Calibration: SOLT, TRL (including multiline), 8/16 term error models.
De-embedding: Open short, Thru only and IEEE P370 standard deembedding.
Media: Models for simulating transmission lines and various lumped element networks.
Vector fitting: Fit a set of rational model functions to an s-parameter response.
The documentation for scikit-rf has many examples you can start from, and build your own analyses. The developers are quite responsive and helpful if you contact them with your questions via google groups or Slack. When you're comfortable with this package, consider contributing your own expertise to the project.
Numpy, Xarray, Pandas
Numpy is the foundational package for numerical computing in Python. You'll probably use it to manipulate S-parameter data matrices. If you want improved multidimensional array handling in Python, check out Xarray. It makes it much easier and more intuitive to work with complex matrix data by allowing dimensions to have labels. Compared to raw Numpy arrays, Xarray provides other extensive and intuitive array handling features while seamlessly integrating with Numpy and Pandas.
Pandas is a powerful data manipulation tool that lets you load data into dataframes and do calculations on them. I like to think of a dataframe as a more advanced spreadsheet. It also allows for the import and export of comma separated value (CSV) files and a wide range of additional file formats.
Like it or not CSV is widely utilized in engineering work, and often simulation and measurement tools produce a lot of them. Here is one of my favorite ways to build a giant dataframe from many CSV files, using glob.
import glob
import pandas as pd
# read in all csv filenames
filenames = glob.glob('./data/*.csv')
df_list = [] # empty list of dataframes
# fill the dataframe list with dataframes from each csv
for fname in filenames:
df = pd.read_csv(fname, index_col=0, header=0)
li.append(df)
# join all the dataframes to make one big one
big_df = pd.concat(li, axis=0, ignore_index=True)
# now you have all your CSV data in one dataframe!
The nice thing about Numpy and Pandas is that virtually every plotting tool in the python universe accepts these data formats as inputs. This nicely leads into the topic of plotting.
Plotting
While Pandas naturally provides dataframe plotting, you may frequently require greater control over the types of charts and finer graphing details. There are so many Python plotting tools that it's difficult to list them all. We will restrict ourselves to the following frameworks, which should be adequate for RF engineering work.
Matplotlib was created to imitate the simple charting methods available in Matlab, but it has since evolved to become the de-facto standard for making Python charts. Today, Matplotlib includes a large number of third-party extensions that can be used to extend its capability to a wide range of applications. If you want to create stunning charts for scientific and engineering articles, check out SciencePlots. It uses the IEEE style, which limits the figure width to one column of an IEEE paper and sets the resolution to 600 DPI. It also ensures that the figures are legible in black and white.
Seaborn is a Python visualization library developed on top of Matplotlib with a focus on statistical plotting. It is especially handy for visualizing huge datasets that are normally difficult to handle with Matplotlib alone. Check out the gallery of plots for ideas on how you can represent your data.
Plotly is another feature-rich plotting library for Python (and many other computer languages). It is also one of the few charting tools that includes smith charts out of the box via the scattersmith graph object. While scikit-rf supports smith chart plots with matplotlib, utilizing plotly results in a more dynamic plot. If you want to create an interactive data dashboard using Plotly's Dash, scattersmith is the way to go.
Measurement
Automating lab measurements provides the best return on time investment. Measurements can be performed on nights and weekends, resulting in reliable data.
Instruments deliver and receive commands from computers using interfaces such as LAN, USB, or GPIB (General Purpose Interface Bus). Most instruments support the IEEE 488.2 standard, which defines a communication protocol between a computer and an instrument. The standard establishes guidelines for message structure and formatting, ensuring that instruments understand and respond to orders.
Standard instructions for Programmable Instruments (SCPI, pronounced "skippy") is a higher-level abstraction that leverages IEEE 488.2 syntax and protocols to establish a collection of easily readable instructions for controlling measurement and instrument state.
SCPI commands can differ based on the actual hardware interface used. Virtual Instrument Software Architecture (VISA) standardizes the software interface regardless of the hardware interface used. It provides a unified programming interface that allows software to communicate with instruments via different types of hardware interfaces like GPIB, Serial, Ethernet, and USB.
If you want to understand the differences better, check out this article.
Python libraries manage the majority of low-level communication and provide direct access to the VISA software interface, making instrument programming simple. Here are a few useful ones:
If you've never written measurement automation before, Pyvisa is the place to start. Pyvisa supports simple commands such as query, write, and read to transmit and receive commands from the instrument. It requires the installation of National Instruments VISA drivers, which are free to download.
To get the exact commands you need to control the instrument, download the SCPI programming handbook for your instrument. Most major instrument manufacturers give PDF files with all of the commands you'll need.
You can create a custom driver for a specific instrument in Python by defining high-level functions that perform major tasks such as startup, instrument state configuration, and measurement sequences.
Pymeasure is a Pyvisa wrapper that enables it simple to develop a driver for a measurement device. Because it is open source, individuals have built drivers for a wide range of regularly used instruments, which you can simply use and extend as you see fit. It also gives simple methods for creating your own instrument from scratch without having to write all of the tedious boilerplate code that Pyvisa requires. Furthermore, it supports realtime graphing of measurement data and a queuing mechanism for huge numbers of measurements via a graphical interface.
Tool-Specific API
Automation of software is equally as crucial as hardware. It is invaluable to be able to programmatically set up simulations, as these might be difficult to put up manually due to intricate settings. Moreover, using code to extract simulation data and run sophisticated calculations on them is helpful.
Major software vendors like Ansys and Keysight provide APIs for this. Here are some noteworthy ones:
This is a python library to interact with Ansys Electronics Desktop. You can create end-to-end workflows to automate problem setup, simulation and plotting results. Check out the step-by-step examples provided to hit the ground running.
Pathwave data tools is a python library that makes it easy to work with various Keysight proprietary and industry-standard data formats. You can easily interact with ADS datasets, generic MDIFs, Touchstone, CITIfile, SMatrixIO, HDF5 or MDM files, and convert them into pandas dataframes; which opens up a world of visualization options as we discussed earlier.
A Final Word
Building software infrastructure takes time. You could argue that it is easier to “just get it done” instead of writing code. If you plan to be in the industry for a long time, build up your software infrastructure slowly. Write yourself tools you know will help. You may have to rewrite them when you change employers, but it is still worth it. Your next iteration will always be a better version.
Share your tools with colleagues and invite them to use it. Help them contribute to your project so that it improves for everybody. Ask for feedback and find ways to make it better. The whole learning experience is worth it.
The views, thoughts, and opinions expressed in this newsletter are solely mine; they do not reflect the views or positions of my employer or any entities I am affiliated with. The content provided is for informational purposes only and does not constitute professional or investment advice.
Great recommendation very helpful .
I would pyawr to the list as well. Useful python library for connecting to cadence awr and automating tasks