Using Python for research and acoustic signal processing

4 downloads 0 Views 1MB Size Report
Using Python for research and acoustic signal processing. Axel Plinge. Pattern Recognition, Computer Science XII,. TU Dortmund University. October 2015.
Using Python for research and acoustic signal processing Axel Plinge Pattern Recognition, Computer Science XII, TU Dortmund University October 2015 Bar-Ilan University

Axel Plinge

Using Python for research and acoustic signal processing 1/15

Motivation (1) Why use Python? I

it’s free

I

cross platform (Windows, Linux, MacOS) clear syntax

I

I I I

I

powerful language I I I

I

code is easy to read encapsulation of code in modules & packages alows for object oriented programing (OOP) functional programing: list comprehension, lambda, . . . object oriented programing iteration, slicing, . . .

powerful libraries – maintained by PhDs :-) I I I I I I

vector and matrix operations (numpy) MATLAB-like plotting (matplotlib) probabilistic functions & models (scipy.stats) machine learing (sklearn, . . . ) signal processing (scipy.signal) ...

Axel Plinge

Using Python for research and acoustic signal processing 2/15

Contents

Motivation Installations I WinPython I Eclipse IDE I Audio I Learning I Introductions & Tutorials I Documentation I Examples I Interactive I Matrix I Functions I Module I Audio Playback I Audio Loopback I

I

Axel Plinge

Using Python for research and acoustic signal processing 3/15

Motivation (2) Live Demo

live acoustic event detection with visualization coded in Python

Axel Plinge

Using Python for research and acoustic signal processing 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

Axel Plinge

Using Python for research and acoustic signal processing 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

Axel Plinge

Using Python for research and acoustic signal processing 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

Axel Plinge

Using Python for research and acoustic signal processing 7/15

Learning Python

Introductions I

Scipy Lectures I I I I

introduction to

Python Language Math with numpy Plotting with matplotlib Science with scipy

I

Python for scientists

I

MATLAB commands in Python

Tutorials I

The Python tutorial

I

Quickstart numpy

I

scipy tutorial

Axel Plinge

Using Python for research and acoustic signal processing 8/15

Documentation

I

Python 2.7

I

numpy

I

scipy

I

matplotlib

Axel Plinge

Using Python for research and acoustic signal processing 9/15

Interactive example >>> 1+1 2 >>> import math >>> math.pi 3.141592653589793 >>> math.sin(math.pi/2.0) 1.0 >>> help(math) Help on built-in module math: NAME math FILE (built-in) DESCRIPTION This module is always available. It provides access to the mathematical functions defined by the C standard. FUNCTIONS acos(...) acos(x) Return the arc cosine (measured in radians) of x. acosh(...)

Axel Plinge

Using Python for research and acoustic signal processing 10/15

Matrix example >>> import numpy as np >>> a=np.eye(3) >>> a array([[ 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]*5 array([[ 5., 0.], [ 0., 5.]]) >>> b=np.array([[1,2,3]]) >>> b.shape (1, 3) >>> a.shape (3, 3) >>> a*b array([[ 1., 0., 0.], [ 0., 2., 0.], [ 0., 0., 3.]])

Axel Plinge

Using Python for research and acoustic signal processing 11/15

Function example >>> def doubleit(x): ... return x*2 ... >>> doubleit(5) 10 >>> for i in range(3): ... doubleit(i) ... 0 2 4 >>> [doubleit(i) for i in range(3)] [0, 2, 4] >>> a=[doubleit(i) for i in range(3)] >>> for ai in a: ... doubleit(ai) ... 0 4 8

Axel Plinge

Using Python for research and acoustic signal processing 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 += x print accumulator return accumulator

use it from another file or interpreter import accumen as a a.accum(1) print a.accumulator

Axel Plinge

Using Python for research and acoustic signal processing 13/15

Playback example

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

Axel Plinge

Using Python for research and acoustic signal processing 14/15

Loopback example

import numpy as np import sounddevice as sd DURATION = 10 # seconds SAMPLERATE = 48000 BLOCKSIZE = 4096 def callback(indata, outdata, frames, time, status): global theFilter if status: print(status) filtered = theFilter.process(indata) outdata[:,1] = filtered outdata[:,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()

Axel Plinge

Using Python for research and acoustic signal processing 15/15

Overlapp-Add Filter import numpy as np import scipy class OverlapAdd(object): def __init__(self,blocksize,gain): self.blocksize= blocksize self.gain = gain self.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.lastin self.lastin = indata[:,0] * self.gain self.inbuf[self.blocksize:] = self.lastin self.inbuf = self.inbuf * self.window self.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.overlap self.overlap = self.filtered[self.blocksize:] self.lastout = self.filtered[:self.blocksize] return self.lastout

Suggest Documents