python的FastICA函数的使用实例,包含自定义的傅里叶变换函数和画多图函数

仅作为记录,大佬请跳过。

直接上代码,程序可直接运行

主文件a9.py

import numpy as np
import matplotlib.pyplot as plt
import a9mysubplot
from sklearn.decomposition import FastICA

ts=0.005
t=np.arange(0,1,ts)
s1=np.sin(2*np.pi*80*t);s2=np.sin(2*np.pi*20*t);s2=np.array(20 * (5 * [2] + 5 * [-2]));a9mysubplot.mysubplot([s1,s2])

def fftspectrum(thesignal,fs):  # add fs
    nsel=thesignal.size
    fsel=fs*(np.arange(0,nsel/2)/nsel)  # add fs*
    ysel=np.fft.fft(thesignal)
    ysel=np.abs(ysel)
    ysel=ysel[0:len(fsel)]
    # ysel=20*np.log(ysel)
    # plt.figure()
    plt.plot(fsel,ysel)
    # plt.show()
fftspectrum(s1,1/ts)
plt.show()

stack=np.array([[0.5,0.5],[0.3,0.7]]);mix=stack.dot([s1,s2]);a9mysubplot.mysubplot([mix[0,:],mix[1,:]])
ica=FastICA(n_components=2)
spro=(ica.fit_transform(mix.T)).T;a9mysubplot.mysubplot([spro[0,:],spro[1,:]])

def fftspectrum(thesignal,fs):  # add fs
    nsel=thesignal.size
    fsel=fs*(np.arange(0,nsel/2)/nsel)  # add fs*
    ysel=np.fft.fft(thesignal)
    ysel=np.abs(ysel)
    ysel=ysel[0:len(fsel)]
    # ysel=20*np.log(ysel)
    # plt.figure()
    plt.plot(fsel,ysel)
    # plt.show()
fftspectrum(spro[0,:],1/ts)
plt.show()

画多图的函数写在一个新的文件a9mysubplot里:

import matplotlib.pyplot as plt

def mysubplot(file):
    l=len(file)

    if l==2:
        plt.figure()
        plt.subplot(211);plt.plot(file[0])
        plt.subplot(212);plt.plot(file[1])

    if l==3:
        plt.figure()
        plt.subplot(311);plt.plot(file[0])
        plt.subplot(312);plt.plot(file[1])
        plt.subplot(313);plt.plot(file[2])

    if l==4:
        plt.figure()
        plt.subplot(411);plt.plot(file[0])
        plt.subplot(412);plt.plot(file[1])
        plt.subplot(413);plt.plot(file[2])
        plt.subplot(414);plt.plot(file[3])

    if l==6:
        plt.figure()
        plt.subplot(611);plt.plot(file[0])
        plt.subplot(612);plt.plot(file[1])
        plt.subplot(613);plt.plot(file[2])
        plt.subplot(614);plt.plot(file[3])
        plt.subplot(615);plt.plot(file[4])
        plt.subplot(616);plt.plot(file[5])

    if l==8:
        plt.figure()
        plt.subplot(811);plt.plot(file[0])
        plt.subplot(812);plt.plot(file[1])
        plt.subplot(813);plt.plot(file[2])
        plt.subplot(814);plt.plot(file[3])
        plt.subplot(815);plt.plot(file[4])
        plt.subplot(816);plt.plot(file[5])
        plt.subplot(817);plt.plot(file[6])
        plt.subplot(818);plt.plot(file[7])

    plt.show()

将a9.py和a9mysubplot.py放在同一个文件夹里,运行a9.py文件

结果显示

博主发现,python的FastICA不能分开两个正弦信号混合起来的信号;因此将一个原信号s1设置为正弦信号,s2设置为方波信号

原始信号

在这里插入图片描述

原始信号中的正弦信号的频谱

在这里插入图片描述

以(5,5)权重、(3,7)权重分别混合的两种正弦方波信号

在这里插入图片描述

FastICA解混后的信号

在这里插入图片描述
发现跟原信号完全一样;但该图有时候解混的正弦波在下面,有时候画在上面

观察解混后的信号频谱(尤其是正弦波的频谱)

在这里插入图片描述
发现频率还是80Hz没错;

这一幅图是方波的频谱
在这里插入图片描述

参考

感谢大佬博主文章

传送门

传送门

博主将大佬博主博文的代码进行了修改,

可直接运行:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.decomposition import FastICA
C = 200
x = np.arange(C)
a = np.linspace(-2, 2, 25)
s1 = np.array([a, a, a, a, a, a, a, a]).reshape(200, )
s2 = 2 * np.sin(0.02 * np.pi * x)
s3 = np.array(20 * (5 * [2] + 5 * [-2]))
ax1 = plt.subplot(311)
ax2 = plt.subplot(312)
ax3 = plt.subplot(313)
ax1.plot(x,s1)
ax2.plot(x,s2)
ax3.plot(x,s3)
plt.show()
s=np.array([s1,s2,s3])
ran=2*np.random.random([3,3])
mix=ran.dot(s)
ax1 = plt.subplot(311)
ax2 = plt.subplot(312)
ax3 = plt.subplot(313)
ax1.plot(x,mix[0,:])
ax2.plot(x,mix[1,:])
ax3.plot(x,mix[2,:])
plt.show()
ica = FastICA(n_components=3)
mix = mix.T
u = ica.fit_transform(mix)
u = u.T
ax1 = plt.subplot(311)
ax2 = plt.subplot(312)
ax3 = plt.subplot(313)
ax1.plot(x,u[0,:])
ax2.plot(x,u[1,:])
ax3.plot(x,u[2,:])
plt.show()

在这里插入图片描述

传送门2

传送门2

传送门3

传送门3

猜你喜欢

转载自blog.csdn.net/weixin_41529093/article/details/112709132