// title: simple peak detection algorithm // author: LFSaw // description: // after http://www.tcs-trddc.com/trddc_website/pdf/SRL/Palshikar_SAPDTS_2009.pdf // code: q = q ? (); ( q.s1 = {|q, left, item, right, rContextSize| //[left, item, right].postln; ((maxItem(item - left) ? 0) + (maxItem(item - right) ? 0)) * 0.5 }; q.peak1 = {|q, signal, windowSize = 4| var mean, sDev, aAbs, peaksSize; var rWindowSize = windowSize.reciprocal; var h = 3; // 1<= h <= 3 var idxs = List[]; var a = signal.collect{|val, i| // compute peak function value for each of the N points in T q.s1( signal.copyRange(i-windowSize, i-1), val, signal.copyRange(i+1, i + windowSize), val ); }; // Compute the mean m and standard deviation s of all positive values in array a; aAbs = a.abs; mean = aAbs.mean; sDev = aAbs.stdDev; // mean.postln; // sDev.postln; a.do{|val, i| // collect all indices that are concidered big if ((a[i]>0) && ((a[i]-mean) > (h*sDev))) { idxs.add(i); } }; peaksSize = idxs.size.postln; // retain only one peak of any set of peaks within windowSize of each other idxs.inject([0], {|last, now, i| ((now - last.last) <= windowSize).if({ (signal[now] > signal[last.last]).if({ last.pop; last ++ now; }, { last; }) }, { last ++ now; }) }) } ) // apply peak1 to any "signal", i.e. a 1-dimensional sequence of numbers, play around with the windowSize param (and possibly h, see above). q.res = q.peak1(q.wireWset.amps, 4);q.res