title: How to randomly generate continuous functions
body: <p>My objective is to randomly generate good looking continuous functions, good looking meaning that functions which can be recovered from their plots. </p>
  
  <p>Essentially I want to generate a random time series data for 1 second with 1024 samples per second. If I randomly choose 1024 values, then the plot looks very noisy and nothing meaningful can be extracted out of it. In the end I have attached plots of two sinusoids, one with a frequency of 3Hz and another with a frequency of 100Hz. I consider 3Hz cosine as a good function because I can extract back the timeseries by looking at the plot. But the 100 Hz sinusoid is bad for me as I cant recover the timeseries from the plot. So in the above mentioned meaning of goodness of a timeseries, I want to randomly generate good looking continuos functions/timeseries. </p>
  
  <p>The method I am thinking of using is as follows (python language):</p>
  
  <p>(1) Choose 32 points in x-axis between 0 to 1 using  <code>x=linspace(0,1,32)</code>.</p>
  
  <p>(2) For each of these 32 points choose a random value using <code>y=np.random.rand(32)</code>.</p>
  
  <p>(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 <code>func=curve_fit(x,y)</code></p>
  
  <p>(4) I can obtain the time seires by sampling from the func function</p>
  
  <p>Following are the questions that I have:</p>
  
  <blockquote>
    <p>1) What is the best curve-fitting or interpolation method that I can
    use. They should also be available in python.</p>
    
    <p>2) Is there a better method to generate good looking functions,
    without using curve fitting or interpolation.</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/vqDuB.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vqDuB.jpg" alt="A Cosine wave with frequency of 3Hz"></a></p>
  
  <p><a href="https://i.stack.imgur.com/8C67x.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8C67x.jpg" alt="A Cosine wave with frequency of 100Hz"></a></p>
  
  <p><strong>Edit</strong></p>
  
  <p>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. Hence for me l=0 and h=0. If that scaling is not needed you just need to uncomment a line in each function to randomize the scaling.</p>
  
  <pre><code>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()
  </code></pre>