Application of Python Programming Language in Measurements
Application of Python Programming Language in Measurements
Series: Electronics and Energetics Vol. 32, No 1, March 2019, pp. 1-23
https://doi.org/10.2298/FUEE1901001P
Predrag Pejović
1. INTRODUCTION
Over the years it exists, software evolved. At the beginning of computers, the program
was not stored in the machine, instead the functionality had been hard wired for each
application. A great step forward occurred with stored programs, at first written in a machine
language. Such languages are considered as the first generation of programming languages.
The second generation of programming languages involves assembly languages, somewhat
more readable than the machine languages, but still heavily dependent on particular
instruction set architecture. Finally, the third generation of programming languages, a
prominent example of which was FORTRAN, which appeared among the first in this
generation and gained huge popularity, provided abstraction that separated programmer
from the machine instruction set architecture, enabling code portability. With portable code,
software libraries appeared, accumulating knowledge and programming experience, and
programming became a social activity. High level libraries, like libraries for numeric
computation, are nowadays very rich and complete, and it is the most likely that an everyday
problem a programmer faces is already solved and included in a library. In this manner,
programming became a social activity: a programmer relies on program development tools,
such as compilers and integrated development environments, developed by other programmers,
multimeter [51]. A Python module used to support communication and control of the
oscilloscope [49] is given in [18].
Communication over Ethernet is provided using VXI-11 protocol [52], implemented
in python-vxi11 module [8]. The module has been successfully used in [38] in
communication with [48] and [53], and is in everyday use in [50, 54].
Another popular communication interface used with older equipment is the RS-232
interface. Communication over that interface is supported by python-serial [9] module. Besides,
this module supports communication over USB to some devices, like the Arduino boards [44].
A Python class that supports communication to Tektronix oscilloscope is provided at [19], and
it had been used successfully with TDS 210, TDS 220, TDS 1000, and TPS 2024
oscilloscopes.
Providing communication to measurement equipment is the most specific part of the
measurement automation and the design of virtual instruments as proposed in this paper.
After the communication has been established, everything else is common general-
purpose programming. Communication to instruments according to SCPI [42, 43] reduces
to exchange of ASCII strings, and conversion of such strings is readily available in
Python even with built in functions, which might be supported with string module of the
Python Standard Library if some more complex string operations are needed.
4. SUPPORTING PROGRAMS
The use of Python programming language in GNU/Linux environment provides an
option of a simple interfacing with other free software tools. Only two of such programs
would be mentioned: LaTeX [55] which was used for automatic report generation in [34],
resulting in [57], and convert used to convert image data formats provided by the digital
oscilloscope, as used in [18]. Any other program could easily be invoked from python,
and its output used in further processing.
6. APPLICATION EXAMPLES
6.1. Applications in Power Electronics and Electric Power
Power electronics is a principal research area of the author, and he started to use
virtual instrumentation in power electronics, to support research in three phase rectifiers
that resulted in a number of papers aggregated in [33]. The measurements required to
Application of Python Programming Language in Measurements 7
support the research included measurement of power, apparent power, reactive power,
power factor, displacement power factor, total harmonic distortion, and efficiency.
Additionally, characterization of components, like recording magnetizing curves and
analyzing component constitutive relations and losses were required. Specific equipment
to perform these tasks are nowadays available, but being narrow in application and highly
expensive. For the research purposes, virtual instruments had been created, performing
digital post-processing of recorded waveforms. After the Python based instrumentation
had been introduced, being entirely based on free software, the methods had been ported
to education, to laboratory exercises in Power Electronics 2 [34]. To illustrate automation
of measurement process, a 92-page measurement report is automatically generated during
a lab exercise that lasts for only two hours, an example being available at [57]. As an
example, in Fig. 1 waweforms of voltages and currents at the 6-pulse three-phase rectifier
inputs are presented, and their spectra are given in Fig. 2. Effects caused by commutation
of the diodes, like the notches in the input voltages and limited slope in the input currents
are observable. In the spectra, absence of harmonic components at triples of the line
frequency is observable, that matches analytical results. To improve the input current
spectra and to reduce the harmonic pollution, 12-pulse rectifiers are applied, and
waveforms that correspond to this rectifier are presented in Fig. 3, while corresponding
spectra are given in Fig. 4. Reduced distortion is readily available. Collected samples are
used to determine input power, output power, efficiency, power factor, displacement
power factor and total harmonic distortions (THD) of the input currents and voltages.
Signal processing is simplified by the fact that the system frequency is the line frequency,
known in advance, and taking an appropriate number of samples spectral leakage is
avoided. In systems with variable frequency this issue should be considered, and it will be
discussed in this paper in the section that covers frequency response measurement.
A direct application of the same technology, with minor extension to provide timed
measurement and timestamping, is presented in [35], where long lasting measurements,
over a week, of the line voltage and its total harmonic distortion (THD) were provided. A
diagram presenting measured THD values is presented in Fig. 5, indicating periodic
behavior during working days, while having a specific pattern during weekends. To
provide the diagram of Fig. 5 measurements were made every minute over a week, and
10080 data points are collected and presented.
To illustrate daily variations of the THD, the waveform of Fig. 5 in the part that
corresponds to workdays is plotted in Fig. 6 such that the curves are plotted for each day one
atop another. Close to periodic behavior could be observed, illustrating effects of human
daily activities on the voltage THD. On the other hand, the THD exposes a different pattern
during weekends. To illustrate that, the same methodology as for the workdays, presented in
Fig. 6 is applied, and the results is presented in Fig. 7. Significant reduction of the bump
from 08 to 16 hours could be readily observed, corresponding to the reduction of business
activity during weekends. For the rest of the day, the THD profile remained about the same.
10 P. PEJOVIĆ
presented in [37], where the solar panel is kept at the maximum power point by an
adjustable resistive load, and harvested power is measured in order to estimate average,
minimum and maximum power that could be harvested in the specified location as it
depends on weather conditions.
After the time scale has been selected, the number of samples taken into account is
computed by rounding 2500 n per T0 Tspan , where 2500 is the number of samples per
time frame for the given oscilloscope, nper is the number of signal periods per time frame,
shown in Fig. 12, T0 is the signal period, and Tspan is the time span covered by the time
frame. The number of samples is solely dependent on the signal frequency, and the
diagram is shown in Fig. 13. In [40], an older version of the algorithm is presented,
reducing the scope to only one signal period, but in cases when more than one signal
period is covered by the oscilloscope screen, due to the limitations imposed by the
discrete set of available time scale values, better results are obtained by taking two
periods into account, and the improved algorithm is presented in this paper.
Application of Python Programming Language in Measurements 15
The algorithm assumes that the number of considered samples ns is known, and that
samples of signals x(t) and y(t) are available as xk and yk for k {0, ... ns 1}. Waiting
functions are computed next, according to
k
ck 2 cos 2 n per
(1)
n S
and
k
sk 2 sin 2 n per (2)
nS
According to the Fourier analysis, for signal xt cosine component is obtained as
nS 1
x
1
XC k ck (3)
nS k 0
while the sine component is
nS 1
x
1
XS k sk . (4)
nS k 0
After the cosine and sine components are determined applying the Fourier analysis,
effectively filtering the noise out, the signal amplitude is obtained as
X m X C2 X S2 (5)
and the phase is obtained as
x atan 2( X S , X C ) (6)
16 P. PEJOVIĆ
using the atan 2 function that takes two arguments and provides the result in the range
( , ] .
The same signal processing is performed over signal y(t), resulting in values of YC, YS,
Ym, and y . Finally, the transfer function magnitude is obtained as
Ym
H ( j0 ) (7)
Xm
and the phase is obtained as
H 0 y x (8)
The value H 0 is named ―raw phase‖ since it takes value in the range 2 H 0 2
since x , y . The value is correct, due to the phase periodicity over 2 , but it
is convenient to provide the phase value in the range H . In this aim, phase
adjustment by appropriate shifting for 2 is performed according to
H 0 H 0
H H 0 2 H 0 (9)
2 .
H0 H0
This concludes the algorithm for the one point, for the specified frequency value. The
algorithm is repeated for specified frequency range and the specified number of data
points.
As the first example, consider a circuit of Fig. 14, used to illustrate frequency
response effects caused by the capacitor, to identify frequency range where it behaves
approximately as an open circuit and the range where it behaves approximately as a short
circuit. The program is run, and the frequency response is obtained as presented in the
diagram of Fig. 15, clearly indicating areas of flat frequency response where the capacitor
could be considered either as open circuit, bellow 1 kHz in the considered case, or as
short circuit, which occurs above 100 kHz in the considered case.
The same system could be used for immittance measurements, for impedance and
admittance, using the circuit of Fig. 16. In the circuit of Fig. 16 R is used as a reference
resistor, and the current through the measured impedance is computed as
v1 v2
i (10)
R
The same data processing algorithm as for the transfer functions is applied, taking signals
v2(t) and i(t) as y(t) and x(t) if impedance computation is the goal.
Application of the method to analyze electronic components provides insight in their
operation and suggest suitable modeling strategies. As an example, in Fig. 17 frequency
response of a capacitor C = 1 nF impedance is presented. The result matches expectations,
and barely noticeable deviations of measured phase from 90o at the beginning and at the
end of the diagram are caused by a huge difference of the capacitor impedance at
considered frequency and the impedance reference of R = 20 k. This is expected, since
measured impedance varies for four decades, i.e. 104 times over the considered frequency
range, and a constant reference impedance is used. To improve the result, suggested
approach that uses Arduino to reconfigure the circuit by adapting the reference impedance
value to the measured impedance should be applied.
Fig. 16 Circuit structure for impedance measurement; R is the impedance reference value
18 P. PEJOVIĆ
In contrast to the capacitor impedance frequency response, which follows the ideal
model, impedance of an inductor is presented in Fig. 18. The inductor has rated
inductance of 10 mH, but it exposes inductive behavior only in the frequency range from
about 1 kHz to about 200 kHz. At low frequencies, parasitic resistance of the winding
dominates the impedance, while at high frequencies parasitic capacitance of the winding
dominates the response, resulting in capacitor-like frequency response above the resonant
frequency of about 400 kHz. The results are obtained using a reference resistor of 500 .
7. CONCLUSIONS
In this paper, application of Python programming language in creating automated
measurement systems and virtual instruments is discussed. It is shown that to create such
systems a set of specific tasks should be performed, not frequent in common application
programming. The tasks are listed, and the Python modules that support performing them
are looked for. It is shown that for all of the specific tasks there are Python modules
readily available, either from the Python Standard Library, either from external sources,
some of them highly specialized to support communication with instruments. Effective
methods of including modules and arranging them in separate namespaces turned out to
be useful in considered application. It is also shown that other programs, like LaTeX for
text processing might be useful in creating automated measurement tools, to provide
automatic report generation, which might be of use in certifying laboratories. The use of
Arduino platform is proposed to provide measurement system controlled automatic
reconfiguration and indication of the system state and performance. Tools for controlling
Arduino platforms directly from Python are identified.
Application of the proposed methods is illustrated in four different areas, as reported
by the author in seven papers. Applications started in power electronics, and positive
experiences spread to metrology, to the design of a DC voltage calibrator, to education,
where the methods were used in modernizing two courses, and in measurements of system
frequency response, as applied in electronics, acoustics, and control system design. In
some of these applications, selection of the time scale and the number of considered
samples in the case of variable signal frequency is controlled by an updated algorithm
presented in this paper.
Overall conclusion is that Python is an adequate tool for creating automated
measurement systems and virtual instruments, due to its modular structure and openness
for contribution of modules. In the choice of programming tools, the attention has been
22 P. PEJOVIĆ
made to favorize general purpose tools and techniques, to minimize specific knowledge
requirements. Having in mind evolution of software and the people who work in
metrology, it is likely to expect wide application of the proposed approach and methods,
which already started in several places independently.
REFERENCES
[1] Python Programming Language — Official Website, [online] Available: http://www.python.org/
[2] The Python Standard Library, [online] Available: https://docs.python.org/3/library/
[3] time — Time access and conversions, [online] Available: https://docs.python.org/2/library/time.html
[4] sys — System-specific parameters and functions, [online] Available: https://docs.python.org/2/library/sys.html
[5] os — Miscellaneous operating system interfaces, [online] Available: https://docs.python.org/2/library/os.html
[6] Graphical User Interfaces with Tk, [online] Available: https://docs.python.org/2/library/tk.html
[7] python-usbtmc, [online] Available: https://github.com/python-ivi/python-usbtmc
[8] Python VXI-11, [online] Available: https://github.com/python-ivi/python-vxi11
[9] pyserial, [online] Available: https://pythonhosted.org/pyserial/
[10] NumPy, [online] Available: http://www.numpy.org/
[11] SciPy, [online] Available: https://www.scipy.org/
[12] SciPy: PyLab, [online] Available: https://scipy.github.io/old-wiki/pages/PyLab
[13] Pandas, [online] Available: https://pandas.pydata.org/
[14] matplotlib, [online] Available: https://matplotlib.org/
[15] PyQt's Modules, [online] Available: http://pyqt.sourceforge.net/Docs/PyQt4/modules.html
[16] wxPython, [online] Available: https://wxpython.org/
[17] Python-Arduino-Proto-API-v2, [online] Available: https://github.com/vascop/Python-Arduino-Proto-API-v2
[18] P. Pejović, oscusb, Python module to support communication with oscilloscopes over USB, [online]
Available: http://tnt.etf.bg.ac.rs/~oe2em/oscusb.py
[19] P. Pejović, oscusb, Python module to support communication with oscilloscopes over RS-232, [online]
Available: http://tnt.etf.bg.ac.rs/~oe2em/oscrs232.py
[20] Pr. Pejović, oscusb, Python module to support presentation of numbers in engineering notation, [online]
Available: http://tnt.etf.bg.ac.rs/~oe2em/engineeringnotation.py
[21] J. M. Hughes, Real World Instrumentation with Python: Automated Data Acquisition and Control
Systems. O'Reilly Media, Inc., 2010
[22] G. Real, L. Raviola, M. F. Jauré, and A. O. Vitali, ―Data acquisition system for didactic laboratories based on
open-source hardware and free software,‖ In Proceedings of the 2015 XVI IEEE Workshop on Information
Processing and Control (RPIC), 2015, pp. 1-6.
[23] J. L. Johnson, H. T. Wörden, and K. V. Wijk, ―PLACE: an open-source python package for laboratory
automation, control, and experimentation,‖ Journal of laboratory automation, vol. 20, no. 1, pp. 10-16, 2015.
[24] I. J. Koenka, J. Sáiz, and P. C. Hauser. ―Instrumentino: an open-source software for scientific instruments,‖
CHIMIA International Journal for Chemistry, vol. 69, no. 4, pp. 172-175, 2015.
[25] I. J. Koenka, J. Sáiz, and P. C. Hauser. ―Instrumentino: An open-source modular Python framework for
controlling Arduino based experimental instruments,‖ Computer Physics Communications, vol. 185, no. 10 pp.
2724-2729, 2014.
[26] F. J. F. Martín, M. V. Llopis, J. C. C. Rodríguez, J. R. B. González, and J. M. Blanco, ―Low-cost open-source
multifunction data acquisition system for accurate measurements,‖ Measurement, vol. 55, pp. 265-271, 2014.
[27] A. J. Lewis, M. Campbell, and P. Stavroulakis, ―Performance evaluation of a cheap, open source, digital
environmental monitor based on the Raspberry Pi,‖ Measurement, vol. 87, pp. 228-235, 2016.
[28] V. Davidović, D. Danković, S. Golubović, S. Djoric-Veljkovic, I. Manić, Z. Prijić, A. Prijić, N. Stojadinović, and
S. Stanković, ―NBT Stress and Radiation Related Degradation and Underlying Mechanisms in Power
VDMOSFETS,‖ Facta Universitatis, Series: Electronics and Energetics, vol 31, no. 3, pp. 367-388, 2018.
[29] S. K. Mohapatra, K. P. Pradhan, and P. K. Sahu, ―Resolving the bias point for wide range of temperature
applications in high-k/metal gate nanoscale DG-MOSFET,‖ Facta Universitatis, Series: Electronics and
Energetics, vol. 27, no. 4, pp. 613-619, 2014.
[30] S. K. Mohapatra, K. P. Pradhan, and P. K. Sahu, ―ZTC bias point of advanced fin based device: The
importance and exploration,‖ Facta Universitatis, Series: Electronics and Energetics, vol. 28, no. 3 pp.
393-405, 2015.
Application of Python Programming Language in Measurements 23
[31] I. Manić, D. Danković, V. Davidović, A. Prijić, S. Djorić-Veljković, S. Golubović, Z. Prijić, and N. Stojadinović,
―Effects of pulsed negative bias temperature stressing in p-channel power VDMOSFETs,‖ Facta Universitatis,
Series, Electronics and Energetics, vol. 29, no. 1, pp. 49-60, 2015.
[32] X. Saura, M. Riccio, J. Suñé, A. Irace, and E. Miranda, ―Study on the spatial generation of breakdown
spots in MIM capacitors with different aspect ratios,‖ Facta Universitatis, Series Electronics and
Energetics, vol. 28, no. 2 pp. 177-192, 2015.
[33] P. Pejović, ―Three-Phase Diode Rectifiers with Low Harmonics - Current Injection Methods,‖ Springer, 2007.
[34] P. Pejović, M. Simić, ―Virtual Instruments for Power Electronics Based on Free Software Tools,‖ In Proceedings
of the17th International Symposium on Power Electronics, Ee 2013, Novi Sad, October-November 2013.
[35] P. Pejović, M. Simić, ―A System for Measuring Mains Voltage Parameters and Logging the Data,‖ In
Proceedings of the 18th International Symposium on Power Electronics, Ee 2015, Novi Sad, October
2015.
[36] V. Lazarević, M. Bjelica, P. Pejović, ―Maximum Power Point Tracking Control System of Photovoltaic
Module Using Free Software and Standard Laboratory Equipment,‖ In Proceedings of the 18th
International Symposium on Power Electronics, Ee 2015, Novi Sad, October 2015.
[37] P. Pejović, M. Bjelica, ―A Simple System to Estimate On-Site Solar Energy Harvesting,‖ In Proceedings
of the 18th International Symposium on Power Electronics, Ee 2015, Novi Sad, October 2015.
[38] P. Pejović, A. Zeković, ―Software Supported DC Voltage Calibrator,‖ In Proceedings of the XI
International Symposium Industrial Electronics, INDEL 2016, Banja Luka, November 3-5, 2016.
[39] P. Pejović, ―Electrical Measurements Revisited — Experiences from Modernizing the Course,‖ In
Proceedings of the IEEE EUROCON 2017, Ohrid, Republic of Macedonia, 6-8 July 2017, pp. 838-844.
[40] P. Pejović, ―An Automated System for Frequency Response Measurement Based on Free Software
Tools,‖ In Proceedings of the XII International Symposium Industrial Electronics, INDEL 2018, Banja
Luka, November 1-3, 2018.
[41] Wikipedia contributors, IEEE-488, [online] Available: https://en.wikipedia.org/wiki/IEEE-488
[42] Wikipedia contributors, Standard Commands for Programmable Instruments, [online] Available:
https://en.wikipedia.org/wiki/Standard_Commands_for_Programmable_Instruments
[43] Standard Commands for Programmable Instruments (SCPI), [online] Available: http://www.ivifoundation.
org/docs/scpi-99.pdf
[44] M. Banzi, Getting Started with Arduino, Second Edition, O’Reilly Media, 2011
[45] Arduino Mega 2560 Rev3, [online] Available: https://store.arduino.cc/arduino-mega-2560-rev3
[46] Universal Serial Bus Test and Measurement Class Specification (US-BTMC), Revision 1.0, April 14,
2003, [online] Available: http://sdpha2.ucsd.edu/Lab_Equip_Manuals/USBTMC_1_00.pdf
[47] P. Pejović, usbtmcinstall.zip, [online] Available: http://tnt.etf.bg.ac.rs/~oe2em/usbtmcinstall.zip
[48] Agilent Technologies Agilent 33220A 20 MHz Waveform Generator User’s Guide, [online] Available:
http://cp.literature.agilent.com/litweb/pdf/33220-90002.pdf
[49] TBS1000B-EDU Series Datasheet, [online] Available: https://www.tek.com/datasheet/digital-storage-
oscilloscope-0
[50] P. Pejović, Electrical Measurements, course web site, [online] Available: http://tnt.etf.bg.ac.rs/~oe2em/
[51] Keysight Technologies Digital Multimeters, 34460A Digital Multimeter, 6 (1/2) Digit, Basic Truevolt, [online]
Available: https://literature.cdn.keysight.com/litweb/pdf/5991-1983EN.pdf
[52] VMEbus Extensions for Instrumentation TCP/IP Instrument Protocol Specification VXI-11, Revision 1.0, The
VXIbus Consortium, 1995, [online] Available: http://www.vxibus.org/files/VXI\_Specs/VXI-11.zip
[53] Agilent 34410A and 34411A Multimeters, [online] Available: http://cp.literature.agilent.com/litweb/pdf/ 5989-
3738EN.pdf
[54] P. Pejović, ―Laboratorijske vežbe iz električnih merenja‖ [online] Available: https://zenodo.org/record/
1311557/files/prirucnik.pdf?download=1
[55] CTAN Comprehensive TeX Archive Network, [online] Available: https://ctan.org/
[56] ImageMagick convert, [online] Available: https://imagemagick.org/script/convert.php
[57] Twelve Pulse Rectifier - Lab Report Example, [online] Available: http://tnt.etf.bg.ac.rs/~ms1ee2/report-12-pulse-
2.pdf