In [361]:
# Kurt Kaminski
# MAT201A HW2: Sound to Image
#
# This program loops through a sound and places the samples
# into an image line-by-line, then moves the pixels to arrange
# them in an archimedean spiral. In this case, the sound is
# data representing the precession index of Earth over 10 million
# years. The sound is one second long, each sample represents
# 200 years.
#
# en.wikipedia.org/wiki/Milankovitch_cycles
%pylab inline
from scipy.io import wavfile;
import wave;
from __future__ import division;
sr,sample = wavfile.read('precessionindex_LFO.wav');
print 'Sample size: ', sample.size;
x = 300;
y = int(sample.size/x);
newimg = sample[:x*y].reshape(y, x);
# print (x, y);
# imshow(newimg, cmap=cm.BrBG);
sortedImage = arange(newimg.size).reshape(y,x);
it = nditer(newimg, flags=['multi_index']); #iterator
a = [0];
b = [0];
j = 0; #current step
while not it.finished:
# print it.index, "=", it[0];
i0 = it.multi_index[0];
i1 = it.multi_index[1];
o = (j/newimg.size)*pi*50; #theta
q = 2; #multiplier
s = (y/2) + ( cos((o*q) + o)) * o * .5;
t = (x/2) + ( sin((o*q) + o)) * o * .5 ;
a.append( s );
b.append( t );
if s < y and t < x:
sortedImage[int(s), int(t)] = it[0];
j+=1;
it.iternext();
gcf().set_figheight(10);
gcf().set_figwidth(10);
imshow(sortedImage, cmap=cm.BrBG, interpolation='none');
In [362]:
# utility
print(min(a), max(a))
print(min(b), max(b))
print(len(a), len(b))
In [ ]: