51
MIN-Fakultät Fachbereich Informatik Arbeitsbereich SAV/BV (KOGS) MMS-Übungen Einführung in die Signalanalyse mit Python Wintersemester 2016/17 Benjamin Seppke

Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

MIN-FakultätFachbereich Informatik

Arbeitsbereich SAV/BV (KOGS)

MMS-Übungen

EinführungindieSignalanalysemitPython

Wintersemester2016/17

BenjaminSeppke

Page 2: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Outline

l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 2

Page 3: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Outline

l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 3

Page 4: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 5: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 6: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 7: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Outline

l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 7

Page 8: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

IntroducingPython

Thefollowingintroductionisbasedontheofficial„Python-Tutorial“

http://docs.python.org/tutorial/index.html

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 8

Page 9: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 10: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 11: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 12: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 13: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 14: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 15: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 16: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 17: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 18: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 19: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 20: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 21: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 22: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 23: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 24: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 25: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 26: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 27: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 28: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Outline

l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 28

Page 29: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 30: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 31: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 32: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 33: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 34: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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 ...

Page 35: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 36: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 37: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 38: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Outline

l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 38

Page 39: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 40: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 41: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 42: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 43: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 44: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Thespyder IDE

25.10.16 University of Hamburg, Dept. Informatics 44

Page 45: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

spyder- Theeditor

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 45

Page 46: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

spyder- Theconsole

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 46

Page 47: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

spyder- Thevariableexplorer

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 47

Page 48: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

Outline

l Introductionl PresentingthePythonprogramminglanguagel SignalanalysisusingNumPyandSciPyl VisualizationwithmatplotlibandthespyderIDEl Summary

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 48

Page 49: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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

Page 50: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

SummaryII

l NumPyandSciPyl EfficientArrayimplementationl Loadingandsavingofmultidimensionalsignalsl AddsscientificstufftoPythonl Containsbasicsignalprocessingfunctionalityl Highlyactiveandwidelyrecommendedpackages

MMS-Übungen:EinführungindieSignalanalysemitPython

25.10.16 University of Hamburg, Dept. Informatics 50

Page 51: Arbeitsbereich SAV/BV (KOGS)seppke... · Outline l Introduction l Presenting the Python programming language l Signal analysis using NumPy and SciPy l Visualization with matplotlib

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