Example #1
0
def gauss_pulse_timeshift_dataset(class_sample_size, visualize_each_data_in_time=False):
    '''
    Designing 3 Gauss pulse with different time shift, contaminated with white noise
    This return 2 classes of xcor map, created from xcor signal[0] & [1] and signal[0] & [2]. 2 classes repr.
    different shift of the pulse in the received signal in sensors.
    :return:
    a 3d array, where axis[0] -> samples size
                      axis[1] -> freq axis
                      axis[2] -> xcor scores
    # This data set was successfully trained by CNN, proving that this pattern is recognizable
    '''
    # time setting
    t = np.linspace(0, 2, 5000, endpoint=False)

    mix_signal_1, mix_signal_2, mix_signal_3 = [], [], []
    # creating n samples for each of the 2 classes
    for i in range(class_sample_size):
        # create noise contaminated gauss pulse
        mix_signal_1.append(gausspulse(t - 0.5, fc=50) + white_noise(time_axis=t, power=0.1))
        mix_signal_2.append(gausspulse(t - 0.6, fc=50) + white_noise(time_axis=t, power=0.1))
        mix_signal_3.append(gausspulse(t - 0.7, fc=50) + white_noise(time_axis=t, power=0.1))

    class_1, class_2 = [], []
    for i in range(class_sample_size):
        # STFT
        t, f, mat1, _ = spectrogram_scipy(sampled_data=mix_signal_1[i],
                                          fs=2500,  # because 2500 points per sec
                                          nperseg=200,
                                          noverlap=0,
                                          mode='magnitude',
                                          return_plot=False,
                                          verbose=False)

        _, _, mat2, _ = spectrogram_scipy(sampled_data=mix_signal_2[i],
                                          fs=2500,  # because 2500 points per sec
                                          nperseg=200,
                                          noverlap=0,
                                          mode='magnitude',
                                          return_plot=False,
                                          verbose=False)

        _, _, mat3, _ = spectrogram_scipy(sampled_data=mix_signal_3[i],
                                          fs=2500,  # because 2500 points per sec
                                          nperseg=200,
                                          noverlap=0,
                                          mode='magnitude',
                                          return_plot=False,
                                          verbose=False)
        l = np.array([mat1, mat2, mat3])
        xcor_map = one_dim_xcor_2d_input(input_mat=l, pair_list=[(0, 1), (0, 2)], verbose=False)
        # signal labelling
        class_1.append(xcor_map[0])
        class_2.append(xcor_map[1])
        # fig1 = three_dim_visualizer(x_axis=np.arange(0, xcor_map.shape[2], 1),
        #                             y_axis=f,
        #                             zxx=xcor_map[0],
        #                             output='2d',
        #                             label=['a', 'b', 'cx'])
        # fig2 = three_dim_visualizer(x_axis=np.arange(0, xcor_map.shape[2], 1),
        #                             y_axis=f,
        #                             zxx=xcor_map[1],
        #                             output='2d',
        #                             label=['a', 'b', 'cx'])
        # plt.show()

    # convert to np array
    class_1 = np.array(class_1)
    class_2 = np.array(class_2)
    dataset = np.concatenate((class_1, class_2), axis=0)
    label = np.array([0] * class_1.shape[0] + [1] * class_2.shape[0])

    print('Data set Dim: ', dataset.shape)
    print('Label Dim: ', label.shape)

    return dataset, label
Example #2
0
        signal_2_denoised = dwt_smoothing(x=segment[sensor_pair[1]],
                                          wavelet=dwt_wavelet,
                                          level=dwt_smooth_level)

        pos1_leak_cwt, _ = pywt.cwt(signal_1_denoised,
                                    scales=scale,
                                    wavelet=cwt_wavelet,
                                    sampling_period=1 / fs)
        pos2_leak_cwt, _ = pywt.cwt(signal_2_denoised,
                                    scales=scale,
                                    wavelet=cwt_wavelet,
                                    sampling_period=1 / fs)

        # xcor for every pair of cwt
        xcor, _ = one_dim_xcor_2d_input(input_mat=np.array(
            [pos1_leak_cwt, pos2_leak_cwt]),
                                        pair_list=[(0, 1)])
        xcor = xcor[0]
        # visualizing
        fig_title = 'Xcor of CWT of Sensor[{}] and Sensor[{}] -- Dist_Diff[{}m] -- Sample[{}]'.format(
            sensor_pair[0], sensor_pair[1], dist_diff, sample_no)

        fig = plot_cwt_with_time_series(
            time_series=[signal_1_denoised, signal_2_denoised],
            no_of_time_series=2,
            cwt_mat=xcor,
            cwt_scale=scale,
            title=fig_title,
            maxpoint_searching_bound=24000)

        # only for showing the max point vector
