History

add spectrogram plot add spectrogram plot

Paul Brossier committed 20 years ago

Commit 431e9d81 parent 7c206df1 file · +66 −4Browse files

Showing the changes to python (1 of 1 file). Show all

Unified · Split

python/aubio/gnuplot.py+66−4
@@ -53,8 +53,8 @@ def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): · 52 unmodified lines
5353 xsize = 1./todraw
5454 g.gnuplot('set multiplot;')
5555 while (len(filenames)):
56− time,data = audio_to_array(filenames.pop(0))
57− d.append(make_audio_plot(time,data))
56+ time,data = audio_to_array(filenames.pop(0))
57+ d.append(make_audio_plot(time,data))
5858 if not noaxis and todraw==1:
5959 g.xlabel('Time (s)')
6060 g.ylabel('Amplitude')
@@ -67,8 +67,69 @@ def plot_audio(filenames, fileout=None, start=0, end=None, noaxis=None): · 6 unmodified lines
6767 xorig += 1./todraw
6868 g.gnuplot('unset multiplot;')
6969
70+def audio_to_spec(filename):
71+ from aubioclass import fvec,cvec,pvoc,sndfile
72+ from math import log
73+ bufsize = 256*16
74+ hopsize = bufsize/4 # could depend on filelength
75+ filei = sndfile(filename)
76+ srate = float(filei.samplerate())
77+ framestep = hopsize/srate
78+ freqstep = srate/bufsize
79+ channels = filei.channels()
80+ myvec = fvec(hopsize,channels)
81+ myfft = cvec(bufsize,channels)
82+ pv = pvoc(bufsize,hopsize,channels)
83+ data,time,freq = [],[],[]
84+ for f in range(bufsize/2):
85+ freq.append(f*freqstep)
86+ readsize = hopsize
87+ frameread = 0
88+ while (readsize==hopsize):
89+ readsize = filei.read(hopsize,myvec)
90+ pv.do(myvec,myfft)
91+ frame = []
92+ 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))
96+ curpos+=1
97+ time.append(frameread*framestep)
98+ data.append(frame)
99+ frameread += 1
100+ # crop data if unfinished frames
101+ if len(data[-1]) != len(data[0]):
102+ data = data[0:-2]
103+ time = time[0:-2]
104+ # verify size consistency
105+ assert len(data) == len(time)
106+ assert len(data[0]) == len(freq)
107+ return data,time,freq
108+
109+def plot_spec(filenames, outplot='',extension='', fileout=None, start=0, end=None, noaxis=None,log=1):
110+ import Gnuplot
111+ g = gnuplot_create(outplot,extension)
112+ todraw = len(filenames)
113+ xorig = 0.
114+ xsize = 1./todraw
115+ data,time,freq = audio_to_spec(filenames.pop(0))
116+
117+ if not noaxis and todraw==1:
118+ g.xlabel('Time (s)')
119+ g.ylabel('Frequency (Hz)')
120+ g.gnuplot('set pm3d map')
121+ #g.gnuplot('set palette rgbformulae 30,31,32')
122+ #g.gnuplot('set palette')
123+ g.gnuplot('set xrange [0.:%f]' % time[-1])
124+ g.gnuplot('set yrange [1.:%f]' % (freq[-1]/1.))
125+ if log:
126+ g.gnuplot('set yrange [10.1:%f]' % (freq[-1]/1.))
127+ g.gnuplot('set log y')
128+ g.splot(Gnuplot.GridData(data,time,freq, binary=1))
129+ xorig += 1./todraw
130+
70131 def downsample_audio(time,data,maxpoints=10000):
71− """ create gnuplot plot from an audio file """
132+ """ resample audio data to last only maxpoints """
72133 import numarray
73134 length = len(time)
74135 downsample = length/maxpoints
@@ -178,7 +239,7 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 103 unmodified lines
178239 import re
179240
180241 d = []
181− maxpitch = 100
242+ maxpitch = 1000.
182243 for i in range(len(pitch)):
183244 #if len(pitch[i]) == 0: pitch[i] = [0.];
184245
@@ -227,6 +288,7 @@ def plot_pitch(filename, pitch, samplerate=44100., hopsize=512, outplot=None): · 42 unmodified lines
227288 g('set xrange [0:%f]' % max(time))
228289 g('set yrange [40:%f]' % maxpitch)
229290 g('set key right top')
291+ g('set noclip one')
230292 g.xlabel('time')
231293 g.ylabel('frequency (Hz)')
232294 g.plot(*d)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t