wave_filter=function(ts, sp, lp){ ########## ts is the time series to be filtered. input as a vector - 1 column ########## sp is the smallest period you want to include in the filtered ts - e.g. 14 day period... ########## lp is the largest period you want to include in the filtered ts e.g. 45 day period... ########## note: lp must be larger than sp source("wavelet_function_r.txt") XD=ts-mean(ts) n=length(ts) DJ=.25 #spacing between discrete frequencies - defaulty 0.25 DT=1 #sampling intterval of time series - for monthly it will be 1/12 = 0.08333 PAD = 1 # default 0, but if set to 1 it will pad with zeros to get Nn upto next higher power of 2 S0 = 2*DT #smallest scale of wavelet - default is 2*DT param = 6 fin=wavelet(XD, DT,PAD,DJ,param) #run after wavelet function L=length(fin$scale) scale_mat=cbind((1:L), fin$scale) scales=subset(scale_mat[,1], scale_mat[,2]<=lp & scale_mat[,2]>=sp) ls=length(scales) ##############revisit how many bands to include in the "significant ts" # frequency band utd=matrix(ncol=n, nrow=ls) I=0 for(i in scales[1]:scales[ls]){ I=I+1 utd[I,]=(sqrt(DT)*DJ)*as.real(fin$wave[(i),])/((sqrt(fin$scale[i]))*.776*(pi^(-1/4))) } xu=1:n for(i in 1:n){ xu[i]=sum(utd[,i]) } LF_T=xu #write(LF_T, file="Filtered_ts.txt", ncol=1) return(LF_T) }