Independent Component Analysis (ICA)
Table of Contents
You're at a crowded party. The music is loud, people are laughing, and a dozen different conversations are happening all around you. However, despite the hubbub, you're able to focus on the one voice you want to hear.
(No reason to shout. Though background noise can be distracting, the brain has the remarkable ability to track conversation and scale down unwanted noise. Courtesy of the National Archives)
Consider two conversations in a room that are happening simultaneously. How is it that the two different acoustic signals of converstaion one and two can be separated out?
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import IPython.display as ipd
import librosa.display
Download
ipd.Audio('.\data_files\The_Phantom_Of_The_Opera.wav')
ipd.Audio('.\data_files\Barack_Obama.wav')
s1, sr = librosa.load('.\data_files\The_Phantom_Of_The_Opera.wav')
s2, sr = librosa.load('.\data_files\Barack_Obama.wav')
s1 = np.asmatrix(s1).reshape(-1,1)
s2 = np.asmatrix(s2).reshape(-1,1)
m = 300000;
s1 = s1[0:m, :]
s2 = s2[0:m, :]
S = np.hstack([s1, s2])
A = np.matrix([[1/2], [2/3]])
mixed = S*A
ipd.Audio(np.ravel(mixed), rate = sr)
Observed random vector $x$ is modelled by a linear variable model
or in matrix form
$$ x= As $$where
Estimate both $A$ and $s_i$, observing only $x$
must assume:
$$ s = A^{-1}x$$
$\quad \;$ However, we do not know $A$ and the signal $s$.
from sklearn.decomposition import FastICA
A = np.matrix([[1/2, 2/3],
[4/5, 1/4]]);
mixed = S*A
ica = FastICA(n_components = 2)
ica_sig = ica.fit_transform(mixed) # Reconstruct signals
ipd.Audio(ica_sig[:,0], rate=sr)
ipd.Audio(ica_sig[:,1], rate=sr)
Download
S1 = plt.imread('./data_files/Suhin.jpg')
S2 = plt.imread('./data_files/nature.jpg')
plt.figure(figsize = (10,10))
plt.subplot(1,2,1), plt.imshow(S1), plt.axis('off')
plt.subplot(1,2,2), plt.imshow(S2), plt.axis('off')
plt.show()
A = np.matrix([[3/4, 1/5],
[1/2, 2/3]])
X1 = A[0,0]*S1 + A[0,1]*S2
X2 = A[1,0]*S1 + A[1,1]*S2
plt.figure(figsize = (10,10))
plt.subplot(1,2,1), plt.imshow(np.uint8(X1)), plt.axis('off')
plt.subplot(1,2,2), plt.imshow(np.uint8(X2)), plt.axis('off')
plt.show()
m, n, k = X1.shape
x1 = X1.reshape(-1, 1)
x2 = X2.reshape(-1, 1)
mixed = np.hstack([x1, x2])
ica = FastICA(n_components = 2)
ica_img = ica.fit_transform(mixed) # Reconstruct signals
ica_img.shape
img_01 = ica_img[:,0]
img_01 = img_01 - img_01.min()
img_01 = img_01*255/img_01.max()
img_01 = img_01.reshape(m, n, 3)
img_02 = ica_img[:,1]
img_02 = img_02 - img_02.min()
img_02 = img_02*255/img_02.max()
img_02 = img_02.reshape(m, n, 3)
plt.figure(figsize = (10,10))
plt.subplot(1,2,1), plt.imshow(np.uint8(img_01)), plt.axis('off')
plt.subplot(1,2,2), plt.imshow(np.uint8(img_02)), plt.axis('off')
plt.show()
%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')