<jupyter_start><jupyter_text>Get data<jupyter_code># dataset


fpaths = []
labels = []
boy_fpaths = []
boy_labels = []

word_spoken = []


dataset='new'

dataset = '84{}'.format(dataset)
input_folder = '..\data\{}'.format(dataset)

# pars the input directory that contains audio files
# get audio files and their lables

for f in os.listdir(input_folder):
    for w in os.listdir(input_folder+'\\'+ f):
        # check wheter files is wav or not

        if (w.find('wav')!=-1):
            
            fpaths.append(input_folder+'\\'+f+'\\'+w)
            labels.append(f)
        

#             idx_schunk=w.find('schunk')
#             idx_chunk=w.find('chunk')

#             if (idx_chunk!=idx_schunk==-1):
#                 if (int(w[idx_chunk-2:idx_chunk])<80):
#                     fpaths.append(input_folder+'\\'+f+'\\'+w)
#                     labels.append(f)
#                 else:
#                     boy_fpaths.append(input_folder+'\\'+f+'\\'+w)
#                     boy_labels.append(f)

#             else:
#                 boy_fpaths.append(input_folder+'\\'+f+'\\'+w)
#                 boy_labels.append(f)


        if f not in word_spoken:
            word_spoken.append(f)
print("Spoken words: "+ str(word_spoken))
print("Number of classes: " + str(len(word_spoken)))
# size of dataset
print(len(labels))
label_set=list(set(labels))
label_set.sort()

# # boys 
# boy_label_set=list(set(boy_labels))
# boy_label_set.sort()
label_set
len(label_set)<jupyter_output><empty_output><jupyter_text>Extracting frequeny domain featuresAt the second stage we convet a signal into the freqency domain. In monst modern speech recognitoon freqeency-domain features are used as key component. In case of multispeakers MFFC feature extraction works best. After convert a signal into a freq domain, it's requered to convert it into a useable form. **Mel Frequency Cepstral Coefficients (MFCC)** is a good way to do that. *MFCC* takes the power spectrum of a signal and then uses a combination of filter banks and disrete cosinetransform to extract pattern of phones or features.After extracting **MFFC** features we exract data into single data matrix, and a label vector with the correct label for eac data file is ceated.<jupyter_code>from scipy.io import wavfile

def get_mfcc(fpaths):
    data = []

    mfcc_max_length = 0

    # first file desitination name and index
    file_name = ''
    word_spoken_index = 0
    
    count=0;

    for n,file in enumerate(fpaths):

        # show current desintation 
        if (file.find(file_name)<=0):
            file_name=word_spoken[word_spoken_index]
            print(word_spoken[word_spoken_index])
            word_spoken_index+=1

        # read file 
        sampling_freq, audio = wavfile.read(file)
        # Extract MFCC features
        mfcc_features = mfcc(audio, sampling_freq)
    
        mfcc_len=mfcc_features.shape[0]*mfcc_features.shape[1]
        # get length of largets feature array
        if mfcc_len>mfcc_max_length:    
            mfcc_max_length=mfcc_len


        # flat data into 2D array
        mfcc_features=np.resize(mfcc_features,(1,mfcc_len))

        data.insert(n,mfcc_features)
        
    return data, mfcc_max_length
data, max_len=get_mfcc(fpaths)
# boy_data=get_mfcc(boy_fpaths)
print("Lenght Longest Audio Array: " + str(max_len))<jupyter_output>Lenght Longest Audio Array: 4914<jupyter_text>Get Labels<jupyter_code>def get_labels(labels, label_set):
    label_idx_dic={} # dict for storing labels

    #Each sample file is one row in data, and has one entry in labels
    print('Number of files total:', len(labels))
    all_labels = np.zeros(len(labels),dtype=int)
    for n, l in enumerate(label_set):
        label_idx_dic[n]=l
        all_labels[np.array([i for i, _ in enumerate(labels) if _ == l])] = n
    print('Labels and label indices', all_labels)
    
    return label_idx_dic, all_labels
label_dic, all_label = get_labels(labels,label_set)
# boy_label_dic, boy_all_label = get_labels(boy_labels,boy_label_set)
# save label dictionary
def save_dict_to_file(dic,dataset):
    with open('dict{}.txt'.format(dataset),'w') as file:
        file.write(json.dumps(dic))
# save dict
save_dict_to_file(label_dic,dataset)<jupyter_output><empty_output><jupyter_text>Pad Zeros to Small Arrays<jupyter_code>def data_x_y(data,all_labels):
    x_data = np.zeros((len(data),2808+1), dtype=float)

    for i,_d in enumerate(data):

        if (_d.shape[1]>2808):
            x_data[i,1:]=_d[0,0:2808]
        else :
            x_data[i,1:_d.shape[1]+1]=_d

        x_data[i,0]=all_labels[i]
        
    return x_data<jupyter_output><empty_output><jupyter_text>SAVE DATASET<jupyter_code>file_name="84new"
# save all features data
np.savetxt('data/{}.csv'.format(file_name), data_x_y(data,all_label), delimiter=',',header=data_header)

# np.savetxt('data/{}/data41{}.csv'.format(save_path,'ADA_boy'), data_x_y(boy_data,boy_all_label), delimiter=',',header=data_header)
# np.savetxt('data/{}/data41{}.csv'.format(save_path,'ADA_girl'), data_x_y(data,all_label), delimiter=',',header=data_header)<jupyter_output><empty_output><jupyter_text>GENERATE  MIX NEW DATASET<jupyter_code>features_df = pd.read_csv('data/data41.csv')
features_df_1 = pd.read_csv('data/data41ADA.csv')
features_df_2 = pd.read_csv('data/new_test/data41new_test.csv')
features_df_3 = pd.read_csv('data/new_test/data41new_test2.csv')
x_data = np.vstack((features_df, features_df_1,features_df_2,features_df_3))
print("normal dataset shape: {}".format(features_df.shape))
print("ada dataset shape: {}".format(features_df_1.shape))
print("ada dataset shape: {}".format(features_df_2.shape))
print("ada dataset shape: {}".format(features_df_3.shape))
print("new mix dataset shape: {}".format(x_data.shape))
save_path=""
np.savetxt('data/{}/data41{}.csv'.format(save_path,'mix2'), x_data, delimiter=',',header=data_header)<jupyter_output><empty_output><jupyter_text>SPLIT TEST AND TRAIN DATA<jupyter_code>save_path="84new"
x_data = pd.read_csv('data/84new.csv')
x_data.tail(5)
def create_test_train(x_data, save_path, data_header):
    from sklearn.model_selection import train_test_split

    X_train, X_test = train_test_split( x_data, test_size=0.15, random_state=42, shuffle=True)

    print('Size of training matrix:', X_train.shape)
    print('Size of testing matrix:', X_test.shape)

    # test data
    np.savetxt('data/{}/{}.csv'.format(save_path,save_path+'_train'), X_train, delimiter=',', header=data_header)
    np.savetxt('data/{}/{}.csv'.format(save_path,save_path+'_test'), X_test, delimiter=',', header=data_header)
create_test_train(x_data, save_path, data_header)<jupyter_output>Size of training matrix: (32811, 2809)
Size of testing matrix: (5791, 2809)
StarcoderdataJupyterStructuredCleanDedup