重采样,插值矩阵 [英] resampling, interpolating matrix

查看:182
本文介绍了重采样,插值矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试插入一些数据以进行绘图.例如,给定N个数据点,我希望能够生成一个平滑"图,该图由10 * N个内插数据点组成.

I'm trying to interpolate some data for the purpose of plotting. For instance, given N data points, I'd like to be able to generate a "smooth" plot, made up of 10*N or so interpolated data points.

我的方法是生成一个N×10 * N的矩阵,并计算原始向量和我生成的矩阵的内积,从而生成一个1×10 * N的向量.我已经计算出要用于插值的数学运算,但是我的代码非常慢.我对Python还是很陌生,所以我希望这里的一些专家可以给我一些我可以尝试加快代码速度的想法.

My approach is to generate an N-by-10*N matrix and compute the inner product the original vector and the matrix I generated, yielding a 1-by-10*N vector. I've already worked out the math I'd like to use for the interpolation, but my code is pretty slow. I'm pretty new to Python, so I'm hopeful that some of the experts here can give me some ideas of ways I can try to speed up my code.

我认为部分问题是生成矩阵需要对以下函数进行10 * N ^ 2调用:

I think part of the problem is that generating the matrix requires 10*N^2 calls to the following function:

def sinc(x):
    import math
    try:
        return math.sin(math.pi * x) / (math.pi * x)
    except ZeroDivisionError:
        return 1.0

(此来自抽样理论..本质上,我是尝试从其样本中重建信号,并将其上采样到更高的频率.)

(This comes from sampling theory. Essentially, I'm attempting to recreate a signal from its samples, and upsample it to a higher frequency.)

矩阵是由以下内容生成的:

The matrix is generated by the following:

def resampleMatrix(Tso, Tsf, o, f):
    from numpy import array as npar
    retval = []

    for i in range(f):
        retval.append([sinc((Tsf*i - Tso*j)/Tso) for j in range(o)])

    return npar(retval)

我正在考虑将任务分解为较小的部分,因为我不喜欢将N ^ 2矩阵存储在内存中的想法.我可能可以将'resampleMatrix'变成一个生成器函数并逐行处理内部产品,但是我认为这不会加快我的代码的速度,直到我开始对内存中的内容进行分页.

I'm considering breaking up the task into smaller pieces because I don't like the idea of an N^2 matrix sitting in memory. I could probably make 'resampleMatrix' into a generator function and do the inner product row-by-row, but I don't think that will speed up my code much until I start paging stuff in and out of memory.

提前感谢您的建议!

推荐答案

这是上采样.有关某些示例解决方案,请参见有关重采样/上采样的帮助.

This is upsampling. See Help with resampling/upsampling for some example solutions.

执行此操作的快速方法(对于离线数据,例如绘图应用程序)是使用FFT.这就是SciPy的本机 resample()函数做.不过,它假定为周期性信号,因此并不完全相同.请参阅此参考:

A fast way to do this (for offline data, like your plotting application) is to use FFTs. This is what SciPy's native resample() function does. It assumes a periodic signal, though, so it's not exactly the same. See this reference:

这是关于时域实信号插值的第二个问题,确实很大.仅当原始x(n)序列在其整个时间间隔内都是周期性的时,这种精确的插值算法才能提供正确的结果.

Here’s the second issue regarding time-domain real signal interpolation, and it’s a big deal indeed. This exact interpolation algorithm provides correct results only if the original x(n) sequence is periodic within its full time inter­val.

您的函数假设信号的样本在定义范围之外均为0,因此这两种方法将偏离中心点.如果您首先用大量零填充信号,它将产生非常接近的结果.超出图的边缘还有几个零(此处未显示):

Your function assumes the signal's samples are all 0 outside of the defined range, so the two methods will diverge away from the center point. If you pad the signal with lots of zeros first, it will produce a very close result. There are several more zeros past the edge of the plot not shown here:

三次插值对于重新采样而言是不正确的.这个例子是一个极端的情况(接近采样频率),但是正如您所看到的,三次插值甚至还不紧密.对于较低的频率,它应该非常准确.

Cubic interpolation won't be correct for resampling purposes. This example is an extreme case (near the sampling frequency), but as you can see, cubic interpolation isn't even close. For lower frequencies it should be pretty accurate.

这篇关于重采样,插值矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