Example #1
0
    def __init__(self, data, fs, n_fft=512, win_len=512, hop_len=512):

        chop_fraction = data.size % win_len

        if chop_fraction != 0:
            data = data[:-chop_fraction]

        self.data = data
        self.fs = fs

        self.envelope = AudioProcessing.get_envelope(self.data,
                                                     frame_size=win_len)
        self.envelope[self.envelope < 0] = 0

        self.n_fft = n_fft
        self.win_len = win_len
        self.hop_len = hop_len

        self.stft = AudioProcessing.get_stft(self.data, n_fft, win_len,
                                             hop_len)
        self.stft = np.absolute(self.stft)
        self.smooth_spectrogram()

        n_drop = int(np.ceil(500 / (float(self.fs) / self.n_fft)))
        self.stft = self.stft[n_drop:, :]

        self._remove_spectrogram_noise()

        self._calculate_background_noise()
        self._get_temporal_entropy()

        self._get_segments_above_noise()
        self._get_spectral_entropy()
    def _get_segments_above_noise(self):
        threshold = self.background_noise + 3
        envelope = AudioProcessing.get_envelope(abs(self.data),
                                                frame_size=1024)
        non_zero = envelope[np.nonzero(envelope)]
        data_log = 20 * np.log10(non_zero)

        # print(data_log.shape)
        # kernel = 1/256.0*np.ones(156)
        #
        # data_log = np.convolve(data_log,kernel)
        # print(data_log.shape)

        ind = np.where(data_log > threshold)

        check_array = np.zeros(self.envelope.size)
        check_array[ind] = 1

        diff = np.diff(check_array)

        ones = np.where(diff == 1)[0]
        minus_ones = np.where(diff == -1)[0]

        if ones.size == 0:
            ones = np.array([0])

        if minus_ones.size == 0:
            minus_ones = np.array([check_array.size - 1])

        if ones[0] >= minus_ones[0]:
            ones = np.append(0, ones)

        if ones[-1] >= minus_ones[-1]:
            minus_ones = np.append(minus_ones, [check_array.size - 1])

        segments = []

        # considering segments which are greater than 100 ms
        min_seg_length = 0.1 * self.fs

        for i in range(ones.size):
            seg_duration = (minus_ones[i] - ones[i])

            if seg_duration > min_seg_length:
                segments.append(
                    (ones[i], minus_ones[i], minus_ones[i] - ones[i]))

        if 0:
            # plotting check array

            segment_array = np.zeros(self.data.size)
            for segment in segments:
                segment_array[segment[0]:segment[1]] = 1
            import matplotlib.pyplot as plt
            plt.plot(self.data)
            plt.plot(segment_array)
            plt.show()

        self.segments = segments