Ako používať rozhranie FastICA v sklearn

How Use Fastica Interface Sklearn



Ako používať rozhranie FastICA v sklearn

#FastICA_learn by Leo import numpy as np import math import matplotlib.pyplot as plt from sklearn.decomposition import FastICA #Application method ''' Function name: Jagged Function: Sawtooth generator Input parameter: time t period The period of the generated waveform Return parameter: jagged_value The value of the sawtooth wave corresponding to time t Author: Leo Ma Time: 2020.01.04 ''' def Jagged(t, period = 4): jagged_value = 0.5*(t-math.floor(t/period)*period)#math.floor(x) returns the rounded down integer of x return jagged_value ''' Function name: create_data Function: Simulate to generate raw data Input parameter: None Return parameter: T time variable S source signal D The observed mixed signal Author: Leo Ma Time: 2020.01.04 ''' def create_data(): #data number m = 500 #Generate time variables T = [0.1*xi for xi in range(m)] #Generate source signal S = np.array([[math.sin(xi) for xi in T], [Jagged(xi) for xi in T]], np.float32) #Define mixed matrix A = np.array([[0.8, 0.2], [-0.3, -0.7]], np.float32) #Generate the observed mixed signal D = np.dot(A, S) return T, S, D ''' Function name: load_wav_data Function: load wav data from file Input parameter: file_name The name of the file to be read Return parameter: T time variable S source signal params wav data parameters Author: Leo Ma Time: 2020.01.04 ''' def load_wav_data(file_name): import wave f = wave.open(file_name,'rb') #Get basic audio parameters params = f.getparams() nchannels, sampwidth, framerate, nframes = params[:4] #Read string format audio strData = f.readframes(nframes) #Close the file f.close() #Convert audio in string format to int type waveData = np.fromstring(strData,dtype=np.int16) #Normalize the wave amplitude S = waveData / (max(abs(waveData))) T = np.arange(0,nframes) / framerate return T,S,params ''' Function name: save_wav_data Function: save wav data to file Input parameter: file_name the name of the file to be saved params save parameters S wav signal Return parameter: None Author: Leo Ma Time: 2020.01.04 ''' def save_wav_data(file_name,params,S): import wave import struct outwave = wave.open(file_name, 'wb') #Setting parameters outwave.setparams((params)) #Normalize the signal amplitude S = S / (max(abs(S))) #Frame-by-frame writing to file for v in S: outwave.writeframes(struct.pack('h', int(v * 64000 / 2)))#S: 16 bits, -32767~32767, be careful not to overflow outwave.close() ''' Function name: load_create_data Function: load wav data from the file and mix it Input parameter: None Return parameter: T time variable S source signal D The observed mixed signal The parameters of params wav data, the audio parameters at both ends are the same Author: Leo Ma Time: 2020.01.04 ''' def load_create_data(): #Two audio clips are the same length T1,S1,params1 = load_wav_data('./voice/sound1.wav') T2,S2,params2 = load_wav_data('./voice/sound2.wav') if np.shape(T1)[0] > np.shape(T2)[0]: T = T1 else: T = T2 #Merge S1 and S2 of size (1,m) into S, and the size of S is (2,m) #Where S1[np.newaxis,:] adds (m,) to a new axis and becomes (1,m) #np.vstack((a,b)) merge the two matrices a and b in the column direction # In addition, np.hstack((a, b)) merges the two matrices a and b in the row direction S = np.vstack((S1[np.newaxis,:],S2[np.newaxis,:])) #Define mixed matrix A = np.array([[0.8, 0.2], [-0.3, -0.7]], np.float32) #Generate the observed mixed signal D = np.dot(A, S) return T,S,D,params1 ''' Function name: show_data Function function: draw a data graph Input parameter: None Return parameter: None Author: Leo Ma Time: 2020.01.04 ''' def show_data(T, S): plt.plot(T, S[0,:], color='r', marker='*') plt.show() plt.plot(T, S[1,:], color='b', marker='o') plt.show() #Main function entry def main(): '''The first method of creating data: reading voice signals from both ends from a file''' #Generate data, T is the time variable, S is the source signal, D is the observed mixed signal T, S, D, params = load_create_data() #Save two mixed signals on the hard disk save_wav_data('./voice/mix1.wav',params,D[0,:]) save_wav_data('./voice/mix2.wav',params,D[1,:]) '''The second method of creating data: using two waveform generators to generate two signals''' #T, S, D = create_data() How to use ICA in #sklearn ica = FastICA(n_components=2) #Independent component is 2 DT = np.transpose(D) #Convert the mixed signal matrix to shape=[m,n], where n=2 SrT = ica.fit_transform(DT) # SrT is the two independent components after unmixing, shape=[m,n] Sr = np.transpose(SrT)#Convert the unmixed signal matrix to shape=[n,m], where n=2 print('times:') print(ica.n_iter_) # Algorithm iterations #Save two signals reconstructed by FastICA algorithm on the hard disk save_wav_data('./voice/reconsruct_sound1.wav',params,Sr[0,:]) save_wav_data('./voice/reconsruct_sound2.wav',params,Sr[1,:]) #Draw the data image print('T,D:') show_data(T, D) print('T,S:') show_data(T, S) print('T,Sr:') show_data(T, Sr) if __name__ == '__main__': main()

výsledok operácie:
obrázok
obrázok
obrázok
Skontrolujte tiež zvukový súbor uložený na pevnom disku a zistite, či algoritmus FastICA funguje dobre:
obrázok

Tento kód tiež experimentoval s dvoma signálmi kriviek, ktoré som sám vytvoril, a efekt je dobrý:
obrázok
obrázok
obrázok