Changeset 431e9d8 for python


Ignore:
Timestamp:
Mar 1, 2006, 4:14:08 AM (19 years ago)
Author:
Paul Brossier <piem@altern.org>
Branches:
feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
Children:
dcc8d90
Parents:
7c206df
Message:

add spectrogram plot
add spectrogram plot

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/aubio/gnuplot.py

    r7c206df r431e9d8  
    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)')
     
    6868        g.gnuplot('unset multiplot;')
    6969
     70def 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
     109def 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
    70131def 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)
     
    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.];
     
    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)')
Note: See TracChangeset for help on using the changeset viewer.