Example #3
0
    def leak_2class(self):
        '''
        This uses the sensor[-1m] & [23m] as a pair and sensor[-2m] & [22m] as a pair. Both of them has distance of 24m.
        This can act as 2 different leak position, differ by 1m. Since we have only 3 sets of time series, each is 5M
        points, we break them into segments of 100k points (equiv. to 0.1s), so each class will have 150 samples.
        :return:
        class_1 -> 3d array where shape[0]-> no. of samples(150)
                                  shape[1]-> freq axis (20kHz to 100kHz)
                                  shape[2]-> xcor steps (2000 units)
        '''
        n_channel_data = read_all_tdms_from_folder(self.path_leak_1bar_10mm)
        # concat all 3 sets into (15M, 4)
        n_channel_data_all_set = np.concatenate(
            (n_channel_data[0], n_channel_data[1], n_channel_data[2]), axis=0)
        # split 15M points into 150 segments
        sample_size = 150
        all_segment_of_4_sensor = np.split(n_channel_data_all_set,
                                           indices_or_sections=sample_size)
        all_segment_of_4_sensor = np.array(all_segment_of_4_sensor)

        # STFT for every sample, for every sensors ----------------------------------
        sensor_0_stft, sensor_1_stft, sensor_2_stft, sensor_3_stft = [], [], [], []
        # initiate progressbar
        pb = ProgressBarForLoop(title='Bandpass + STFT',
                                end=all_segment_of_4_sensor.shape[0])
        progress = 0
        # for all samples
        for sample in all_segment_of_4_sensor:
            temp_stft_bank_4_sensor = []
            # for all sensors
            for i in range(4):
                # bandpass from 20kHz t0 100kHz
                filtered_signal = butter_bandpass_filtfilt(
                    sampled_data=sample[:, i],
                    fs=1e6,
                    f_hicut=1e5,
                    f_locut=20e3)
                # STFT
                _, _, sxx, _ = spectrogram_scipy(sampled_data=filtered_signal,
                                                 fs=1e6,
                                                 mode='magnitude',
                                                 nperseg=100,
                                                 nfft=500,
                                                 noverlap=0,
                                                 return_plot=False,
                                                 verbose=False)
                # Take only
                temp_stft_bank_4_sensor.append(
                    sxx[10:51, :])  # index_10 -> f=20kHz; index_50 -> f=100kHz
            sensor_0_stft.append(temp_stft_bank_4_sensor[0])
            sensor_1_stft.append(temp_stft_bank_4_sensor[1])
            sensor_2_stft.append(temp_stft_bank_4_sensor[2])
            sensor_3_stft.append(temp_stft_bank_4_sensor[3])
            # update progressbar
            pb.update(now=progress)
            progress += 1
        pb.destroy()
        # convert to np array
        sensor_0_stft = np.array(sensor_0_stft)
        sensor_1_stft = np.array(sensor_1_stft)
        sensor_2_stft = np.array(sensor_2_stft)
        sensor_3_stft = np.array(sensor_3_stft)

        # Xcor in freq band
        class_1, class_2 = [], []

        # initiate progressbar
        pb = ProgressBarForLoop(title='Cross Correlation',
                                end=all_segment_of_4_sensor.shape[0])
        progress = 0
        for i in range(sample_size):
            # for class 1, sensor[-2m] & [22m]
            stft_map = np.array([sensor_0_stft[i], sensor_2_stft[i]])
            sensor_pair = [(0, 1)]
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=stft_map,
                                                pair_list=sensor_pair,
                                                verbose=False)
            class_1.append(xcor_map[0, :, 700:1300])

            # for class 1, sensor[-1m] & [23m]
            stft_map = np.array([sensor_1_stft[i], sensor_3_stft[i]])
            sensor_pair = [(0, 1)]
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=stft_map,
                                                pair_list=sensor_pair,
                                                verbose=False)
            class_2.append(xcor_map[0, :, 700:1300])

            pb.update(now=progress)
        pb.destroy()

        # packaging dataset and label
        class_1 = np.array(class_1)
        class_2 = np.array(class_2)
        dataset = np.concatenate((class_1, class_2), axis=0)
        label = np.array([0] * class_1.shape[0] + [1] * class_2.shape[0])

        print('Dataset Dim: ', dataset.shape)
        print('Label Dim: ', label.shape)

        return dataset, label
