def computeSpectrum(signal):
    #gen = VectorInput(signal)
    #fc = FrameCutter(startFromZero=False, frameSize=48, hopSize=1)
    #w = Windowing(zeroPhase=False)
    #spec = Spectrum()

    #p = essentia.Pool()
    #gen.data >> fc.signal
    #fc.frame >> w.frame >> spec.frame
    #spec.spectrum >> (p,'spectrum')
    #essentia.run(gen)

    #pyplot.imshow(p['spectrum'], cmap=pyplot.cm.hot, aspect='auto', origin='lower')

    corr = std.AutoCorrelation()(signal)
    pyplot.plot(corr)
    pyplot.show()
    print argmax(corr[2:]) + 2
Example #2
0
import essentia.standard as es
import numpy as np
import scipy.signal

FS = 44100

w = es.Windowing(type='hann')
spectrum = es.Spectrum()
centroid = es.Centroid()
moments = es.CentralMoments()

# Temporal descriptors
power = es.InstantPower()
log_attack_time = es.LogAttackTime()
effective_duration = es.EffectiveDuration()
auto_correlation = es.AutoCorrelation()
zero_crossing_rate = es.ZeroCrossingRate()

# Spectral descriptors
peak_freq = es.MaxMagFreq()
roll_off = es.RollOff()
flux = es.Flux()
flatness = es.Flatness()

# Harmonic descriptors
pitch = es.PitchYin(frameSize=1024)
spectral_peaks = es.SpectralPeaks(minFrequency=1e-5)
harmonic_peaks = es.HarmonicPeaks()
inharmonicity = es.Inharmonicity()
oer = es.OddToEvenHarmonicEnergyRatio()
tristimulus = es.Tristimulus()
import essentia.standard as ess
# matplotlib without any blocking GUI
import matplotlib as mpl

mpl.use('Agg')
import matplotlib.pyplot as plt
import numpy as np

from smst.utils import audio

(fs, x) = audio.read_wav('../../../sounds/piano.wav')
start = 13860
M = 800
xp = x[start:start + M] / float(max(x[start:start + M]))
r = ess.AutoCorrelation(normalization='standard')(xp)
r = r / max(r)
peaks = ess.PeakDetection(threshold=.11, interpolate=False, minPosition=.01)(r)

plt.figure(1, figsize=(9, 7))
plt.subplot(211)
plt.plot(np.arange(M) / float(fs), xp, lw=1.5)
plt.axis([0, (M - 1) / float(fs), min(xp), max(xp)])
plt.xlabel('time (sec)')
plt.ylabel('amplitude')
plt.title('x (piano.wav)')

