插值到特定时间

编程入门 行业动态 更新时间:2024-10-28 05:22:59
本文介绍了插值到特定时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有以下代码:

import numpy as np import time from datetime import datetime class Measurements(): def __init__(self, time_var, value): self.time_var = time_var self.value = value a = np.array([ Measurements('30-01-2017 12:02:15.880922', 100), Measurements('30-01-2017 12:02:16.880922', 100), Measurements('30-01-2017 12:02:17.880922', 110), Measurements('30-01-2017 12:02:18.880922', 99), Measurements('30-01-2017 12:02:19.880922', 96)]) b = np.array([ Measurements('30-01-2017 12:02:15.123444', 10), Measurements('30-01-2017 12:02:18.880919', 12), ])

所以,我有5个来自a的测量值和2个来自b的测量值.

So, I have 5 measurements from a and 2 from b.

我希望以a为基础,在发生a的特定时间查找丢失的b值.

I want, by using the a as base, to find the missing b values at the specific time where a happens.

因此,最终的b将始终具有a时间值和长度.(对于该时间,我想到了使用time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple())以秒为单位返回时间

So, the final b will always have the a time values and length.( for the time, I thought of taking the time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) to return time in seconds

所以b将是:

np.array([ Measurements('30-01-2017 12:02:15.880922', MISSING_VALUE), Measurements('30-01-2017 12:02:16.880922', MISSING_VALUE), Measurements('30-01-2017 12:02:17.880922', MISSING_VALUE), Measurements('30-01-2017 12:02:18.880922', MISSING_VALUE), Measurements('30-01-2017 12:02:19.880922', MISSING_VALUE)])

现在,我不确定该如何处理.

Now, I am not sure how to deal with this.

一个想法是首先执行interp 此处所示,然后将b的长度拉伸为与a相等.

One thought is to execute first the interp as here and stretch the b length to be equal with a.

或使用interp1d(更灵活):

from scipy import interpolate a = np.array([100, 123, 123, 118, 123]) b = np.array([12, 11, 14, 13]) b_interp = interpolate.interp1d(np.arange(b.size),b, kind ='cubic', assume_sorted=False) b_new = b_interp(np.linspace(0, b.size-1, a.size))

但是,如何处理时间呢?

But then , how to deal with the time?

推荐答案

这是您的问题的解决方案:

Here is the solution of your problem :

  • 首先,如果使用三次插值,则a至少需要4个值,b至少需要4个值(scipy.interpolate.interp1d和kind="cubic"不能正常工作)
  • 第二次,您无法使用scipy.interpolate.interp1d插值的数值不在您定义的范围内(b倍的范围)
  • first, if you use cubic interpolation, you need at least 4 values for a and 4 values for b (scipy.interpolate.interp1d with kind="cubic" is not working otherwise)
  • second, you can not interpolate values with scipy.interpolate.interp1d that are not in the range you define (the range of b times)

我稍微更改了您的初始代码以显示给您:

I changed a bit your initial code to show you :

time_a_full = ['30-01-2017 12:02:15.880922','30-01-2017 12:02:16.880922','30-01-2017 12:02:17.880922','30-01-2017 12:02:18.880922','30-01-2017 12:02:19.880922','30-01-2017 12:02:22.880922'] time_b_full = ['30-01-2017 12:02:15.123444','30-01-2017 12:02:16.880919','30-01-2017 12:02:18.880920', '30-01-2017 12:02:19.880922','30-01-2017 12:02:20.880922'] # Here I transform the time in seconds as suggested time_a = np.array([time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) for s in time_a_full]) time_b = np.array([time.mktime(datetime.strptime(s, "%d-%m-%Y %H:%M:%S.%f").timetuple()) for s in time_b_full]) values_a = np.array([100,100,110,99,96,95]) values_b = np.array([10,12,13,16,20]) # result of the linear interp with the numpy function np.interp(time_a, time_b, values_b) # result of the cubic interpolation f = interpolate.interp1d(time_b,values_b, kind="cubic") time_a[time_a<time_b.min()]=time_b.min() # use this to stay on range define by the times of b time_a[time_a>time_b.max()]=time_b.max() # use this to stay on range define by the times of b f(time_a)

更多推荐

插值到特定时间

本文发布于:2023-11-24 09:55:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624746.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:插值   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!