def gerador_de_caracteriticas(): all_features = [] for base in bases_B: features_list = [] files.append(func.get_filenames(path=base, filetype='.wav')) for i in range(len(files[-1])): instance, rate = librosa.load(base + '/' + files[-1][i]) features = func.extract_feature(instance, rate) # rotulacao if base == btraning_normal: features.append("Normal") elif base == btraning_mumur: features.append("Murmur") elif base == btraining_extrastole: features.append("Extrastole") features_list.append(features) all_features.append(features) print('Done with: ' + files[-1][i] + " - " + features[6]) # escrita em arquivo CSV dir_db = os.path.basename(base) func.write_csv('librosa_features_of_' + dir_db, features_list) print('Done with: ' + base + '\n') os.chdir(path_B) func.write_csv("BaseB_librosa_features", all_features)
def main_a(): files = [] all_features = [] for base in bases_A: instances_list = [] features_list = [] filtered_instances_list = [] files.append(func.get_filenames( path=base, filetype='.wav')) # gera array com nome dos audios for i in range(len(files[-1])): # instance, rate = func.load_sound_files(base + '/' + files[-1][i]) # rate, instance = wavfile.read(base + '/' + files[-1][i]) instance, rate = librosa.load(base + '/' + files[-1][i]) # [rate, instance] = audioBasicIO.readAudioFile(base + '/' + files[-1][i]) instances_list.append(instance) # filtragem e decomposicao rec_signal = func.wavelet_filtering(instance, th, rate) filtered_instances_list.append(rec_signal) # extracao de caracteriticas features = func.extract_feature(instance, rate) # librosa # features = audioFeatureExtraction.stFeatureExtraction(instance, rate, 0.5 * rate, 0.25 * rate) # features = audioFeatureExtraction.mtFeatureExtraction( # instance, rate, 0.5 * rate, 0.5 * rate,0.25 * rate, 0.25 * rate # ) # rotulacao if base == atraning_normal: features.append("Normal") elif base == atraning_mumur: features.append("Murmur") elif base == atraining_extrahls: features.append("Extra Heart Sound") elif base == atraining_artifact: features.append("Artifact") features_list.append(features) all_features.append(features) print('Done with: ' + files[-1][i]) # escritas no CVS dir_db = os.path.basename(base) func.write_csv('librosa_filtered_features_of_' + dir_db, features_list) print('Done with: ' + base + '\n') os.chdir(path_A) func.write_csv("BaseA_librosa_filtered_features", all_features) print 'Done!'
def test_write_csv(): """ Test procedure for the function write_csv() """ print('Testing write_csv()') # Access the file relative to this one, not the user's terminal parent = os.path.split(__file__)[0] # First test (erase any existing file) fpath = os.path.join(parent, 'files', 'temp1.csv') open(fpath, 'w').close() func.write_csv(FILE1, fpath) # Check file was created introcs.assert_true(os.path.exists(fpath)) file = open(fpath) actual = file.read() file.close() file = open(os.path.join(parent, 'files', 'writecsv1.csv')) correct = file.read() file.close() # Check to see if they are the same introcs.assert_equals(correct, actual) # Second test (erase any existing file) fpath = os.path.join(parent, 'files', 'temp2.csv') open(fpath, 'w').close() func.write_csv(FILE2, fpath) # Check file was created introcs.assert_true(os.path.exists(fpath)) file = open(fpath) actual = file.read() file.close() file = open(os.path.join(parent, 'files', 'writecsv2.csv')) correct = file.read() file.close() # Check to see if they are the same introcs.assert_equals(correct, actual)
def main_pn(): files = [] labels = [] for base in bases_Physioef: instances_list = [] features_list = [] files.append(func.get_filenames(path=base, filetype='.wav')) labels.append(func.get_filenames(path=base, filetype='.hea')) for i in range(len(files[-1])): # instance, rate = func.load_sound_files(base + '/' + files[-1][i]) # rate, instance = wavfile.read(base + '/' + files[-1][i]) instance, rate = librosa.load(base + '/' + files[-1][i]) # filtragem e decomposicao rec_signal = func.wavelet_filtering(instance, th) instances_list.append(rec_signal) # extracao de caracteriticas features = func.extract_feature(rec_signal, rate) # rotulacao physionet label_object = open(base + '/' + labels[-1][i], 'r') label = label_object.read().split('\n')[-2] features.append(label) # features = func.pyAudioAnalysis_features(recSignal, rate) features_list.append(features) print('Done with: ' + files[-1][i]) func.write_csv(path_Physio + base[-1] + '_filtered', features_list) # dir_db = os.path.basename(base) # func.write_csv('librosa_features_of_' + dir_db, features_list) print('Done with: ' + base + '\n') print 'Done!'
def main(): files = [] # instance = [] labels = [] file_paths = [] files = func.get_filenames(mumur, '.wav') # carrega arquivos de audio for s in files: file_paths.append(mumur + '/' + s) instance, rates = func.load_sound_files(file_paths) # ecg = pywt.data.ecg() # dataset do pywavelets # Wavelet Daubechies order 6 (ortogonal) wavelet = pywt.Wavelet('db6') (phi, psi, x) = wavelet.wavefun() # wavelet = signal.daub(6) # identification print "Rotulo:", label[1] print "Nome Arquivo:", files[0] print "Frequencia (fs): ", rates[0] # print "\n", wavelet # filtragem e decomposicao decimateSignal = signal.decimate(instance[0], 10) # decomposicao de sinal # y = func.butter_lowpass_filter(decimateSignal, lowcut, fs, order=6) # passa baixa coeffs = pywt.wavedec(decimateSignal, 'db6', level=4) # transformada wavelet cA4, cD4, cD3, cD2, cD1 = coeffs cD4 = pywt.threshold(cD4, th, mode='less') cD3 = pywt.threshold(cD3, th, mode='less') cD2 = pywt.threshold(cD2, th, mode='less') cD1 = pywt.threshold(cD1, th, mode='less') coeffs = cA4, cD4, cD3, cD2, cD1 # new_coeffs = [] # aux = [] # for cd in coeffs[1:]: # for d in cd: # if 288.0 >= d > 0.0: # aux = np.append(aux, d) # new_coeffs.append(aux) # reconstrucao do sinal recSignal = pywt.waverec(coeffs, 'db6') # verifica se sinais sao iguais if np.array_equal(recSignal, instance[0]): print('! sinais iguais !') # smooth = signal.savgol_filter(recSignal, 5, 2) # print smooth # z = np.polyfit(smooth, , 3) # print z # std = np.std(smooth) # print "std", std # helper = [] # for i, x in enumerate(smooth): # if x > std: # helper.append(i) # # print helper # Spectrogram # f, Pwelch_spec = signal.welch(recSignal, fs, scaling='spectrum') # plt.semilogy(f, Pwelch_spec) # plt.xlabel('frequency [Hz]') # plt.ylabel('PSD') # plt.grid() # plt.show() # # f = pywt.threshold(f, 195, mode='less') # plt.semilogy(f, Pwelch_spec) # plt.show() # # spectro = aF.stSpectogram(recSignal, rates[0], 0.05*rates[0], 0.025*rates[0], PLOT=True) # print spectro func.write_csv(files[0], recSignal) # Plotagem dos sinais func.plot_imagens(psi, 'Wavelet Daubechies de Ordem 6') # wavelet func.plot_imagens(instance[0], 'sinal nao filtrado') # func.plot_imagens(decimateSignal, 'sinal reduzido - fator 10') func.plot_imagens(recSignal, 'sinal reconstruido') # func.plot_imagens(recSignal2, 'sinal reconstruido - upcoef') # func.plot_imagens(smooth, 'suavizacao triangular') # func.plot_imagens(z,'polyfit') plt.show()