plt.subplot(212)
plt.plot(np.arange(M) / float(fs), r, 'r', lw=1.5)
plt.plot(peaks[0] * (M - 1) / float(fs),
         peaks[1],
         'x',
def plot(pool, title, outputfile='out.svg', subplot=111):
    ''' plots bars for each beat'''

    #computeSpectrum(pool['loudness'])

    ticks = pool['ticks']
    #barSize = min([ticks[i+1] - ticks[i] for i in range(len(ticks[:-1]))])/2.
    barSize = 0.8
    offset = barSize / 2.

    loudness = pool['loudness']
    loudnessBand = pool['loudnessBandRatio']  # ticks x bands

    medianRatiosPerTick = []
    meanRatiosPerTick = []
    for tick, energy in enumerate(loudnessBand):
        medianRatiosPerTick.append(median(energy))
        meanRatiosPerTick.append(mean(energy))

    loudnessBand = copy.deepcopy(loudnessBand.transpose())  # bands x ticks

    #xcorr = std.CrossCorrelation(minLag=0, maxLag=16)
    #acorr = std.AutoCorrelation()
    #bandCorr = []
    #for iBand, band in enumerate(loudnessBand):
    #    bandCorr.append(acorr(essentia.array(band)))

    nBands = len(loudnessBand)
    nticks = len(loudness)
    maxRatiosPerBand = []
    medianRatiosPerBand = []
    meanRatiosPerBand = []
    for idxBand, band in enumerate(loudnessBand):
        maxRatiosPerBand.append([0] * nticks)
        medianRatiosPerBand.append([0] * nticks)
        meanRatiosPerBand.append([0] * nticks)
        for idxTick in range(nticks):
            start = idxTick
            end = start + BEATWINDOW
            if (end > nticks):
                howmuch = end - nticks
                end = nticks - 1
                start = end - howmuch
                if start < 0: start = 0
            medianRatiosPerBand[idxBand][idxTick] = median(band[start:end])
            maxRatiosPerBand[idxBand][idxTick] = max(band[start:end])
            meanRatiosPerBand[idxBand][idxTick] = mean(band[start:end])

    for iBand, band in enumerate(loudnessBand):
        for tick, ratio in enumerate(band):
            #if ratio < medianRatiosPerBand[iBand][tick] and\
            #   ratio <= medianRatiosPerTick[tick]: loudnessBand[iBand][tick]=0
            bandThreshold = max(medianRatiosPerBand[iBand][tick],
                                meanRatiosPerBand[iBand][tick])
            tickThreshold = max(medianRatiosPerTick[tick],
                                meanRatiosPerTick[tick])
            if ratio < bandThreshold and ratio <= tickThreshold:
                loudnessBand[iBand][tick] = 0
            else:
                loudnessBand[iBand][tick] *= loudness[tick]
                #if loudnessBand[iBand][tick] > 1 : loudnessBand[iBand][tick] = 1

    acorr = std.AutoCorrelation()
    bandCorr = []
    maxCorr = []
    for iBand, band in enumerate(loudnessBand):
        bandCorr.append(acorr(essentia.array(band)))
        maxCorr.append(argmax(bandCorr[-1][2:]) + 2)

    # use as much window space as possible:
    pyplot.subplots_adjust(left=0.05, right=0.95, bottom=0.05, top=0.95)

    pyplot.subplot(511)
    pyplot.imshow(bandCorr,
                  cmap=pyplot.cm.hot,
                  aspect='auto',
                  origin='lower',
                  interpolation='nearest')
    print 'max correlation', maxCorr

    sumCorr = []
    for tick in range(nticks):
        total = 0
        for band in bandCorr:
            total += band[tick]
        sumCorr.append(total)

    sumCorr[0] = 0
    sumCorr[1] = 0
    pyplot.subplot(512)
    maxAlpha = max(sumCorr)
    for i, val in enumerate(sumCorr):
        alpha = max(0, min(val / maxAlpha, 1))
        pyplot.bar(i,
                   1,
                   barSize,
                   align='edge',
                   bottom=0,
                   alpha=alpha,
                   color='r',
                   edgecolor='w',
                   linewidth=.3)

    print 'max sum correlation', argmax(sumCorr[2:]) + 2

    hist = getHarmonics(sumCorr)
    maxHist = argmax(hist)
    print 'max histogram', maxHist
    #for idx,val in enumerate(hist):
    #    if val < maxHist: hist[idx] = 0

    pyplot.subplot(513)
    for i, val in enumerate(hist):
        pyplot.bar(i,
                   val,
                   barSize,
                   align='edge',
                   bottom=0,
                   color='r',
                   edgecolor='w',
                   linewidth=.3)

    peakDetect = std.PeakDetection(maxPeaks=5,
                                   orderBy='amplitude',
                                   minPosition=0,
                                   maxPosition=len(sumCorr) - 1,
                                   range=len(sumCorr) - 1)
    peaks = peakDetect(sumCorr)[0]
    peaks = [round(x + 1e-15) for x in peaks]
    print 'Peaks:', peaks

    pyplot.subplot(514)
    maxAlpha = max(sumCorr)
    for i, val in enumerate(sumCorr):
        alpha = max(0, min(val / maxAlpha, 1))
        pyplot.bar(i,
                   val,
                   barSize,
                   align='edge',
                   bottom=0,
                   alpha=alpha,
                   color='r',
                   edgecolor='w',
                   linewidth=.3)

    # multiply both histogram and sum corr to have a weighted histogram:
    wHist = essentia.array(hist) * sumCorr * acorr(loudness)
    maxHist = argmax(wHist)
    print 'max weighted histogram', maxHist
    pyplot.subplot(515)

    maxAlpha = max(wHist)
    for i, val in enumerate(wHist):
        alpha = max(0, min(val / maxAlpha, 1))
        pyplot.bar(i,
                   val,
                   barSize,
                   align='edge',
                   bottom=0,
                   alpha=alpha,
                   color='r',
                   edgecolor='w',
                   linewidth=.3)

    pyplot.savefig(outputfile, dpi=300)
    #pyplot.show()
    return