如何使用SciPy对非均匀信号进行统一重采样? [英] How to uniformly resample a non-uniform signal using SciPy?

查看:368
本文介绍了如何使用SciPy对非均匀信号进行统一重采样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(x, y)信号,其中x中的采样率不均匀. (采样率大致与1/x成正比).我尝试使用scipy.signalresample函数统一对其进行重新采样.根据我对文档的了解,我可以将以下参数传递给它:

I have an (x, y) signal with non-uniform sample rate in x. (The sample rate is roughly proportional to 1/x). I attempted to uniformly re-sample it using scipy.signal's resample function. From what I understand from the documentation, I could pass it the following arguments:

scipy.resample(array_of_y_values, number_of_sample_points, array_of_x_values)

,它将返回

[[resampled_y_values],[new_sample_points]]

我希望它返回的采样数据与原始格式大致相同,并且具有最小和最大x值.但事实并非如此:

I'd expect it to return an uniformly sampled data with a roughly identical form of the original, with the same minimal and maximalx value. But it doesn't:

# nu_data = [[x1, x2, ..., xn], [y1, y2, ..., yn]] 
# with x values in ascending order

length = len(nu_data[0])
resampled = sg.resample(nu_data[1], length, nu_data[0])

uniform_data = np.array([resampled[1], resampled[0]])

plt.plot(nu_data[0], nu_data[1], uniform_data[0], uniform_data[1])
plt.show()

蓝色:nu_data,橙色:uniform_data

blue: nu_data, orange: uniform_data

它看起来并没有改变,并且x比例尺也已调整大小.如果我尝试固定范围:自己构造所需的统一x值并改用它们,则失真仍然存在:

It doesn't look unaltered, and the x scale have been resized too. If I try to fix the range: construct the desired uniform x values myself and use them instead, the distortion remains:

length = len(nu_data[0])
resampled = sg.resample(nu_data[1], length, nu_data[0])
delta = (nu_data[0,-1] - nu_data[0,0]) / length
new_samplepoints = np.arange(nu_data[0,0], nu_data[0,-1], delta)
uniform_data = np.array([new_samplepoints, resampled[0]])

plt.plot(nu_data[0], nu_data[1], uniform_data[0], uniform_data[1])
plt.show()

如果不是这样,统一重新采样数据的正确方法是什么?

推荐答案

请查看以下粗略解决方案:

Please look at this rough solution:

import matplotlib.pyplot as plt
from scipy import interpolate
import numpy as np

x = np.array([0.001, 0.002, 0.005, 0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20])
y = np.exp(-x/3.0)
flinear = interpolate.interp1d(x, y)
fcubic = interpolate.interp1d(x, y, kind='cubic')

xnew = np.arange(0.001, 20, 1)
ylinear = flinear(xnew)
ycubic = fcubic(xnew)
plt.plot(x, y, 'X', xnew, ylinear, 'x', xnew, ycubic, 'o')
plt.show()

这是scipy页面上的更新示例.如果执行它,应该会看到以下内容:

That is a bit updated example from scipy page. If you execute it, you should see something like this:

蓝叉是初始功能,您的信号具有不均匀的采样分布.并且有两个结果-橙色x-表示线性插值,绿色点-三次插值.问题是您更喜欢哪个选项?我个人不喜欢它们两个,这就是为什么我通常要在四个点之间进行插值,然后在另一个点之间进行插值...以进行三次插值而不产生奇怪的波动.那是更多的工作,而且我看不到scipy这样做,所以会很慢.这就是为什么我要问数据大小的原因.

Blue crosses are initial function, your signal with non uniform sampling distribution. And there are two results - orange x - representing linear interpolation, and green dots - cubic interpolation. Question is which option you prefer? Personally I don't like both of them, that is why I usually took 4 points and interpolate between them, then another points... to have cubic interpolation without that strange ups. That is much more work, and also I can't see doing it with scipy, so it will be slow. That is why I've asked about size of the data.

这篇关于如何使用SciPy对非均匀信号进行统一重采样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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