資源簡介
基于Python自相關法時間序列的時間延遲計算,用matlab程序翻譯成python并修改調試成功,很辛苦的額。
用于混沌系統、故障診斷等的相空間重構中的時間延遲計算。

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?numpy?as?np
import?matplotlib.pyplot?as?plt
import?random
import?matplotlib?as?mpl
import?warnings
#混沌時間序列分析源程序
#時間序列的時間延遲計算
def?autocorrelation(datatau_max):
????“““#data:輸入的時間序列
????#tau_max:最大時間延遲
????#Tau:返回所求時間序列的時間延遲“““
????N=len(data)
????#時間序列長度
????x_mean=np.mean(data)
????#時間序列的平均值
????data=data-x_mean
????SSd=np.dot(datadata)
????R_xx=np.zeros((1tau_max))[0]#=zeros(1tau_max)
????
????#自相關函數初始化
????for?tau?in?range(0tau_max):#1:tau_max
????#計算自相關函數
????????????for?ii?in?range(0N-tau):#1:N-tau
????????????????#print(tauiiii+tauN)
????????????????R_xx[tau]?=?R_xx[tau]+data[ii]*data[ii+tau]#
????????????R_xx[tau]=R_xx[tau]/SSd
????##end
????#print(len(R_xx))
????print(R_xx)
????#print(np.arange(1tau_max+1))
????plt.plot(np.arange(1tau_max+1)R_xx)?#plot(1:tau_maxR_xx);
????#plt.show()##hold?on
????#作自相關函數圖
????plt.plot([1tau_max][00])#line([1tau_max][00])
????plt.title(‘自相關法求時間延遲‘)
????plt.ylabel(‘自相關函數‘)
????plt.xlabel(‘時間延遲‘)?;plt.show()#
????Tau=0
????#求時間序列的時間延遲
????R_tau?=?list(map(lambda?x:x>=0R_xx))
????#print(R_tau)
????jj?=?0;R_jj?=R_tau[0]?
????while?jj?????????jj?=?jj+1
????????if?R_jj!=R_tau[jj]:
????????????break
????tau?=?jj
????#print(‘tau?jj:‘tau)
????if?abs(R_xx[jj-1])>abs(R_xx[jj]):
????????Tau=Tau+1
????????#print(‘tau?ok:‘tau)?
????‘‘‘jj?=?0
????while?jj?????????jj?=?jj+1#print(jj)
????????Tau=jj;print(Tau)
????????print(R_xx[jj-1]R_xx[jj]R_xx[jj-1]*R_xx[jj])
????????if?R_xx[jj-1]*R_xx[jj]<=0:
????????????print(jj)
????????????if?abs(R_xx[jj-1])>abs(R_xx[jj]):
????????????????pass#Tau=jj;print(‘tau1:‘tau)?#break
????????????else:
????????????????Tau=Tau-1#Tau=jj-1;print(‘tau2:‘tau)#break
????????????print(‘tau:‘tau);break‘‘‘
????#end
????#end
????#end
????print(‘tau:‘tau)
????return?tau
#生成隨機數字序列用于測試
def?gen_series(n):
????result?=?[]
????for?i?in?range(n):
????????t?=?random.randint(099)#random.uniform(1?10)#random.random()#random.randint(050)#uniform(1?3)
????????#隨機浮點數random.random()?#random.uniform(1?10)
????????#隨機字符:random.choice(‘abcdefg#^*f‘)#多個字符中選取特定數量的字符:random.sample(‘abcdefghij‘3)
????????#隨機整數:random.randint(099)#隨機選取0到100間的偶數:random.randrange(0?101?2)
????????result.append(t)
????print(result)
????return?result
if?__name__?==?“__main__“:
#解決中文的問題?
????mpl.use(‘TkAgg‘)
????mpl.rcParams[‘font.sans-serif‘]?=?[‘SimHei‘]???
????mpl.rcParams[‘font.family‘]=‘sans-serif‘??
????#解決負號‘-‘顯示為方塊的問題??
????mpl.rcParams[‘axes.unicode_minus‘]?=?False
????
????x?=?gen_series(100)
????print(‘autocorrelation:‘)
????print(autocorrelation(x20))
????
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???10044356??2008-04-14?20:00??simhei.ttf
?????文件???????3116??2018-02-11?18:06??autocorrelation.py
-----------?---------??----------?-----??----
?????????????10047472????????????????????2
評論
共有 條評論