Changeset 733c2f8


Ignore:
Timestamp:
Nov 6, 2005, 12:30:12 PM (19 years ago)
Author:
Paul Brossier <piem@altern.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:
cd9b142
Parents:
d45520a
Message:

remove obsolete function and factorise gnuplot init
remove obsolete function and factorise gnuplot init

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/aubio/gnuplot.py

    rd45520a r733c2f8  
    2121"""
    2222
     23
    2324__notesheight = 0.25
    2425
    25 from numarray import *
    26 import Gnuplot, Gnuplot.funcutils
    27 
    28 def plotnote(la,title=None) :
    29         if la[0,:].size() == 3:
    30                 d = plotnote_withends(la, plot_title=title)
    31         else:
    32             # scale data if in freq (for REF.txt files)
    33             if max(la[:,1] > 128 ):
    34                 print "scaling frequency data to midi range"
    35                 la[:,1] /= 6.875
    36                 la[:,1] = log(la[:,1])/0.6931
    37                 la[:,1] *= 12
    38                 la[:,1] -= 3
    39             d = plotnote_withoutends(la, plot_title=title)
    40         return d
    41 
    42 def plotnote_multi(lalist,title=None,fileout=None) :
    43         d=list()
    44         for i in range(len(lalist)):
    45             d.append(plotnote(lalist[i], title=title))
    46         return d
    47        
    48 
    49 def plotnote_withends(la,plot_title=None) :
    50         d=[]
    51         x_widths = array(la[:,1]-la[:,0])/2.
    52         d.append(Gnuplot.Data(
    53                 la[:,0]+x_widths,               # x centers
    54                 la[:,2],                        # y centers
    55                 x_widths,                       # x errors
    56                 __notesheight*ones(len(la)),    # y errors
    57                 title=plot_title,with=('boxxyerrorbars fs 3')))
    58         return d
    59 
    60 
    61 def plotnote_withoutends(la,plot_title=None) :
    62         """ bug: fails drawing last note """
    63         d=[]
    64         x_widths = array(la[1:,0]-la[:-1,0])/2;
    65         d.append(Gnuplot.Data(
    66                 la[:-1,0]+x_widths,             # x centers
    67                 la[:-1,1],                      # y centers
    68                 x_widths,                       # x errors
    69                 __notesheight*ones(len(la)-1),  # y errors
    70                 title=plot_title,with=('boxxyerrorbars fs 3')))
    71         return d
    72 
    73 def plotnote_do(d,fileout=None):
    74     g = Gnuplot.Gnuplot(debug=1, persist=1)
    75     g.gnuplot('set style fill solid border 1; \
    76     set size ratio 1/6; \
    77     set boxwidth 0.9 relative; \
    78     set mxtics 2.5; \
    79     set mytics 2.5; \
    80     set xtics 5; \
    81     set ytics 1; \
    82     set grid xtics ytics mxtics mytics')
    83 
    84     g.xlabel('Time (s)')
    85     g.ylabel('Midi pitch')
    86     # do the plot
    87     #g.gnuplot('set multiplot')
    88     #for i in d:
    89     g.plot(d[0])
    90     #g.gnuplot('set nomultiplot')
    91     if fileout != None:
    92         g.hardcopy(fileout, enhanced=1, color=0)
    9326
    9427def audio_to_array(filename):
    9528        import aubio.aubioclass
     29        import numarray
    9630        hopsize  = 2048
    9731        filei    = aubio.aubioclass.sndfile(filename)
     
    10943                        data.append(myvec.get(curpos,i))
    11044                        curpos+=1
    111         time = arange(len(data))*framestep
     45        time = numarray.arange(len(data))*framestep
    11246        return time,data
    11347
    11448def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None):
    115         g = Gnuplot.Gnuplot(debug=1, persist=1)
     49        g = gnuplot_init(fileout)
    11650        d = []
    11751        todraw = len(filenames)
     
    12054        g.gnuplot('set multiplot;')
    12155        while (len(filenames)):
    122                 d.append(plot_audio_make(filenames.pop(0)))
     56                time,data = audio_to_array(filenames.pop(0))
     57                d.append(make_audio_plot(time,data))
    12358                if not noaxis and todraw==1:
    12459                        g.xlabel('Time (s)')
     
    12863                g.gnuplot('set style data lines; \
    12964                        set yrange [-1.:1.]; \
    130                         set xrange [0:%f]' % b[-1])
     65                        set xrange [0:%f]' % time[-1])
    13166                g.plot(d.pop(0))
    13267                xorig += 1./todraw
    13368        g.gnuplot('unset multiplot;')
    134         if fileout != None:
    135                 g.hardcopy(fileout, enhanced=1, color=0)
    13669
    13770def make_audio_plot(time,data,maxpoints=10000):
    13871        """ create gnuplot plot from an audio file """
     72        import numarray
     73        import Gnuplot, Gnuplot.funcutils
    13974        length = len(time)
    14075        downsample = length/maxpoints
    14176        if downsample == 0: downsample = 1
    142         x = array(time).resize(length)[0:-1:downsample]
    143         y = array(data).resize(length)[0:-1:downsample]
     77        x = numarray.array(time).resize(length)[0:-1:downsample]
     78        y = numarray.array(data).resize(length)[0:-1:downsample]
    14479        return Gnuplot.Data(x,y,with='lines')
    14580
    14681
    14782def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None):
     83        import Gnuplot, Gnuplot.funcutils
    14884        import aubio.txtfile
    14985        import os.path
    15086        import numarray
     87        import re
    15188        from aubio.onsetcompare import onset_roc
    15289
     
    186123                        ((100*float(orig-missed-merged)/(orig)),
    187124                         (100*float(bad+doubled)/(orig)))
    188                 #print  orig, missed, merged, expc, bad, doubled
    189                 #print "GD %2.8f\t"        % (100*float(orig-missed-merged)/(orig)),
    190                 #print "FP %2.8f\t"        % (100*float(bad+doubled)/(orig))       ,
    191                 #print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig))       ,
    192                 #print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))               
    193125
    194126        # audio data
     
    197129
    198130        # prepare the plot
    199         g = Gnuplot.Gnuplot(debug=1, persist=1)
    200         if outplot:
    201                 extension = outplot.split('.')[-1]
    202                 if extension == 'ps': extension = 'postscript'
    203                 g('set terminal %s' % extension)
    204                 g('set output \'%s\'' % outplot)
    205 
    206         g('set title \'%s %s\'' % (filename,title))
     131        g = gnuplot_init(outplot)
     132
     133        g('set title \'%s %s\'' % (re.sub('.*/','',filename),title))
    207134
    208135        g('set multiplot')
     
    237164        import os.path
    238165        import numarray
     166        import Gnuplot
     167        import re
    239168
    240169        d = []
     
    246175                maxpitch = max(maxpitch,max(pitch[i][:])*1.1)
    247176
    248         # check if datafile exists truth
     177        # check if ground truth exists
    249178        datafile = filename.replace('.wav','.txt')
    250179        if not os.path.isfile(datafile):
     
    254183                title = "truth file plotting not implemented yet"
    255184                values = aubio.txtfile.read_datafile(datafile)
    256                 time, pitch = [], []
    257                 for i in range(len(values)):
    258                         time.append(values[i][0])
    259                         pitch.append(values[i][1])
    260                 d.append(Gnuplot.Data(time,pitch,with='lines',title='ground truth'))
     185                if (len(datafile[0])) > 1:
     186                        time, pitch = [], []
     187                        for i in range(len(values)):
     188                                time.append(values[i][0])
     189                                pitch.append(values[i][1])
     190                        d.append(Gnuplot.Data(time,pitch,with='lines',
     191                                title='ground truth'))
    261192               
    262                 #orig, missed, merged, expc, bad, doubled = \
    263                 #        onset_roc(x2,x1,tol)
    264                 #title = "GD %2.3f%% FP %2.3f%%" % \
    265                 #        ((100*float(orig-missed-merged)/(orig)),
    266                 #         (100*float(bad+doubled)/(orig)))
    267                 #print  orig, missed, merged, expc, bad, doubled
    268                 #print "GD %2.8f\t"        % (100*float(orig-missed-merged)/(orig)),
    269                 #print "FP %2.8f\t"        % (100*float(bad+doubled)/(orig))       ,
    270                 #print "GD-merged %2.8f\t" % (100*float(orig-missed)/(orig))       ,
    271                 #print "FP-pruned %2.8f\t" % (100*float(bad)/(orig))               
    272 
    273193        # audio data
    274194        time,data = audio_to_array(filename)
    275195        f = make_audio_plot(time,data)
    276196
    277         # prepare the plot
    278         g = Gnuplot.Gnuplot(debug=1, persist=1)
    279         if outplot:
    280                 extension = outplot.split('.')[-1]
    281                 if extension == 'ps': extension = 'postscript'
    282                 g('set terminal %s' % extension)
    283                 g('set output \'%s\'' % outplot)
    284 
    285         g('set title \'%s %s\'' % (filename,title))
    286 
     197        g = gnuplot_init(outplot)
     198        g('set title \'%s %s\'' % (re.sub('.*/','',filename),title))
    287199        g('set multiplot')
    288 
    289200        # hack to align left axis
    290201        g('set lmargin 15')
    291 
    292202        # plot waveform and onsets
    293203        g('set size 1,0.3')
     
    297207        g.ylabel('amplitude')
    298208        g.plot(f)
    299        
    300209        g('unset title')
    301 
    302210        # plot onset detection function
    303211        g('set size 1,0.7')
     
    309217        g.ylabel('frequency (Hz)')
    310218        g.plot(*d)
    311 
    312219        g('unset multiplot')
     220
     221def gnuplot_init(outplot,debug=0,persist=1):
     222        import Gnuplot
     223        # prepare the plot
     224        g = Gnuplot.Gnuplot(debug=debug, persist=persist)
     225        if outplot == 'stdout':
     226                g("set terminal png fontfile 'p052023l.pfb'")
     227                #g('set output \'%s\'' % outplot)
     228        elif outplot:
     229                extension = outplot.split('.')[-1]
     230                if extension == 'ps': extension = 'postscript'
     231                g('set terminal %s' % extension)
     232                g('set output \'%s\'' % outplot)
     233        return g
Note: See TracChangeset for help on using the changeset viewer.