def generate_covsig(covmat, n):
    global randn_index
    global randn

    covmat = np.atleast_2d(covmat)
    m = covmat.shape[0]
    l = np.linalg.cholesky(covmat)

    x = []
    while len(x) < n * m:
        to_go = min(randn_index + n * m - len(x), len(randn))
        x.extend(randn[randn_index:to_go])
        randn_index = to_go % len(randn)
    x = np.reshape(x, (m, n))

    # matrix to make cov(x) = I
    d = np.linalg.inv(np.linalg.cholesky(np.atleast_2d(np.cov(x))))

    x = l.dot(d).dot(x)

    return x


# 497 random numbers drawn from the standard normal distribution
#noinspection PyRedeclaration
generate pseudorandom stochastic signals with covariance matrix covmat
dataset/ETHPy150Open scot-dev/scot/scot/tests/generate_testdata.py/generate_covsig