python中的interp函数就像matlab一样

时间:2021-07-31 18:15:29

Could someone explain how to use an interpolation function on an existing array, in python as the one exists in matlab ?

有人可以解释如何在现有数组中使用插值函数,在python中如matlab中存在的那样?

example:

例:

x =

x =

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

interp(x,2)

interp的(X,2)

ans =

ans =

1.0000
1.4996
2.0000
2.4993
3.0000
3.4990
4.0000
4.4987
5.0000
5.4984
6.0000
6.4982
7.0000
7.4979
8.0000
8.4976
9.0000
9.4973
10.0000
10.4970

I'd like a function in python which does exactly this, i.e. adds more points keeping the original intact.

我想在python中使用一个函数来完成这个,即添加更多的点来保持原始的完整。

2 个解决方案

#1


0  

A couple of issues need to be raised:

需要提出几个问题:

  • Are you just looking at linear interpolation (ie "connect the dots" with straight line segments)? That's simple but kind of nasty. You can get nicer results with higher-order curves (ie bicubic splines) but for that you need to provide more information to nail down a unique solution (ie endpoint first-derivatives).

    你只是看线性插值(即用直线段“连接点”)?这很简单但很讨厌。您可以使用高阶曲线(即双三次样条曲线)获得更好的结果,但为此您需要提供更多信息来确定唯一解决方案(即端点一阶导数)。

  • Do you want to smooth the curve at the same time, or do you expect it to go exactly through your given points?

    您是否希望同时平滑曲线,或者您是否希望它完全通过您的给定点?

  • Are your input points uniformly spaced (ie along the x axis)?

    您的输入点是均匀间隔的(即沿x轴)?

  • Your data shows not just interpolation but also extrapolation (ie your last point is off the end of your data) - is this actually what you want?

    您的数据不仅显示插值,还显示外推(即您的最后一点不在数据末尾) - 这实际上是您想要的吗?

Matlab documentation says "interp inserts 0s into the original signal and then applies a lowpass interpolating filter to the expanded sequence".

Matlab文档说“interp将0插入原始信号,然后将低通内插滤波器应用于扩展序列”。


Edit: I think the closest equivalent is scipy.interpolate.interp1d - see http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d

编辑:我认为最接近的等价物是scipy.interpolate.interp1d - 请参阅http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d

You could make a wrapper like so:

你可以像这样制作一个包装器:

import numpy as np
from scipy.interpolate import interp1d

def interp(ys, mul):
    # linear extrapolation for last (mul - 1) points
    ys = list(ys)
    ys.append(2*ys[-1] - ys[-2])
    # make interpolation function
    xs = np.arange(len(ys))
    fn = interp1d(xs, ys, kind="cubic")
    # call it on desired data points
    new_xs = np.arange(len(ys) - 1, step=1./mul)
    return fn(new_xs)

which then works like

然后工作就像

>>> interp([1,2,3,4,5,6,7,8,9,10], 2)
array([  1. ,   1.5,   2. ,   2.5,   3. ,   3.5,   4. ,   4.5,   5. ,
         5.5,   6. ,   6.5,   7. ,   7.5,   8. ,   8.5,   9. ,   9.5,
        10. ,  10.5])

#2


0  

You can get the results similar to Matlab's interp() function for example like this:

您可以获得类似于Matlab的interp()函数的结果,例如:

def interpolate_1d_vector(vector, factor):
    """
    Interpolate, i.e. upsample, a given 1D vector by a specific interpolation factor.
    :param vector: 1D data vector
    :param factor: factor for interpolation (must be integer)
    :return: interpolated 1D vector by a given factor
    """
    x = np.arange(np.size(vector))
    y = vector
    f = scipy.interpolate.interp1d(x, y)

    x_extended_by_factor = np.linspace(x[0], x[-1], np.size(x) * factor)
    y_interpolated = np.zeros(np.size(x_extended_by_factor))

    i = 0
    for x in x_extended_by_factor:
        y_interpolated[i] = f(x)
        i += 1

    return y_interpolated

#1


0  

A couple of issues need to be raised:

需要提出几个问题:

  • Are you just looking at linear interpolation (ie "connect the dots" with straight line segments)? That's simple but kind of nasty. You can get nicer results with higher-order curves (ie bicubic splines) but for that you need to provide more information to nail down a unique solution (ie endpoint first-derivatives).

    你只是看线性插值(即用直线段“连接点”)?这很简单但很讨厌。您可以使用高阶曲线(即双三次样条曲线)获得更好的结果,但为此您需要提供更多信息来确定唯一解决方案(即端点一阶导数)。

  • Do you want to smooth the curve at the same time, or do you expect it to go exactly through your given points?

    您是否希望同时平滑曲线,或者您是否希望它完全通过您的给定点?

  • Are your input points uniformly spaced (ie along the x axis)?

    您的输入点是均匀间隔的(即沿x轴)?

  • Your data shows not just interpolation but also extrapolation (ie your last point is off the end of your data) - is this actually what you want?

    您的数据不仅显示插值,还显示外推(即您的最后一点不在数据末尾) - 这实际上是您想要的吗?

Matlab documentation says "interp inserts 0s into the original signal and then applies a lowpass interpolating filter to the expanded sequence".

Matlab文档说“interp将0插入原始信号,然后将低通内插滤波器应用于扩展序列”。


Edit: I think the closest equivalent is scipy.interpolate.interp1d - see http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d

编辑:我认为最接近的等价物是scipy.interpolate.interp1d - 请参阅http://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html#scipy.interpolate.interp1d

You could make a wrapper like so:

你可以像这样制作一个包装器:

import numpy as np
from scipy.interpolate import interp1d

def interp(ys, mul):
    # linear extrapolation for last (mul - 1) points
    ys = list(ys)
    ys.append(2*ys[-1] - ys[-2])
    # make interpolation function
    xs = np.arange(len(ys))
    fn = interp1d(xs, ys, kind="cubic")
    # call it on desired data points
    new_xs = np.arange(len(ys) - 1, step=1./mul)
    return fn(new_xs)

which then works like

然后工作就像

>>> interp([1,2,3,4,5,6,7,8,9,10], 2)
array([  1. ,   1.5,   2. ,   2.5,   3. ,   3.5,   4. ,   4.5,   5. ,
         5.5,   6. ,   6.5,   7. ,   7.5,   8. ,   8.5,   9. ,   9.5,
        10. ,  10.5])

#2


0  

You can get the results similar to Matlab's interp() function for example like this:

您可以获得类似于Matlab的interp()函数的结果,例如:

def interpolate_1d_vector(vector, factor):
    """
    Interpolate, i.e. upsample, a given 1D vector by a specific interpolation factor.
    :param vector: 1D data vector
    :param factor: factor for interpolation (must be integer)
    :return: interpolated 1D vector by a given factor
    """
    x = np.arange(np.size(vector))
    y = vector
    f = scipy.interpolate.interp1d(x, y)

    x_extended_by_factor = np.linspace(x[0], x[-1], np.size(x) * factor)
    y_interpolated = np.zeros(np.size(x_extended_by_factor))

    i = 0
    for x in x_extended_by_factor:
        y_interpolated[i] = f(x)
        i += 1

    return y_interpolated