venerdì 26 settembre 2008

Haar wavelet transform in Scala

This is my first try to write a wavelet(ondine in Italian) transform in Scala.
Naturally i've choose Haar wavelet transform, the simplest!
This is the core of the transformation:

def waveletCalc( values: List[Double] ):List[List[Double]]= haarCalc( values , List( ) ).reverse
def haarCalc( vals: List[Double], cc: List[List[Double]] )             :List[List[Double]] =  vals.length / 2 match {    case 1 => cc ::: List(  List((vals(0) - vals(1) ) / 2 ) ,        List(( vals(0) + vals(1) ) / 2 ))    case _ => haarCalc( vals.indices.dropRight(vals.length/2)               .map(_*2).map(el=>(vals(el) + vals(el+1))/2),                 cc ::: List( vals.indices.dropRight(                  vals.length/2).map(2*_).map(el=>                   (vals (el) - vals(el + 1))/2)))}

The algorithm is recursive and returns as a result a list of lists.
This arrangement is the most convenient since a base of wavelets has two indexes.
Soon i'll try to post the complete class with the inverse transform and a brief
description on this particular wavelets transform.
I strongly recommend to copy and paste the code in an editor to read it better...
you can find a more complete code here