【问题标题】:How to randomly generate continuous functions如何随机生成连续函数
【发布时间】:2019-06-11 07:59:28
【问题描述】:

我的目标是随机生成好看的连续函数,好看的意思是可以从它们的图中恢复的函数。

基本上我想生成一个随机时间序列数据,持续 1 秒,每秒 1024 个样本。如果我随机选择 1024 个值,那么情节看起来非常嘈杂,无法从中提取任何有意义的内容。最后,我附上了两个正弦曲线图,一个频率为 3Hz,另一个频率为 100Hz。我认为 3Hz 余弦是一个很好的函数,因为我可以通过查看绘图来提取时间序列。但是 100 Hz 正弦波对我不利,因为我无法从图中恢复时间序列。所以在上面提到的时间序列的好含义中,我想随机生成好看的连续函数/时间序列。

我想用的方法如下(python语言):

(1) 使用x=linspace(0,1,32)在x轴0到1之间选择32个点。

(2) 对于这 32 个点中的每一个,使用y=np.random.rand(32) 选择一个随机值。

(3) 然后我需要一个插值或曲线拟合方法,它以 (x,y) 为输入并输出一个类似于 func=curve_fit(x,y) 的连续函数。

(4)我可以通过func函数采样得到时间序列

以下是我的问题:

1) 我能做到的最好的曲线拟合或插值方法是什么 采用。它们也应该在 python 中可用。

2) 有没有更好的方法来生成好看的函数, 不使用曲线拟合或插值。

编辑

这是我目前用于生成长度为 1024 的随机时间序列的代码。在我的情况下,我需要在 y 轴上将函数缩放到 0 和 1 之间。因此对我来说 l=0 和 h=0。如果不需要该缩放,您只需取消注释每个函数中的一行以随机缩放。

import numpy as np
from scipy import interpolate
from sklearn.preprocessing import MinMaxScaler
import matplotlib.pyplot as plt

## Curve fitting technique
def random_poly_fit():
    l=0
    h=1
    degree = np.random.randint(2,11)
    c_points = np.random.randint(2,32)
    cx = np.linspace(0,1,c_points)
    cy = np.random.rand(c_points)
    z = np.polyfit(cx, cy, degree)
    f = np.poly1d(z)
    y = f(x)
    # l,h=np.sort(np.random.rand(2))
    y = MinMaxScaler(feature_range=(l,h)).fit_transform(y.reshape(-1, 1)).reshape(-1)
    return y

## Cubic Spline Interpolation technique
def random_cubic_spline():
    l=0
    h=1
    c_points = np.random.randint(4,32)
    cx = np.linspace(0,1,c_points)
    cy = np.random.rand(c_points)
    z = interpolate.CubicSpline(cx, cy)
    y = z(x)
    # l,h=np.sort(np.random.rand(2))
    y = MinMaxScaler(feature_range=(l,h)).fit_transform(y.reshape(-1, 1)).reshape(-1)
    return y

func_families = [random_poly_fit, random_cubic_spline]
func = np.random.choice(func_families)
x = np.linspace(0,1,1024)
y = func()
plt.plot(x,y)
plt.show()

【问题讨论】:

  • 只是澄清一下:你不希望你的函数中出现那种使其不适合的随机性(至少通过拟合不容易访问封闭形式)。您需要可以安装的随机函数组合。我问是因为那是相当不同的。
  • 通常人们会生成带有高斯(白)噪声的平滑曲线,沿线v(t)=f(t) + eps*N(0,1)machinelearningmastery.com/white-noise-time-series-python
  • 将随机数据拟合到平滑样条曲线是否适合您的目的?
  • 这就是我目前正在做的事情。我将编辑问题并发布我正在使用的代码

标签: python random interpolation curve-fitting curve


【解决方案1】:

添加sincosine 信号

from numpy.random import randint
x= np.linspace(0,1,1000)
for i in range(10):    
    y = randint(0,100)*np.sin(randint(0,100)*x)+randint(0,100)*np.cos(randint(0,100)*x)
    y = MinMaxScaler(feature_range=(-1,1)).fit_transform(y.reshape(-1, 1)).reshape(-1)
    plt.plot(x,y)
plt.show()

输出:

卷积sincosine 信号

for i in range(10):    
    y = np.convolve(randint(0,100)*np.sin(randint(0,100)*x), randint(0,100)*np.cos(randint(0,100)*x), 'same')
    y = MinMaxScaler(feature_range=(-1,1)).fit_transform(y.reshape(-1, 1)).reshape(-1)
    plt.plot(x,y)
plt.show()

输出:

【讨论】:

  • 这或多或少等同于从高斯过程模型中采样(即,通过谱法使用高斯过程模拟,参见 [Cressie, 1993, Statistics for Spatial Data]),我认为是这个问题的一个很好的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-20
  • 2020-07-24
  • 2011-09-26
  • 1970-01-01
相关资源
最近更新 更多