import numpy as np
import scipy as sp
from sklearn import decomposition as decomp
#functions to play with
xs=np.linspace(0,1,10e3)
def sinewave(xs,**kwargs):
nw=kwargs.setdefault('nw',100) #num waves
return np.sin(xs*nw*2*np.pi)
def const(xs,**kwargs):
c=kwargs.setdefault('c',0)
cs=np.empty(len(xs));cs.fill(c)
return cs
#put an even number of waves in a matrix
nts=np.empty((100,len(xs)))
for i in xrange(len(nts)):
nts[i]=sinewave(xs,nw=(i+1)*2)
nts[-1]=const(xs,c=123) #throw in a const
from matplotlib.pylab import plot
%matplotlib inline
for i in xrange(3): plot(nts[i])
nc=int(len(nts)*.5) #arbitrary choice of num of components
#pd=decomp.PCA(n_components=nc)
pd=decomp.FastICA(max_iter=int(3e3),n_components=nc)
pdc=pd.fit(nts)
#try to reconstruct...
x=np.array([+10*sinewave(xs,nw=4) #an even num of waves
,+7*sinewave(xs,nw=5) #an odd num
,const(xs,c=3)]) #a constant
#recontruct signal
recon=pd.inverse_transform(pd.transform(x))
plot(x[0]);plot(recon[0]);#even number of waves
#is sort of reconstruted
[<matplotlib.lines.Line2D at 0x14aeac88>]
plot(x[1]);plot(recon[1]);#odd number of waves is not
[<matplotlib.lines.Line2D at 0x14af3eb8>]
plot(x[2]);plot(recon[2]);#constant is sort of
#reconstructed
[<matplotlib.lines.Line2D at 0x154e5080>]