Changeset c54b2c0


Ignore:
Timestamp:
Dec 2, 2013, 6:14:33 PM (11 years ago)
Author:
Paul Brossier <piem@piem.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
41121f5
Parents:
3f27a98
Message:

python/demos/demo_pitch.py: add plotting

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/demos/demo_pitch.py

    r3f27a98 rc54b2c0  
    22
    33import sys
    4 from aubio import source, pitch
    5 
    6 win_s = 1024 # fft size
    7 hop_s = win_s # hop size
     4from aubio import source, pitch, freqtomidi
    85
    96if len(sys.argv) < 2:
     
    1310filename = sys.argv[1]
    1411
    15 samplerate = 0
     12downsample = 1
     13samplerate = 44100 / downsample
    1614if len( sys.argv ) > 2: samplerate = int(sys.argv[2])
     15
     16win_s = 4096 / downsample # fft size
     17hop_s = 512  / downsample # hop size
    1718
    1819s = source(filename, samplerate, hop_s)
    1920samplerate = s.samplerate
    2021
    21 pitch_o = pitch("default", win_s, hop_s, samplerate)
    22 pitch_o.set_unit("midi")
     22tolerance = 0.8
     23
     24pitch_o = pitch("yin", win_s, hop_s, samplerate)
     25pitch_o.set_unit("freq")
     26pitch_o.set_tolerance(tolerance)
    2327
    2428pitches = []
    25 
     29confidences = []
    2630
    2731# total number of frames read
     
    3034    samples, read = s()
    3135    pitch = pitch_o(samples)[0]
    32     print "%f %f" % (total_frames / float(samplerate), pitch)
    33     #pitches += [pitches]
     36    #pitch = int(round(pitch))
     37    confidence = pitch_o.get_confidence()
     38    #if confidence < 0.8: pitch = 0.
     39    print "%f %f %f" % (total_frames / float(samplerate), pitch, confidence)
     40    pitches += [pitch]
     41    confidences += [confidence]
    3442    total_frames += read
    3543    if read < hop_s: break
    3644
     45if 0: sys.exit(0)
     46
    3747#print pitches
     48from numpy import array, ma
     49import matplotlib.pyplot as plt
     50from demo_waveform_plot import get_waveform_plot, set_xlabels_sample2time
     51
     52skip = 1
     53
     54pitches = array(pitches[skip:])
     55confidences = array(confidences[skip:])
     56times = [t * hop_s for t in range(len(pitches))]
     57
     58fig = plt.figure()
     59ax1 = fig.add_subplot(311)
     60ax1 = get_waveform_plot(filename, samplerate = samplerate, block_size = hop_s, ax = ax1)
     61ax1.set_xticklabels([])
     62
     63def array_from_text_file(filename, dtype = 'float'):
     64    import os.path
     65    from numpy import array
     66    filename = os.path.join(os.path.dirname(__file__), filename)
     67    return array([line.split() for line in open(filename).readlines()],
     68        dtype = dtype)
     69
     70ax2 = fig.add_subplot(312, sharex = ax1)
     71import sys, os.path
     72ground_truth = os.path.splitext(filename)[0] + '.f0.Corrected'
     73if os.path.isfile(ground_truth):
     74    ground_truth = array_from_text_file(ground_truth)
     75    true_freqs = ground_truth[:,2]
     76    true_freqs = ma.masked_where(true_freqs < 2, true_freqs)
     77    true_times = float(samplerate) * ground_truth[:,0]
     78    ax2.plot(true_times, true_freqs, 'r')
     79    ax2.axis( ymin = 0.9 * true_freqs.min(), ymax = 1.1 * true_freqs.max() )
     80# plot raw pitches
     81ax2.plot(times, pitches, '--g')
     82# plot cleaned up pitches
     83cleaned_pitches = pitches
     84#cleaned_pitches = ma.masked_where(cleaned_pitches < 0, cleaned_pitches)
     85#cleaned_pitches = ma.masked_where(cleaned_pitches > 120, cleaned_pitches)
     86cleaned_pitches = ma.masked_where(confidences < tolerance, cleaned_pitches)
     87ax2.plot(times, cleaned_pitches, '.-')
     88#ax2.axis( ymin = 0.9 * cleaned_pitches.min(), ymax = 1.1 * cleaned_pitches.max() )
     89#ax2.axis( ymin = 55, ymax = 70 )
     90ax2.set_xticklabels([])
     91
     92# plot confidence
     93ax3 = fig.add_subplot(313, sharex = ax1)
     94# plot the confidence
     95ax3.plot(times, confidences)
     96# draw a line at tolerance
     97ax3.plot(times, [tolerance]*len(confidences))
     98ax3.axis( xmin = times[0], xmax = times[-1])
     99plt.show()
     100set_xlabels_sample2time(ax3, times[-1], samplerate)
     101#plt.savefig(os.path.basename(filename) + '.png', dpi=200)
Note: See TracChangeset for help on using the changeset viewer.