def demoPCA():
n = 1000
t = np.linspace(0.0, 30*np.pi, n)
s1 = spsig.sawtooth(t)
s2 = np.cos(0.5*t)
#s3 = np.random.normal(scale=1.2, size=t.size)
s3 = np.random.uniform(-2.0, 2.0, size=t.size)
s = np.vstack((s1,s2,s3)).T
theta1 = np.pi/6.0
rot1 = np.array([[np.cos(theta1), -np.sin(theta1), 0.0],
[np.sin(theta1), np.cos(theta1), 0.0],
[0.0, 0.0, 1.0]])
theta2 = np.pi/4.0
rot2 = np.array([[ np.cos(theta2), 0.0, np.sin(theta2)],
[ 0.0, 1.0, 0.0],
[-np.sin(theta2), 0.0, np.cos(theta2)]])
theta3 = np.pi/5.0
rot3 = np.array([[1.0, 0.0, 0.0],
[0.0, np.cos(theta3), -np.sin(theta3)],
[0.0, np.sin(theta3), np.cos(theta3)]])
sMixed = s.dot(rot1).dot(rot2).dot(rot3)
lags = 0
pcaFilt = PCA(sMixed, lags=lags)
##pcaFilt.plotMags()
fig = plt.figure()
axOrig = fig.add_subplot(4,1, 1)
axOrig.plot(s+util.colsep(s))
axOrig.set_title('Unmixed Signal')
axOrig.autoscale(tight=True)
axMixed = fig.add_subplot(4,1, 2)
axMixed.plot(sMixed+util.colsep(sMixed))
axMixed.set_title('Mixed Signal (3d rotation)')
axMixed.autoscale(tight=True)
axUnmixed = fig.add_subplot(4,1, 3)
pcaFilt.plotTransform(sMixed, ax=axUnmixed)
axUnmixed.set_title('PCA Components')
axUnmixed.autoscale(tight=True)
axCleaned = fig.add_subplot(4,1, 4)
pcaFilt.plotFilter(sMixed, comp=(1,2,), ax=axCleaned)
axCleaned.set_title('Cleaned Signal (First Component Removed)')
axCleaned.autoscale(tight=True)
fig.tight_layout()
评论列表
文章目录