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