---------------------------------------------------------------------------------- |-- Module : HarmTrace.Audio.Statistical-- Copyright : (c) 2010-2012 Universiteit Utrecht, 2012 University of Oxford-- License : GPL3---- Maintainer : bash@cs.uu.nl, jpm@cs.ox.ac.uk-- Stability : experimental-- Portability : non-portable---- Summary: Some statistical functions used in processing audio.--------------------------------------------------------------------------------moduleHarmTrace.Audio.StatisticalwhereimportHarmTrace.Base.MusicTimeimportData.List(genericLength,tails,inits,maximumBy,sort,group)importData.Ord(comparing)---------------------------------------------------------------------------------- Statistical functions---------------------------------------------------------------------------------- | Similar to group, but in case a group is smaller than s, -- the group is filled with the same number of a's:-- -- >>> groupMinSize 3 0 [1,2,2,3,3,3,4,4,4,4,5,5,5,5,5]-- [[0],[0,0],[0,0,0],[4,4,4,4],[5,5,5,5,5]]groupMinSize::Eqa=>Int->a->[a]->[[a]]groupMinSize__[]=[]groupMinSizesa(x:xs)=grp:groupMinSizesazswhere(ys,zs)=span(==x)xslys=lengthysgrp=iflengthys>=sthenx:yselsereplicate(lys+1)a-- | Returns the mean of list.listMean::[NumData]->NumDatalistMeana=suma/genericLengtha-- | a median filter: see <http://en.wikipedia.org/wiki/Median_filter>.medianFilter::Orda=>Int->[a]->[a]medianFilterwsizel=mapmode$getWindowswsizel-- | Returns a list with all "sliding windows" of a particular size.-- The left and right edge of the list are filled with the first and last (size -- /2) items, respectively and the remainder is filled with the mode/median of-- the complete list.getWindows::Orda=>Int->[a]->[[a]]getWindowssizel=lbor++mid++rborwheremid=takeWhile(hasSizesize).map(takesize)$tailslgmed=modells=size`div`2rs=size-ls-1lbor=reverse$map(fillWithsizegmed)(reverse.dropWhilenull.inits$takelsl)rbor=map(reverse.fillWithsizegmed)(takeWhile(not.null).tails.takers$reversel)-- in general (length l) < x, but this is guaranteed within this letfillWith::Int->a->[a]->[a]fillWithxalt=replicate(x-lengthl)a++lthasSize::Int->[a]->BoolhasSizesl=lengthl>=s-- | The mode: the element that occurs most often in the collection.mode::Orda=>[a]->amode[]=error"Key.hs: mode called on []"model=head.maximumBy(comparinglength).group$sortl-- | Returns the median of a list.median::Orda=>[a]->a-- the median not good for keys... median[]=error"Key.hs: median called on []"medianl=sortl!!(lengthl`div`2)-- | Returns the index of th maximal element of a list.maxListIndex::Orda=>[a]->IntmaxListIndex=fst.maxPairwheremaxPair::Orda=>[a]->(Int,a)maxPair=maximumBy(comparingsnd).zip[0..]