Example #4
0
    def generate_leak_1bar_in_cwt_xcor_maxpoints_vector(
            self, saved_filename=None, file_to_process=None, denoise=False):
        '''
        this method read all tdms file from a folder, split each of them into certain parts, perform CWT follow by XCOR
        according to the sensor pair list, then append into a dataset with labels
        :param saved_filename: filename Label for the dataset generated
        :param file_to_process: a list of strings, which is full dir and filename of the tdms to be processed. if none,
                                it is taken as all tdms in the 1bar leak
        :param denoise: True it will denoise the signal bfore CWT and xcor
        :return: dataset where shape[0] -> no of samples of all classes
                               shape[1] -> no of elements in a vector
                 label where shape[0] -> aligned with the shape[0] of dataset
                             shape[1] -> 1
        '''
        # CONFIG -------------------------------------------------------------------------------------------------------
        # DWT
        dwt_wavelet = 'db2'
        dwt_smooth_level = 2

        # CWT
        m_wavelet = 'gaus1'
        scale = np.linspace(2, 10, 100)
        fs = 1e6

        # segmentation per tdms (sample size by each tdms)
        no_of_segment = 2

        # file dir
        if file_to_process is None:
            # list full path of all tdms file in the specified folder
            folder_path = self.path_leak_1bar_2to12
            all_file_path = [(folder_path + f) for f in listdir(folder_path)
                             if f.endswith('.tdms')]
        else:
            all_file_path = file_to_process

        # DATA READING -------------------------------------------------------------------------------------------------
        # creating dict to store each class data
        all_class = {}
        for i in range(0, 11, 1):
            all_class['class_[{}]'.format(i)] = []

        # for all tdms file in folder (Warning: It takes 24min for 1 tdms file)
        for tdms_file in all_file_path:

            # read raw from drive
            n_channel_data_near_leak = read_single_tdms(tdms_file)
            n_channel_data_near_leak = np.swapaxes(n_channel_data_near_leak, 0,
                                                   1)

            # split on time axis into no_of_segment
            n_channel_leak = np.split(n_channel_data_near_leak,
                                      axis=1,
                                      indices_or_sections=no_of_segment)

            if denoise:
                temp = []
                for signal in n_channel_leak:
                    denoised_signal = dwt_smoothing(x=signal,
                                                    wavelet=dwt_wavelet,
                                                    level=dwt_smooth_level)
                    temp.append(denoised_signal)
                n_channel_leak = temp

            dist_diff = 0
            # for all sensor combination
            for sensor_pair in self.sensor_pair_near:
                segment_no = 0
                pb = ProgressBarForLoop(
                    title='CWT+Xcor using {}'.format(sensor_pair),
                    end=len(n_channel_leak))
                # for all segmented signals
                for segment in n_channel_leak:
                    pos1_leak_cwt, _ = pywt.cwt(segment[sensor_pair[0]],
                                                scales=scale,
                                                wavelet=m_wavelet)
                    pos2_leak_cwt, _ = pywt.cwt(segment[sensor_pair[1]],
                                                scales=scale,
                                                wavelet=m_wavelet)

                    # xcor for every pair of cwt
                    xcor, _ = one_dim_xcor_2d_input(input_mat=np.array(
                        [pos1_leak_cwt, pos2_leak_cwt]),
                                                    pair_list=[(0, 1)])
                    xcor = xcor[0]

                    # midpoint in xcor
                    mid = xcor.shape[1] // 2 + 1

                    max_xcor_vector = []
                    # 24000 = fs*24ms(max deviation in ToA)
                    upper_xcor_bound = mid + 24000
                    lower_xcor_bound = mid - 24000
                    # for every row of xcor, find max point index
                    for row in xcor:
                        max_along_x = np.argmax(
                            row[lower_xcor_bound:upper_xcor_bound])
                        max_xcor_vector.append(max_along_x + lower_xcor_bound -
                                               mid)

                    # store all feature vector for same class
                    all_class['class_[{}]'.format(dist_diff)].append(
                        max_xcor_vector)

                    # progress
                    pb.update(now=segment_no)
                    segment_no += 1

                    # free up memory for unwanted variable
                    pos1_leak_cwt, pos2_leak_cwt, xcor = None, None, None
                    gc.collect()

                pb.destroy()
                dist_diff += 1

            # just to display the dict full dim
            temp = []
            for _, value in all_class.items():
                temp.append(value[0])
            temp = np.array(temp)
            print('all_class dim: ', temp.shape)

            # free up memory for unwanted variable
            pos1_leak_cwt, pos2_leak_cwt, n_channel_data_near_leak = None, None, None
            gc.collect()

        # transfer all data from dict to array
        dataset = []
        label = []
        # for all class
        for i in range(0, 11, 1):
            # for all samples in a class
            for sample in all_class['class_[{}]'.format(
                    i)]:  # a list of list(max vec)
                dataset.append(sample)
                label.append(i)

        # convert to array
        dataset = np.array(dataset)
        label = np.array(label)
        print('Dataset Dim: ', dataset.shape)
        print('Label Dim: ', label.shape)

        # save to csv
        label = label.reshape((-1, 1))
        all_in_one = np.concatenate([dataset, label], axis=1)
        # column label
        freq = pywt.scale2frequency(wavelet=m_wavelet, scale=scale) * fs
        column_label = [
            'Scale_{:.4f}_Freq_{:.4f}Hz'.format(i, j)
            for i, j in zip(scale, freq)
        ] + ['label']
        df = pd.DataFrame(all_in_one, columns=column_label)
        filename = direct_to_dir(
            where='result'
        ) + 'cwt_xcor_maxpoints_vector_dataset_{}.csv'.format(saved_filename)
        df.to_csv(filename)
