#!/usr/bin/env ruby
require("gsl")
require("wavelet")

n = 256
nc = 20

data = Vector.new(n)
data.fscanf("ecg.dat")

abscoeff = Vector.new(n)

w = GSL::Wavelet.new("daubechies", 4)
work = GSL::Wavelet::Workspace.new(n)

w.transform(data, Wavelet::FORWARD, work)
#w.transform(data, work)
#w.transform(data)
#w.transform(data, Wavelet::FORWARD)
#w.transform_forward(data, work)
#w.transform_forward(data)
#Wavelet.transform(w, data, Wavelet::FORWARD, work)
#Wavelet.transform(w, data, Wavelet::FORWARD)
#Wavelet.transform(w, data, work)
#Wavelet.transform(w, data)
#Wavelet.transform_forward(w, data, work)
#Wavelet.transform_forward(w, data)

i = 0
while i < n
  abscoeff[i] = data[i].abs
  i += 1
end

perm = abscoeff.sort_index

i = 0
while (i + nc) < n
  data[perm[i]] = 0.0
  i += 1  
end

#w.transform(data, Wavelet::BACKWARD, work)
#w.transform(data, Wavelet::BACKWARD)
#w.transform_inverse(data, work)
#w.transform_inverse(data)
#Wavelet.transform(w, data, Wavelet::BACKWARD, work)
#Wavelet.transform(w, data, Wavelet::BACKWARD)
#Wavelet.transform_inverse(w, data, work)
Wavelet.transform_inverse(w, data)


data.each do |val|
  printf("%g\n", val)
end

