Transcript build18.org

Tutorial 2
Signals, Sensors, and Features
Table of Contents
•
•
•
•
Why Python?
Examples of Signals
Determining the Period
o
A Bouncing Ball...
o
•
•
The Fourier (Frequency) Transform
o
Finding the Feature
Dealing With Noise
Programming Gotchas
o
Python, OpenCV
More Resources
Python vs Matlab
•
All code in tutorial 2 will be in Python
o
o
•
•
A little clunkier than Matlab, but *free* and highly
extensible with packages like OpenCV, etc.
We use Numpy, Scipy, and Matplotlib to get similar
featureset
Keep http://www.scipy.org/NumPy_for_Matlab_Users open
Let's Get Started!
import numpy as npimport
matplotlib.pyplot as plt
Python Gotchas
•
Numpy:
:) Array indexing starts at 0.
o :/ array([[1.,2.,3.],[4.,5.,6.]]) initializes an array
 Matlab: a = [1,2,3;4,5,6];
o :/ Assigns are by reference. To duplicate:
 y = x.copy()
o
•
•
Matplotlib
o
:/ Doesn't show plot automatically. Do plt.show()
Don't worry, examples follow
Signals (guitar)
Signals (cymbal)
Signals (vocal chords)
Doing Stuff with Signals
t = np.arange(0,1,.001)
plt.figure(0)
s = np.sin(t*2*np.pi*10)
plt.plot(t,s)
plt.xlabel('Time (s)')
plt.ylabel('Voltage')
plt.savefig('sine.png',bbox_inc
hes=0)
How often does the signal repeat itself?
Doing Stuff with Signals
#Threshold the sine wave
thresh = s.copy()
thresh[thresh<.8] = np.NaN
plt.plot(t,s,'b--',t,thresh,'k')
plt.hold(True)
plt.axhline(y=.8,color='black',
lw=2)
plt.xlabel('Time (s)')
plt.ylabel('Voltage')
plt.savefig('sine_thresh.png',b
box_inches=0)
plt.hold(False)
How often does the signal repeat itself?
Doing Stuff with Signals
#Show a semi-complicated signal
s2 = np.sin(t*2*np.pi*10)+
np.sin(t*2*np.pi*15) +
np.sin(t*2*np.pi*25) +
np.sin(t*2*np.pi*29)
plt.plot(t,s2)
plt.xlabel('Time (s)')
plt.ylabel('Voltage')
plt.savefig('complicated.png',b
box_inches=0)
How often does the signal repeat itself?
Fourier Transform
•
•
Discovered by Jean Baptiste Fourier
Transforms time into frequency
using sine waves as orthogonal
bases
Sees how well signal matches a set of
sinusoids at different frequencies
F
(Frequency) Transform
Take the Fourier Transform!
F
Peaks at 10, 15, 25, and 29 Hz. Hmm...
Fundamental period is lcm(10,15,25,29) = 145 sec!
Point: That would've been hard to find in time domain...
# Find the magnitude of the
FFT#RFFT is an optimization as
we know we have a non-imaginary
input
fftData=abs(np.fft.rfft(s2))/500
plt.stem(np.arange(len(fftData))
,fftData)#Adjust the limits
plt.xlim(0,50)
plt.ylim(0,1.1)
plt.xlabel('Frequency (Hertz)')
plt.ylabel('Magnitude')
plt.savefig('freq_complicated.pn
g',bbox_inches=0)
Timbre = Frequency Spectra
Clarinet
from: http://www.phys.unsw.edu.au/jw/basics.html
Flute
Demo!
Sharp Signals are Hard To Represent
as sums of sinusoids
Every frequency is needed!
so you might have to keep them in the time domain
Bouncing Ball
#Raw signal
s =
np.abs(np.cos(t*20*(t))*np.exp(t*2));
plt.plot(t,s)
plt.xlabel('Time (sec)')
plt.ylabel('Height')
plt.savefig('ball_bounce.png',bb
ox_inches=0)
At what time instants does the ball hit the ground,
and what velocity is it going?
What Tool Do We Use?
•
Fourier Transform?
o
•
o
Best on stationary signals (identical
repetitions over time)
Nope...our frequency changes over time
Time Domain?
o
o
o
We could detect when the height = 0 and
when its a max
But "max" changes over time!
You can hack something together, but we
really want a "cleaner" signal...like:
Bouncing Ball - Velocity
#Take the derivative...to get
velocity!
v = np.convolve(s,np.array([1,1]),'same')/.001#Clean up a
data element
v[0] = 1.99800153e-03
plt.subplot(211)
plt.plot(t,s,'r-')
plt.xlabel('Time (sec)')
plt.ylabel('Height')
plt.subplot(212)
plt.plot(t,v,'b-')
plt.xlabel('Time (sec)')
plt.ylabel('Velocity')
plt.savefig('ball_bounce_vel.png
',bbox_inches=0)
At what time instants does the ball hit the ground,
and what velocity is it going?
How to Take a Discrete Derivative
•
•
•
dy / dx = (y2-y1) / (x2-x1)
(signal[n] - signal[n-1]) / Ts
conv([-1 1],signal) / Ts
Bouncing Ball - Acceleration
#Take the derivative again...to
get acceleration!
a = np.convolve(v,np.array([1,1]),'same')/.001#Clean up some
data elements
a[0] = 3.98922130e-06
a[1] = 3.98922130e-06
plt.subplot(311)
plt.plot(t,s,'r-')
plt.xlabel('Time (sec)')
plt.ylabel('Height')
plt.subplot(312)
plt.plot(t,v,'b-')
plt.xlabel('Time (sec)')
plt.ylabel('Velocity')
plt.subplot(313)
plt.plot(t,a,'k-')
plt.xlabel('Time (sec)')
plt.ylabel('Acceleration')
plt.savefig('ball_bounce_acc.png
',bbox_inches=0)
At what time instants does the ball hit the ground,
and what velocity is it going?
Pyo
•
•
•
Great toolkit for audio on Python
Run 'e-pyo' from the Start Menu for sample
code, also: http://code.google.com/p/pyo/
Use it to get generative audio output!
Virtual Drum Machine
•
•
•
Use starter code (gives x,y,width of bright
objects)
Take some derivatives
Get drum sounds as output using pyo
HSV and Norm
(Python) OpenCV Tips
•
•
Old: import cv
New: import cv2.cv as cv
o
•
•
cv2 has much reduced overhead compared
to cv2.cv
Code running slow? Capture a smaller image
size from your webcam
o
self.cam = video.create_capture('0:size=320x240')
Many examples in
C:\<opencv_dir>\samples\python2
o
lk_homography, facedetect, digits_video
Project Ideas
•
•
Adafruit Voice Changer
Depth-enabled Drum Machine?
Notes
•
•
Better Peak Detection:
http://www.billauer.co.il/peakdet.html
http://music.columbia.edu/cmc/MusicAnd
Computers/ (making music with computers)