Home > Software engineering >  generate time series for day & year fluctuations using python
generate time series for day & year fluctuations using python

Time:06-23

I want to generate a time series with temperature as the y axis and hours in a year as the x axis. I am thinking of two sine waves: 1 - One with the period set to 1 day (to simulate low temp at night and high temp during the day). The amplitude can also be simple, i.e. 1, and with vertical_shift = 25. 2 - the first wave is "embedded" inside a bigger, seasonal wave.

What is the correct way to simulate this using python?

CodePudding user response:

A possible implementation is enclosed. It makes use of the fact that cos(2*pi*t/T) will have a period of T. Taking T=24 and T=24*365 creates cosine waves that vary over a day and year, respectively. The minus signs resemble the situation with observations starting in the middle of winter and the middle of the night.

import numpy as np
import matplotlib.pyplot as plt

# parameters
MeanTemp = 15           # Average temperature in the country
DailyAmpl = 10          # Amplitude of the daily cycle
YearlyAmpl = 1          # Amplitude of the yearly cycle
NoiseStd = 0.1          # Standard deviation of normally distributed error term

# Total hours in year
TotalHours = 24*365

# Generate the frequency components of the data
x = np.arange(0, TotalHours)
DailyCycle = -DailyAmpl*np.cos( (2*np.pi)*x/24 )
YearlyCycle = -YearlyAmpl*np.cos( (2*np.pi)*x/TotalHours )
Noise = np.random.normal(0, NoiseStd, TotalHours)

# Final series
y = MeanTemp   DailyCycle   YearlyCycle   Noise

# Visualise result
fig, axs = plt.subplots(2, 1)
axs[0].plot(y)
axs[0].set_title('Complete series')
axs[1].plot(y[0:(10*24)])
axs[1].set_title('First ten days')
plt.show()
  • Related