<s> import sys <EOL> import numpy as np <EOL> import scipy <EOL> from . . utils import RMS_energy <EOL> def novelty ( song , k = <NUM_LIT:64> , wlen_ms = <NUM_LIT:100> , start = <NUM_LIT:0> , duration = None , nchangepoints = <NUM_LIT:5> , feature = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if feature != "<STR_LIT>" and feature != "<STR_LIT>" : <EOL> raise ValueError , "<STR_LIT>" <EOL> if feature == "<STR_LIT>" : <EOL> frames = song . all_as_mono ( ) <EOL> wlen_samples = int ( wlen_ms * song . samplerate / <NUM_LIT:1000> ) <EOL> if duration is None : <EOL> frames = frames [ start * song . samplerate : ] <EOL> else : <EOL> frames = frames [ start * song . samplerate : ( start + duration ) * <EOL> song . samplerate ] <EOL> hamming = np . hamming ( wlen_samples ) <EOL> nwindows = int ( <NUM_LIT:2> * song . duration / wlen_samples - <NUM_LIT:1> ) <EOL> energies = np . empty ( nwindows ) <EOL> for i in range ( nwindows ) : <EOL> energies [ i ] = RMS_energy ( <EOL> hamming * <EOL> frames [ i * wlen_samples / <NUM_LIT:2> : <EOL> i * wlen_samples / <NUM_LIT:2> + wlen_samples ] <EOL> ) <EOL> energies_list = [ [ x ] for x in energies ] <EOL> elif feature == "<STR_LIT>" : <EOL> analysis = song . analysis <EOL> energies_list = np . array ( analysis [ "<STR_LIT>" ] ) <EOL> S_matrix = <NUM_LIT:1> - scipy . spatial . distance . squareform ( <EOL> scipy . spatial . distance . pdist ( energies_list , '<STR_LIT>' ) ) <EOL> C_matrix = np . kron ( np . eye ( <NUM_LIT:2> ) , np . ones ( ( k , k ) ) ) - np . kron ( [ [ <NUM_LIT:0> , <NUM_LIT:1> ] , [ <NUM_LIT:1> , <NUM_LIT:0> ] ] , np . ones ( ( k , k ) ) ) <EOL> g = scipy . signal . gaussian ( <NUM_LIT:2> * k , k ) <EOL> C_matrix = np . multiply ( C_matrix , np . multiply . outer ( g . T , g ) ) <EOL> N_vec = np . zeros ( np . shape ( S_matrix ) [ <NUM_LIT:0> ] ) <EOL> for i in xrange ( k , len ( N_vec ) - k ) : <EOL> S_part = S_matrix [ i - k : i + k , i - k : i + k ] <EOL> N_vec [ i ] = np . sum ( np . multiply ( S_part , C_matrix ) ) <EOL> peaks = naive_peaks ( N_vec , k = k / <NUM_LIT:2> + <NUM_LIT:1> ) <EOL> out_peaks = [ ] <EOL> if feature == "<STR_LIT>" : <EOL> for p in peaks : <EOL> frame = p [ <NUM_LIT:0> ] <EOL> if frame > k : <EOL> left_frames = frames [ int ( ( frame - k ) * wlen_samples / <NUM_LIT:2> ) : <EOL> int ( frame * wlen_samples / <NUM_LIT:2> ) ] <EOL> right_frames = frames [ int ( frame * wlen_samples / <NUM_LIT:2> ) : <EOL> int ( ( frame + k ) * wlen_samples / <NUM_LIT:2> ) ] <EOL> if RMS_energy ( left_frames ) < RMS_energy ( right_frames ) : <EOL> out_peaks . append ( p ) <EOL> out_peaks = [ ( x [ <NUM_LIT:0> ] * wlen_ms / <NUM_LIT> , x [ <NUM_LIT:1> ] ) for x in out_peaks ] <EOL> for i , p in enumerate ( out_peaks ) : <EOL> if i == nchangepoints : <EOL> break <EOL> return [ x [ <NUM_LIT:0> ] for x in out_peaks [ : nchangepoints ] ] <EOL> elif feature == "<STR_LIT>" : <EOL> beats = analysis [ "<STR_LIT>" ] <EOL> return [ beats [ int ( b [ <NUM_LIT:0> ] ) ] for b in peaks [ : nchangepoints ] ] <EOL> def smooth_hanning ( x , size = <NUM_LIT:11> ) : <EOL> """<STR_LIT>""" <EOL> if x . ndim != <NUM_LIT:1> : <EOL> raise ValueError , "<STR_LIT>" <EOL> if x . size < size : <EOL> raise ValueError , "<STR_LIT>" <EOL> if size < <NUM_LIT:3> : <EOL> return x <EOL> s = np . r_ [ x [ size - <NUM_LIT:1> : <NUM_LIT:0> : - <NUM_LIT:1> ] , x , x [ - <NUM_LIT:1> : - size : - <NUM_LIT:1> ] ] <EOL> w = np . hanning ( size ) <EOL> y = np . convolve ( w / w . sum ( ) , s , mode = '<STR_LIT>' ) <EOL> return y <EOL> def naive_peaks ( vec , k = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> a = smooth_hanning ( vec , k ) <EOL> k2 = ( k - <NUM_LIT:1> ) / <NUM_LIT:2> <EOL> peaks = np . r_ [ True , a [ <NUM_LIT:1> : ] > a [ : - <NUM_LIT:1> ] ] & np . r_ [ a [ : - <NUM_LIT:1> ] > a [ <NUM_LIT:1> : ] , True ] <EOL> p = np . array ( np . where ( peaks ) [ <NUM_LIT:0> ] ) <EOL> maxidx = np . zeros ( np . shape ( p ) ) <EOL> maxvals = np . zeros ( np . shape ( p ) ) <EOL> for i , pk in enumerate ( p ) : <EOL> maxidx [ i ] = np . argmax ( vec [ pk - k2 : pk + k2 ] ) + pk - k2 <EOL> maxvals [ i ] = np . max ( vec [ pk - k2 : pk + k2 ] ) <EOL> out = np . array ( [ maxidx , maxvals ] ) . T <EOL> return out [ ( - out [ : , <NUM_LIT:1> ] ) . argsort ( ) ] <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> novelty ( sys . argv [ <NUM_LIT:1> ] , k = int ( sys . argv [ <NUM_LIT:2> ] ) ) </s>
