---
url: /dyad/stable/analyses/system_identification.md
description: >-
  Identify linear state-space models from input-output data using subspace
  identification or prediction error methods.
---

# System Identification Analysis {#SystemIdentificationAnalysis}

The System Identification Analysis fits a linear time-invariant (LTI) state-space model (blackbox) to measured input-output data.

Transfer functions can be obtained by converting the estimated model using the function `tf`, and a continuous-time model may be obtained using `d2c`.

## Method Overview {#Method-Overview}

System identification in DyadControlSystems uses two primary methods from [ControlSystemIdentification.jl](https://github.com/baggepinnen/ControlSystemIdentification.jl):

1. **Subspace Identification (`subspaceid`)**: A non-iterative method that uses numerical linear algebra to identify state-space models. Fast and reliable for most applications.

2. **Prediction Error Method (`newpem`)**: An iterative optimization method that minimizes prediction errors. This method works for closed-loop data and may provide better fit, but takes longer to run.

Both methods identify discrete-time state-space models of the form:

$$\begin{align}
x\_{k+1} &= Ax_k + Bu_k + Ke_k \\
y_k &= Cx_k + Du_k + e_k
\end{align}$$

where $K$ is the measurement-feedback gain and $e_k$ is the innovation process.

## Example Definition {#Example-Definition}

The following example demonstrates system identification using measured data from a double-mass model

```julia
using DyadControlSystems
using DyadInterface

# Generate simulated data
dmm = DemoSystems.double_mass_model()
Ts  = 0.001
f0  = 0.01
f1  = 10
Tf  = 30
u   = DyadControlSystems.chirp(Ts, f0, f1, Tf; logspace = true, phi=deg2rad(139))'
res = lsim(dmm, u, 0:Ts:Tf, method=:zoh)

# Perform analysis
spec = DyadControlSystems.SystemIdentificationAnalysisSpec(;
    input_data = res.u,
    output_data = res.y,
    Ts,
    method = "subspaceid",
    nx = 4,
    name = :SysID,
    detrend = false,
)
sol = DyadInterface.run_analysis(spec)
artifacts(sol, :predplot)
```

![](zpexavx.svg)

```julia
artifacts(sol, :bodeplot, lab="Estimated")
bodeplot!(dmm, l=:dash, lab="True")
```

![](qakglnt.svg)

## Analysis Arguments {#Analysis-Arguments}

The following arguments define a System Identification Analysis:

### Data Input Options {#Data-Input-Options}

You must provide data using **one** of these approaches:

#### Option 1: Direct Arrays {#Option-1:-Direct-Arrays}

* `input_data::Real[nu, nT]`: Input data matrix where rows are input signals and columns are time points

* `output_data::Real[ny, nT]`: Output data matrix where rows are output signals and columns are time points

#### Option 2: DyadTimeseries {#Option-2:-DyadTimeseries}

* `dataset`: A `DyadTimeseries` object containing the time series data

* `input_cols::String[nu]`: Names of input signal columns in the dataset

* `output_cols::String[ny]`: Names of output signal columns in the dataset

### Required Parameters {#Required-Parameters}

* `Ts::Real`: Sample time of the data (must match actual sampling rate). Only periodic sampled data is supported.

### Identification Method Parameters {#Identification-Method-Parameters}

* `method::String = "subspaceid"`: Identification method
  * `"subspaceid"`: Subspace identification (fast and easy)

  * `"newpem"`: Prediction error method (PEM, iterative optimization)

* `nx::Integer`: Model order (state dimension)
  * Set to a positive integer for fixed order

  * Set to `-1` for automatic selection (subspaceid only)

### Common Options {#Common-Options}

* `simulation_focus::Boolean = false`: Optimization focus
  * `false`: Optimize for short-term prediction (default)

  * `true`: Optimize for open-loop simulation

* `stable::Boolean = false`: Enforce stability of the identified model

* `zeroD::Boolean = false`: Force feedthrough matrix $D$ to zero (no direct input-output path)

* `detrend::Boolean = true`: Remove mean from data before identification

### Method-Specific Parameters {#Method-Specific-Parameters}

#### For `subspaceid`: {#For-subspaceid:}

* `r::Integer = 10`: Prediction horizon (affects accuracy and computation time)

* `W::String = "MOESP"`: Weighting method
  * `"MOESP"`: Multivariable Output-Error State Space

  * `"CVA"`: Canonical Variate Analysis

  * `"N4SID"`: Numerical Subspace State Space System Identification

  * `"IVM"`: Instrumental Variable Method

#### For `newpem`: {#For-newpem:}

* `h::Integer = 1`: Prediction horizon. Large values may lead to long computational times.

### Analysis Parameters {#Analysis-Parameters}

* `wl::Real = -1`: Lower frequency bound for Bode plot (set to -1 for automatic)

* `wu::Real = -1`: Upper frequency bound for Bode plot (set to -1 for automatic)

* `num_frequencies::Integer = 3000`: Number of frequency points for result analysis

* `duration::Real = -1`: Duration for step response in step-response artifact (set to -1 for automatic)

## Model Validation {#Model-Validation}

The identified model quality is assessed through the fit metrics and artifacts detailed below:

### Fit Percentage {#Fit-Percentage}

Measures how well the model predictions match the measured output:

$$\text{fit} = 100 \cdot \left(1 - \frac{||y - \hat{y}||}{||y - \bar{y}||}\right)$$

where $\hat{y}$ is the model prediction and $\bar{y}$ is the mean of $y$.

## Artifacts {#Artifacts}

A `SystemIdentificationAnalysis` returns the following artifacts:

### Plots {#Plots}

* `:bodeplot`: Frequency response of the identified model

* `:stepresponse`: Step response showing system dynamics

* `:pzmap`: Pole-zero map for stability and dynamics analysis

* `:predplot`: Comparison of model predictions vs measured data, uses feedback from measurements (observer)

* `:simplot`: Comparison of simulated vs measured output, similar to prediction but without measurement feedback

* `:residualplot`: Residual correlation analysis for validation

### Tables {#Tables}

* `:modelinfo`: DataFrame containing:
  * Model order (nx)

  * Number of inputs and outputs

  * Sample time

  * Fit percentage

  * Stability status

## Further Reading {#Further-Reading}

* [ControlSystemIdentification.jl Documentation](https://baggepinnen.github.io/ControlSystemIdentification.jl/stable/)

* [Prediction Error Methods (Wikipedia)](https://en.wikipedia.org/wiki/System_identification#Prediction_error_methods)

* [System identification and state estimation in Julia (Youtube playlist)](https://www.youtube.com/playlist?list=PLC0QOsNQS8ha6SwaNOZDXyG9Bj8MPbF-q)
