我想生成一個時間序列,其中溫度為 y 軸,一年中的小時數為 x 軸。我在考慮兩個正弦波:1 - 一個周期設定為 1 天(模擬夜間低溫和白天高溫)。幅度也可以很簡單,即 1,并且vertical_shift = 25。2 - 第一個波“嵌入”在更大的季節性波中。
使用python模擬這個的正確方法是什么?
uj5u.com熱心網友回復:
附上一個可能的實作。它利用了 cos(2*pi*t/T) 的周期為 T 的事實。取 T=24 和 T=24*365 會產生分別在一天和一年內變化的余弦波。減號類似于從冬季中期和深夜開始觀察的情況。
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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/495352.html