1 computeCoeffs = function(data, index, nb_series_per_chunk, wf)
4 if (is.data.frame(data) && index < nrow(data))
7 coeffs_chunk = curvesToCoeffs(
8 data[index:(min(index+nb_series_per_chunk-1,nrow(data))),], wf)
10 else if (is.function(data))
12 #custom user function to retrieve next n curves, probably to read from DB
13 coeffs_chunk = curvesToCoeffs( data(rank=(index-1)+seq_len(nb_series_per_chunk)), wf )
15 else if (exists(data_con))
17 #incremental connection ; TODO: more efficient way to parse than using a temp file
18 ascii_lines = readLines(data_con, nb_series_per_chunk)
19 if (length(ascii_lines > 0))
21 series_chunk_file = ".series_chunk"
22 writeLines(ascii_lines, series_chunk_file)
23 coeffs_chunk = curvesToCoeffs( read.csv(series_chunk_file), wf )
24 unlink(series_chunk_file)
30 curvesToCoeffs = function(series, wf)
32 if (!require(wavelets, quietly=TRUE))
33 stop("Couldn't load wavelets library")
34 L = length(series[1,])
35 D = ceiling( log2(L) )
36 nb_sample_points = 2^D
37 #TODO: parallel::parApply() ?!
38 as.data.frame( apply(series, 1, function(x) {
39 interpolated_curve = spline(1:L, x, n=nb_sample_points)$y
40 W = wavelets::dwt(interpolated_curve, filter=wf, D)@W
41 rev( sapply( W, function(v) ( sqrt( sum(v^2) ) ) ) )