def load_snore(self):
     snores = []
     wavpaths = [os.path.join(self.snorepath,i) for i in os.listdir(self.snorepath) if '.wav' in i]
     for wavpath in wavpaths:
         wavsignal, fs = read_wav_data(wavpath)
         wavsignal = wavsignal[0]
         snores.append( (wavsignal, wavpath) )
     return snores
 def load_one_noise(self, noise_type):
     noise_segs = []
     paths = self.__load_one_noise_paths__(noise_type)
     # self.number_check(paths)
     for apath in paths:
         seg_path = os.path.join(self.setpath, apath[0], apath[1], apath[2])
         wavsignal, fs = read_wav_data(seg_path)
         wavsignal = wavsignal[0]
         output = (wavsignal, seg_path)
         noise_segs.append(output)
     return noise_segs
    def GetData(self, n_start, n_amount=32, dataType = 'same'):
        assert(n_amount%CLASS_NUM==0)
        if dataType == self.pathSameLabel:
            path = self.listSame[n_start][0]
            data_label = np.array([self.listSame[n_start][1]])
        elif dataType == self.pathDistinctLabel:
            path = self.listDistinct[n_start][0]
            data_label = np.array([self.listDistinct[n_start][1]])
        wavsignal, fs = read_wav_data(path)
        if FEATURE_TYPE in ['spec', 'Spec', 'SPEC', 'Spectrogram', 'SPECTROGRAM']:
            data_input = GetFrequencyFeatures(wavsignal, fs, self.feat_dimension, self.frame_length, shift=160)
            if PRIOR_ART == True:
               data_input = hog(data_input, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1),visualise=False)
               return data_input, data_label
        elif FEATURE_TYPE in ['mfcc', 'MFCC', 'Mfcc']:
            data_input = SimpleMfccFeatures(wavsignal, fs)
        else:
            print('Unknown feature type.')
            assert (0)


        data_input = data_input.reshape(data_input.shape[0], data_input.shape[1], 1)
        return data_input, data_label
import os
from feature_transformation import read_wav_data, GetFrequencyFeatures 
import matplotlib.pyplot as plt
if __name__ == '__main__':
    apath = '../../Data/BSfilteringData/data/762758/bowels/81730697302190_2018_12_20_12_02_42.wav'
    wavsignal, fs = read_wav_data(apath)
    wavsignal = wavsignal[0]
    plt.plot(wavsignal)
    plt.show()
    def GetData(self, n_start, n_amount=32, featureType = 'spectrogram',  mode='balanced'):
        '''
        读取数据,返回神经网络输入值和输出值矩阵(可直接用于神经网络训练的那种)
        参数:
            n_start:从编号为n_start数据开始选取数据
            n_amount:选取的数据数量,默认为1,即一次一个wav文件
        返回:
            四个音频四个label,
        '''
        # 随机从四个文件夹中拿一条数据,判断是否大于1s,否就重拿
        assert(n_amount%CLASS_NUM==0)
        category = (self.list_bowel1, self.list_non0)
        link = ('bowels', 'non')
        label = (1, 0)
        # extract = {'spectrogram':GetFrequencyFeatures,'mfcc':mfccFeatures}
        path = ''
        if FEATURE_TYPE in ['spec','Spec','SPEC','Spectrogram','SPECTROGRAM']:
            if mode == 'balanced':
                data = []
                labels = []
                for genre in range(CLASS_NUM):
                    for file in range(n_amount//CLASS_NUM):
                        filename = category[genre][(n_start + file)%self.DataNum[genre]]
                        # filename = category[genre][(n_start + file) % min(self.DataNum)]
                        path = self.common_path + link[genre] + self.slash + filename
                        wavsignal, fs = read_wav_data(path)
                        # data_input = SimpleMfccFeatures(wavsignal, fs)
                        data_input = GetFrequencyFeatures(wavsignal, fs, self.feat_dimension, self.frame_length,shift=160)
                        data_input = self.shifting(data_input)
                        if PRIOR_ART == True:
                            data_input = hog(data_input, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1), visualise=False)
                        # data_input = MelSpectrogram(wavsignal, fs,frame_length = self.frame_length, shift=160,filternum = 26)
                        else:

                            data_input = data_input.reshape(data_input.shape[0], data_input.shape[1], 1)
                        data.append(data_input)
                        data_label = np.array([label[genre]])
                        labels.append(data_label)
                return data, labels
            if mode == 'non-repetitive':
                path = self.list_path[n_start][0]
                data_label = np.array([self.list_path[n_start][1]])
                wavsignal, fs = read_wav_data(path)
                data_input = GetFrequencyFeatures(wavsignal, fs, self.feat_dimension, self.frame_length,shift=160)
                # data_input = MelSpectrogram(wavsignal, fs, frame_length=self.frame_length, shift=160, filternum=26)
                # data_input = SimpleMfccFeatures(wavsignal, fs)
                if PRIOR_ART == True:
                    data_input = hog(data_input, orientations=8, pixels_per_cell=(16, 16), cells_per_block=(1, 1),visualise=False)
                else:
                    data_input = data_input.reshape(data_input.shape[0], data_input.shape[1], 1)
                return data_input,  data_label
        elif FEATURE_TYPE in ['mfcc','MFCC','Mfcc']:
            if mode == 'balanced':
                data = []
                labels = []
                for genre in range(CLASS_NUM):
                    for file in range(n_amount//CLASS_NUM):
                        filename = category[genre][(n_start + file)%self.DataNum[genre]]
                        # filename = category[genre][(n_start + file) % min(self.DataNum)]
                        path = self.common_path + link[genre] + self.slash + filename
                        wavsignal, fs = read_wav_data(path)
                        data_input = SimpleMfccFeatures(wavsignal, fs)
                        data_label = np.array([label[genre]])
                        # if data_label[0] == 0:
                        # if PRIOR_ART == False:
                        data_input = self.shifting(data_input)
                        data_input = data_input.reshape(data_input.shape[0], data_input.shape[1], 1)
                        data.append(data_input)
                        labels.append(data_label)
                return data, labels
            if mode == 'non-repetitive':
                path = self.list_path[n_start][0]
                data_label = np.array([self.list_path[n_start][1]])
                wavsignal, fs = read_wav_data(path)
                data_input = SimpleMfccFeatures(wavsignal, fs)
                data_input = data_input.reshape(data_input.shape[0], data_input.shape[1], 1)
                return data_input,  data_label
        else:
            print('Unknown feature type.')
            assert(0)