Exemplo n.º 1
0
def train_classifier(dataset,n_classes,Fs=16000):
    datalist = os.listdir(dataset)
    n_data_set = len(datalist)

    for i in range(n_data_set) :
        filepath = dataset + '/' + datalist[i]
        if datalist[i].find('.wav')==1 :
            try:
                [x, Fs, n_channels, n_samples] = read_wave(filepath)
            except:
                print(e.msg)

        elif datalist[i].find('.raw') :
            try:
                x = read_raw(filepath)
                if len(x.shape)>1:
                    n_channels = x.shape[1]
                    n_samples = x.shape[0]
                else:
                    n_channels = 1
                    n_samples = len(x)
            except:
                print(e.msg)
        features = get_mfcc(x,Fs)
        dimension = features.shape[1]
        size = features.shape[0]

        emHMM_algorithm(features,dimension,2,size)
Exemplo n.º 2
0
def train_classifier(dataset, n_classes, Fs=16000):
    datalist = os.listdir(dataset)
    n_data_set = len(datalist)

    for i in range(n_data_set):
        filepath = dataset + '/' + datalist[i]
        if datalist[i].find('.wav') == 1:
            try:
                [x, Fs, n_channels, n_samples] = read_wave(filepath)
            except:
                print(e.msg)

        elif datalist[i].find('.raw'):
            try:
                x = read_raw(filepath)
                if len(x.shape) > 1:
                    n_channels = x.shape[1]
                    n_samples = x.shape[0]
                else:
                    n_channels = 1
                    n_samples = len(x)
            except:
                print(e.msg)
        features = get_mfcc(x, Fs)
        dimension = features.shape[1]
        size = features.shape[0]

        emHMM_algorithm(features, dimension, 2, size)
Exemplo n.º 3
0
def dir_scan_azimuth(scan_folder, elevation, result):
    scan_str = 'H' + str(elevation) + 'e'
    
    for file in glob.glob(scan_folder + "/" + scan_str + "*.wav"):
        azimuthstrpos = string.find(file, scan_str)
        if azimuthstrpos > 0:
            azimuth = int(file[azimuthstrpos + len(scan_str):azimuthstrpos + len(scan_str) + 3])
            left_c_str, right_c_str, length = read_wave(file)  
            result.append((elevation, azimuth, left_c_str, right_c_str, length))
Exemplo n.º 4
0
def dir_scan_azimuth(scan_folder, elevation, result):
    scan_str = 'H' + str(elevation) + 'e'

    for file in glob.glob(scan_folder + "/" + scan_str + "*.wav"):
        azimuthstrpos = string.find(file, scan_str)
        if azimuthstrpos > 0:
            azimuth = int(file[azimuthstrpos + len(scan_str):azimuthstrpos +
                               len(scan_str) + 3])
            left_c_str, right_c_str, length = read_wave(file)
            result.append(
                (elevation, azimuth, left_c_str, right_c_str, length))
Exemplo n.º 5
0
def loadSample():
    print("Enter full path to .wav file")
    path = input()
    signal, sr = rw.read_wave(path, normalize=True, length=1, threshold=0.001)
    spec = sp.get_spectrogram(signal,
                              sr,
                              frame_length=frame_length,
                              frame_offset=frame_offset,
                              lowFreq=lowFreq,
                              hiFreq=hiFreq,
                              numFilters=numFilters,
                              numFrames=numFrames)
    return np.array([spec])
numDataPoints = numFilters * numFrames

for dir, out in zip(directories, out_files):
    print("Writing " + dir + " data to: " + out)
    # Get filepaths of samples for each class
    class_files = {}
    for c in CLASSES:
        files = os.listdir(dir + '/' + c)
        class_files[c] = files

    # Read, process, and write data of each sample to a csv file
    data = []
    for c in class_files:
        print("Class being processed: " + c)
        files = class_files[c]
        paths = []
        for file in files:
            paths.append(dir + '/' + c + '/' + file)

        for path in tqdm(paths):
            signal, sr = rw.read_wave(path, normalize=True, length=1, threshold=0.001)
            spec = sp.get_spectrogram(signal, sr, frame_length=frame_length, frame_offset=frame_offset, lowFreq=lowFreq, hiFreq=hiFreq, numFilters=numFilters, numFrames=numFrames)
            dataToWrite = np.append(np.array([c]), spec.flatten())
            data.append(dataToWrite)

    header = ['Class']
    header.extend(['D' + str(i) for i in range(numDataPoints)])
    df = pd.DataFrame(columns=header, data=data)
    df.to_csv(out)