export tesm

function fdelay!(d::Float64,y::Array{Float64,1},a::Array{Float64,1},x::Array{Float64},env::AcEnv)

	#construct Thiran coefficients 
	τs = d*env.Fs/env.c #delay in samples
	N = ceil(Int64,τs)
	D = τs-N		
	for k  = 1:N
		a[k+1] =  -(((D-1)+k)/k) * ((N-(k-1))/(τs+k))*a[k]
	end
	filt!(y,flipdim(a[1:N+1],1),a[1:N+1],1/(4*π*d)*x)

	return y

end	


#tesm core
function tesm!(p::Array{Float64},s::Array{Float64},y::Array{Float64,1},a::Array{Float64,1},
	       d::Array{Float64},Nt::Int64,env::AcEnv)

	#for i_1 = 1:xs.N, i_2 = 1:xr.N
	p[:] = 0*p
	@nloops 2 i d begin
		p[:,i_2] += fdelay!(d[i_1,i_2],y,a,s[:,i_1],env)
	end
end

#tesm core
function tesm!(p::Array{Float64},s::Array{Float64},y::Array{Float64,1},a::Array{Float64,1},
	       b::Array{Float64,1},
	       d::Array{Float64},Nt::Int64,env::AcEnv)

	#for i_1 = 1:xs.N, i_2 = 1:xr.N
	p[:] = 0*p
	@nloops 2 i d begin
		p[:,i_2] += fdelay!(d[i_1,i_2],y,a,s[:,i_1],env)
	end
	for i = 1:size(d,2)
		p[:,i] = filt!(p[:,i],p[:,i],b)
	end
end

#tesm
function tesm(s::Array{Float64},
	     xs::AbstractCartPos,xr::AbstractCartPos,Nt::Int64,env::AcEnv, adj = false)

	y = zeros(Float64,Nt) #preallocation for fdelay!
	a = zeros(Float64,Nt) #preallocation for filter coefficients!
	a[1] = 1.
	if adj == false
		if(xs.N != size(s,2)) error("s and xs must have equal size") end
		d = cart_dist(xs,xr)
		p = zeros(Float64,Nt,xr.N)
		tesm!(p,s,y,a,d,Nt,env)
		return p
	else #adjoint
		if(xr.N != size(s,2)) error("p and xr must have equal size") end
		d = cart_dist(xr,xs)
		p = zeros(Float64,Nt,xs.N)
		s = flipdim(s,1)
		tesm!(p,s,y,a,d,Nt,env)
		return flipdim(p,1)
	end

end

#free field method with basis
function tesm(s::Array{Float64},b::Array{Float64,1},
	     xs::AbstractCartPos,xr::AbstractCartPos,Nt::Int64,env::AcEnv, adj = false)

	y = zeros(Float64,Nt) #preallocation for fdelay output!
	a = zeros(Float64,Nt) #preallocation for filter coefficients!
	a[1] = 1.
	if adj == false
		if(xs.N != size(s,2)) error("s and xs must have equal size") end
		d = cart_dist(xs,xr)
		p = zeros(Float64,Nt,xr.N)
		tesm!(p,s,y,a,b,d,Nt,env)
		return p
	else #adjoint
		if(xr.N != size(s,2)) error("p and xr must have equal size") end
		d = cart_dist(xr,xs)
		p = zeros(Float64,Nt,xs.N)
		s = flipdim(s,1)
		tesm!(p,s,y,a,b,d,Nt,env)
		return flipdim(p,1)
	end

end

tesm(s::Array{Float64}, xs::Array{Float64},xr::Array{Float64},Nt::Int64, args...) = 
tesm(s,CartPos(xs),CartPos(xr),Nt, args...)

tesm(s::Array{Float64},xs::AbstractCartPos,xr::Array{Float64},Nt::Int64, args...) = 
tesm(s,xs,CartPos(xr),Nt, args...)

tesm(s::Array{Float64},xs::Array{Float64},xr::AbstractCartPos,Nt::Int64, args...) = 
tesm(s,CartPos(xs),xr,Nt, args...)

tesm(s::Array{Float64},b::Array{Float64,1}, xs::Array{Float64},xr::Array{Float64},Nt::Int64, args...) = 
tesm(s,b,CartPos(xs),CartPos(xr),Nt, args...)

tesm(s::Array{Float64},b::Array{Float64,1},xs::AbstractCartPos,xr::Array{Float64},Nt::Int64, args...) = 
tesm(s,b,xs,CartPos(xr),Nt, args...)

tesm(s::Array{Float64},b::Array{Float64,1},xs::Array{Float64},xr::AbstractCartPos,Nt::Int64, args...) = 
tesm(s,b,CartPos(xs),xr,Nt, args...)


