Showing the changes to python (1 of 1 file). Show all
Unified · Split
| @@ -45,43 +45,54 @@ def audio_to_array(filename): · 44 unmodified lines | ||
| 45 | 45 | time = numarray.arange(len(data))*framestep |
| 46 | 46 | return time,data |
| 47 | 47 | |
| 48 | −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,xsize=1.,ysize=1.): | |
| 49 | 49 | g = gnuplot_init(fileout) |
| 50 | 50 | d = [] |
| 51 | 51 | todraw = len(filenames) |
| 52 | 52 | xorig = 0. |
| 53 | − xsize = 1./todraw | |
| 53 | + xratio = 1./todraw | |
| 54 | + g.gnuplot('set size %f,%f;' % (xsize,ysize) ) | |
| 54 | 55 | g.gnuplot('set multiplot;') |
| 55 | 56 | while (len(filenames)): |
| 56 | 57 | time,data = audio_to_array(filenames.pop(0)) |
| 57 | − d.append(make_audio_plot(time,data)) | |
| 58 | 58 | if not noaxis and todraw==1: |
| 59 | − g.xlabel('Time (s)') | |
| 59 | + if max(time) < 1.: | |
| 60 | + time = [t*1000. for t in time] | |
| 61 | + g.xlabel('Time (ms)') | |
| 62 | + else: | |
| 63 | + g.xlabel('Time (s)') | |
| 60 | 64 | g.ylabel('Amplitude') |
| 61 | − g.gnuplot('set size %f,1.;' % (xsize) ) | |
| 65 | + d.append(make_audio_plot(time,data)) | |
| 66 | + g.gnuplot('set size %f,%f;' % (xsize*xratio,ysize) ) | |
| 62 | 67 | g.gnuplot('set origin %f,0.;' % (xorig) ) |
| 63 | 68 | g.gnuplot('set style data lines; \ |
| 64 | 69 | set yrange [-1.:1.]; \ |
| 65 | 70 | set xrange [0:%f]' % time[-1]) |
| 66 | 71 | g.plot(d.pop(0)) |
| 67 | − xorig += 1./todraw | |
| 72 | + xorig += xsize*xratio | |
| 68 | 73 | g.gnuplot('unset multiplot;') |
| 69 | 74 | |
| 70 | −def audio_to_spec(filename): | |
| 75 | +def audio_to_spec(filename,minf = 0, maxf = 0, lowthres = 0.): | |
| 71 | 76 | from aubioclass import fvec,cvec,pvoc,sndfile |
| 72 | − from math import log | |
| 73 | − bufsize = 256*16 | |
| 74 | − hopsize = bufsize/4 # could depend on filelength | |
| 77 | + from math import log10 | |
| 78 | + bufsize = 8192 | |
| 79 | + hopsize = bufsize/8 # could depend on filelength | |
| 75 | 80 | filei = sndfile(filename) |
| 76 | 81 | srate = float(filei.samplerate()) |
| 77 | 82 | framestep = hopsize/srate |
| 78 | 83 | freqstep = srate/bufsize |
| 79 | 84 | channels = filei.channels() |
| 80 | 85 | myvec = fvec(hopsize,channels) |
| 81 | − myfft = cvec(bufsize,channels) | |
| 82 | − pv = pvoc(bufsize,hopsize,channels) | |
| 86 | + myfft = cvec(bufsize,channels) | |
| 87 | + pv = pvoc(bufsize,hopsize,channels) | |
| 83 | 88 | data,time,freq = [],[],[] |
| 84 | − for f in range(bufsize/2): | |
| 89 | + | |
| 90 | + if maxf == 0.: maxf = bufsize/2 | |
| 91 | + else: maxf = int(maxf/freqstep) | |
| 92 | + if minf: minf = int(minf/freqstep) | |
| 93 | + else: minf = 0 | |
| 94 | + | |
| 95 | + for f in range(minf,maxf): | |
| 85 | 96 | freq.append(f*freqstep) |
| 86 | 97 | readsize = hopsize |
| 87 | 98 | frameread = 0 |
| @@ -90,9 +101,9 @@ def audio_to_spec(filename): · 2 unmodified lines | ||
| 90 | 101 | pv.do(myvec,myfft) |
| 91 | 102 | frame = [] |
| 92 | 103 | i = 0 #for i in range(channels): |
| 93 | − curpos = 0 | |
| 94 | − while (curpos < bufsize/2): | |
| 95 | − frame.append(log(myfft.get(curpos,i)**2+0.000001)) | |
| 104 | + curpos = minf | |
| 105 | + while (curpos < maxf): | |
| 106 | + frame.append(max(lowthres,20.*log10(myfft.get(curpos,i)**2+0.00001))) | |
| 96 | 107 | curpos+=1 |
| 97 | 108 | time.append(frameread*framestep) |
| 98 | 109 | data.append(frame) |
| @@ -106,24 +117,24 @@ def audio_to_spec(filename): · 7 unmodified lines | ||
| 106 | 117 | assert len(data[0]) == len(freq) |
| 107 | 118 | return data,time,freq |
| 108 | 119 | |
| 109 | −def plot_spec(filename, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1): | |
| 120 | +def plot_spec(filename, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1, minf=0, maxf= 0, xsize = 1., ysize = 1.): | |
| 110 | 121 | import Gnuplot |
| 111 | 122 | g = gnuplot_create(outplot,extension) |
| 112 | − data,time,freq = audio_to_spec(filename) | |
| 123 | + data,time,freq = audio_to_spec(filename,minf=minf,maxf=maxf) | |
| 113 | 124 | xorig = 0. |
| 114 | − xsize = 1.#/todraw | |
| 115 | 125 | if not noaxis: |
| 116 | 126 | g.xlabel('Time (s)') |
| 117 | 127 | g.ylabel('Frequency (Hz)') |
| 118 | − g.gnuplot('set pm3d map') | |
| 119 | − #g.gnuplot('set palette rgbformulae 30,31,32') | |
| 120 | − #g.gnuplot('set palette') | |
| 121 | − g.gnuplot('set xrange [0.:%f]' % time[-1]) | |
| 122 | − g.gnuplot('set yrange [1.:%f]' % (freq[-1]/1.)) | |
| 128 | + g('set size %f,%f' % (xsize, ysize)) | |
| 129 | + g('set pm3d map') | |
| 130 | + g('set palette rgbformulae -25,-24,-32') | |
| 131 | + #g('set colorbox horizontal') | |
| 132 | + g('set xrange [0.:%f]' % time[-1]) | |
| 133 | + g('set yrange [%f:%f]' % (minf,maxf)) | |
| 123 | 134 | if log: |
| 124 | − g.gnuplot('set yrange [10.1:%f]' % (freq[-1]/1.)) | |
| 125 | − g.gnuplot('set log y') | |
| 126 | − g.splot(Gnuplot.GridData(data,time,freq, binary=1)) | |
| 135 | + g('set yrange [%f:%f]' % (max(10,minf),maxf)) | |
| 136 | + g('set log y') | |
| 137 | + g.splot(Gnuplot.GridData(data,time,freq, binary=1, title='mag. (dB)')) | |
| 127 | 138 | #xorig += 1./todraw |
| 128 | 139 | |
| 129 | 140 | def downsample_audio(time,data,maxpoints=10000): |
| @@ -166,6 +177,8 @@ def gnuplot_create(outplot='',extension='',debug=0,persist=1): · 36 unmodified lines | ||
| 166 | 177 | g = Gnuplot.Gnuplot(debug=debug, persist=persist) |
| 167 | 178 | if not extension or not outplot: return g |
| 168 | 179 | if extension == 'ps': ext, extension = '.ps' , 'postscript' |
| 180 | + elif extension == 'eps': ext, extension = '.eps' , 'postscript enhanced' | |
| 181 | + elif extension == 'epsc': ext, extension = '.eps' , 'postscript enhanced color' | |
| 169 | 182 | elif extension == 'png': ext, extension = '.png', 'png' |
| 170 | 183 | elif extension == 'svg': ext, extension = '.svg', 'svg' |
| 171 | 184 | else: exit("ERR: unknown plot extension") |