Axel Plinge Pattern Recognition, Computer Science XII, TU ...

16
Using Python for research and acoustic signal processing Axel Plinge Pattern Recognition, Computer Science XII, TU Dortmund University October 2015 Bar-Ilan University

Transcript of Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Page 1: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Using Python for research and acoustic signal processing

Axel Plinge

Pattern Recognition, Computer Science XII,TU Dortmund University

October 2015

Bar-Ilan University

Page 2: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 1/15

Motivation (1)

Why use Python?

I it’s free

I cross platform (Windows, Linux, MacOS)I clear syntax

I code is easy to readI encapsulation of code in modules & packagesI alows for object oriented programing (OOP)

I powerful languageI functional programing: list comprehension, lambda, . . .I object oriented programingI iteration, slicing, . . .

I powerful libraries – maintained by PhDs :-)I vector and matrix operations (numpy)I MATLAB-like plotting (matplotlib)I probabilistic functions & models (scipy.stats)I machine learing (sklearn, . . . )I signal processing (scipy.signal)I . . .

Page 3: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 2/15

Contents

I MotivationI Installations

I WinPythonI Eclipse IDEI Audio

I LearningI Introductions & TutorialsI Documentation

I ExamplesI InteractiveI MatrixI FunctionsI ModuleI Audio PlaybackI Audio Loopback

Page 4: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 3/15

Motivation (2) Live Demo

live acoustic event detection with visualization coded in Python

Page 5: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 4/15

Installation: Python

I Python installation

+ pre-bulid scientific libraries

+ Qt GUI framework

Hints

1. Python 2.7 recommended, as still more libraries support 2.7 than 3.x

2. Download and unzip to Program Files/WinPython

3. Register using the control panel

Page 6: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 5/15

Installation: IDE

I Comfortable IDE

I Projects

I Debugging

I Version Control integration

Setup

1. Install Java Runtime (JRE) if necessary

2. Download and install Eclipse (current version is ‘Mars’)

3. Go to Help > Install new software and add http://pydev.org/updates

Page 7: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 6/15

Installation: Audio

/ Audio support from scipy is still rudimentary

, Several good projects exists..

Setup

1. open the WinPython console

2. pip install sounddevice --user

3. pip install pysoundfile --user

Page 9: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 8/15

Documentation

I Python 2.7

I numpy

I scipy

I matplotlib

Page 10: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 9/15

Interactive example>>> 1+12>>> import math>>> math.pi3.141592653589793>>> math.sin(math.pi/2.0)1.0>>> help(math)Help on built-in module math:

NAMEmath

FILE(built-in)

DESCRIPTIONThis module is always available. It provides access to themathematical functions defined by the C standard.

FUNCTIONSacos(...)

acos(x)Return the arc cosine (measured in radians) of x.

acosh(...)acosh(x)Return the inverse hyperbolic cosine of x.

Page 11: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 10/15

Matrix example

>>> import numpy as np>>> a=np.eye(3)>>> aarray([[ 1., 0., 0.],

[ 0., 1., 0.],[ 0., 0., 1.]])

>>> a[:,2]array([ 0., 0., 1.])>>> a[:,:2]array([[ 1., 0.],

[ 0., 1.],[ 0., 0.]])

>>> a[:2,:2]*5array([[ 5., 0.],

[ 0., 5.]])>>> b=np.array([[1,2,3]])>>> b.shape(1, 3)>>> a.shape(3, 3)>>> a*barray([[ 1., 0., 0.],

[ 0., 2., 0.],[ 0., 0., 3.]])

Page 12: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 11/15

Function example

>>> def doubleit(x):... return x*2...

>>> doubleit(5)10

>>> for i in range(3):... doubleit(i)...024

>>> [doubleit(i) for i in range(3)][0, 2, 4]

>>> a=[doubleit(i) for i in range(3)]>>> for ai in a:... doubleit(ai)...048

Page 13: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 12/15

Module example

A module is a file, it can contain variables, functions and classes

accumen.py’’’ variable is global within this module ’’’accumulator = 0

’’’ define function, global within this module ’’’def accum(x):

’’’ use the global variable ’’’global accumulator

accumulator += xprint accumulatorreturn accumulator

use it from another file or interpreterimport accumen as a

a.accum(1)print a.accumulator

Page 14: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 13/15

Playback example

import soundfileimport sounddevice as sd

data, fs = soundfile.read(’sentence.wav’)sd.play(data,fs)

Page 15: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 14/15

Loopback example

import numpy as npimport sounddevice as sdDURATION = 10 # secondsSAMPLERATE = 48000BLOCKSIZE = 4096

def callback(indata, outdata, frames, time, status):global theFilterif status:

print(status)filtered = theFilter.process(indata)outdata[:,1] = filteredoutdata[:,0] = filtered

ios = sd.Stream(samplerate=SAMPLERATE, channels=2, callback=callback,dtype=np.int16, blocksize=BLOCKSIZE)

ios.start()for i in xrange(10*DURATION):

sd.sleep(100)ios.stop()

Page 16: Axel Plinge Pattern Recognition, Computer Science XII, TU ...

Axel Plinge Using Python for research and acoustic signalprocessing 15/15

Overlapp-Add Filterimport numpy as npimport scipy

class OverlapAdd(object):def __init__(self,blocksize,gain):

self.blocksize= blocksizeself.gain = gainself.window = scipy.hanning(blocksize*2)self.lastin = np.zeros((blocksize),dtype=np.int16)self.overlap = np.zeros((blocksize),dtype=np.int16)self.lastout = np.zeros((blocksize),dtype=np.int16)self.inbuf = np.zeros((2*blocksize),dtype=np.int16)

def process(self,indata):self.inbuf[:self.blocksize] = self.lastinself.lastin = indata[:,0] * self.gainself.inbuf[self.blocksize:] = self.lastinself.inbuf = self.inbuf * self.windowself.filtered = scipy.fft(self.inbuf)self.filtered = self.stft_filter(self.filtered)self.filtered = scipy.real(scipy.ifft(self.filtered))self.filtered[:self.blocksize] += self.overlapself.overlap = self.filtered[self.blocksize:]self.lastout = self.filtered[:self.blocksize]return self.lastout