Source code for analysis.SpectralCrestFactorAnalysis

from __future__ import print_function, division
import numpy as np
import logging
import pdb
import warnings

from Analysis import Analysis

[docs]class SpectralCrestFactorAnalysis(Analysis): """ Spectral crest factor descriptor class for generation of spectral crest factor audio analysis. This descriptor calculates the spectral crest factor for overlapping grains of an AnalysedAudioFile object. A full definition can be found in the documentation. Arguments: - analysis_group: the HDF5 file group to use for the storage of the analysis. - config: The configuration module used to configure the analysis """ def __init__(self, AnalysedAudioFile, frames, analysis_group, config=None): super(SpectralCrestFactorAnalysis, self).__init__(AnalysedAudioFile, frames, analysis_group, 'SpcCrestFactor') # Create logger for module self.logger = logging.getLogger(__name__+'.{0}Analysis'.format(self.name)) # Store reference to the file to be analysed self.AnalysedAudioFile = AnalysedAudioFile self.nyquist_rate = self.AnalysedAudioFile.samplerate / 2. try: fft = self.AnalysedAudioFile.analyses["fft"] except KeyError: raise KeyError("FFT analysis is required for spectral spread " "analysis.") self.analysis_group = analysis_group self.logger.info("Creating Spectral CrestFactor analysis for {0}".format(self.AnalysedAudioFile.name)) self.create_analysis( self.create_spccf_analysis, fft.analysis['frames'], ) self.spccf_window_count = None
[docs] def hdf5_dataset_formatter(self, analysis_method, *args, **kwargs): ''' Formats the output from the analysis method to save to the HDF5 file. ''' samplerate = self.AnalysedAudioFile.samplerate output = self.create_spccf_analysis(*args, **kwargs) times = self.calc_spccf_frame_times(output, self.AnalysedAudioFile.frames, samplerate) return ({'frames': output, 'times': times}, {})
@staticmethod
[docs] def create_spccf_analysis(fft): ''' Calculate the spectral crest factor of the fft frames. ''' fft = fft[:] # Get the positive magnitudes of each bin. magnitudes = np.abs(fft) # Get highest magnitude if not np.nonzero(magnitudes)[0].size: y = np.empty(magnitudes.shape[0]) y.fill(np.nan) return y # Get the highest magnitude value for each spectral frame max_bins = np.max(magnitudes, axis=1) mag_sum = np.sum(magnitudes, axis=1) with warnings.catch_warnings(): warnings.filterwarnings('ignore') spectral_cf = max_bins / mag_sum return spectral_cf
@staticmethod
[docs] def calc_spccf_frame_times(spccf_frames, sample_frame_count, samplerate):
"""Calculate times for frames using sample size and samplerate.""" # Get number of frames for time and frequency timebins = spccf_frames.shape[0] # Create array ranging from 0 to number of time frames scale = np.arange(timebins+1) # divide the number of samples by the total number of frames, then # multiply by the frame numbers. spccf_times = (float(sample_frame_count)/float(timebins)) * scale[:-1].astype(float) # Divide by the samplerate to give times in seconds spccf_times = spccf_times / samplerate return spccf_times
[docs] def mean_formatter(self, data): """Calculate the mean value of the analysis data""" values = data[0] output = np.empty(len(values)) for ind, i in enumerate(values): mean_i = np.mean(i) if mean_i == 0: output[ind] = np.nan else: output[ind] = np.log10(np.mean(i))/self.nyquist_rate return output
[docs] def median_formatter(self, data): """Calculate the median value of the analysis data""" values = data[0] output = np.empty(len(data)) for ind, i in enumerate(values): median_i = np.median(i) if median_i == 0: output[ind] = np.nan else: output[ind] = np.log10(np.median(i))/self.nyquist_rate return output