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