arbeitsbereich sav/bv (kogs)seppke... · outline l introduction l presenting the python programming...
TRANSCRIPT
MIN-FakultätFachbereich Informatik
Arbeitsbereich SAV/BV (KOGS)
MMS-Übungen
EinführungindieSignalanalysemitPython
Wintersemester2016/17
BenjaminSeppke
Outline
l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 2
Outline
l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 3
Prerequisites(Software)
l Python(weuseversion3.X)l http://www.python.org
l NumPyandSciPy(withPIL:http://www.pythonware.com/products/pil )l http://www.scipy.org
l matplotlibl http://matplotlib.org
l spyderIDEl http://code.google.com/p/spyderlib
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 4
InstallingPythonandpackages
l Linuxl Alloftheprerequisitesshouldbeinstallablebymeansofthepackagemanager
ofthedistributionofyourchoice.
l MacOSX(macOS)l InstalltheMacPorts packagemanager(http://www.macports.org)andusethis
togetallnecessarypackages.
l Windowsl TheAnacondadistributionbringswitheverything– plusaniceinstaller.
Downloadandinstallitfrom:https://www.continuum.io/downloads
Note:YoumayalsouseAnacondaforLinuxorMacOSX,ifyoupreferto.
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 5
Goalsfortoday...
l Drawinteresttoanotherprogramminglanguage,namely:Python
l MotivationofaninteractiveWorkflow(„Spielwiese“)l „Easyaccess”intopracticalimageprocessingtasksusingNumPy,SciPy,matplotlibandspyder
l Finally:Giveyoutheabilitytosolvetheexercisesofthiscourse
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 6
Outline
l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 7
IntroducingPython
Thefollowingintroductionisbasedontheofficial„Python-Tutorial“
http://docs.python.org/tutorial/index.html
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 8
Python„Pythonisaneasytolearn,powerfulprogramminglanguage.[...]
Python’selegantsyntaxanddynamictyping,togetherwithitsinterpretednature,makeitanideallanguageforscriptingandrapidapplicationdevelopmentinmanyareasonmostplatforms.“
„Bytheway,thelanguageisnamedaftertheBBCshow“MontyPython’sFlyingCircus”andhasnothingtodowithreptiles.“
ThePythonTutorial,Sep.2010
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 9
Whyanotherlanguage?WhyPython?
l Interactive:nocode/compile/test-cycle!l Alotofcurrentlyneededandeasyaccessiblefunctionalitycomparedwithtraditionalscriptinglanguages!
l Platformindependentandfreelyavailable!l Largeuserbaseandgooddocumentation!l Forcescompactnessandreadabilityofprogramsbysyntax!
l Somesay:canbelearnedin10minutes...
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 10
GettingintouchwithPython(3.X)
l Allofthistutorialwillusetheinteractivemode:l Starttheinterpreter:pythonl Or,fortheadvancediPython interpreter:ipython
1.Example:
MMS-Übungen:EinführungindieSignalanalysemitPython
> python
Python 3.5.2 |Anaconda 4.2.0 (64-bit)| (default, Jul 5 2016, 11:41:13)
[MSC v.1900 64 bit (AMD64)]
Type "help", "copyright", "credits" or "license" for more information.
>>> the_world_is_flat = True
>>> if the_world_is_flat:
... print("Be careful not to fall off!“)
...
Be careful not to fall off!
25.10.16 University of Hamburg, Dept. Informatics 11
Datatypes– numbers(1)
l Pythonsupportsinteger,floatingpointandcomplexvaluednumbersbydefault:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> 2+2
4
>>> # This is a comment
... 2+2
4
>>> # Integer division returns the floor:
... 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
Datatypes– numbers(2)
l Assignmentsandconversions:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> a=3.0+4.0j
>>> float(a)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: can't convert complex to float; use abs(z)
>>> a.real
3.0
>>> a.imag
4.0
>>> abs(a) # sqrt(a.real**2 + a.imag**2)
5.0
25.10.16 University of Hamburg, Dept. Informatics 13
Specialvariables
l Example:lastresult„_“(onlyininteractivemode):
l Manymore,whenusingiPython!
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> tax = 12.5 / 100
>>> price = 100.50
>>> price * tax
12.5625
>>> price + _
113.0625
>>> round(_, 2)
113.06
25.10.16 University of Hamburg, Dept. Informatics 14
Datatypes– strings
l Sequencesofchars(likee.g.inC),butimmutable!
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'
>>> 'str' 'ing' # <- This is ok
'string'
>>> word[4]
'A'
>>> word[0:2]
'He'
>>> word[2:] # Everything except the first two characters
'lpA'
25.10.16 University of Hamburg, Dept. Informatics 15
Datatypes– lists
l Listsmaycontaindifferenttypesofentriesatonce!l Firstelementhasindex:0,lastelement:length-1.
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> 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
Thefirstprogram(1)
l CountingFibonacciseries
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while b < 10:
... print b
... a, b = b, a+b
...
1
1
2
3
5
8
25.10.16 University of Hamburg, Dept. Informatics 17
Thefirstprogram(2)
l CountingFibonacciseries(withacolonaftertheprint)
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> # Fibonacci series:
... # the sum of two elements defines the next
... a, b = 0, 1
>>> while 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
Conditionals– if
l Dividecasesinif/then/elsemanner:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> x = int(raw_input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
More
25.10.16 University of Hamburg, Dept. Informatics 19
Controlflow– for(1)
l Python‘sfor-loop:
l isactuallyafor-each-loop!
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> # Measure the length of some strings:
... a = ['two', 'three', 'four']
>>> for x in a:
... print x, len(x)
...
two 3
three 5
four 4
25.10.16 University of Hamburg, Dept. Informatics 20
Controlflow– for(2)
l Whataboutacountingforloop?l Quiteeasytoget:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> a = ['Mary', 'had', 'a', 'little', 'lamb']
>>> for i, val in enumerate(a):
... print i, val
...
0 Mary
1 had
2 a
3 little
4 lamb
25.10.16 University of Hamburg, Dept. Informatics 21
Definingfunctions(1)
l Functionsareoneofthemostimportantwaytoabstractfromproblemsandtodesignprograms:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> def fib(n): # write Fibonacci series up to n
... """Print a Fibonacci series up to n."""
... a, b = 0, 1
... while a < n:
... print a,
... a, b = b, a+b
...
>>> # Now call 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
Definingfunctions(2)
l Functionsare(themselves)justPythonsymbols!
l Noexplicitreturnvalueneeded(default:“None”)
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> fib
<function fib at 10042ed0>
>>> f = fib
>>> f(100)
0 1 1 2 3 5 8 13 21 34 55 89
>>> fib(0)
>>> print fib(0)
None
25.10.16 University of Hamburg, Dept. Informatics 23
Definingfunctions(3)
l Fibonacciserieswithalistofnumbersasreturnvalue:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> def fib2(n): # return Fibonacci series up to n
... """Return a list containing the Fibonacci series up to n."""
... result = []
... a, b = 0, 1
... while a < n:
... result.append(a) # see below
... a, b = b, a+b
... return result
...
>>> f100 = fib2(100) # call it
>>> f100 # write the result
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
25.10.16 University of Hamburg, Dept. Informatics 24
Functionargumentdefinitions(1)
l Nameddefaultarguments:
MMS-Übungen:EinführungindieSignalanalysemitPython
def ask_ok(prompt, retries=4, complaint='Yes or no, please!'):
while True:
ok = raw_input(prompt)
if ok in ('y', 'ye', 'yes'):
return True
if ok in ('n', 'no', 'nop', 'nope'):
return False
retries = retries - 1
if retries < 0:
raise IOError('refuse user')
print complaint
25.10.16 University of Hamburg, Dept. Informatics 25
Functionargumentdefinitions(2)
l Callingstrategyinmoredetail:
MMS-Übungen:EinführungindieSignalanalysemitPython
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'):
print "-- This parrot wouldn't", action,
print "if you put", voltage, "volts through it."
print "-- Lovely plumage, the", type
print "-- It's", state, "!"
parrot(1000)
parrot(action = 'VOOOOOM', voltage = 1000000)
parrot('a thousand', state = 'pushing up the daisies')
parrot('a million', 'bereft of life', 'jump')
25.10.16 University of Hamburg, Dept. Informatics 26
Modules
l Ifyouhavesavedthisas„fibo.py“:
…youhavealreadywrittenyourfirstPythonmodule.Callitusing:
MMS-Übungen:EinführungindieSignalanalysemitPython
# Fibonacci numbers module
def fib(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
>>> 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
Outline
l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 28
Signalanalysis usingNumPyandSciPy
Unfortunately,itisnotpossibletogiveacompleteintroductionineitherNumPyorSciPy.
Theimageprocessingintroductionisbasedon:http://scipy-
lectures.github.io/advanced/image_processing
MorematerialregardingNumPycane.g.befoundat:http://numpy.scipy.org
Agoodbeginner‘stutorialisprovidedat:http://www.scipy.org/Tentative_NumPy_Tutorial
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 29
DiscreteSignalsasefficient arrays?!
l Inmanyprogrammingenvironments,likee.g.MatLab,signalsarerepresentedas(randomaccess)arraysofdifferentdatatypes.
l Unfortunately,Python‘sbuilt-inarrayisoftenneitherflexiblenorpowerfulenoughforsignalanalysis
l Thus:UseNumPyarraysforsignalrepresentation.l Ideaofafirst(verybasic)workflow:
l Loadsignalsusingcsv- orNumPybinaryformatsl AnalyzethesignalsusingNumPy(andmaybeSciPy)l Savetheresultsusingcsv- ornumpybinaryformats.
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 30
NumPyataglance• „NumPyisthefundamentalpackageneededforscientific
computingwithPython.Itcontainsamongotherthings:apowerfulN-dimensionalarrayobject[…]“
• NumPyHomepage,2010
l Mayhaverequiredawholecourseonitsown...l Stillgrowingscientificusercommunityl Reliablealgorithmsl Quitefast,comparedtocommercialsoftwareimplementations,likeMatLab
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 31
Loadingandstoringsignals
l Generateanoisyrect signalandsaveit:
l Readthesignalfromfilesystem
l Attention:CSVexportmayreduce(float)accurancy!
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> import numpy as np
>>> sig = np.zeros(3000)
>>> sig[1000:2000]=1
>>> sig = sig + np.random.normal(0, 0.01, 3000)
>>> np.save(„FILENAME.npy“, sig) # numpy binary format
>>> np.savetxt(„FILENAME.csv“, sig) # comma-separated-values
>>> import numpy as np
>>> sig_npy = np.load(„FILENAME.npy“)
>>> sig_csv = np.loadtxt(„FILENAME.csv“)
25.10.16 University of Hamburg, Dept. Informatics 32
NumPysignalrepresentation(1)
l Notethatnumpyarraysarefixedtooneuniquedatatype,whichisfloat64 a.k.a. double bydefault
l Payattentiontothedatatype,especiallywhenperformingmathematicaloperations!
MMS-Übungen:EinführungindieSignalanalysemitPython
...
>>> def_sig = np.zeros(1000)
>>> def_sig.dtype
dtype(‚float64‘)
>>> int_sig = np.zeros(1000, dtype=np.uint8) # values from 0..255
25.10.16 University of Hamburg, Dept. Informatics 33
NumPysignalrepresentation(2)
l Singlevs.multidimensionalsignals:
l Note:Theshapeisalwayspassedasoneparameterl Eitheranumber(1D)orl Alistforn-dimensionalarrays
l Accessonmultidimensionalarrays:
MMS-Übungen:EinführungindieSignalanalysemitPython
...
>>> sig1D = np.zeros(1000)
>>> sig2D = np.zeros( (1000,2000) )
25.10.16 University of Hamburg, Dept. Informatics 34
>>> sig2D[800, 1800]
0 >>> sig2D[1800, 800]
IndexError: index 1800 is out of bounds ...
NumPyslicingandindextricksl Extractdimensionsusingslicing
l Extractsub-signalsusingindexranges:
l Attention:NumPyoftencreatesviewsanddoesnotcopyyourdata,whenusingindextricks!à ComparetoCall-By-ReferenceSemantics
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> sig2D[:,0] # first signal (fixing second dimension)
>>> sig2D[...,-1] # last signal (fixing second dimension)
>>> sig = np.zeros(3000)
>>> sig[1000:2000]=1
25.10.16 University of Hamburg, Dept. Informatics 35
Basicsignal analysis (1)
l Example:Addingenergy/signals:
l Note:l Scalarsandarrays(ofcompatibleshape)canbecombinedl Basicarithmeticfunctionsl Manymore(advancedfunctions)l 2D-Arraysmayalsobeinterpretedasmatrices,butcomparetonp.matrix class!
MMS-Übungen:EinführungindieSignalanalysemitPython
...>>> res1 = 2 + sig
>>> res2 = sig + sig
>>> res2 = sig * sig
25.10.16 University of Hamburg, Dept. Informatics 36
Basicsignal analysis (2)
l Example:Thresholdasignal(atagivenamplitude):
MMS-Übungen:EinführungindieSignalanalysemitPython
...>>> mask = sig < 0.5>>> masked_sig = sig.copy()>>> masked_sig[mask] = 0
25.10.16 University of Hamburg, Dept. Informatics 37
Outline
l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 38
Visualizationwithmatplotlib
“matplotlibisapython2Dplottinglibrarywhichproducespublicationqualityfiguresinavarietyofhardcopyformatsandinteractiveenvironmentsacrossplatforms.matplotlibcanbeusedinpythonscripts,thepythonandipython shell...“
http://matplotlib.org,October2013
Thisintroductionisbasedonthematplotlibimagetutorial:http://matplotlib.org/users/image_tutorial.html
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 39
Showingsignalsinteractively(1)
• Usematplotlibtoshowasignalplot:
MMS-Übungen:EinführungindieSignalanalysemitPython
>>> import numpy as np>>> import matplotlib.pyplot as plt>>> sig = np.load(„FILENAME.npy“) # Load the signal
>>> sig_plot = plt.plot(sig)>>> sig_plot.show()
25.10.16 University of Hamburg, Dept. Informatics 40
Showingsignalsinteractively(2)
• Showsignal’s(centered)magnitudespectrum:
MMS-Übungen:EinführungindieSignalanalysemitPython
...>>> centered_spectrum = np.fft.fftshift(np.fft.fft(sig))>>> spec_plot = plt.plot(abs(centered_spectrum)>>> spec_plot.show()
25.10.16 University of Hamburg, Dept. Informatics 41
Histograms
• Usematplotlibtoinspectthehistogram:
MMS-Übungen:EinführungindieSignalanalysemitPython
...>>> plt.hist(sig, 300) # collect values in 300 bins>>> plt.show()
25.10.16 University of Hamburg, Dept. Informatics 42
WorkingwiththespyderIDE„spyder(previouslyknownasPydee)isapowerfulinteractivedevelopmentenvironmentforthePythonlanguagewithadvancedediting,interactivetesting,debuggingandintrospectionfeatures.[...]spyderletsyoueasilyworkwiththebesttoolsofthePythonscientificstackinasimpleyetpowerfulenvironment.[...]“
http://code.google.com/p/spyderlib,October2013
Thescreenshotsofthisintroductionhavebeentakenfromthespyderhomepage.
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 43
Thespyder IDE
25.10.16 University of Hamburg, Dept. Informatics 44
spyder- Theeditor
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 45
spyder- Theconsole
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 46
spyder- Thevariableexplorer
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 47
Outline
l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 48
SummaryI
l ThePythonprogramminglanguagel Readable,meaningfulsyntax(rememberthetabs!)l Highlyfunctional,fulloffunctionalityl Steeplearningexperienceandfastresultsl Perfectlypracticableforinteractiveworkl Canbeextendedeasilyl Largeglobalcommunity
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 49
SummaryII
l NumPyandSciPyl EfficientArrayimplementationl Loadingandsavingofmultidimensionalsignalsl AddsscientificstufftoPythonl Containsbasicsignalprocessingfunctionalityl Highlyactiveandwidelyrecommendedpackages
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 50
SummaryIII
l matplotlibl Plotsalmosteverything...l WorkswellwithNumPyarrays
l spyderl NiceIDEl Integratesscientificworkflow(abitlikeMatLab)
l Everythingisthereandfreelyavailable:Timetostartwiththeexercises!
MMS-Übungen:EinführungindieSignalanalysemitPython
25.10.16 University of Hamburg, Dept. Informatics 51