Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -20,79 +20,13 @@ __LICENSE__ = """\ · 19 unmodified lines | ||
| 20 | 20 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 | 21 | """ |
| 22 | 22 | |
| 23 | −__notesheight = 0.25 | |
| 24 | − | |
| 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 | 23 | |
| 24 | +__notesheight = 0.25 | |
| 60 | 25 | |
| 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) | |
| 93 | 26 | |
| 94 | 27 | def audio_to_array(filename): |
| 95 | 28 | import aubio.aubioclass |
| 29 | + import numarray | |
| 96 | 30 | hopsize = 2048 |
| 97 | 31 | filei = aubio.aubioclass.sndfile(filename) |
| 98 | 32 | framestep = 1/(filei.samplerate()+0.) |
| @@ -108,18 +42,19 @@ def audio_to_array(filename): · 9 unmodified lines | ||
| 108 | 42 | while (curpos < readsize): |
| 109 | 43 | data.append(myvec.get(curpos,i)) |
| 110 | 44 | curpos+=1 |
| 111 | − time = arange(len(data))*framestep | |
| 45 | + time = numarray.arange(len(data))*framestep | |
| 112 | 46 | return time,data |
| 113 | 47 | |
| 114 | 48 | def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): |
| 115 | − g = Gnuplot.Gnuplot(debug=1, persist=1) | |
| 49 | + g = gnuplot_init(fileout) | |
| 116 | 50 | d = [] |
| 117 | 51 | todraw = len(filenames) |
| 118 | 52 | xorig = 0. |
| 119 | 53 | xsize = 1./todraw |
| 120 | 54 | g.gnuplot('set multiplot;') |
| 121 | 55 | 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)) | |
| 123 | 58 | if not noaxis and todraw==1: |
| 124 | 59 | g.xlabel('Time (s)') |
| 125 | 60 | g.ylabel('Amplitude') |
| @@ -127,27 +62,29 @@ def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): · 1 unmodified line | ||
| 127 | 62 | g.gnuplot('set origin %f,0.;' % (xorig) ) |
| 128 | 63 | g.gnuplot('set style data lines; \ |
| 129 | 64 | set yrange [-1.:1.]; \ |
| 130 | − set xrange [0:%f]' % b[-1]) | |
| 65 | + set xrange [0:%f]' % time[-1]) | |
| 131 | 66 | g.plot(d.pop(0)) |
| 132 | 67 | xorig += 1./todraw |
| 133 | 68 | g.gnuplot('unset multiplot;') |
| 134 | − if fileout != None: | |
| 135 | − g.hardcopy(fileout, enhanced=1, color=0) | |
| 136 | 69 | |
| 137 | 70 | def make_audio_plot(time,data,maxpoints=10000): |
| 138 | 71 | """ create gnuplot plot from an audio file """ |
| 72 | + import numarray | |
| 73 | + import Gnuplot, Gnuplot.funcutils | |
| 139 | 74 | length = len(time) |
| 140 | 75 | downsample = length/maxpoints |
| 141 | 76 | 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] | |
| 144 | 79 | return Gnuplot.Data(x,y,with='lines') |
| 145 | 80 | |
| 146 | 81 | |
| 147 | 82 | def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot=None): |
| 83 | + import Gnuplot, Gnuplot.funcutils | |
| 148 | 84 | import aubio.txtfile |
| 149 | 85 | import os.path |
| 150 | 86 | import numarray |
| 87 | + import re | |
| 151 | 88 | from aubio.onsetcompare import onset_roc |
| 152 | 89 | |
| 153 | 90 | d,d2 = [],[] |
| @@ -185,25 +122,15 @@ def plot_onsets(filename, onsets, ofunc, samplerate=44100., hopsize=512, outplot · 31 unmodified lines | ||
| 185 | 122 | title = "GD %2.3f%% FP %2.3f%%" % \ |
| 186 | 123 | ((100*float(orig-missed-merged)/(orig)), |
| 187 | 124 | (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)) | |
| 193 | 125 | |
| 194 | 126 | # audio data |
| 195 | 127 | time,data = audio_to_array(filename) |
| 196 | 128 | d2.append(make_audio_plot(time,data)) |
| 197 | 129 | |
| 198 | 130 | # 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) | |
| 131 | + g = gnuplot_init(outplot) | |
| 205 | 132 | |
| 206 | − g('set title \'%s %s\'' % (filename,title)) | |
| 133 | + g('set title \'%s %s\'' % (re.sub('.*/','',filename),title)) | |
| 207 | 134 | |
| 208 | 135 | g('set multiplot') |
| 209 | 136 | |
| @@ -236,6 +163,8 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 26 unmodified lines | ||
| 236 | 163 | import aubio.txtfile |
| 237 | 164 | import os.path |
| 238 | 165 | import numarray |
| 166 | + import Gnuplot | |
| 167 | + import re | |
| 239 | 168 | |
| 240 | 169 | d = [] |
| 241 | 170 | maxpitch = 100 |
| @@ -245,7 +174,7 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 3 unmodified lines | ||
| 245 | 174 | title=('%d' % i))) |
| 246 | 175 | maxpitch = max(maxpitch,max(pitch[i][:])*1.1) |
| 247 | 176 | |
| 248 | − # check if datafile exists truth | |
| 177 | + # check if ground truth exists | |
| 249 | 178 | datafile = filename.replace('.wav','.txt') |
| 250 | 179 | if not os.path.isfile(datafile): |
| 251 | 180 | title = "truth file not found" |
| @@ -253,42 +182,23 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 1 unmodified line | ||
| 253 | 182 | else: |
| 254 | 183 | title = "truth file plotting not implemented yet" |
| 255 | 184 | 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')) | |
| 261 | 192 | |
| 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 | − | |
| 273 | 193 | # audio data |
| 274 | 194 | time,data = audio_to_array(filename) |
| 275 | 195 | f = make_audio_plot(time,data) |
| 276 | 196 | |
| 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)) | |
| 287 | 199 | g('set multiplot') |
| 288 | − | |
| 289 | 200 | # hack to align left axis |
| 290 | 201 | g('set lmargin 15') |
| 291 | − | |
| 292 | 202 | # plot waveform and onsets |
| 293 | 203 | g('set size 1,0.3') |
| 294 | 204 | g('set origin 0,0.7') |
| @@ -296,9 +206,7 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 1 unmodified line | ||
| 296 | 206 | g('set yrange [-1:1]') |
| 297 | 207 | g.ylabel('amplitude') |
| 298 | 208 | g.plot(f) |
| 299 | − | |
| 300 | 209 | g('unset title') |
| 301 | − | |
| 302 | 210 | # plot onset detection function |
| 303 | 211 | g('set size 1,0.7') |
| 304 | 212 | g('set origin 0,0') |
| @@ -308,5 +216,18 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 3 unmodified lines | ||
| 308 | 216 | g.xlabel('time') |
| 309 | 217 | g.ylabel('frequency (Hz)') |
| 310 | 218 | g.plot(*d) |
| 311 | − | |
| 312 | 219 | g('unset multiplot') |
| 220 | + | |
| 221 | +def 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 | |