Tricky interpolation issue

Hi All,

I am trying to create a function that turns a shorter vector (len nk) into a longer vector (len nkp). For example, 1x10 would become 1x20. I’m running into an issue with indexing. I see why mine does not work, but does anyone have ideas of alternative ways to do this?? The key is that the entries in the vector can be spaced in any way (for example exponentially) not necessarily just linearly.

function fillin(short_grid)
        long_grid = zeros(nkp)
        for i in 1:nkp
            #find the indexes on short K below and above   
            down = Int(floor((nk-1.0)*(i-1.0)/(nkp-1.0))+1)
            up = down+1

            #compare exact location 
            num = i*nk/nkp - down
            denom = short_grid[down] - short_grid[up]
            frak = num/denom

            long_grid[i] = frak*short_grid[up]+(1-frak)*short_grid[down]
        end;
    end;
    return long_grid 
 end;

kg_long = fillin(kg)

I get:
BoundsError: attempt to access 11-element Vector{Float64} at index [12]

Stacktrace:
[1] getindex(A::Vector{Float64}, i1::Int64)
@ Base ./array.jl:861
[2] fillin(short_grid::Vector{Float64})
@ Main ./In[23]:15
[3] top-level scope
@ In[23]:24
[4] eval
@ ./boot.jl:373 [inlined]
[5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196

I couldn’t run your code (it looks like there was an error when copy-pasting)… but what exactly are you trying to do? There are many packages out there that provide interpolation routines, where you able to take a look at Interpolations.jl for example?

2 Likes

First a general note:
nkp and nk are not defined inside the function. My guess is, you are using global vars here, this is not recommended. Try to use them as parameters to the function. This would also help for a MWE.

Your error

results from accessing short_grid[up] and short_grid[down]. From your code it is not clear that up and down are really restricted to the allowed indices from 1 to 11.
For an example with nk=11 and nkp=20 you get in the loop at the last i=nkp:

julia> nk=11
11

julia> nkp=20
20

julia> i=nkp
20

julia> down = Int(floor((nk-1.0)*(i-1.0)/(nkp-1.0))+1)
11

julia> up = down+1
12

Now it is clear that short_grid[up] should fail.

Perhaps it is already solved with

for i in 1:(nkp-1)

but that is just guessing.

To give you some alternative we need to know what exactly you try to do. Best would be a MWE with some example input and your desired output and some explanations on the how and why the desired result should be achieved.

1 Like

This is a very simple way to interpolate the vector from N elements to 2N-1:

using Interpolations
xshort = exp.(0:9)
nk = length(xshort)
itp = LinearInterpolation(1:nk, xshort)
xlong = itp(1:0.5:nk)
5 Likes

Thanks everyone!

By the way, LinearInterpolation is currently deprecated in favor of linear_interpolation as not to imply there is a LinearInterpolation type.

Also please see Home · Interpolations.jl about how to use Interpolations.jl efficiently.

If you do not need extrapolation or scaling, then you can just do:

using Interpolations
xshort = exp.(0:9)
nk = length(xshort)
itp = interpolate(xshort, BSpline(Linear()))
xlong = itp(1:0.5:nk) 
1 Like

You can also use imresize from Images