%pylab inline rcParams['figure.figsize'] = (10, 4) #wide graphs by default from __future__ import print_function from __future__ import division samps = normal(scale=2.0, size= 10000) samps.var() samps.std(), samps.std()**2 a = rand(100) b = rand(100) cov(a,b) a = rand(100)* 100 b = rand(100) * 100 cov(a,b) a = rand(100000)* 100 b = rand(100000) * 100 cov(a,b) var(a), var(b) a = rand(100) b = rand(100) c = rand(100) cov(a + (2*c),b + (2*c)) a = rand(100000) b = rand(100000) c = rand(100000) cov(a + (2*c),b + (2*c)) a = rand(100) b = rand(100) d = sin(linspace(0, 2*pi*7, 100)) plot(a+d) plot(b+d) cov(a + d,b + d) plot(a+d + 2) plot(-2*(b+d)) cov(a + d + 2,-2*(b+d)) plot(a + (d*3)) plot(-(b + (d*3))) cov(a + (d*3),-(b + (d*3))) sig1 = sin(linspace(0, 100*2*pi, 44100)) sig2 = sin(linspace(0.1, 0.1 + (100*2*pi), 44100)) plot(sig1) plot(sig2) xlim((0, 1000)) lags, c, lines, line = xcorr(sig1, sig2, maxlags=500, usevlines=False); grid() lags, c, lines, line = xcorr(sig1, sig2, maxlags=50, usevlines=False); grid() len(c) plot(c) argmax(c) cc_index = lags[argmax(c)] print(cc_index) plot(lags, c) vlines(cc_index, 0.5, 1.09, color='r', lw=2) text(cc_index + 1, 1.05, 'CC peak', color='r') grid() sr = 44100 f = 100.0 Ps = sr/f Ps # period in number of samples cc_index cc_index/Ps 2 * pi * cc_index/Ps 2 * pi * (cc_index + 1)/Ps noise1 = random.random(44100) - 0.5 noise2 = random.random(44100) - 0.5 lags, c, lines, line = xcorr(noise1, noise2, maxlags=500, usevlines=False); grid() noise1 = normal(size=44100) noise2 = normal(size=44100) lags, c, lines, line = xcorr(noise1, noise2, maxlags=500, usevlines=False); grid() a = (rand(44100)*2) - 1 plot(a) b = sin(linspace(0, 2*pi*200, 44100))*0.3 plot(a+b) c = (rand(44100)*2) - 1 d = sin(linspace(0.7, 0.7 + (2*pi*200), 44100))*0.3 plot(c+d) plot(c+d) xlim(0,1000) lags, c, lines, line = xcorr(a+b, c+d, maxlags=500); m = argmax(c[500:600]) + 500 lags[m] sr = 44100 f = 200.0 Ps = sr/f Ps # period in number of samples lags[m]/Ps (lags[m]/Ps) * 2 * pi lags, c, lines, line = acorr(a + b, maxlags=500); plot(c[501:]) vlines(220.5, -0.15, 0.15) from scipy.io import wavfile sr, in_sig = wavfile.read('cars.wav') win_size = 1024 hop = 256 cc_lags = 0.001*sr # 1 ms max lag rccf = [] win_start = arange(0, in_sig.shape[0] - win_size, hop) for start in win_start: win = in_sig[start: start+win_size].astype(float) lags, c, lines, line = xcorr(win[:,0], win[:,1], maxlags=cc_lags) rccf.append(c) imshow(array(rccf).T, aspect='auto') plot(in_sig) sr, in_sig = wavfile.read('superstition.wav') !aplay superstition.wav plot(in_sig) in_sig.shape lags,c, lines, line = acorr(in_sig[:100000,0].astype(double), maxlags= 50000); i = imread('galileo.png') imshow(i) plot(i[:,:,3].T); i.shape i.dtype i = sum(i[:,:,:-1], axis=2)/3.0 imshow(i) colorbar() i.shape imshow(i) colorbar() imshow(i, cmap=cm.gray) colorbar() i = where(i > 0.9, 0, 1) imshow(i, cmap='gray') colorbar() o = imread('o.png') imshow(o) colorbar() o = o.astype(float).sum(axis=-1)/3 o = where(o > 0.9, 0, 1) imshow(o, cmap=cm.gray, interpolation='nearest') from scipy.signal import correlate2d cc = correlate2d(i, o) imshow(cc) colorbar() gcf().set_figheight(8) imshow(cc, cmap=cm.gray) colorbar() gcf().set_figheight(8) imshow(where(cc > 200, 1, 0), interpolation='nearest', cmap=cm.gray) colorbar() gcf().set_figheight(8) imshow(where(cc > 300, 1, 0), interpolation='nearest', cmap=cm.gray) colorbar() gcf().set_figheight(8) from scipy.ndimage.filters import maximum_filter imshow(maximum_filter(cc, (10,10))) gcf().set_figheight(8) subplot(121) imshow(maximum_filter(cc, (50,50))) subplot(122) imshow(i, cmap=cm.gray) gcf().set_figheight(8) mf = maximum_filter(cc, (50,50)) argmax(mf) unravel_index(argmax(mf), mf.shape) argmax(cc) unravel_index(argmax(cc), cc.shape)