Example #5
0
    def plb_unseen(self):
        # use near only
        n_channel_data = read_all_tdms_from_folder(self.path_plb_2to12)

        # swap axis, so shape[0] is sensor (for easy using)
        n_channel_data = np.swapaxes(n_channel_data, 1,
                                     2)  # swap axis[0] and [1]

        # pairing order for xcor
        sensor_pair = [(0, 5)]
        class_1 = []
        # initiate progressbar
        pb = ProgressBarForLoop(title='Bandpass + STFT + XCOR',
                                end=n_channel_data.shape[0])
        progress = 0
        # for all plb samples
        for sample in n_channel_data:
            all_channel_stft = []
            # for all sensors
            for each_sensor_data in sample:
                # band pass filter
                filtered_signal = butter_bandpass_filtfilt(
                    sampled_data=each_sensor_data[90000:130000],
                    fs=1e6,
                    f_hicut=1e5,
                    f_locut=20e3)
                # stft
                _, _, sxx, _ = spectrogram_scipy(sampled_data=filtered_signal,
                                                 fs=1e6,
                                                 mode='magnitude',
                                                 nperseg=100,
                                                 nfft=500,
                                                 noverlap=0,
                                                 return_plot=False,
                                                 verbose=True)
                all_channel_stft.append(
                    sxx[10:51, :])  # index_10 -> f=20kHz; index_50 -> f=100kHz
            all_channel_stft = np.array(all_channel_stft)

            # xcor for sensor pair
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=all_channel_stft,
                                                pair_list=sensor_pair,
                                                verbose=False)
            # visualize and saving the training data
            savepath = 'C:/Users/YH/PycharmProjects/AE-signal-model/result/'
            visualize = False
            if visualize:
                fig_1 = heatmap_visualizer(
                    x_axis=np.arange(0, xcor_map.shape[2], 1),
                    y_axis=np.arange(0, xcor_map.shape[1], 1),
                    zxx=xcor_map[0],
                    output='2d',
                    label=['xcor step', 'freq'],
                    title='(-2, 2)')
                fig_2 = heatmap_visualizer(
                    x_axis=np.arange(0, xcor_map.shape[2], 1),
                    y_axis=np.arange(0, xcor_map.shape[1], 1),
                    zxx=xcor_map[1],
                    output='2d',
                    label=['xcor step', 'freq'],
                    title='(-2, 4)')
                fig_3 = heatmap_visualizer(
                    x_axis=np.arange(0, xcor_map.shape[2], 1),
                    y_axis=np.arange(0, xcor_map.shape[1], 1),
                    zxx=xcor_map[2],
                    output='2d',
                    label=['xcor step', 'freq'],
                    title='(-2, 6)')

                fig_1_title = '{}sample{}_xcormap(-2, 2)'.format(
                    savepath, progress)
                fig_2_title = '{}sample{}_xcormap(-2, 4)'.format(
                    savepath, progress)
                fig_3_title = '{}sample{}_xcormap(-2, 6)'.format(
                    savepath, progress)

                fig_1.savefig(fig_1_title)
                fig_2.savefig(fig_2_title)
                fig_3.savefig(fig_3_title)

                plt.close('all')

            class_1.append(xcor_map[0, 10:20, 300:500])

            # update progress
            pb.update(now=progress)
            progress += 1
        pb.destroy()

        class_1 = np.array(class_1)
        print('Dataset Dim: ', class_1.shape)

        return class_1
