简体   繁体   English

如何随机生成连续函数

[英]How to randomly generate continuous functions

My objective is to randomly generate good looking continuous functions, good looking meaning that functions which can be recovered from their plots. 我的目标是随机生成外观连续的函数,这些函数可以从其图中恢复。

Essentially I want to generate a random time series data for 1 second with 1024 samples per second. 本质上,我想以每秒1024个样本的速度生成1秒钟的随机时间序列数据。 If I randomly choose 1024 values, then the plot looks very noisy and nothing meaningful can be extracted out of it. 如果我随机选择1024个值,则该图看起来非常嘈杂,并且无法从中提取任何有意义的信息。 In the end I have attached plots of two sinusoids, one with a frequency of 3Hz and another with a frequency of 100Hz. 最后,我附加了两个正弦曲线的图,一个正弦波的频率为3Hz,另一个正弦波的频率为100Hz。 I consider 3Hz cosine as a good function because I can extract back the timeseries by looking at the plot. 我认为3Hz余弦是一个很好的函数,因为我可以通过查看曲线图来提取时间序列。 But the 100 Hz sinusoid is bad for me as I cant recover the timeseries from the plot. 但是100 Hz正弦波对我不利,因为我无法从绘图中恢复时间序列。 So in the above mentioned meaning of goodness of a timeseries, I want to randomly generate good looking continuos functions/timeseries. 因此,按照上述时间序列优度的含义,我想随机生成美观的连续函数/时间序列。

The method I am thinking of using is as follows (python language): 我正在考虑使用的方法如下(python语言):

(1) Choose 32 points in x-axis between 0 to 1 using x=linspace(0,1,32) . (1)使用x=linspace(0,1,32)在0到1之间的x轴上选择32个点。

(2) For each of these 32 points choose a random value using y=np.random.rand(32) . (2)对于这32个点中的每一个,使用y=np.random.rand(32)选择一个随机值。

(3) Then I need an interpolation or curve fitting method which takes as input (x,y) and outputs a continuos function which would look something like func=curve_fit(x,y) (3)然后,我需要一种插值法或曲线拟合方法,该方法将(x,y)作为输入并输出一个func=curve_fit(x,y)函数,该函数看起来像func=curve_fit(x,y)

(4) I can obtain the time seires by sampling from the func function (4)我可以通过func函数采样来获取时间序列

Following are the questions that I have: 以下是我的问题:

1) What is the best curve-fitting or interpolation method that I can use. 1)我可以使用的最佳曲线拟合或插值方法是什么? They should also be available in python. 它们也应该在python中可用。

2) Is there a better method to generate good looking functions, without using curve fitting or interpolation. 2)是否有一种更好的方法可以生成美观的函数,而无需使用曲线拟合或插值。

频率为3Hz的余弦波

频率为100Hz的余弦波

Edit 编辑

Here is the code I am using currently for generating random time-series of length 1024. In my case I need to scale the function between 0 and 1 in the y-axis. 这是我当前用于生成长度为1024的随机时间序列的代码。就我而言,我需要在y轴上将函数缩放为0到1之间。 Hence for me l=0 and h=0. 因此对我来说,l = 0,h = 0。 If that scaling is not needed you just need to uncomment a line in each function to randomize the scaling. 如果不需要缩放,则只需在每个函数中取消注释一行即可随机缩放。

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()

Add sin and cosine signals 添加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()

Output: 输出:

在此处输入图片说明

convolve sin and cosine signals 融合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()

Output: 输出:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM