3

I have multiple time-series of length 149 and I would like to denoise them by using wavelet transformations.

This is an example of my data:

t=ts(rnorm(149,5000,1000),start=1065,end=1213) 

When I try using the packages wavetresh and waveslim, they both point me to the same problem:

library(wavetresh)
wd(t)
  Error in wd(t) : Data length is not power of two
library(waveslim)
dwt(t)
  Error in dwt(t) : Sample size is not divisible by 2^J

I understand that my data length should be of lenght 2^x, but I can't overcome this problem. I thought the function up.sample() in waveslim was supposed to help with this, but it didn't do the trick (e.g. up.sample(t,2^8) gives a vector of length 38144). So how do I increase my vector length without inserting an error? I know I could pad with zero's,... but I want to know the best way to do this.

Also, when looking at the example of waveslim, it looks as if the length of the imput serie doesn't fulfill this requirement either (although the example of course does work):

data(ibm)     
ibm.returns <- diff(log(ibm))
ibmr.haar <- dwt(ibm.returns, "haar")  #works
log2(length(ibm.returns))
  [1] 8.523562

I feel like I'm missing something basic, but I can't figure it out. Thanks for any help.

Ps: I know I can use other techniques to do this, but I really want to test this approach.

Wave
  • 1,216
  • 1
  • 9
  • 22
  • I'm not familiar with R, but there is a function in MATLAB `nextpow2`, which can be used like `2^nextpow2(149)=256` and would give you the the number you need. – Rashid Oct 21 '14 at 06:45

1 Answers1

3

I had a look into the code of dwt and the reason why it works, is that dwtdoes not check whether the length is a power of 2 but whether the length is a multiple of 2^J (actually that is what the error message says: Error in dwt(t) : Sample size is not divisible by 2^J).

With J=4 the length of your time series must thus be a multiple of 16. As you were asuming, up.sample can be used to overcome this issue as it pads the time series with 0's. But you you don't provide the final length but the frequency of upsampling.

Thus

dwt(up.sample(t, 16, 0))

should do the trick.

thothal
  • 16,690
  • 3
  • 36
  • 71
  • Thanks, that answers a part of the question. But what's the effect of the zero's? (is there a better/other way to do this?). Would it be better to use a continuous wavelet transform? Or would that be to fundamentally different? – Wave Oct 14 '14 at 13:58
  • Well, I am not at all an expert in wavelet transforms, but you could try to add a bunch of zeros to a time series of the right size (that is you create a timeseries with `length = 16 * k` and then add the sime amount of 0#s to it and see how the results differ. Just some food for thoughts. – thothal Oct 14 '14 at 14:18