Example #6
0
    def plb(self):
        '''
        This function returns a dataset of xcor map, each belongs to a class of PLB captured by 2 sensors at different
        distance. 51 samples for each class.
        :return:
        Xcor map dataset where axis[0] -> total sample size of all classes
                               axis[1] -> frequency bin
                               axis[2] -> time step (xcor steps)
        '''
        # initialize
        n_channel_data_near = read_all_tdms_from_folder(self.path_plb_2to12)
        n_channel_data_far = read_all_tdms_from_folder(self.path_plb_10to22)
        # take only first 51 samples, so it has same class sizes with data_near
        n_channel_data_far = n_channel_data_far[:51]

        # swap axis, so shape[0] is sensor (for easy using)
        n_channel_data_near = np.swapaxes(n_channel_data_near, 1,
                                          2)  # swap axis[1] and [2]
        n_channel_data_far = np.swapaxes(n_channel_data_far, 1, 2)

        # -----------[BANDPASS + STFT + XCOR]-------------
        # xcor pairing commands - [near] = 0m, 1m,..., 10m
        sensor_pair_near = self.sensor_pair_near
        # invert the sensor pair to generate the opposite lag
        sensor_pair_near_inv = [(pair[1], pair[0])
                                for pair in sensor_pair_near]

        # xcor pairing commands - [far] = 11m, 12m,..., 20m
        sensor_pair_far = self.sensor_pair_far
        # invert the sensor pair to generate the opposite lag
        sensor_pair_far_inv = [(pair[1], pair[0]) for pair in sensor_pair_far]

        # horizontal segmentation of the xcormap (along xcor step axis)
        xcormap_extent = (250, 550)

        # initialize a dict of lists for every classes
        all_class = {}
        for i in range(-20, 21, 1):
            all_class['class_[{}]'.format(i)] = []

        # class_1, class_2, class_3, class_4, class_5, class_6, class_7, class_8, class_9, class_10 = \
        #     [], [], [], [], [], [], [], [], [], []

        # Sensor [Near] -------------------------------------------------
        # initiate progressbar
        pb = ProgressBarForLoop(title='Bandpass + STFT + XCOR --> [Near]',
                                end=n_channel_data_near.shape[0])
        progress = 0
        # for all plb samples
        for sample in n_channel_data_near:
            all_channel_stft = []

            # for all sensors
            for each_sensor_data in sample:
                # band pass filter
                filtered_signal = butter_bandpass_filtfilt(
                    sampled_data=each_sensor_data[90000:130000],
                    fs=1e6,
                    f_hicut=1e5,
                    f_locut=20e3)

                # denoise
                # dwt denoising setting
                dwt_wavelet = 'db2'
                dwt_smooth_level = 3
                filtered_signal = dwt_smoothing(x=filtered_signal,
                                                wavelet=dwt_wavelet,
                                                level=dwt_smooth_level)

                # stft
                _, _, sxx, _ = spectrogram_scipy(sampled_data=filtered_signal,
                                                 fs=1e6,
                                                 mode='magnitude',
                                                 nperseg=100,
                                                 nfft=500,
                                                 noverlap=0,
                                                 return_plot=False,
                                                 verbose=False)
                all_channel_stft.append(
                    sxx[10:51, :])  # index_10 -> f=20kHz; index_50 -> f=100kHz
            all_channel_stft = np.array(all_channel_stft)

            # xcor for sensor pair (0m) - (10m)
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=all_channel_stft,
                                                pair_list=sensor_pair_near,
                                                verbose=False)

            for i in range(0, 11, 1):
                all_class['class_[{}]'.format(i)].append(
                    xcor_map[i, 10:20, xcormap_extent[0]:xcormap_extent[1]])

            # xcor for sensor pair (0m) - (-10m)
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=all_channel_stft,
                                                pair_list=sensor_pair_near_inv,
                                                verbose=False)
            for i in range(0, 11, 1):
                all_class['class_[{}]'.format(-i)].append(
                    xcor_map[i, 10:20, xcormap_extent[0]:xcormap_extent[1]])

            # update progress bar
            pb.update(now=progress)
            progress += 1
        pb.destroy()

        # shuffle the class_0 and truncate only 51 samples
        shuffle(all_class['class_[0]'])
        all_class['class_[0]'] = all_class['class_[0]'][:51]

        # Sensor [Far] -------------------------------------------------
        # initiate progressbar
        pb = ProgressBarForLoop(title='Bandpass + STFT + XCOR --> [Far]',
                                end=n_channel_data_near.shape[0])
        progress = 0
        for sample in n_channel_data_far:
            all_channel_stft = []
            # for all sensors
            for each_sensor_data in sample:
                # band pass filter
                filtered_signal = butter_bandpass_filtfilt(
                    sampled_data=each_sensor_data[90000:130000],
                    fs=1e6,
                    f_hicut=1e5,
                    f_locut=20e3)
                # stft
                _, _, sxx, _ = spectrogram_scipy(sampled_data=filtered_signal,
                                                 fs=1e6,
                                                 mode='magnitude',
                                                 nperseg=100,
                                                 nfft=500,
                                                 noverlap=0,
                                                 return_plot=False,
                                                 verbose=False)
                all_channel_stft.append(
                    sxx[10:51, :])  # index_10 -> f=20kHz; index_50 -> f=100kHz
            all_channel_stft = np.array(all_channel_stft)

            # horizontal segmentation of the xcormap (along xcor step axis)
            xcormap_extent = (250, 550)

            # xcor for sensor pair (11m) - (20m)
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=all_channel_stft,
                                                pair_list=sensor_pair_far,
                                                verbose=False)

            for i in range(0, 10, 1):
                all_class['class_[{}]'.format(i + 11)].append(
                    xcor_map[i, 10:20, xcormap_extent[0]:xcormap_extent[1]])

            # xcor for sensor pair (11m) - (-20m)
            xcor_map, _ = one_dim_xcor_2d_input(input_mat=all_channel_stft,
                                                pair_list=sensor_pair_far_inv,
                                                verbose=False)

            for i in range(0, 10, 1):
                all_class['class_[{}]'.format(-11 - i)].append(
                    xcor_map[i, 10:20, xcormap_extent[0]:xcormap_extent[1]])

            # visualize and saving the training data
            # savepath = 'C:/Users/YH/PycharmProjects/AE-signal-model/result/'
            # visualize = False
            # if visualize:
            #     for i in range(xcor_map.shape[0]):
            #         fig = three_dim_visualizer(x_axis=np.arange(xcormap_extent[0], xcormap_extent[1], 1),
            #                                    y_axis=np.arange(10, 20, 1),
            #                                    zxx=xcor_map[i, 10:20, xcormap_extent[0]:xcormap_extent[1]],
            #                                    output='2d',
            #                                    label=['xcor step', 'freq'],
            #                                    title='({}, {}) = -{}m'.format(sensor_pair_far_inv[i][0],
            #                                                                   sensor_pair_far_inv[i][1],
            #                                                                   i+11))
            #         fig_title = '{}sample{}_xcormap(dist=-{}m)'.format(savepath, progress, i+11)
            #         fig.savefig(fig_title)
            #         plt.close('all')

            # update progress
            pb.update(now=progress)
            progress += 1
        pb.destroy()

        dataset = []
        for i in range(-20, 21, 1):
            temp = np.array(all_class['class_[{}]'.format(i)])
            dataset.append(temp)

        dataset = np.concatenate(dataset, axis=0)
        label = [[i] * 51 for i in np.arange(-20, 21, 1)]
        label = np.array([item for l in label for item in l])

        print('Dataset Dim: ', dataset.shape)
        print('Label Dim: ', label.shape)

        return dataset, label
