1

I have an interesting exercise that I cannot figure out. This is the exercise:

Start now with a time series of 1 Myr, a saw tooth with a wavelength of 0.1 Myr on average. The length of the wave varies randomly ±0.02 Myr (so between 80 and 120 kyrs), amplitude again 1.

So I have to create a time series of a sawtooth function, of 1000 kyrs long with timesteps of 1 kyr. The wavelength of the sawtooths have to be random, with an average of 100 kyr and varying from 80 kyr to 120 kyr. I want the time series to start at zero and end at zero, so not stop halfway during a sawtooth. Right now I am using this:

time = np.arange(0,1000,1)
func = scipy.signal.sawtooth(2 * np.pi* 0.01 * time-np.pi) 

Which results in this:

Sawtooth function

I am trying to figure out how to make the wavelength irregular, but still let the time series end at zero. Does anyone have an idea?

Thanks in advance

Simo
  • 955
  • 8
  • 18
UniMss
  • 27
  • 5
  • I'm not sure it's possible to have the variation be random and still have it end at 0 if you need a fixed time interval. You could try something like https://stackoverflow.com/a/2640079/7748810 to generate the periods, and have them all sum to the total length. But this may violate the wavelength restrictions – NateTheGrate Sep 19 '18 at 12:12

1 Answers1

1

You could concatenate waves with distinct wavelengths which you create with np.linspace and random.randrange:

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt
import random

waveform = np.concatenate(
    [signal.sawtooth(2 * np.pi * np.linspace(0, 1, random.randrange(30, 150))) for _ in range(10)]
)

plt.plot(waveform)
plt.show()

The output looks like this: enter image description here

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
  • Thanks! This is a nice start and a method a hadn't thought of yet. However, this method does not make the time series the desired length of 1000 kyrs... I'll try to make it work with this. – UniMss Sep 19 '18 at 12:24
  • Sure, you'll need to modify it a bit to fit the requirements. This article might help you http://sunny.today/generate-random-integers-with-fixed-sum/ in order to get a list of ints with a given sum, min and max. – Eric Duminil Sep 19 '18 at 12:33