from scipy.io import wavfile from scipy.signal import resample def load_wav(filename,samplerate=44100): # load file rate, data = wavfile.read(filename) # convert stereo to mono if len(data.shape) > 1: data = data[:,0]/2 + data[:,1]/2 # re-interpolate samplerate ratio = float(samplerate) / float(rate) data = resample(data, len(data) * ratio) return samplerate, data.astype(np.int16) import StringIO import base64 import struct from IPython.core.display import HTML def wavPlayer(data, rate): """ will display html 5 player for compatible browser The browser need to know how to play wav through html5. there is no autoplay to prevent file playing when the browser opens Adapted from SciPy.io. and github.com/Carreau/posts/blob/master/07-the-sound-of-hydrogen.ipynb """ buffer = StringIO.StringIO() buffer.write(b'RIFF') buffer.write(b'\x00\x00\x00\x00') buffer.write(b'WAVE') buffer.write(b'fmt ') if data.ndim == 1: noc = 1 else: noc = data.shape[1] bits = data.dtype.itemsize * 8 sbytes = rate*(bits // 8)*noc ba = noc * (bits // 8) buffer.write(struct.pack('' or (data.dtype.byteorder == '=' and sys.byteorder == 'big'): data = data.byteswap() buffer.write(data.tostring()) # return buffer.getvalue() # Determine file size and place it in correct # position at start of the file. size = buffer.tell() buffer.seek(4) buffer.write(struct.pack(' Simple Test """.format(base64=base64.encodestring(val)) display(HTML(src)) # change to the shogun-data directoy import os os.chdir('../../../data/ica') import pylab as pl # load fs1,s1 = load_wav('tbawht02.wav') # Terran Battlecruiser - "Good day, commander." # plot pl.figure(figsize=(6.75,2)) pl.plot(s1) pl.title('Signal 1') pl.show() # player wavPlayer(s1, fs1) # load fs2,s2 = load_wav('TMaRdy00.wav') # Terran Marine - "You want a piece of me, boy?" # plot pl.figure(figsize=(6.75,2)) pl.plot(s2) pl.title('Signal 2') pl.show() # player wavPlayer(s2, fs2) # load fs3,s3 = load_wav('PZeRdy00.wav') # Protoss Zealot - "My life for Aiur!" # plot pl.figure(figsize=(6.75,2)) pl.plot(s3) pl.title('Signal 3') pl.show() # player wavPlayer(s3, fs3) import numpy as np # Adjust for different clip lengths fs = fs1 length = max([len(s1), len(s2), len(s3)]) s1.resize((length,1)) s2.resize((length,1)) s3.resize((length,1)) S = (np.c_[s1, s2, s3]).T # Mixing Matrix #A = np.random.uniform(size=(3,3)) #A = A / A.sum(axis=0) A = np.array([[1, 0.5, 0.5], [0.5, 1, 0.5], [0.5, 0.5, 1]]) print 'Mixing Matrix:' print A.round(2) # Mix Signals X = np.dot(A,S) # Mixed Signal i for i in range(X.shape[0]): pl.figure(figsize=(6.75,2)) pl.plot((X[i]).astype(np.int16)) pl.title('Mixed Signal %d' % (i+1)) pl.show() wavPlayer((X[i]).astype(np.int16), fs) from shogun.Features import RealFeatures # Convert to features for shogun mixed_signals = RealFeatures((X).astype(np.float64)) from shogun.Converter import Jade # Separating with JADE jade = Jade() signals = jade.apply(mixed_signals) S_ = signals.get_feature_matrix() A_ = jade.get_mixing_matrix() A_ = A_ / A_.sum(axis=0) print 'Estimated Mixing Matrix:' print A_ # Show separation results # Separated Signal i gain = 4000 for i in range(S_.shape[0]): pl.figure(figsize=(6.75,2)) pl.plot((gain*S_[i]).astype(np.int16)) pl.title('Separated Signal %d' % (i+1)) pl.show() wavPlayer((gain*S_[i]).astype(np.int16), fs)