Example #7
0
def dual_sensor_xcor_with_stft_qiuckview(data_1,
                                         data_2,
                                         stft_mode,
                                         stft_nperseg=500,
                                         plot_label=None,
                                         save_selection=[0, 0, 0, 0]):
    '''
    Expecting a time series AE input with 1 PLB peak. Note that the sampling rate is default to 1MHz or 1e6
    :param data_1: 1 dimensional time series sensor data
    :param data_2: 1 dimensional time series sensor data
    :param stft_mode: 'angle', 'phase' or 'magnitude'
    :param stft_nperseg: nperseg actually. noverlap is zero here.
    :param plot_label: list of string, ['leak_position', 'sensor_1_pos', 'sensor_2_pos']
    :param save_selection: a list of 0 n 1 which means dont save n save. The index is equal to the 4 fig in return
    :return:
    fig_time -> time series plot of 2 sensors data
    fig_stft_1 -> spectrogram output of sensor 1 data
    fig_stft_2 -> spectrogram output of sensor 2 data
    fig_xcor -> xcor map color plot where axis[0]-freq band, axis[1]-xcor steps
    '''
    # input data validation
    assert len(data_1) == len(
        data_2), 'Input data must contains equal no of data points'
    assert len(plot_label) == 3, 'plot_label is incomplete'

    # Time series plot of Data 1 n 2 ---------------------------------------------------------
    fig_time = plt.figure()
    fig_time.subplots_adjust(hspace=0.5)
    # fig 1
    ax1 = fig_time.add_subplot(2, 1, 1)
    ax1.set_title('Time series sensor [{}] @ {}'.format(
        plot_label[1], plot_label[0]))
    ax2 = fig_time.add_subplot(2, 1, 2)
    ax2.set_title('Time series sensor [{}] @ {}'.format(
        plot_label[2], plot_label[0]))
    ax1.plot(data_1)
    ax2.plot(data_2)

    # STFT plot of Data 1 n 2 ---------------------------------------------------------------
    _, freq_axis, sxx1, fig_stft_1 = spectrogram_scipy(
        sampled_data=data_1,
        fs=1e6,
        nperseg=stft_nperseg,
        nfft=500,
        noverlap=0,
        mode=stft_mode,
        return_plot=True,
        plot_title='Freq-Time rep ({}) of sensor [{}] @ {}'.format(
            stft_mode, plot_label[1], plot_label[0]),
        verbose=False,
        vis_max_freq_range=1e5)  # for now we only visualize up to 100kHz

    _, _, sxx2, fig_stft_2 = spectrogram_scipy(
        sampled_data=data_2,
        fs=1e6,
        nperseg=stft_nperseg,
        nfft=500,
        noverlap=0,
        mode=stft_mode,
        return_plot=True,
        plot_title='Freq-Time rep ({}) of sensor [{}] @ {}'.format(
            stft_mode, plot_label[2], plot_label[0]),
        verbose=False,
        vis_max_freq_range=1e5)  # for now we only visualize up to 100kHz

    # Xcor of FT rep of Data 1 n 2 ----------------------------------------------------------
    stft_map = np.array([sxx1, sxx2])
    sensor_pair = [(0, 1)]
    xcor_map = one_dim_xcor_2d_input(input_mat=stft_map,
                                     pair_list=sensor_pair,
                                     verbose=True)
    fig_xcor = heatmap_visualizer(
        x_axis=np.arange(1, xcor_map.shape[2] + 1, 1),
        y_axis=freq_axis,
        zxx=xcor_map[0],
        label=['Xcor_steps', 'Frequency', 'Correlation Score'],
        output='2d',
        vis_range=[0, 1e5, None, None],
        title='PLB {} Map Xcor - Sensor[{}] x Sensor[{}] - @ {}'.format(
            stft_mode, plot_label[1], plot_label[2], plot_label[0]))

    # saving 4 fig ---------------------------------------------------------------
    dir = 'C:/Users/YH/PycharmProjects/AE-signal-model/result/'
    if save_selection[0] is 1:
        fig_time.savefig(dir + 'time series @ {}.png'.format(plot_label[0]))
    elif save_selection[1] is 1:
        fig_stft_1.savefig(dir + 'ft map 1 @ {}.png'.format(plot_label[1]))
    elif save_selection[2] is 1:
        fig_stft_2.savefig(dir + 'ft map 2 @ {}.png'.format(plot_label[2]))
    elif save_selection[3] is 1:
        fig_xcor.savefig(dir + 'xcor map @ {}.png'.format(plot_label[0]))

    # plt.close()

    return fig_time, fig_stft_1, fig_stft_2, fig_xcor