Esempio n. 1
0
 def test_spectral_snalysis_real_data(self):
     """This unittest checks the spectral analysis works on real data."""
     file_path = os.path.join(
         os.path.dirname(__file__), 'test_data', '1k_2k.raw')
     binary = open(file_path, 'rb').read()
     data = audio_data.AudioRawData(binary, 2, 'S32_LE')
     saturate_value = audio_data.get_maximum_value_from_sample_format(
         'S32_LE')
     golden_frequency = [1000, 2000]
     for channel in [0, 1]:
         normalized_signal = audio_analysis.normalize_signal(
             data.channel_data[channel], saturate_value)
         spectral = audio_analysis.spectral_analysis(normalized_signal,
                                                     48000, 0.02)
         logging.debug('channel %s: %s', channel, spectral)
         self.assertTrue(
             abs(spectral[0][0] - golden_frequency[channel]) < 5,
             'Dominant frequency is not correct')
Esempio n. 2
0
    def do_spectral_analysis(self, ignore_high_freq, check_quality,
                             quality_params):
        """Gets the spectral_analysis result.

        Args:
            ignore_high_freq: Ignore high frequencies above this threshold.
            check_quality: Check quality of each channel.
            quality_params: A QualityParams object for quality measurement.

        """
        self.has_data()
        for channel_idx in range(self._raw_data.channel):
            signal = self._raw_data.channel_data[channel_idx]
            max_abs = max(numpy.abs(signal))
            logging.debug('Channel %d max abs signal: %f', channel_idx,
                          max_abs)
            if max_abs == 0:
                logging.info('No data on channel %d, skip this channel',
                             channel_idx)
                continue

            saturate_value = audio_data.get_maximum_value_from_sample_format(
                self._raw_data.sample_format)
            normalized_signal = audio_analysis.normalize_signal(
                signal, saturate_value)
            logging.debug('saturate_value: %f', saturate_value)
            logging.debug('max signal after normalized: %f',
                          max(normalized_signal))
            spectral = audio_analysis.spectral_analysis(
                normalized_signal, self._rate)

            logging.debug('Channel %d spectral:\n%s', channel_idx,
                          pprint.pformat(spectral))

            # Ignore high frequencies above the threshold.
            spectral = [(f, c) for (f, c) in spectral if f < ignore_high_freq]

            logging.info('Channel %d spectral after ignoring high frequencies '
                         'above %f:\n%s', channel_idx, ignore_high_freq,
                         pprint.pformat(spectral))

            try:
                if check_quality:
                    quality = audio_quality_measurement.quality_measurement(
                        signal=normalized_signal,
                        rate=self._rate,
                        dominant_frequency=spectral[0][0],
                        block_size_secs=quality_params.block_size_secs,
                        frequency_error_threshold=quality_params.
                        frequency_error_threshold,
                        delay_amplitude_threshold=quality_params.
                        delay_amplitude_threshold,
                        noise_amplitude_threshold=quality_params.
                        noise_amplitude_threshold,
                        burst_amplitude_threshold=quality_params.
                        burst_amplitude_threshold)

                    logging.debug('Channel %d quality:\n%s', channel_idx,
                                  pprint.pformat(quality))
                    self._quality_result.append(quality)
                self._spectrals.append(spectral)
            except Exception as error:
                logging.warning(
                    "Failed to analyze channel {} with error: {}".format(
                        channel_idx, error))
Esempio n. 3
0
 def test_normalize(self):
     y = [1, 2, 3, 4, 5]
     normalized_y = audio_analysis.normalize_signal(y, 10)
     expected = numpy.array([0.1, 0.2, 0.3, 0.4, 0.5])
     for i in range(len(y)):
         self.assertEqual(expected[i], normalized_y[i])