Examples of the use of Python's FastICA function, including custom Fourier transform functions and multi-graph functions

Just for the record, please skip it.

Directly upload the code, the program can be run directly

Master filea9.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()

The function for drawing multiple graphs is written in a new file 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()

Put a9.py and a9mysubplot.py in the same folder and run the a9.pyfile

The results show that

The blogger found that Python FastICAcan’t separate the mixed signals of two sinusoidal signals; therefore, set an original signal s1 as a sinusoidal signal and s2 as a square wave signal.

original signal

Insert picture description here

The frequency spectrum of the sinusoidal signal in the original signal

Insert picture description here

Two sine square wave signals mixed with (5,5) weight and (3,7) weight respectively

Insert picture description here

FastICA unmixed signal

Insert picture description here
It is found to be exactly the same as the original signal; but sometimes the unmixed sine wave in the picture is below, and sometimes it is drawn on top

Observe the signal spectrum after unmixing (especially the spectrum of a sine wave)

Insert picture description here
It is found that the frequency is still 80Hz;

This picture is the frequency spectrum of a square wave
Insert picture description here

reference

Thanks to the big blogger for the article

Portal

Portal

The blogger modified the code of the blogger’s blog post,

Can run directly:

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()

Insert picture description here

Portal 2

Portal 2

Portal 3

Portal 3

Guess you like

Origin blog.csdn.net/weixin_41529093/article/details/112709132