自回归(AR)模型的功率谱估计(实现)

上一部分介绍了AR模型的理论知识,这一部分将介绍AR模型的各种估计方法。点击这里,快速查看理论知识。

Yule-Walker法

流程图

自回归(AR)模型的功率谱估计(实现)_第1张图片

python 代码

import scipy
import numpy as np
from matplotlib import pyplot as plt


# 自相关函数
def x_corr(x):
    x_inv = x[::-1]
    # r_x = np.correlate(x, x)
    r_x = np.convolve(x, x_inv)
    r_x /= len(x)
    return r_x


if __name__ == "__main__":
    N = int(input("采样点数N:"))
    P = int(input("模型阶数P:"))
    # a_N [0, 1, ..., N-1]
    a_N = np.arange(0, N, 1)

    # 信号频率
    f1 = 0.1
    f2 = 0.13
    a_X = 2 * np.sin(2 * np.pi * f1 * a_N + np.pi / 3) + 10 * np.sin(2 * np.pi * f2 * a_N + np.pi / 4)

    # 自相关函数Rxx
    # Rxx(0), Rxx(1), ..., Rxx(N-1)
    Rxx = x_corr(a_X)[N-1:]
    # 取前P+1个
    # Rxx(0), Rxx(1), Rxx(P-1), Rxx(P)
    Rxx = Rxx[: P+1]

    # 自相关矩阵a_Rxx
    # [Rxx(0)   Rxx(1)   ... Rxx(p-1)]
    # [Rxx(-1)  Rxx(0)   ... Rxx(p-2)]
    # ...
    # [Rxx(1-p) Rxx(2-p) ... Rxx(0)  ]
    a_Rxx = np.zeros([P, P])
    for i in range(P):
        for j in range(P):
            a_Rxx[i, j] = Rxx[abs(i-j)]

    # 结果矩阵a_rx
    # [-Rxx(1)]
    # [-Rxx(2)]
    # ...
    # [-Rxx(p)]
    a_rx = np.zeros([P, 1])
    for i in range(P):
        a_rx[i, 0] = -Rxx[i + 1]

    # 系数矩阵a_apk
    # [a_p1]
    # [a_p2]
    # ...
    # [a_pp]
    a_Rxx_inv = np.linalg.inv(a_Rxx)
    a_apk = np.matmul(a_Rxx_inv, a_rx)

    # 标准差sigma
    sigma = Rxx[0]
    for i in range(P):
        sigma += a_apk[i, 0] * Rxx[i + 1]
    sigma = np.sqrt(sigma)

    # [ap1, ..., app]->[1, ap1, ..., app]
    a_ap = np.insert(a_apk, 0, [1])
    w, h = scipy.signal.freqz(sigma, a_ap, worN=N)

    Hf = abs(h)
    psd = Hf**2
    f = w / (2 * np.pi)

    plt.figure(1)
    plt.subplot(211)
    plt.plot(f, Hf)
    plt.xlabel("f/hz")
    plt.ylabel("Hf")

    plt.subplot(212)
    plt.plot(f, psd)
    plt.xlabel("f/hz")
    plt.ylabel("psd")
    plt.show()

实验结果

自回归(AR)模型的功率谱估计(实现)_第2张图片

Levinson-Durbin法

流程图

自回归(AR)模型的功率谱估计(实现)_第3张图片

python代码

import numpy as np
import scipy
from matplotlib import pyplot as plt


def x_corr(a_x):
    a_x_inv = a_x[::-1]
    a_rx = np.convolve(a_x, a_x_inv)
    a_rx /= len(a_x)
    return a_rx


if __name__ == "__main__":
    N = int(input("采样点数N:"))
    P = int(input("模型阶数P:"))

    a_N = np.arange(N)
    # 信号频率
    f1 = 0.1
    f2 = 0.3
    # 信号
    a_X = 2 * np.sin(2 * np.pi * f1 * a_N + np.pi / 3) + 10 * np.sin(2 * np.pi * f2 * a_N + np.pi / 4)

    # 自相关函数Rx
    Rxx = x_corr(a_X)[N - 1:]
    Rxx = Rxx[: P + 1]

    # 初始化参数apk, sigma
    # p=1
    a_apk = np.zeros([P + 1, P + 1])
    a_apk[1, 1] = -Rxx[1] / Rxx[0]
    a_sigma = np.zeros(P + 1)
    a_sigma[1] = (1 - np.square(a_apk[1, 1])) * Rxx[0]

    # p>1
    for p in range(2, P + 1):
        kp_sum = 0
        for i in range(1, p):
            kp_sum += a_apk[p - 1, i] * Rxx[p - i]
        a_apk[p, p] = - (Rxx[p] + kp_sum) / a_sigma[p - 1]
        for k in range(p):
            a_apk[p, k] = a_apk[p - 1, k] + a_apk[p, p] * a_apk[p - 1, p - k]
        a_sigma[p] = (1 - np.square(a_apk[p, p])) * a_sigma[p - 1]
    
    a_ap = a_apk[- 1][1:]

    # a_apk = np.zeros([P, P])
    # a_apk[0, 0] = -Rxx[1] / Rxx[0]
    # a_sigma = np.zeros(P)
    # a_sigma[0] = (1 - np.square(a_apk[0, 0])) * Rxx[0]
    #
    # for p in range(1, P):
    #     kp_sum = 0
    #     for i in range(0, p):
    #         kp_sum += a_apk[p - 1, i] * Rxx[p - i]
    #     a_apk[p, p] = - (Rxx[p + 1] + kp_sum) / a_sigma[p - 1]
    #     for k in range(p):
    #         a_apk[p, k] = a_apk[p - 1, k] + a_apk[p, p] * a_apk[p - 1, p - k - 1]
    #     a_sigma[p] = a_sigma[p - 1] * (1 - np.square(a_apk[p, p]))

    # a_ap = a_apk[- 1]
    
    sigma = a_sigma[- 1]
    sigma = np.sqrt(sigma)
    a_ap = np.insert(a_ap, 0, [1])
    w, h = scipy.signal.freqz(sigma, a_ap, worN=N)

    Hf = abs(h)
    psd = Hf**2
    f = w / (2 * np.pi)

    plt.figure(1)
    plt.subplot(211)
    plt.plot(f, Hf)
    plt.xlabel("f/hz")
    plt.ylabel("Hf")

    plt.subplot(212)
    plt.plot(f, psd)
    plt.xlabel("f/hz")
    plt.ylabel("psd")
    plt.show()

实验结果

自回归(AR)模型的功率谱估计(实现)_第4张图片

Burg法

流程图

自回归(AR)模型的功率谱估计(实现)_第5张图片

python代码

待续…

实验结果

待续…

协方差法

自回归(AR)模型的功率谱估计(实现)_第6张图片

python代码

待续…

实验结果

待续…

修正协方差法

流程图

自回归(AR)模型的功率谱估计(实现)_第7张图片

python代码

待续…

实验结果

待续…

你可能感兴趣的:(脑机接口,回归,ar,python)