MIN-Fakutät Fachbereich Informatik Arbeitsbereich SAV/BV (KOGS) MMS-Übungen Einführung in die Signaanayse mit Python Wintersemester 2016/17 Benjamin Seppke
MMS-Übungen: Einführung in die Signaanayse mit Python Outine Introduction Presenting the Python programming anguage Signa anaysis using NumPy and SciPy Visuaization with matpotib and the spyder IDE Summary 25.10.16 University of Hamburg, Dept. Informatics 2
MMS-Übungen: Einführung in die Signaanayse mit Python Outine Introduction Presenting the Python programming anguage Signa anaysis using NumPy and SciPy Visuaization with matpotib and the spyder IDE Summary 25.10.16 University of Hamburg, Dept. Informatics 3
MMS-Übungen: Einführung in die Signaanayse mit Python Prerequisites (Software) Python (we use version 3.X) http://www.python.org NumPy and SciPy (with PIL: http://www.pythonware.com/products/pi ) http://www.scipy.org matpotib http://matpotib.org spyder IDE http://code.googe.com/p/spyderib 25.10.16 University of Hamburg, Dept. Informatics 4
MMS-Übungen: Einführung in die Signaanayse mit Python Instaing Python and packages Linux A of the prerequisites shoud be instaabe by means of the package manager of the distribution of your choice. Mac OS X (macos) Insta the MacPorts package manager (http://www.macports.org) and use this to get a necessary packages. Windows The Anaconda distribution brings with everything pus a nice instaer. Downoad and insta it from: https://www.continuum.io/downoads Note: You may aso use Anaconda for Linux or Mac OS X, if you prefer to. 25.10.16 University of Hamburg, Dept. Informatics 5
MMS-Übungen: Einführung in die Signaanayse mit Python Goas for today... Draw interest to another programming anguage, namey: Python Motivation of an interactive Workfow ( Spiewiese ) Easy access into practica image processing tasks using NumPy, SciPy, matpotib and spyder Finay: Give you the abiity to sove the exercises of this course 25.10.16 University of Hamburg, Dept. Informatics 6
MMS-Übungen: Einführung in die Signaanayse mit Python Outine Introduction Presenting the Python programming anguage Signa anaysis using NumPy and SciPy Visuaization with matpotib and the spyder IDE Summary 25.10.16 University of Hamburg, Dept. Informatics 7
MMS-Übungen: Einführung in die Signaanayse mit Python Introducing Python The foowing introduction is based on the officia Python-Tutoria http://docs.python.org/tutoria/index.htm 25.10.16 University of Hamburg, Dept. Informatics 8
MMS-Übungen: Einführung in die Signaanayse mit Python Python Python is an easy to earn, powerfu programming anguage. [...] Python s eegant syntax and dynamic typing, together with its interpreted nature, make it an idea anguage for scripting and rapid appication deveopment in many areas on most patforms. By the way, the anguage is named after the BBC show Monty Python s Fying Circus and has nothing to do with repties. The Python Tutoria, Sep. 2010 25.10.16 University of Hamburg, Dept. Informatics 9
MMS-Übungen: Einführung in die Signaanayse mit Python Why another anguage? Why Python? Interactive: no code/compie/test-cyce! A ot of currenty needed and easy accessibe functionaity compared with traditiona scripting anguages! Patform independent and freey avaiabe! Large user base and good documentation! Forces compactness and readabiity of programs by syntax! Some say: can be earned in 10 minutes... 25.10.16 University of Hamburg, Dept. Informatics 10
MMS-Übungen: Einführung in die Signaanayse mit Python Getting in touch with Python (3.X) A of this tutoria wi use the interactive mode: Start the interpreter: python Or, for the advanced ipython interpreter: ipython 1. Exampe: > python Python 3.5.2 Anaconda 4.2.0 (64-bit) (defaut, Ju 5 2016, 11:41:13) [MSC v.1900 64 bit (AMD64)] Type "hep", "copyright", "credits" or "icense" for more information. >>> the_word_is_fat = True >>> if the_word_is_fat:... print("be carefu not to fa off! )... Be carefu not to fa off! 25.10.16 University of Hamburg, Dept. Informatics 11
MMS-Übungen: Einführung in die Signaanayse mit Python Data types numbers (1) Python supports integer, foating point and compex vaued numbers by defaut: >>> 2+2 4 >>> # This is a comment... 2+2 4 >>> # Integer division returns the foor:... 7/3 2 >>> 7.0 / 2 # but this works... 3.5 >>> 1.0j * 1.0j (-1+0j) 25.10.16 University of Hamburg, Dept. Informatics 12
MMS-Übungen: Einführung in die Signaanayse mit Python Data types numbers (2) Assignments and conversions: >>> a=3.0+4.0j >>> foat(a) Traceback (most recent ca ast): Fie "<stdin>", ine 1, in? TypeError: can't convert compex to foat; use abs(z) >>> a.rea 3.0 >>> a.imag 4.0 >>> abs(a) # sqrt(a.rea**2 + a.imag**2) 5.0 25.10.16 University of Hamburg, Dept. Informatics 13
MMS-Übungen: Einführung in die Signaanayse mit Python Specia variabes Exampe: ast resut _ (ony in interactive mode): >>> tax = 12.5 / 100 >>> price = 100.50 >>> price * tax 12.5625 >>> price + _ 113.0625 >>> round(_, 2) 113.06 Many more, when using ipython! 25.10.16 University of Hamburg, Dept. Informatics 14
MMS-Übungen: Einführung in die Signaanayse mit Python Data types strings Sequences of chars (ike e.g. in C), but immutabe! >>> word = 'Hep' + 'A' >>> word 'HepA' >>> '<' + word*5 + '>' '<HepAHepAHepAHepAHepA>' >>> 'str' 'ing' # <- This is ok 'string' >>> word[4] 'A' >>> word[0:2] 'He' >>> word[2:] # Everything except the first two characters 'pa' 25.10.16 University of Hamburg, Dept. Informatics 15
MMS-Übungen: Einführung in die Signaanayse mit Python Data types ists Lists may contain different types of entries at once! First eement has index: 0, ast eement: ength-1. >>> a = ['spam', 'eggs', 100, 1234] >>> a ['spam', 'eggs', 100, 1234] >>> a[0] 'spam' >>> a[-2] 100 >>> a[1:-1] ['eggs', 100] >>> a[:2] + ['bacon', 2*2] ['spam', 'eggs', 'bacon', 4] 25.10.16 University of Hamburg, Dept. Informatics 16
MMS-Übungen: Einführung in die Signaanayse mit Python The first program (1) Counting Fibonacci series >>> # Fibonacci series:... # the sum of two eements defines the next... a, b = 0, 1 >>> whie b < 10:... print b... a, b = b, a+b... 1 1 2 3 5 8 25.10.16 University of Hamburg, Dept. Informatics 17
MMS-Übungen: Einführung in die Signaanayse mit Python The first program (2) Counting Fibonacci series (with a coon after the print) >>> # Fibonacci series:... # the sum of two eements defines the next... a, b = 0, 1 >>> whie b < 10:... print b,... a, b = b, a+b... 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 25.10.16 University of Hamburg, Dept. Informatics 18
MMS-Übungen: Einführung in die Signaanayse mit Python Conditionas if Divide cases in if/then/ese manner: >>> x = int(raw_input("pease enter an integer: ")) Pease enter an integer: 42 >>> if x < 0:... x = 0... print 'Negative changed to zero'... eif x == 0:... print 'Zero'... eif x == 1:... print 'Singe'... ese:... print 'More'... More 25.10.16 University of Hamburg, Dept. Informatics 19
MMS-Übungen: Einführung in die Signaanayse mit Python Contro fow for (1) Python s for-oop: >>> # Measure the ength of some strings:... a = ['two', 'three', 'four'] >>> for x in a:... print x, en(x)... two 3 three 5 four 4 is actuay a for-each-oop! 25.10.16 University of Hamburg, Dept. Informatics 20
MMS-Übungen: Einführung in die Signaanayse mit Python Contro fow for (2) What about a counting for oop? Quite easy to get: >>> a = ['Mary', 'had', 'a', 'itte', 'amb'] >>> for i, va in enumerate(a):... print i, va... 0 Mary 1 had 2 a 3 itte 4 amb 25.10.16 University of Hamburg, Dept. Informatics 21
MMS-Übungen: Einführung in die Signaanayse mit Python Defining functions (1) Functions are one of the most important way to abstract from probems and to design programs: >>> def fib(n): # write Fibonacci series up to n... """Print a Fibonacci series up to n."""... a, b = 0, 1... whie a < n:... print a,... a, b = b, a+b... >>> # Now ca the function we just defined:... fib(2000) 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 25.10.16 University of Hamburg, Dept. Informatics 22
MMS-Übungen: Einführung in die Signaanayse mit Python Defining functions (2) Functions are (themseves) just Python symbos! >>> fib <function fib at 10042ed0> >>> f = fib >>> f(100) 0 1 1 2 3 5 8 13 21 34 55 89 No expicit return vaue needed (defaut: None ) >>> fib(0) >>> print fib(0) None 25.10.16 University of Hamburg, Dept. Informatics 23
MMS-Übungen: Einführung in die Signaanayse mit Python Defining functions (3) Fibonacci series with a ist of numbers as return vaue: >>> def fib2(n): # return Fibonacci series up to n... """Return a ist containing the Fibonacci series up to n."""... resut = []... a, b = 0, 1... whie a < n:... resut.append(a) # see beow... a, b = b, a+b... return resut... >>> f100 = fib2(100) # ca it >>> f100 # write the resut [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] 25.10.16 University of Hamburg, Dept. Informatics 24
MMS-Übungen: Einführung in die Signaanayse mit Python Function argument definitions (1) Named defaut arguments: def ask_ok(prompt, retries=4, compaint='yes or no, pease!'): whie True: ok = raw_input(prompt) if ok in ('y', 'ye', 'yes'): return True if ok in ('n', 'no', 'nop', 'nope'): return Fase retries = retries - 1 if retries < 0: raise IOError('refuse user') print compaint 25.10.16 University of Hamburg, Dept. Informatics 25
MMS-Übungen: Einführung in die Signaanayse mit Python Function argument definitions (2) Caing strategy in more detai: def parrot(votage, state='a stiff', action='voom', type='norwegian Bue'): print "-- This parrot woudn't", action, print "if you put", votage, "vots through it." print "-- Lovey pumage, the", type print "-- It's", state, "!" parrot(1000) parrot(action = 'VOOOOOM', votage = 1000000) parrot('a thousand', state = 'pushing up the daisies') parrot('a miion', 'bereft of ife', 'jump') 25.10.16 University of Hamburg, Dept. Informatics 26
MMS-Übungen: Einführung in die Signaanayse mit Python Modues If you have saved this as fibo.py : # Fibonacci numbers modue def fib(n): # return Fibonacci series up to n resut = [] a, b = 0, 1 whie b < n: resut.append(b) a, b = b, a+b return resut you have aready written your first Python modue. Ca it using: >>> import fibo >>> fibo.fib(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] 25.10.16 University of Hamburg, Dept. Informatics 27
MMS-Übungen: Einführung in die Signaanayse mit Python Outine Introduction Presenting the Python programming anguage Signa anaysis using NumPy and SciPy Visuaization with matpotib and the spyder IDE Summary 25.10.16 University of Hamburg, Dept. Informatics 28
MMS-Übungen: Einführung in die Signaanayse mit Python Signa anaysis using NumPy and SciPy Unfortunatey, it is not possibe to give a compete introduction in either NumPy or SciPy. The image processing introduction is based on: http://scipyectures.github.io/advanced/image_processing More materia regarding NumPy can e.g. be found at: http://numpy.scipy.org A good beginner s tutoria is provided at: http://www.scipy.org/tentative_numpy_tutoria 25.10.16 University of Hamburg, Dept. Informatics 29
MMS-Übungen: Einführung in die Signaanayse mit Python Discrete Signas as efficient arrays?! In many programming environments, ike e.g. MatLab, signas are represented as (random access) arrays of different data types. Unfortunatey, Python s buit-in array is often neither fexibe nor powerfu enough for signa anaysis Thus: Use NumPy arrays for signa representation. Idea of a first (very basic) workfow: Load signas using csv- or NumPy binary formats Anayze the signas using NumPy (and maybe SciPy) Save the resuts using csv- or numpy binary formats. 25.10.16 University of Hamburg, Dept. Informatics 30
MMS-Übungen: Einführung in die Signaanayse mit Python NumPy at a gance NumPy is the fundamenta package needed for scientific computing with Python. It contains among other things: a powerfu N-dimensiona array object [ ] NumPy Homepage, 2010 May have required a whoe course on its own... Sti growing scientific user community Reiabe agorithms Quite fast, compared to commercia software impementations, ike MatLab 25.10.16 University of Hamburg, Dept. Informatics 31
MMS-Übungen: Einführung in die Signaanayse mit Python Loading and storing signas Generate a noisy rect signa and save it: >>> import numpy as np >>> sig = np.zeros(3000) >>> sig[1000:2000]=1 >>> sig = sig + np.random.norma(0, 0.01, 3000) >>> np.save( FILENAME.npy, sig) # numpy binary format >>> np.savetxt( FILENAME.csv, sig) # comma-separated-vaues Read the signa from fie system >>> import numpy as np >>> sig_npy = np.oad( FILENAME.npy ) >>> sig_csv = np.oadtxt( FILENAME.csv ) Attention: CSV export may reduce (foat) accurancy! 25.10.16 University of Hamburg, Dept. Informatics 32
MMS-Übungen: Einführung in die Signaanayse mit Python NumPy signa representation (1) Note that numpy arrays are fixed to one unique data type, which is foat64 a.k.a. doube by defaut... >>> def_sig = np.zeros(1000) >>> def_sig.dtype dtype( foat64 ) >>> int_sig = np.zeros(1000, dtype=np.uint8) # vaues from 0..255 Pay attention to the datatype, especiay when performing mathematica operations! 25.10.16 University of Hamburg, Dept. Informatics 33
MMS-Übungen: Einführung in die Signaanayse mit Python NumPy signa representation (2) Singe vs. mutidimensiona signas:... >>> sig1d = np.zeros(1000) >>> sig2d = np.zeros( (1000,2000) ) Note: The shape is aways passed as one parameter Either a number (1D) or A ist for n-dimensiona arrays Access on mutidimensiona arrays: >>> sig2d[800, 1800] 0 >>> sig2d[1800, 800] IndexError: index 1800 is out of bounds... 25.10.16 University of Hamburg, Dept. Informatics 34
MMS-Übungen: Einführung in die Signaanayse mit Python NumPy sicing and index tricks Extract dimensions using sicing >>> sig2d[:,0] # first signa (fixing second dimension) >>> sig2d[...,-1] # ast signa (fixing second dimension) Extract sub-signas using index ranges: >>> sig = np.zeros(3000) >>> sig[1000:2000]=1 Attention: NumPy often creates views and does not copy your data, when using index tricks! à Compare to Ca-By-Reference Semantics 25.10.16 University of Hamburg, Dept. Informatics 35
MMS-Übungen: Einführung in die Signaanayse mit Python Basic signa anaysis (1) Exampe: Adding energy / signas:... >>> res1 = 2 + sig >>> res2 = sig + sig >>> res2 = sig * sig Note: Scaars and arrays (of compatibe shape) can be combined Basic arithmetic functions Many more (advanced functions) 2D-Arrays may aso be interpreted as matrices, but compare to np.matrix cass! 25.10.16 University of Hamburg, Dept. Informatics 36
MMS-Übungen: Einführung in die Signaanayse mit Python Basic signa anaysis (2) Exampe: Threshod a signa (at a given ampitude):... >>> mask = sig < 0.5 >>> masked_sig = sig.copy() >>> masked_sig[mask] = 0 25.10.16 University of Hamburg, Dept. Informatics 37
MMS-Übungen: Einführung in die Signaanayse mit Python Outine Introduction Presenting the Python programming anguage Signa anaysis using NumPy and SciPy Visuaization with matpotib and the spyder IDE Summary 25.10.16 University of Hamburg, Dept. Informatics 38
MMS-Übungen: Einführung in die Signaanayse mit Python Visuaization with matpotib matpotib is a python 2D potting ibrary which produces pubication quaity figures in a variety of hardcopy formats and interactive environments across patforms. matpotib can be used in python scripts, the python and ipython she... http://matpotib.org, October 2013 This introduction is based on the matpotib image tutoria: http://matpotib.org/users/image_tutoria.htm 25.10.16 University of Hamburg, Dept. Informatics 39
MMS-Übungen: Einführung in die Signaanayse mit Python Showing signas interactivey (1) Use matpotib to show a signa pot: >>> import numpy as np >>> import matpotib.pypot as pt >>> sig = np.oad( FILENAME.npy ) # Load the signa >>> sig_pot = pt.pot(sig) >>> sig_pot.show() 25.10.16 University of Hamburg, Dept. Informatics 40
MMS-Übungen: Einführung in die Signaanayse mit Python Showing signas interactivey (2) Show signa s (centered) magnitude spectrum:... >>> centered_spectrum = np.fft.fftshift(np.fft.fft(sig)) >>> spec_pot = pt.pot(abs(centered_spectrum) >>> spec_pot.show() 25.10.16 University of Hamburg, Dept. Informatics 41
MMS-Übungen: Einführung in die Signaanayse mit Python Histograms Use matpotib to inspect the histogram:... >>> pt.hist(sig, 300) # coect vaues in 300 bins >>> pt.show() 25.10.16 University of Hamburg, Dept. Informatics 42
MMS-Übungen: Einführung in die Signaanayse mit Python Working with the spyder IDE spyder (previousy known as Pydee) is a powerfu interactive deveopment environment for the Python anguage with advanced editing, interactive testing, debugging and introspection features.[...] spyder ets you easiy work with the best toos of the Python scientific stack in a simpe yet powerfu environment.[...] http://code.googe.com/p/spyderib, October 2013 The screenshots of this introduction have been taken from the spyder homepage. 25.10.16 University of Hamburg, Dept. Informatics 43
The spyder IDE 25.10.16 University of Hamburg, Dept. Informatics 44
MMS-Übungen: Einführung in die Signaanayse mit Python spyder - The editor 25.10.16 University of Hamburg, Dept. Informatics 45
MMS-Übungen: Einführung in die Signaanayse mit Python spyder - The consoe 25.10.16 University of Hamburg, Dept. Informatics 46
MMS-Übungen: Einführung in die Signaanayse mit Python spyder - The variabe exporer 25.10.16 University of Hamburg, Dept. Informatics 47
MMS-Übungen: Einführung in die Signaanayse mit Python Outine Introduction Presenting the Python programming anguage Signa anaysis using NumPy and SciPy Visuaization with matpotib and the spyder IDE Summary 25.10.16 University of Hamburg, Dept. Informatics 48
MMS-Übungen: Einführung in die Signaanayse mit Python Summary I The Python programming anguage Readabe, meaningfu syntax (remember the tabs!) Highy functiona, fu of functionaity Steep earning experience and fast resuts Perfecty practicabe for interactive work Can be extended easiy Large goba community 25.10.16 University of Hamburg, Dept. Informatics 49
MMS-Übungen: Einführung in die Signaanayse mit Python Summary II NumPy and SciPy Efficient Array impementation Loading and saving of mutidimensiona signas Adds scientific stuff to Python Contains basic signa processing functionaity Highy active and widey recommended packages 25.10.16 University of Hamburg, Dept. Informatics 50
MMS-Übungen: Einführung in die Signaanayse mit Python Summary III matpotib Pots amost everything... Works we with NumPy arrays spyder Nice IDE Integrates scientific work fow (a bit ike MatLab) Everything is there and freey avaiabe: Time to start with the exercises! 25.10.16 University of Hamburg, Dept. Informatics 51