4D array exercise

In [1]:
import pickle
with open('uv.pkl','rb') as f:
        uv = pickle.load(f)
In [4]:
uv.shape,uv.dtype
Out[4]:
((3, 10, 5, 2), dtype('bool'))

Ping pong ball bounces

In [12]:
# Ping pong
from numpy import *
import matplotlib.pyplot as plt
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
from scipy.io.wavfile import read
samplerate,signal = read('PingPong2.wav')

plt.figure(figsize=(15,10))
plt.subplot(2,1,1)
plt.plot(signal)
plt.subplot(2,1,2)
plt.plot(signal)
plt.xlim(370000,400000);
In [44]:
i = 0
threshold = 0.5
skip = 4000
t = [0] # impact times (with bogus first element)
peak = [0] # peak value near impact time
indices = arange(len(signal))

while True:
    ti = argmax( (signal>threshold) &  (indices>t[-1]+skip) )
    t.append(ti)
    peaki = signal[ti-skip:ti+skip].max()
    peak.append(peaki)
    threshold = peaki*.5
    if len(t)>1: skip = (t[-1]-t[-2])//2  # shrink the skip amount as appropriate
    if len(t)-2>80: break
t = array(t[1:])
print(len(t)-1,'bounces measured')
peak = array(peak[1:])
tau = t[1:]-t[:-1]

plt.figure(figsize=(15,15))

plt.subplot(3,1,1)
plt.semilogy(tau,'ro')
alpha,beta = polyfit(range(len(tau)),log10(tau),1)
plt.semilogy(10**(alpha*arange(len(tau))+beta),'g',lw=3,alpha=0.5)
plt.xlabel('bounce #')
plt.ylabel('time between peaks i and i+1')

plt.subplot(3,1,2)
plt.plot(signal)
plt.plot(t,peak,'ro',alpha=0.5)
plt.xlabel('time (sample #)')
plt.ylabel('signal')

plt.subplot(3,1,3)
plt.plot(signal)
plt.plot(t,peak,'ro',alpha=0.5)
plt.xlabel('time (sample #)')
plt.ylabel('signal')
plt.title('zoom in on later part')
plt.xlim(370000,400000);
81 bounces measured
In [46]:
plt.figure(figsize=(15,5))
plt.plot(signal)
plt.plot(t,peak,'ro',alpha=0.5)
plt.xlabel('time (sample #)')
plt.ylabel('signal')
plt.title('zoom in on later part')
plt.xlim(385000,390000)
plt.ylim(-.3,.2);