<s> import numpy as np <EOL> import matplotlib <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . cm as cm <EOL> from matplotlib . image import NonUniformImage <EOL> import matplotlib . colors as colo <EOL> from dear . spectrum import cqt , dft , auditory , SpectrogramFile <EOL> from dear . analysis import MFCCs <EOL> def plot_spectrogram ( spec , Xd = ( <NUM_LIT:0> , <NUM_LIT:1> ) , Yd = ( <NUM_LIT:0> , <NUM_LIT:1> ) , norm = colo . LogNorm ( vmin = <NUM_LIT> ) , figname = None ) : <EOL> x_min , x_max = Xd <EOL> y_min , y_max = Yd <EOL> fig = plt . figure ( num = figname ) <EOL> nf = len ( spec ) <EOL> for ch , data in enumerate ( spec ) : <EOL> x = np . linspace ( x_min , x_max , data . shape [ <NUM_LIT:0> ] ) <EOL> y = np . linspace ( y_min , y_max , data . shape [ <NUM_LIT:1> ] ) <EOL> ax = fig . add_subplot ( nf * <NUM_LIT:100> + <NUM_LIT:11> + ch ) <EOL> im = NonUniformImage ( ax , interpolation = '<STR_LIT>' , cmap = cm . gray_r , <EOL> norm = norm ) <EOL> im . set_data ( x , y , data . T ) <EOL> ax . images . append ( im ) <EOL> ax . set_xlim ( x_min , x_max ) <EOL> ax . set_ylim ( y_min , y_max ) <EOL> ax . set_title ( '<STR_LIT>' % ch ) <EOL> ax . set_ylabel ( '<STR_LIT>' ) <EOL> print '<STR_LIT>' % ( data . max ( ) , data . min ( ) , data . mean ( ) , np . median ( data ) ) <EOL> plt . show ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import getopt , sys <EOL> def exit_with_usage ( ) : <EOL> print """<STR_LIT>""" <EOL> exit ( ) <EOL> try : <EOL> opts , args = getopt . getopt ( sys . argv [ <NUM_LIT:1> : ] , "<STR_LIT>" ) <EOL> except getopt . GetoptError as ex : <EOL> print ex <EOL> exit_with_usage ( ) <EOL> if len ( args ) != <NUM_LIT:1> : <EOL> exit_with_usage ( ) <EOL> import dear . io as io <EOL> decoder = io . get_decoder ( name = '<STR_LIT>' ) <EOL> audio = decoder . Audio ( args [ <NUM_LIT:0> ] ) <EOL> print "<STR_LIT>" % ( audio . samplerate , audio . channels , audio . duration ) <EOL> graph = '<STR_LIT>' <EOL> st = <NUM_LIT:0> <EOL> to = None <EOL> outfile = None <EOL> norm = colo . LogNorm ( vmin = <NUM_LIT> ) <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> st = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> to = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> graph = a <EOL> assert graph in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif o == '<STR_LIT>' : <EOL> outfile = a <EOL> if to is None or to > audio . duration : <EOL> r_to = audio . duration <EOL> else : <EOL> r_to = to <EOL> if graph == '<STR_LIT>' : <EOL> win = <NUM_LIT> <EOL> hop = <NUM_LIT> <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> win = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> hop = int ( a ) <EOL> spec = [ [ ] ] <EOL> gram = dft . PowerSpectrum ( audio ) <EOL> for freqs in gram . walk ( win , hop , start = st , end = to , join_channels = True ) : <EOL> spec [ <NUM_LIT:0> ] . append ( freqs ) <EOL> elif graph == '<STR_LIT>' : <EOL> N = <NUM_LIT:20> <EOL> fmin , fmax = <NUM_LIT:0.> , <NUM_LIT> <EOL> win = <NUM_LIT> <EOL> hop = <NUM_LIT> <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> win = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> hop = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> N = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> fmin , fmax = [ float ( f ) for f in a . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) ] <EOL> spec = [ [ ] ] <EOL> gram = MFCCs ( audio ) <EOL> for freqs in gram . walk ( N , fmin , fmax , win , hop , st , to ) : <EOL> spec [ <NUM_LIT:0> ] . append ( freqs ) <EOL> norm = colo . Normalize ( ) <EOL> elif graph == '<STR_LIT>' : <EOL> Q = <NUM_LIT> <EOL> hop = <NUM_LIT> <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> Q = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> hop = float ( a ) <EOL> spec = [ [ ] ] <EOL> gram = cqt . CQTPowerSpectrum ( audio ) <EOL> print '<STR_LIT>' , int ( ( r_to - st ) / hop ) <EOL> for t , freqs in enumerate ( gram . walk ( Q = Q , freq_base = <NUM_LIT> , freq_max = <NUM_LIT> , hop = hop , start = st , end = to , join_channels = True ) ) : <EOL> if t % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> sys . stdout . write ( '<STR_LIT>' % t ) <EOL> sys . stdout . flush ( ) <EOL> spec [ <NUM_LIT:0> ] . append ( freqs ) <EOL> elif graph == '<STR_LIT>' : <EOL> N = <NUM_LIT> <EOL> hop = <NUM_LIT> <EOL> rw = False <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> N = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> hop = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> rw = True <EOL> spec = [ [ ] ] <EOL> gram = cqt . CNTPowerSpectrum ( audio ) <EOL> print '<STR_LIT>' , int ( ( r_to - st ) / hop ) <EOL> for t , freqs in enumerate ( <EOL> gram . walk ( N = N , freq_base = <NUM_LIT> , freq_max = <NUM_LIT> , hop = hop , start = st , end = to , resize_win = rw ) ) : <EOL> if t % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> sys . stdout . write ( '<STR_LIT>' % t ) <EOL> sys . stdout . flush ( ) <EOL> spec [ <NUM_LIT:0> ] . append ( freqs ) <EOL> print "<STR_LIT>" <EOL> elif graph == '<STR_LIT>' : <EOL> N = <NUM_LIT:64> <EOL> win = <NUM_LIT> <EOL> hop = <NUM_LIT> <EOL> freqs = [ <NUM_LIT> , <NUM_LIT> ] <EOL> combine = False <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> N = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> hop = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> win = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> freqs = [ float ( f ) for f in a . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) ] <EOL> elif o == '<STR_LIT:-c>' : <EOL> combine = True <EOL> spec = [ [ ] ] <EOL> gram = auditory . GammatoneSpectrum ( audio ) <EOL> print '<STR_LIT>' , int ( ( r_to - st ) / hop ) <EOL> for t , freqs in enumerate ( <EOL> gram . walk ( N = N , freq_base = freqs [ <NUM_LIT:0> ] , freq_max = freqs [ <NUM_LIT:1> ] , <EOL> start = st , end = to , combine = combine , twin = win , thop = hop ) ) : <EOL> if t % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> sys . stdout . write ( '<STR_LIT>' % t ) <EOL> sys . stdout . flush ( ) <EOL> spec [ <NUM_LIT:0> ] . append ( freqs ) <EOL> print "<STR_LIT>" <EOL> elif graph in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> N = <NUM_LIT:64> <EOL> win = <NUM_LIT> <EOL> hop = <NUM_LIT> <EOL> freqs = [ <NUM_LIT> , <NUM_LIT> ] <EOL> combine = False <EOL> for o , a in opts : <EOL> if o == '<STR_LIT>' : <EOL> N = int ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> hop = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> win = float ( a ) <EOL> elif o == '<STR_LIT>' : <EOL> freqs = [ float ( f ) for f in a . split ( '<STR_LIT:U+002C>' , <NUM_LIT:1> ) ] <EOL> elif o == '<STR_LIT:-c>' : <EOL> combine = True <EOL> spec = [ [ ] ] <EOL> gram = getattr ( auditory , graph ) <EOL> gram = gram ( audio ) <EOL> print '<STR_LIT>' , int ( ( r_to - st ) / hop ) <EOL> for t , freqs in enumerate ( <EOL> gram . walk ( N = N , freq_base = freqs [ <NUM_LIT:0> ] , freq_max = freqs [ <NUM_LIT:1> ] , <EOL> start = st , end = to , combine = combine , twin = win , thop = hop ) ) : <EOL> if t % <NUM_LIT:100> == <NUM_LIT:0> : <EOL> sys . stdout . write ( '<STR_LIT>' % t ) <EOL> sys . stdout . flush ( ) <EOL> spec [ <NUM_LIT:0> ] . append ( freqs ) <EOL> print "<STR_LIT>" <EOL> dBmax , dBmin = - <NUM_LIT> , - <NUM_LIT> <EOL> if graph in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> magmin = <NUM_LIT:10> ** ( dBmin / <NUM_LIT:20> ) <EOL> for g in spec : <EOL> for i , frame in enumerate ( g ) : <EOL> g [ i ] = <NUM_LIT:20> * np . log10 ( np . maximum ( frame / <NUM_LIT> , magmin ) ) <EOL> norm = colo . Normalize ( vmin = dBmin , vmax = dBmax ) <EOL> elif graph in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> magmin = <NUM_LIT:10> ** ( dBmin / <NUM_LIT:20> ) <EOL> for g in spec : <EOL> for i , frame in enumerate ( g ) : <EOL> g [ i ] = <NUM_LIT:20> * np . log10 ( np . maximum ( frame , magmin ) ) <EOL> norm = colo . Normalize ( vmin = dBmin , vmax = dBmax ) <EOL> figname = "<STR_LIT>" % ( graph , audio . path ) <EOL> plot_spectrogram ( np . array ( spec ) , ( <NUM_LIT:0> , len ( spec [ <NUM_LIT:0> ] ) ) , ( <NUM_LIT:0> , len ( spec [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] ) ) , norm = norm , figname = figname ) <EOL> if outfile : <EOL> out = SpectrogramFile ( outfile , '<STR_LIT:w>' ) <EOL> out . dump ( spec [ <NUM_LIT:0> ] ) <EOL> out . close ( ) </s>
