Source code for pysdkit._ssa.ssa
# -*- coding: utf-8 -*-
"""
Created on 2025/02/06 18:26:39
@author: Whenxuan Wang
@email: wwhenxuan@gmail.com
"""
import numpy as np
from numpy import linalg
from typing import Optional
from pysdkit.utils import diagonal_average
from pysdkit.utils import lags_matrix
[docs]
class SSA(object):
"""
Singular Spectral Analysis (SSA) algorithm
Zhigljavsky, Anatoly Alexandrovich.
"Singular spectrum analysis for time series: Introduction to this special issue."
Statistics and its Interface 3.3 (2010): 255-258.
MATLAB code: https://www.mathworks.com/matlabcentral/fileexchange/58967-singular-spectrum-analysis-beginners-guide
following steps of an SSA analysis:
- creation of the trajectory matrix
- calculation of the covariance matrix
- eigendecomposition of the covariance matrix
- resulting eigenvalues, eigenvectors
- calculation of the principal components
- reconstruction of the time series.
"""
[docs]
def __init__(
self,
K: int = 3,
mode="covar",
lags: Optional[int] = None,
averaging: Optional[bool] = True,
extra_size: Optional[bool] = False,
) -> None:
"""
Estimation the signal components based on the Singular Spectral Analysis (SSA) algorithm
:param K: order of the model (number of valuable components, size of signal subspace)
:param mode: the mode of lags matrix (i.e. trajectory (or caterpillar) matrix or its analouge), mode = {traj, full, covar, toeplitz, hankel}
:param lags: number of lags in correlation function (x.shape[0]//2 by default)
:param averaging: if True, then mean of each diagonal will be taken for diagonal averaging instead of just summarizing (True, by default)
:param extra_size: if True, than near doubled size of output will be returned
"""
self.K = K
self.mode = mode
self.lags = lags
self.averaging = averaging
self.extra_size = extra_size
self.EPSILON = 1e-4
[docs]
def __call__(self, signal: np.ndarray) -> np.ndarray:
"""allow instances to be called like functions"""
return self.fit_transform(signal=signal)
[docs]
def __str__(self) -> str:
"""Get the full name and abbreviation of the algorithm"""
return "Singular Spectral Analysis (SSA)"
if __name__ == "__main__":
from pysdkit.data import test_emd
from pysdkit.plot import plot_IMFs
from matplotlib import pyplot as plt
time, signal = test_emd()
ssa = SSA(K=2, mode="covar")
IMFs = ssa.fit_transform(signal)
print(IMFs.shape)
plot_IMFs(signal, IMFs)
plt.show()