Construction of Haar functions in R

I want to build Haar functions, which are defined as:

h_1_0 = function(t){ if ((t >= 0) & (t <= 1/2)){return(1)} else if ((t >= 1/2) & (t <= 1)){return(-1)} else{return(0)} } 

Then the kth Haar function is:

 h = function(t,n,k){return(2^((n-1)/2) * h_1_0((2^(n-1)) * t - k))} 

For example, I want to draw h(t,1,1) , it should be a function:

  1, if 1<=t<=3/2 -1, if 3/2 <=t<=2 0, otherwise 

So how can I build the kth functions with fixed k and n in R?

+4
source share
2 answers

Create a sequence for the domain. Use appropriate borders based on k and n:

 x <- seq(.5, 2.5, .001) plot(x, sapply(x, function(x) h(x,1,1)), pch='.', type='l') 
+2
source

If you can successfully vectorize your function as it looks, you can use curve to draw it.

Use Vectorize to create a wrapper for your h function, which allows you to pass a vector to the t argument

 Vh <- Vectorize(h, "t") 

Use curve to draw a vectorized function

 curve( Vh(t = x, n = 1, k = 1), from = .5, to = 5) 

curve of Vh (t = x, n = 1, k = 1)

+2
source

Source: https://habr.com/ru/post/1410864/


All Articles