def pca(pool, namespace=''): llspace = 'lowlevel.' if namespace: llspace = namespace + '.lowlevel.' sccoeffs = pool[llspace + 'sccoeffs'] scvalleys = pool[llspace + 'scvalleys'] numFrames = len(sccoeffs) poolSc = Pool() merged = essentia.zeros(2 * len(sccoeffs[0])) for frame in xrange(numFrames): j = 0 for i in xrange(len(sccoeffs[frame])): merged[j] = sccoeffs[frame][i] merged[j + 1] = scvalleys[frame][i] j += 2 poolSc.add('contrast', merged) poolTransformed = standard.PCA(namespaceIn='contrast', namespaceOut='contrast')(poolSc) contrast = poolTransformed['contrast'] pool.set(llspace + 'spectral_contrast.mean', mean(contrast, axis=0)) pool.set(llspace + 'spectral_contrast.var', var(contrast, axis=0)) pool.remove(llspace + 'sccoeffs') pool.remove(llspace + 'scvalleys')
def spectralContrastPCA(scPool, pool): scCoeffs = scPool['lowlevel.sccoeffs'] scValleys = scPool['lowlevel.scvalleys'] frames = len(scCoeffs) coeffs = len(scCoeffs[0]) # merged = numpy.zeros([frames, coeffs], dtype='f4') merged = numpy.zeros(2 * coeffs, dtype='f4') for i in range(frames): k = 0 for j in range(coeffs): merged[k] = scCoeffs[i][j] merged[k + 1] = scValleys[i][j] k += 2 scPool.add('contrast', merged) pca = ess.PCA(namespaceIn='contrast', namespaceOut='contrast')(scPool) pool.add(namespace + '.' + 'spectral_contrast', [c[:] for c in pca['contrast']])