使用Python 和matlab 进行AR模型的仿真(自回归模型)

"""对钙信号的动力学进行建模,AR模型。"""

import matplotlib.pyplot as plt
import numpy as np

if __name__ == '__main__':
    length = 500
    time = range(length)

    gamma = 0.99
    c0 = 1
    # st = np.random.poisson(lam=10, size=length).astype(float)*0.01
    st = np.random.normal(loc=0.0, scale=1.0, size=length)*0.05

    ct=np.zeros(shape = length)
    ctn=np.zeros(shape = length)
    spikes = [100, 350, 470]
    spikeAmplitude = 1

    #自回归模型
    for i in range(1, length):
        if i in spikes:
            ct[i] = gamma*ct[i-1] + spikeAmplitude
        else:
            ct[i] = gamma*ct[i-1]

        ctn[i] = ct[i]+ st[i]

        print(ct[i])

    fig,ax = plt.subplots()
    ax.plot(time, ct, color = 'b', label = 'true')
    ax.scatter(time, ctn, color = 'r',s=3, label = 'add noise')

    ax.set_xlabel('Frame')
    ax.set_ylabel('Calcium Transient')
    plt.title('Gamma is {}'.format(gamma))
    plt.legend(loc = "best")
    fig.show()


--------------------

matalb

len = 500;
gamma = 0.98;% 0.99,200帧;
spikes = [100];
c0 = 1;
ct = zeros(1,len,'double');
ctn = ct;% noise
spikeAmplitude = 2;

mu = 0;sigma = 1;
st = normrnd(mu, sigma, 1, len) .* 0.05;

for i = 2:len
    % do something
    
    if ismember(i,spikes)
        ct(1,i) = gamma*ct(1,i-1) + spikeAmplitude;
    else
        ct(1,i) = gamma*ct(1,i-1);
    end    
    ctn(1,i) = ct(1,i)+ st(1,i);    
end

figure;
plot(ct);hold on;
plot(ctn);

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值