title: Python Sinusoidal white noise
body: <p>So what I'm doing is creating sine waves with normally distributed amplitudes and frequencies - within given ranges. Eg 5V with 2-10Hz. So my attempt at this is to get my function with the given amplitude and frequency and then run it till the first turning point. From there I calculate the next function and add the y value of the previous functions turning point (as a shift) so it starts from that point. My problem is for some of the function changes I get straight lines rather than curves. If someone could tell me where I'm going wrong I'd appreciate it. Just to note, I use 8ms increments for each value to be plotted.</p>
  
  <pre><code>import numpy as np
  import matplotlib.pyplot as plt
  import scipy.stats as stats
  import serial
  
  newlist = np.zeros(1)
  timesnew = np.zeros(1)
  volts = []
  
  def main(amp, lowerFreq, upperFreq, time, incr):
      #Creates graph and saves it in newlist and timesnew
      amt = np.int(time / incr)
      list = []
      timels = []  # np.zeros(amt+amt)
      curtime = 0
      loweramp = -amp
      mu, sigma = 0, 1
      ybefore = 0
      rand = stats.truncnorm((loweramp - mu) / sigma, (amp - mu) / sigma, loc=mu, scale=sigma)
      freqr = stats.truncnorm((lowerFreq - mu) / sigma, (upperFreq - mu) / sigma, loc=mu, scale=sigma)
      i = 0
      while i &lt; amt:
          # get amp
          thisAmp = rand.rvs()
          angleFreq = 2 * np.pi * freqr.rvs()
          xtp = np.arccos(0) / angleFreq #x value of turning point
          yval = thisAmp * np.sin(angleFreq * xtp)
  
          # check that yvalue(voltage) is okay to be used - is within +-amp range
          while not loweramp &lt;= yval + ybefore &lt;= amp:
              thisAmp = rand.rvs()
              angleFreq = 2 * np.pi * freqr.rvs()
              xtp = np.arccos(0) / angleFreq
  
              yval = thisAmp * np.sin(angleFreq * xtp)
          # now add values to list
  
          t = 0
          while t &lt;= xtp:
              ynow = thisAmp * np.sin(angleFreq * t) + ybefore
              # print ynow
              list.append(ynow)
              curtime += incr
              timels.append(curtime)
              t += incr
              i += 1
              print i
  
          ybefore = ynow
  
      newlist = np.asarray(list)
      timesnew = np.asarray(timels)
  
      #a = np.column_stack((timesnew, newlist))
  
      np.savetxt("C://foo.csv", a, delimiter=";", fmt='%.10f')
      addvolts()
      plt.plot(timels,list)
      plt.show()
  
  if __name__ == "__main__":
      main(5, 1, 2, 25, 0.00008)
  </code></pre>
  
  <p>EDIT:
  Basically here is the problem, after the turning point the function does not seem to be sinusodial (the line seems to be linear) and I can't understand why or atleast how to get the functions to end up being more "curvy" and not "sharp" at the turning points. </p>
  
  <p><a href="https://i.stack.imgur.com/zOP46.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zOP46.png" alt="My graph"></a></p>
  
  <p><a href="https://i.stack.imgur.com/MP9ke.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MP9ke.png" alt="zoomed"></a></p>
  
  <p>I'm thinking maybe the function changes shouldn't be too different from the previous function but then I would lose the randomness. I'd like it to "look better" but I'm not sure how to achieve that unless I ran the frequencies in order. I'm trying to emulate a "whitenoise file" that was given to me as part of a job that I applied for - the whitenoise would be sent to a digital to analog converter and be used to test equipment. Obviously I didn't get the position BUT for knowledge purposes I want to complete this.</p>
  
  <p>Here is the graph of the whitenoise file I was given - 700 mins long:</p>
  
  <p><a href="https://i.stack.imgur.com/C3vYL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/C3vYL.png" alt="Given file"></a></p>
  
  <p><a href="https://i.stack.imgur.com/UBO74.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UBO74.png" alt="zoomed"></a></p>
  
  <p><a href="https://i.stack.imgur.com/5l9gQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5l9gQ.png" alt="zoomed to show curves"></a>
  From the last pic the difference between mine and the given can be seen, I think I'm going to attempt to run each function for an entire period rather than a single turning point.</p>