End of day commit. Problems with spectral flux in time calculation

This commit is contained in:
2016-04-12 02:36:58 +01:00
parent 0e503ac924
commit cfc4e285c9
8 changed files with 63 additions and 16 deletions
+2 -2
View File
@@ -118,10 +118,10 @@ class Analysis(object):
################################################################################
def log2_median(self, x):
return np.log2(np.median(x))
return np.median(1000 * np.log2(1+x/1000))
def log2_mean(self, x):
return np.log2(np.mean(x))
return np.mean(1000 * np.log2(1+x/1000))
def formatter_func(self, selection, frames, valid_inds, formatter=None):
# get all valid frames from current grain
+11 -2
View File
@@ -52,8 +52,13 @@ class KurtosisAnalysis(Analysis):
self.create_analysis(frames, variance.analysis['frames'][:], self.window_size, overlapFac=self.overlap)
@staticmethod
def create_kurtosis_analysis(frames, variance, window_size=512,
overlapFac=0.5):
def create_kurtosis_analysis(
frames,
variance,
window_size=512,
window=signal.hanning,
overlapFac=0.5
):
"""
Calculate the Kurtosis values of windowed segments of the audio file and
save to disk.
@@ -82,6 +87,10 @@ class KurtosisAnalysis(Analysis):
strides=(samples.strides[0]*hopSize, samples.strides[0])
).copy()
if window:
win = window(window_size)
frames *= win
frame_mean = np.mean(frames, axis=1)
variance_sqrd = variance**2
+7 -3
View File
@@ -53,9 +53,13 @@ class RMSAnalysis(Analysis):
self.create_analysis(frames, self.AnalysedAudioFile.samplerate, window_size=self.window_size, overlapFac=self.overlap, )
@staticmethod
def create_rms_analysis(frames,samplerate, window_size=512,
window=signal.triang,
overlapFac=0.5):
def create_rms_analysis(
frames,
samplerate,
window_size=512,
window=signal.hanning,
overlapFac=0.5
):
"""
Generate RMS contour analysis.
+11 -2
View File
@@ -52,8 +52,13 @@ class SkewnessAnalysis(Analysis):
self.create_analysis(frames, variance.analysis['frames'][:], self.window_size, overlapFac=self.overlap)
@staticmethod
def create_skewness_analysis(frames, variance, window_size=512,
overlapFac=0.5):
def create_skewness_analysis(
frames,
variance,
window_size=512,
window=signal.hanning,
overlapFac=0.5
):
"""
Calculate the skewness values of windowed segments of the audio file and
save to disk.
@@ -82,6 +87,10 @@ class SkewnessAnalysis(Analysis):
strides=(samples.strides[0]*hopSize, samples.strides[0])
).copy()
if window:
win = window(window_size)
frames *= win
frame_mean = np.mean(frames, axis=1)
variance_cubed = np.sqrt(variance)**3
@@ -72,7 +72,6 @@ class SpectralCrestFactorAnalysis(Analysis):
warnings.filterwarnings('ignore')
spectral_cf = max_bins / mag_sum
return spectral_cf
@staticmethod
@@ -53,18 +53,16 @@ class SpectralFluxAnalysis(Analysis):
return ({'frames': output, 'times': times}, {})
@staticmethod
def create_spcflux_analysis(fft, samplerate):
def create_spcflux_analysis(fft):
'''
Calculate the spectral flux of the fft frames.
length: the length of the window used to calculate the FFT.
samplerate: the samplerate of the audio analysed.
output_format = Choose either "freq" for output in Hz or "ind" for bin
index output
'''
# 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)
+13 -3
View File
@@ -35,7 +35,13 @@ class ZeroXAnalysis(Analysis):
self.create_analysis(frames)
@staticmethod
def create_zerox_analysis(frames, window_size=512, overlapFac=0.5, *args, **kwargs):
def create_zerox_analysis(
frames,
window_size=512,
overlapFac=0.5,
*args,
**kwargs
):
"""Generate zero crossing value for window of the signal"""
hopSize = int(window_size - np.floor(overlapFac * window_size))
@@ -47,13 +53,17 @@ class ZeroXAnalysis(Analysis):
# zeros at end (thus samples can be fully covered by frames)
samples = np.append(samples, np.zeros(window_size))
# TODO: Better handeling of zeros based on previous sign would improve
# accuracy.
epsilon = np.finfo(float).eps
samples[samples == 0.] += epsilon
frames = stride_tricks.as_strided(
samples,
shape=(cols, window_size),
strides=(samples.strides[0]*hopSize, samples.strides[0])
).copy()
zero_crossing = (1./(2.*samples.size))*np.sum(np.abs(np.diff(np.sign(frames))), axis=1)
zero_crossing = np.sum(np.abs(np.diff(np.sign(frames))), axis=1)
return zero_crossing
@staticmethod
+18
View File
@@ -513,6 +513,24 @@ class RMSAnalysisTests(globalTests):
np.testing.assert_almost_equal(output1[1], 1./np.sqrt(3), decimal=1)
np.testing.assert_almost_equal(output2[1], 1./np.sqrt(2), decimal=1)
class ZeroXAnalysisTests(globalTests):
"""Tests Zero-Crossing analysis generation"""
def setUp(self):
"""Create functions and variables before each test is run."""
self.silence = np.zeros(512)
self.sine = np.sin(2*np.pi*5000*np.arange(44100)/44100)
self.noise = np.random.uniform(low=-1.0, high=1.0, size=512)
def test_GenerateRMS(self):
"""Check that RMS values generated are the expected values"""
output = analysis.ZeroXAnalysis.create_zerox_analysis(self.silence)
output1 = analysis.ZeroXAnalysis.create_zerox_analysis(self.noise)
output2 = analysis.ZeroXAnalysis.create_zerox_analysis(self.sine, window_size=44100)
#TODO: Finish this test...
class PeakAnalysisTests(globalTests):
"""Tests Peak analysis generation"""