Hello World...¶

(or Getting Started with Python, Anaconda, and JupyterLabs...)¶

Install the latest version of Anaconda3¶

(currently version 5.0.1)

It is > 500 MB download !!

  • Comes with python version 3.6.2
  • And also includes QT5 with pyQT version 5.6.2

You will have a choice of 32 bit or 64 bit. In order to use pyVisa, you need to install the same bitness of VISA and Anaconda. On my PC I used 64 bit for both. The recommended visa is from National Instruments, but other visa suppliers can be made to work... (I use NI Visa, so I have not tried anything else...)

During Installation:

  • I installed for "All Users"
  • I selected C:\Anaconda3 as the install path. (No spaces in the path)
  • Do add Anaconda to the system path variable.
  • Do Register Anaconda as the system Python 3.6

Add packages that we need¶

Open a command window in the current folder

  • Shift – Click in the white space of a folder and you can open command prompt here.
  • Or go in window bar (where the path is) and type cmd.
  • Or go to the start menu and type CMD into the search box

Add Conda-Forge as a lower priority channel. This channel has some additional libraries that we will be using.

conda config --append channels conda-forge

Then install the packages that we need:

conda install pyinstaller   (useful for distribution)
conda install pyVisa        (to control instruments)

conda install scikit-rf     (for network tools)
conda install pyqtgraph

conda list                  (summary of current installation)

conda install python=3.5    (this is how to downgrade, for example)

Conda keeps track of all installed packages, and also does its best to ensure compatible versions of each package are being used.

Conda is also used to update to the latest version of packages. This is where the various libraries and dependencies are analyzed, and only a compatible mixture of libraries is installed. For example, pyVisa needs python 3.5, so the update will know about this, and will not update python to a higher version.

conda update conda (update the package manager) conda update anaconda (good way to update packages)

Some useful Conda Commands:¶

Solution for Conda IO Error - Run Conda Prompt as Administrator. This is not the usual windows cmd prompt. You'll find this prompt in the Anaconda group of programs along with the Conda Navigator. Right click on the prompt and click Run as Administrator.

conda install <package>       (we used this in the last section)
conda update conda            (Run cmd prompt as administrator !!!)
conda update anaconda         (Run cmd prompt as administrator !!!)
conda update python           (don't do this)
conda update --all            (don't do this)
conda info <package>

For completeness, I list these additional commands. But you should not need them.

To add conda-forge as a high priority channel instead of lower priority, use

conda config --get channels                 (list installed chans)
conda config --add channels <package>       (to add a high priority chan
conda config --remove channels <package>    (to remove a channel)

Best Tutorial Website¶

Go to this website if you are serious...¶

Go to this website if you are serious about learning to write Python programs with a GUI

This will guide you for making python programs with a QT based GUI. All the examples can be copied and pasted into a text file (for example, notepad++), named with .pyw extension, and double clicked to run.

The examples just work, so it is a great way to see how things are done.

The GUI is designed directly in the python program, via text commands. So it is an approach that does not need any external files.

It is a good way to prepare for the QT Designer application where the GUI is built by dragging widgets from a pallet and placing them in the window.

Ready for hello world (in a console window)¶

Create a text file call hello.py

Copy the following python code into the file and save it

python print("Hello World") # first program input("") # wait for user to press a key

To launch console python programs with python.py¶

  • Right click hello.py and select open with...
  • Select python.exe as default.
  • It is in C:\Anaconda3\python.exe
  • After that double click any .py file to run it

You can also run it here:¶

(but you do not want the input("") line here...)

print("Hello World.")
Hello World.

Ready for hello instrument (in a console window)¶

Create a text file call hello inst.py

Copy this into the file and save it

import visa
rm= visa.ResourceManager()
rm.list_resources()
vna = rm.open_resource('TCPIP::192.168.1.6::INSTR')
print(vna.query('*idn?'))
# input("")   # if runningin a console

Double click the file to run it.

For more guidance on pyVisa go to http://pyvisa.readthedocs.io/en/stable/

You can also run it here:¶

 

FFT of a pulse¶

%matplotlib inline
%config InlineBackend.figure_format = 'retina'

import numpy as np
import matplotlib.pyplot as plt
import seaborn
from scipy import fft


Fs = 800                         # sampling rate
Ts = 1.0/Fs                      # sampling interval
t = np.arange(0,1,Ts)            # time vector
ff = 5                           # frequency of the signal

nPulse = int(Fs/20)  
y = np.ones(nPulse)
y = np.append(y, np.zeros(Fs-nPulse))

plt.subplot(2,1,1)
plt.plot(t,y,'k-')
plt.xlabel('time')
plt.ylabel('amplitude')

plt.subplot(2,1,2)
n = len(y)                       # length of the signal
k = np.arange(n)
T = n/Fs
frq = k/T                        # two sides frequency range
freq = frq[1:int(n/2)]           # one side frequency range

Y = np.fft.fft(y)/n              # fft computing and normalization
Y = Y[1:int(n/2)]

plt.plot(freq, abs(Y), 'r-')
plt.xlabel('freq (Hz)')
plt.ylabel('|Y(freq)|')

plt.show()
Example: Yellow call out box

Does that work?

Sucess: green box
Tip: blue call out box

List option one¶

  1. Press Preset
  2. Press Points key in 2001 and press ENTER
  3. Press Start and key in 1 GHz
  4. Press Stop and key in 3 GHz
  5. Press BW and key in 1 KHz
  6. don't do this PRESET anymore
 
%matplotlib inline
%config InlineBackend.figure_format = 'retina'


import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)

dt = 0.01
Fs = 1/dt
t = np.arange(0, 10, dt)
nse = np.random.randn(len(t))
r = np.exp(-t/0.05)

cnse = np.convolve(nse, r)*dt
cnse = cnse[:len(t)]
s = 0.1*np.sin(2*np.pi*t) + cnse

plt.subplot(3, 2, 1)
plt.plot(t, s)

plt.subplot(3, 2, 3)
plt.magnitude_spectrum(s, Fs=Fs)

plt.subplot(3, 2, 4)
plt.magnitude_spectrum(s, Fs=Fs, scale='dB')

plt.subplot(3, 2, 5)
plt.angle_spectrum(s, Fs=Fs)

plt.subplot(3, 2, 6)
plt.phase_spectrum(s, Fs=Fs)

plt.show()