The following block has some customizations for better figures in ipython notebooks


In [5]:
%pylab inline --no-import-all 
#comment this line and uncomment the next line for interactive figures

#%pylab gtk --no-import-all 
pylab.rcParams['figure.figsize'] = (10.0, 8.0)


Populating the interactive namespace from numpy and matplotlib

Import necessary modules


In [1]:
import pickle
from pypeaks import Data, Intervals

Load data

We already have provided some data samples and a script in examples/ directory. If you don't have it, you can either load your own data, or download them from https://github.com/gopalkoduri/pypeaks.


In [3]:
data = pickle.load(file("../examples/sample-histogram.pickle"))
hist = Data(data[0], data[1])

Get peaks by slope method and plot them


In [6]:
hist.get_peaks(method='slope')
hist.plot()


Get peaks by interval method and plot them


In [7]:
#In the example/ folder, there is a pickle file with some example intervals, 
#in this case, just-intonation intervals for music. They can refer to any intervals!
ji_intervals = pickle.load(file('../examples/ji-intervals.pickle'))
ji_intervals = Intervals(ji_intervals)

hist.get_peaks(method='interval', intervals=ji_intervals)
hist.plot(intervals=ji_intervals.intervals)


Accessing the peaks data

The Data object has _x, yraw, y, smoothness and peaks variables available. The help functions shows the methods available for it:


In [2]:
help(Data)


Help on class Data in module pypeaks.data:

class Data
 |  Methods defined here:
 |  
 |  __init__(self, x, y, smoothness=7)
 |      Initializes the data object for peak detection with x, y, smoothness
 |      parameter and empty peaks. In a histogram, x refers to bin centers (not
 |      edges), and y refers to the corresponding count/frequency.
 |      
 |      The smoothness parameter refers to the standard deviation of the
 |      gaussian kernel used for smoothing.
 |  
 |  extend_peaks(self, prop_thresh=50)
 |      Each peak in the peaks of the object is checked for its presence in 
 |      other octaves. If it does not exist, it is created. 
 |      
 |      prop_thresh is the cent range within which the peak in the other octave 
 |      is expected to be present, i.e., only if there is a peak within this 
 |      cent range in other octaves, then the peak is considered to be present
 |      in that octave.
 |      
 |      Note that this does not change the peaks of the object. It just returns 
 |      the extended peaks.
 |  
 |  get_peaks(self, method='slope', peak_amp_thresh=5e-05, valley_thresh=3e-05, intervals=None, lookahead=20, avg_interval=100)
 |      This function expects SMOOTHED histogram. If you run it on a raw histogram,
 |      there is a high chance that it returns no peaks.
 |      
 |      method can be interval/slope/hybrid.
 |          The interval-based method simply steps through the whole histogram
 |          and pick up the local maxima in each interval, from which irrelevant
 |          peaks are filtered out by looking at the proportion of points on 
 |          either side of the detected peak in each interval.
 |      
 |          Slope approach uses, of course slope information, to find peaks, 
 |          which are then filtered by applying peal_amp_thresh and 
 |          valley_thresh bounds. 
 |          
 |          Hybrid approach first finds peaks using slope method and then filters 
 |          them heuristically as in interval-based approach.
 |      
 |      peak_amp_thresh is the minimum amplitude/height that a peak should have
 |      in a normalized smoothed histogram, to be qualified as a peak. 
 |      valley_thresh is viceversa for valleys!
 |      
 |      If the method is interval/hybrid, then the intervals argument must be passed
 |      and it should be an instance of Intervals class.
 |      
 |      If the method is slope/hybrid, then the lookahead and avg_window
 |      arguments should be changed based on the application. 
 |      They have some default values though.
 |      
 |      The method returns:
 |      {"peaks":[[peak positions], [peak amplitudes]], 
 |      "valleys": [[valley positions], [valley amplitudes]]}
 |  
 |  normalize(self)
 |      Normalizes the given data such that the area under the histogram/curve
 |      comes to 1. Also re applies smoothing once done.
 |  
 |  plot(self, intervals=None, new_fig=True)
 |      This function plots histogram together with its smoothed
 |      version and peak information if provided. Just intonation
 |      intervals are plotted for a reference.
 |  
 |  serialize(self, path)
 |      Saves the raw (read unsmoothed) histogram data to the given path using
 |      pickle python module.
 |  
 |  set_smoothness(self, smoothness)
 |      This method (re)sets the smoothness parameter.
 |  
 |  smooth(self)
 |      Smooths the data using a gaussian kernel of the given standard deviation
 |      (as smoothness parameter)


In [ ]: