Skip to content

Commit 98da3e0

Browse files
committed
1) Aggregating of build tasks 2) Added Haar Wavelet transform
1 parent be9fd66 commit 98da3e0

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ packageDescription := "DynaML is a scala library/repl for implementing and worki
1212
"which can be extended easily to implement advanced models for small and large scale applications.\n\n"+
1313
"But the library can also be used as an educational/research tool for data analysis."
1414

15-
val mainVersion = "v1.4-beta.8"
15+
val mainVersion = "v1.4-beta.9"
1616

1717
val dataDirectory = settingKey[File]("The directory holding the data files for running example scripts")
1818

dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/DynaMLPipe.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,31 @@ object DynaMLPipe {
309309
def duplicate[Source, Destination](pipe: DataPipe[Source, Destination]) =
310310
DataPipe(pipe, pipe)
311311

312+
/**
313+
* Constructs a data pipe which performs discrete Haar wavelet transform
314+
* on the original signal.
315+
* */
316+
def waveletFilter(order: Int) = DataPipe((signal: DenseVector[Double]) => {
317+
//Check size of signal before constructing DWT matrix
318+
assert(
319+
signal.length == math.pow(2.0, order).toInt,
320+
"Length of signal must be dyadic power: 2^"+order
321+
)
322+
323+
// Now construct DWT matrix
324+
val invSqrtTwo = 1.0/math.sqrt(2.0)
325+
326+
val rowFactors = (0 until order).reverse.map(i => {
327+
(1 to math.pow(2.0, i).toInt).map(k =>
328+
invSqrtTwo/math.sqrt(order-i))})
329+
.reduceLeft((a,b) => a ++ b).reverse
330+
331+
val appRowFactors = Seq(rowFactors.head) ++ rowFactors
332+
333+
val dwtvec = utils.haarMatrix(math.pow(2.0, order).toInt)*signal
334+
335+
dwtvec.mapPairs((row, v) => v*appRowFactors(row))
336+
})
312337

313338
def trainParametricModel[
314339
G, T, Q, R, S, M <: ParameterizedLearner[G, T, Q, R, S]

dynaml-core/src/main/scala-2.11/io/github/mandar2812/dynaml/utils/package.scala

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ under the License.
1818
* */
1919
package io.github.mandar2812.dynaml
2020

21-
import java.io.{FileWriter, BufferedWriter, File}
22-
import breeze.linalg.{DenseMatrix, DenseVector}
23-
import com.github.tototoshi.csv.{QUOTE_NONNUMERIC, DefaultCSVFormat, CSVReader}
21+
import java.io.{BufferedWriter, File, FileWriter}
22+
23+
import breeze.linalg.{DenseMatrix, DenseVector, kron}
24+
import com.github.tototoshi.csv.{CSVReader, DefaultCSVFormat, QUOTE_NONNUMERIC}
2425
import org.apache.spark.mllib.regression.LabeledPoint
2526
import org.apache.spark.rdd.RDD
27+
2628
import scala.io.Source
2729
import scala.reflect.runtime.{universe => ru}
2830
import scala.annotation.tailrec
@@ -216,5 +218,26 @@ package object utils {
216218

217219
transformData(tFunc)(lines)
218220
}
221+
222+
/**
223+
* Construct a haar transform matrix of size n
224+
*
225+
* NOTE: n must be a power of 2.
226+
*
227+
* */
228+
def haarMatrix(n: Int) = {
229+
val pos = DenseMatrix(Array(1.0, 1.0))
230+
val neg = DenseMatrix(Array(-1.0, 1.0))
231+
val hMat = DenseMatrix(Array(1.0, 1.0), Array(-1, 1.0))
232+
def haarMatrixAcc(i: Int, hMatAcc: DenseMatrix[Double]): DenseMatrix[Double] = i match {
233+
case `n` => hMatAcc
234+
case index =>
235+
haarMatrixAcc(i*2,
236+
DenseMatrix.vertcat[Double](
237+
kron(hMatAcc, pos),
238+
kron(DenseMatrix.eye[Double](i), neg)))
239+
}
240+
haarMatrixAcc(2, hMat)
241+
}
219242
}
220243

0 commit comments

Comments
 (0)