计算具有不同采样点数量的离散函数的均值 [英] Calculate mean over discrete functions with different amount of sampling points

查看:140
本文介绍了计算具有不同采样点数量的离散函数的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组测量曲线(表示为x数组和y值数组).我需要计算该组的平均曲线. 集合中的曲线可能在采样点数量(x值)和采样点位置上均发生变化.

I have a set of measurement curves (represented as an array of x and an array of y values). I need to calculate the average curve of this set. The curves in the set may vary both in number of sampling points (x-values) and position of the sampling points.

我首先使用scipys interp1d线性地对每条曲线进行插值.然后,我确定所有曲线重叠的x值范围,以便定义插值函数.最后,我需要计算平均值,这就是我遇到的问题.

I first interpolate every curve linearly using scipys interp1d. I then determine the range of x-values where all curves overlap, in order for the interpolated functions to be defined. Finally i need to calculate the mean, this is where I am stuck.

推荐答案

恐怕您的问题是概念性的,而不是编码相关的.但是,以下示例应为您提供帮助:

I'm afraid that your question is rather conceptual than coding related. However, the following example should help you:

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

# make up three datasets for testing
x1 = np.linspace(0, 10, num=11, endpoint=True)
x2 = np.linspace(0, 10, num=13, endpoint=True)
x3 = np.linspace(0, 10, num=23, endpoint=True)

y1 = np.cos(-x1**2/9.0) + 0.2*np.random.rand((len(x1)))
y2 = np.cos(-x2**2/9.0) + 0.2*np.random.rand((len(x2)))
y3 = np.cos(-x3**2/9.0) + 0.2*np.random.rand((len(x3)))

# interpolate data
f1 = interp1d(x1, y1,'cubic')
f2 = interp1d(x2, y2,'cubic')
f3 = interp1d(x3, y3,'cubic')

# define common carrier for calculation of average curve
x_all = np.linspace(0, 10, num=101, endpoint=True)

# evaluation of fits on common carrier
f1_int = f1(x_all)
f2_int = f2(x_all)
f3_int = f3(x_all)

# put all fits to one matrix for fast mean calculation
data_collection = np.vstack((f1_int,f2_int,f3_int))

# calculating mean value
f_avg = np.average(data_collection, axis=0)

# plot this example
plt.figure()
plt.hold('on')
plt.plot(x1,y1,'ro',label='row1')
plt.plot(x2,y2,'bo',label='row2')
plt.plot(x3,y3,'go',label='row3')

plt.plot(x_all,f1_int,'r-',label='fit1')
plt.plot(x_all,f2_int,'b-',label='fit2')
plt.plot(x_all,f3_int,'g-',label='fit3')

plt.plot(x_all, f_avg,'k--',label='fit average')
plt.legend(loc=3)

plt.hold('off')
plt.show()

最重要的行是使用np.vstack组合测量结果和使用np.average进行测量结果平均值的那些行.剩下的只是一个可行的例子!

The most important lines are those using np.vstack to combine the measurements and np.average to take the average of the measurements. The rest is just to have a working example!

对于拟合的非等距载体,例如以下:

For nonequidistant carrier of the fits do e.g. the following:

# define common carrier for calculation of average curve
x_all_1 = np.linspace(0, 1, num=101, endpoint=True)
x_all_2 = np.linspace(1, 10, num=21, endpoint=True)
x_all = np.concatenate((x_all_1, x_all_2))

这篇关于计算具有不同采样点数量的离散函数的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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