This commit is contained in:
Christian Risi 2025-01-14 19:14:22 +01:00
parent be4ef59b42
commit df2d8f8f29
12 changed files with 8260 additions and 1798 deletions

View File

@ -0,0 +1,17 @@
# Sensitivity
Whenever we introduce our reference input,
we should check the associated [Sensitivity Function](./../Formularies/CONTROL-FORMULARY.md/#sensitivity-function).
<!-- TODO: Add graphs here-->
As we can see, a reference put
just after the output og $G_p(s)$
yealds a more robust system, overall.
# Augmented state
Each time we need to make the system
faster, we need an `integrator`.
In order to being able to place it, we just
need to augment the state. By doing that
we introduce some ***helping***
`state variables` and the rest is as it is.

View File

@ -0,0 +1,192 @@
# Congestion Avoidance
Internet can be represented as a huge number
of links and buffers, where each buffer is
technically a `router`.
Each time we send a packet, the `time` it
gets to go from `Point S` to `Point D` is
called `One Way Delay` or `Forward Delay`
$T_{fw}$.
From `Point D` to `Point S` is
called `Backward Delay` $T_{bw}$
The `Round Trip Time` (`RTT`) is:
$RTT = T_{fw} + T_{bw}$
## Phil Karn Algorithm
To get an RTT estimation, we sum all the
delays introduced to physically transfer
packets $T_{pi}$ and the queue time of those
$T_{qi}$ on each node $N_{i}$.
$$
RTT = \sum_i
\underbrace{T_{pi}}_{
\text{propagation delay}
} +
\sum_i \underbrace{T_{qi}}_{
\text{queueing delay}
}
$$
While we can't control any $T_{pi}$ as it is
a physical constraint, we can control $T_{qi}$
## Internet Delay Model
<!-- TODO: Add image here-->
### Variables
- $Bw_Delay = e^{-sT_{bw}}$ : Backward Delay
- $Fw_Delay = e^{-sT_{fw}}$ : Forward Delay
- $q(t)$ : receiving data $bits$
- $d(t)$ : disturbance or draining of the buffer $\frac{bits}{s}$
- $AdW$ : Advertised Window (AKA free buffer space)
- $G_c(s)$ : Controller that checks how much data can be sent to the receiver
### Control
In order to control this, we need to model a Smith Predictor,
working without delay first and then equaling the two systems
later.
## TCP Models
TCP was modeled in order to be a `Greedy Protocol`, thus using as much as bandwidth
it could grab. In order to have congestions (when a flow can't be transmitted)
TCP had to be controlled. The difficulty in controlling such system lies in the
inability to know many variables such as intermediate buffers.
Tee way we control TCP has several `flavours` like:
- TCP Tahoe
- TCP Reno
- TCP New Reno
- TCP Santa Cruz
- TCP New Vegas
- TCP Westwood+
### TCP Tahoe
This `flavour` has 2 phases to control the single TCP flow.
It introduces concepts used in many other `flavours`:
- $cwndw$ : Congestion Window | If you send data more than this window, you'll likely
end up having a congestion and lose packets
- $t_k$ : Time when you received $k$ RTTs
- $sstresh$: Slow Start Treshold | after this value, we need to avoid having a
congestion and so we increase the $cwndw$ linearly
#### Slow Start (SS)
This is the fastest phase as it is exponential.
Each time all packets sent are successfully received, the $cwndw$ is exponentially increased:
$$
cwndw = cwndw(k - 1) * 2 = cwndw(0) * 2^k
$$
Or in other words, on each successful `ACK` :
$$
cwndw_{n} = cwndw_{n-1} + 1
$$
#### Congestion Avoidance
We are in this phase once $cwndw \geq sshtresh$. This phase `probes`
(check the max capacity) the available bandwidth.
Here on each successful `ACK`:
$$
cwndw_{n} = cwndw_{n-1} + \frac{1}{cwndw_{n-1}}
$$
Once we receive a `3DUPACK` or an `RTO`, we get that the bandwidth is somewhere
full and we are losing packets.
#### Congestion Phase
Eithe rwe receive a `3DUPACK` or an `RTO`:
- $sstresh = cwndw / 2$
- $cwndw = 2$
### TCP Reno / New Reno[^tcp-new-reno-freebsd]
They differ from TCP Tahoe only on the Congestion Phase
#### `3DUPACK` Congestion
- $sstresh = cwndw / 2$
- $cwndw = sshtresh$
#### `RTO` Congestion
- $sstresh = cwndw / 2$
- $cwndw = 2$
#### TCP New Reno Throughput Formula (Tuenis Ott)
$$
\frac{W}{2}\frac{W}{2} + \frac{1}{2}\frac{W}{2}\frac{W}{2} =
\frac{3}{8}W^2
$$
This formula is equal to how much $window$ is acked before a loss.
Therefore,
$
\frac{3}{8}W^2 = \frac{1}{p} \leftrightarrow W = \frac{2\sqrt{2}}{\sqrt{3p}}
$
### TCP Westwood
This algorithm tries to estimate the available bandwidth without blindly `probing`:
$$
bw_k = \frac{
acked_{k} - acked_{k-1}
}{
t_{k} - t_{k-1}
} = \frac{data_k}{time\_interval}
$$
The thing is that if you try to sample in such a short period, you'll
have spikes that can't be filtered because of the high frequency
components.
So, we should sample at $f_c \geq 2f_{max}$ as per Nyquist/Shannon
Theorem:
$$
\begin{cases}
bw_k = \frac{
acked_{k} - acked_{k-1}
}{
t_{k} - t_{k-1}
} = \frac{data_k}{time\_interval} \\
\hat{bw}_k = \alpha bw_k + (1 - \alpha) \hat{bw}_{k-1} \rightarrow
\text{this is also called moving average}
\end{cases} \\
cwndw = \hat{bw}_k RTT_{min} \rightarrow
\text{we are assuming $T_q = 0$}
$$
#### `3DUPACK` Congestion
- $sstresh = \hat{bw}_k RTT_{min}$
- $cwndw = sshtresh$
#### `RTO` Congestion
- $sstresh = \hat{bw}_k RTT_{min}$
- $cwndw = 2$
### TCP Cubic
This method has many differences with all preceding ones[^tcp-cubic-linux].
Here this algorithm is `real-time` dependent and not `RTT` one.
This algorithm is slow at start, after $t=K$ it becomes exponential and
`probes` the bandwidth
#### Parameters
- $\beta$ : Multiplicative decrease factor | Set to $0.7$, $0.75$, $0.8$
- $W_{max}$ : Windows size `right-before` its last reduction
- $T$ : Time elapsed since last reduction
- $C$ : Scaling constant | Set to $0.4$
- $cwndw$: Congestion Window at current time
# Congestion
- $cwndw = C(t-k)^3+ W_{max}$
- $K = \sqrt[3]{\frac{(1 -\beta) W_{max}}{C}}$
## See also
- Statistical Multiplexing
- Congestion Avoidance: Van Jacobson ACM CCR 2004
[^tcp-new-reno-freebsd]: TCP New Reno is used as the default algorithm on FreeBSD
[^tcp-cubic-linux]: TCP Cubic is used as the default in Linux

50
docs/Chapters/EXTRAS.md Normal file
View File

@ -0,0 +1,50 @@
# Extras
## WebRTC
Read GCC
## PageRank
Whenever we need to search for a result on Google, this one
of the most famous algorithm used.
First we take some pages and all the links available
on that page linking to the other gathered pages:
```bash
p1, p2, p3, p4, ..., pn
# Links found on the page
p1:
- a1 -> p2
- a3 -> p4
...
- al -> p8
p2:
...
```
Then we make an oriented graph where all the `points` are pages
and links are `arrows` pointing to the ***landing page***
### Variables
- $B_p$ : Set of pages that backlinks to $p$
- $|a|$ : Cardinality of $a$ | Number of outgoing links from
$a$
### Formula
$$
rank_k(p) = \sum_{a \in B_p}\frac{
rank_{k-1}(a)
}{
|a|
} \in [0, \dots, 1]
$$
Here the state is composed of all ranks for our gathered pages.
If $| rank_k(p) - rank_{k-1}(p)| < \epsilon$ we will stop
iterating.
At time $k = 0$ all pages have the same importance that is
$rank_0(p) = \frac{1}{n}$

View File

@ -0,0 +1,222 @@
# Kalman Filter
Until now, we dealed with ***ideal*** conditions, such as the ***complete
absence of noise***.
Let's now put ourselves in in this position:
$$
\begin{cases}
\dot{x}(t) = Ax(t) + Bu(t) + Gw(t) \\
y(t) = C{x}(t) + Du(t) + v(t)
\end{cases}
$$
- $w(t)$ : Process Noise
- $v(t)$ : Measurement Noise
## Analyzing the noise[^unimore]
Usually the Expectation of Gaussian Nose is $0$, but if that's not the case,
we just need to shift coordinates.
> [!NOTE]
> $w_k$ and $v_k$ are $w(t)$ and $v(t)$ with $t = k$
>
> [!TIP]
> if $E[X] = 0$ and $E[Y] = 0$, since $Cov(X,Y) = E[(X - E[X])(Y - E[Y])]$,
> then $Cov(X,Y) = E[XY]$
- $E[w_k] = E[v_k] = 0 \rightarrow$ If this is not true, we can shift
coordinates
- $E[w_k w_q^T] = \begin{cases}R_w\; \text{if} \;q = k \\0\; \text{if} \;q \neq k\end{cases}$
- $E[v_k v_q^T] = \begin{cases}R_v\; \text{if} \;q = k \\0\; \text{if} \;q \neq k\end{cases}$
- $E[w_k v_q^T] = 0 \;\forall k,q$
In particular, $v_k$ and $w_k$ are incorrelated between
themselves, their states in time and the initial state
$x(0)$
Since our state is affected by noise, we make an estimation of it, and it will be affected by an error:
$$
\hat{x}_k = x_k + e_k \leftrightarrow e_k = \hat{x}_k - x_k
$$
The error is measured as $e_k^T e_k = \sum_i e_i^2 \in \R$
Now we define some extra errors. The `a priori` happens
before measuring the output, the `a posteriori` happens
just right after:
$$
\begin{align*}
e_k^+ &= \hat{x}_k^+ - x_k = e_k\rightarrow
\text{error a posteriori} \\
e_k^- &= \hat{x}_k^- - x_k = \epsilon_k \rightarrow
\text{error a priori} \\
\hat{x}_k^+ &= \hat{x}_k^- - L_k(C\hat{x}_k^- - y_k)
\rightarrow \text{Observed value} \\
\end{align*}
$$
Now, let's expand the formula:
$$
\begin{align*}
e_k^+ &= e_k = \hat{x}_k^+ - x_k = \\
&= \hat{x}_k^- - L_k(C\hat{x}_k^- - y_k) - x_k = \\
&= \hat{x}_k^- - x_k - L_k(C\hat{x}_k^- - y_k) = \\
&= \epsilon_k - L_k(C\hat{x}_k^- - y_k) = \\
&= \epsilon_k - L_k(C\hat{x}_k^- - Cx_k -v_k) = \\
&= \epsilon_k - L_k(C(\epsilon_k) -v_k) = \\
&= \epsilon_k - L_k C \epsilon_k -Lv_k = \\
&= \epsilon_k - L_k C \epsilon_k -Lv_k = \\
&= (I - L_k C\textcolor{red}{_k} )\epsilon_k -L
\textcolor{red}{_k}v_k
\end{align*} \\
\text{Now let's compute } Cov(e_k^+)\\
\begin{align*}
Cov(e_k^+) &= E[e_k e_k^T] = \\
&= E \{[(I - L_k C_k )\epsilon_k -L_kv_k] [(I - L_k C_k )
\epsilon_k -L_kv_k ]^T\} = \\
&= E \{[(I - L_k C_k )\epsilon_k -L_kv_k]
[\epsilon_k^T (I - L_k C_k )^T -v_k^T L_k^T ]\} = \\
&= E [
(I - L_k C_k )\epsilon_k \epsilon_k^T (I - L_k C_k )^T -
(I - L_k C_k )\epsilon_k v_k^T L_k^T -
L_k v_k \epsilon_k^T (I - L_k C_k )^T +
L_k v_k v_k^T L_k^T
]\rightarrow \\
&\text{Noises and errors are not correlated } \\
&E[
(I - L_k C_k )\epsilon_k \epsilon_k^T (I - L_k C_k )^T
] = (I - L_kC_k) P_k^- (I - L_kC_k)^T\\
&-E[
(I - L_k C_k )\epsilon_k v_k^T L_k^T
] = 0 \rightarrow
\text{
Remember that only $\epsilon_k$ and $v_k$ are random variables
}\\
&-E[
L_k v_k \epsilon_k^T (I - L_k C_k )^T
] = 0 \rightarrow
\text{
Remember that only $\epsilon_k$ and $v_k$ are random variables
}\\
&E[
L_k v_k v_k^T L_k^T
] = L_k R_v L_k^T \rightarrow \\
P_k^+ &= (I - L_kC_k) P_k^- (I - L_kC_k)^T + L_k R_v L_k^T
\end{align*}
$$
We can see that our cost is
$E[e_k^T e_k] = \sum_i e_i^2 = trace(P_k^+)$.
To minimize this we derive over $L_k$ and equal to $0$, as this will be our
`minimum point` (assuming `Hessian` defined ***semi-positive***)
$$
\begin{align*}
\frac{d\,tr(P_k^+)}{dL_k} &= -2(I - L_kC_k)P_k^- C_k + 2L_kR_v
= 0 \rightarrow\\
&\rightarrow (-I +L_kC_k)P_k^- C_k^T + L_kR_v =0 \rightarrow\\
&\rightarrow -P_k^- C_k^T +L_kC_k P_k^- C_k^T + L_kR_v =0 \rightarrow\\
&\rightarrow L_kC_k P_k^- C_k^T + L_kR_v = + P_k^- C_k^T\rightarrow\\
&\rightarrow L_k (C_k P_k^- C_k^T + R_v) = P_k^- C_k^T\rightarrow\\
&\rightarrow L_k = P_k^- C_k^T
(\underbrace{C_k P_k^- C_k^T + R_v}_{S_k})^{-1}
\end{align*}
$$
Substitute for this minimum value:
> [!WARNING]
> You just need to expand this equationto get to the final solution
$$
\begin{align*}
P_k^+ &= (I - L_kC_k) P_k^- (I - L_kC_k)^T + L_k R_v L_k^T = \\
&= (I - C_kP_k^- C_k^T S_k^{-1}) P_k^- (I - P_k^- C_k^T S_k^{-1}C_k)^T + P_k^-
C_k^T S_k^{-1}
R_v (P_k^- C_k^T S_k^{-1})^T = \\
&= (P_k^- - C_kP_k^- C_k^T S_k^{-1}P_k^-) (I - P_k^- C_k^T S_k^{-1}C_k)^T + P_k^-
C_k^T S_k^{-1}
R_v (P_k^- C_k^T S_k^{-1})^T= \\
&= \dots = (I - L_kC_K)P_k^- \rightarrow \\
\rightarrow L_kC_kP_k^- &= P_k^- - P_k^+ \rightarrow \\
\rightarrow
L_kC_kP_k^-C_k^T &= (P_k^- - P_k^+)C_k^T =- L_kR_v + P_k^-C_k^T
\rightarrow \\
\rightarrow P_k^+C_k^T &= L_kR_v \rightarrow \\
\rightarrow L_k &= P_k^+C_k^TR_v^{-1}\\
&\dots \\
P_K^- &= AP_{k-1}^+A^T + GR_wG^T
\end{align*}
$$
### The actual Algorithm
0. $P_0^- = P_0$
Loop:
1. $P_k^+ = P_k^- - P_k^-C^T_k(C_kP^-_kC_k^T + R_v)^{-1}C_kP_k^-$
2. $\hat{x}_k^+ = \hat{x}_k^- + P_k^+C^T_kR_v^{-1}(y_k - C_k\hat{x}_k^-)$
3. $\hat{x}_{k+1}^- = A\hat{x}_k^+ + Bu_k$
4. $P_{k+1}^- = AP_k^+A^T + GR_wG^T$
[^unimore]: [Unimore | 14 January 2025](http://www.dii.unimore.it/~lbiagiotti/MaterialeTSC1516/TSC-03-KalmanFilter.pdf)

View File

@ -0,0 +1,172 @@
# Video Streaming
Nowadays video streaming works by downloading
$n$ chunks of a video $v$ at different qualities $l_{i}$.
In order to model a device reproducing a video, we can consider the device
having a `buffer` that has a dimension $b_{max}$. Whenever we play the video
we are removing content from the `buffer`, making space for new chunks.
When the `buffer` is full, we don't donwload new chunks. When it's empty the
reproduction will stop.
Our objective is to keep the `buffer size` in this range:
$$
0 < b < b_{max}
$$
## Downard Spiraling
The reason why we should never get our buffer full is as we will stop
using bandwidth. This means that if there's another `greedy flow` going
on, it will perceive an increase in bandwidth, getting more and more of it.
This means that our video streaming will, on the contrary, perceive a lower
bandwidth and the quality will drop.
## Existing Protocols to Control Buffer Size
### DASH (Dynamic Adaptive Streaming over Http)
### HLS (Http Live Streaming)
Made by Apple, have a length of chunks equal to $10s$
## Controlling Video Streaming
First of all, we must say that Video Streaming should be controlled
with a controller ***built-on-top*** of the TCP controller, as the TCP
controller itself was ***built-on-top*** the IP controller, etc...
This is a generic scheme:
![video streaming model](./../Images/Video-Streaming/buffer.png)
Our Player buffer must be measured in **seconds!!!**
$$
\begin{align*}
r(t) &= \frac{
\Delta D
}{
\Delta t
} = \frac {
\text{data\_quality}
}{
\text{time\_interval}
} bps\\
\Delta t_f &= \text{frame\_time} \\
l(t) &= \frac{
\Delta D
}{
\Delta t_f
}bps = \text{coded\_movie} \\
t &= \text{real\_time} \\
t_f &= \text{move\_time} \\
\frac{
\Delta t_f
} {
\Delta t
} &= \left\{ 1, < 1, > 1\right\} \rightarrow
\text{When we 1.5x or 0.5x on video} \\
\dot{t}_f &= \frac{dt_f}{dt}
\end{align*}
$$
From here we have:
$$
\begin{align*}
r(t) &= \frac{
\Delta D
}{
\Delta t
} = \frac{
\Delta D
}{
\Delta t
}
\frac{
\Delta t_f
}{
\Delta t_f
} \\
&= \frac{
\Delta D
}{
\Delta t_f
}
\frac{
\Delta t_f
}{
\Delta t
} \\
&= l(t)\dot{t}_f \\
\dot{t}_f &= \frac{r(t)}{l(t)}
\end{align*}
$$
Now, let's suppose we are reproducing the video:
$$
\dot{t}_f = \frac{r(t)}{l(t)} -o(t) \rightarrow \text{$o(t) = 1$ 1s to see 1s}
$$
> [!TIP]
> $o(t)$ is just how many seconds we are watching per seconds. It basically
> depends on the multiplier of speed we put on the video:
> ```
> 0.5x -> 0.5s of video seen per second
> 2x -> 2s of video seen per second
> ...
> ```
### Feedback Linearization
Whenever we have a `non-linear-system` we can substitute the `input` for
a function dependent on the `state`
$$
\begin{align*}
\dot{x} &= f(x,u) \\
\dot{x} &= Ax + Bu(x)
\end{align*}
$$
Coming back to our goal of controlling video streaming. We want to have a
set point for our system $t^s_f$. As usual, we get the error and, in this
case, we want to have a 0 position error over the input, so we add an
integrator:
$$
t_I = \int^t_{- \infty} t^s_f - t_f(\tau) d\tau
$$
Because we have integrated, we are augmenting our state space
$$
\begin{align*}
x &= \begin{bmatrix}
t_f = \frac{r(t)}{l(t)} - o(t)\\
t_I
\end{bmatrix} && \text{as we can see, $t_f$ is non linear} \\
\end{align*}
$$
Because $t_f$ is non linear, we must find a way to linearize it[^feedback-linearization]:
$$
\frac{r(t)}{l(t)} - o(t) = k_pt_f + k_It_I \rightarrow \\
l(t) = \frac{
r(t)
}{
o(t) + k_pt_f + k_It_I
}
$$
Now, simply, this algorithm will take the video quality that is nearer $l(t)$
## See also
- Automatica 1999 c3lab
- Control Engineering Practice c3lab
[^feedback-linearization]: [Feedback Linearization | 13 January 2025](https://aleksandarhaber.com/introduction-to-feedback-linearization/#google_vignette)

View File

@ -32,4 +32,15 @@ $X = X_no \bigoplus X_{no}^{\perp} = X_no \bigoplus X_{o}$
> [!TIP]
> Since $X_{o} = X_no^{\perp}$ we can find a set of perpendicular
> vectors by finding $Ker(X_{no}^{T})$
> vectors by finding $Ker(X_{no}^{T})$
## Sensitivity Function
This function tells us how much `disturbances` in our system
affects our $G(s)$ (here $T$):\
$
S = \frac{dG}{dT} \frac{G}{T}
$
Once found $S$ we do a `Bode Plot` of it to
see how much we differ from our original
`system`

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 289 KiB