# Lab 6. Non-Linear Transformations
## Content
[TOC]
## Problem description
Up until this point, we have been interested only in **linear shift-invariant (LSI)** transformations.They possess many nice properties, which makes it easy to work with them from a mathematical perspective. Some physical phenomena, including propagation of the sound, are well approximated as LSI systems. However, there are many cases when non linear effects are desired. For instance, electric guitarists frequently and intentionally apply various techniques to add non-linearity to their sound. Entire music genres are based on **non-linear** effects. In this assignment, we will study two such filters, namely **clipping** and **distortion**.
We have a [sample track](https://voca.ro/cGZafm1peng) that will be used to test the non-linear effects.
## Solution
### Clipping
This filter introduces a certain threshold $\alpha$, which limits the amplitude of the original signal. All amplitudes above or below this threshold are clipped, i.e., replaced with $\alpha$.

Let's implement it in scilab programming language.
```scilab
function transformed = clip(signal, alphA)
transformed = signal
mask = abs(transformed) > alphA
transformed(mask == %t) = sign(transformed(mask == %t)) .* alphA
endfunction
```
After application of this filter we got the music with rock&roll effect.
It grows as $\alpha$ goes to zero and otherwise.
Let's stop on the [best one](https://vocaroo.com/bY8npls22JU). (A matter of taste ($\alpha = 0.1$)).
### Distortion
There are many ways to introduce distortion into a signal, but we chose to use the $atan$ function.
Let's check what is an $atan$ function:

This function reguralizes the signal but does it softly.

It is how it looks like in the code:
```scilab
function transformed = distort(signal, alphA, betA)
transformed = alphA .* atan(betA .* signal)
endfunction
```
Here $\alpha$ is a constant that will control the amplitude and consequently the volume of the output.
$\beta$ is a gain constant that will change the amount of distortion.
$\alpha$ parameter just changes an amplitude but $\beta$ is exactly about "rock&rollness". It grows as $\beta$ grows and otherwise. Let's stop on the [most pleasant](https://vocaroo.com/6kkQT2N1RVj). ($\alpha = 0.1 , \beta= 30$)
### Comparison
Let's investigate effects of **clipping** and **distortion** in temporal and in spectral domains.

In the time domain:
- For **clipping** $\alpha$ strictly assigns the amplitude boundaries but for **distortion** not.
- **distorted** signal is more averaged across both axis (more dense).
In the frequency spectrum:
- frequency difference form is more square in **clipping** case
- frequency difference form is more paraboloidal in **distortion** case
## Bonus Task
Linearity condition:
$$y(w1 * x1 + w2 * x2) = w1 * y(x1) + w2 * y(x2)$$
But for prove NON-linearity we need just to provide at least one example when this condition breaks.
Let's try to apply tree filters:
- $y(x) = 2 * x$
- $y(x) = clip(x, 0.1)$
- $y(x) = distort(x, 0.1, 30)$
If plots are not overlapped then filter is not linear:

## Conclusion
In this assignment I tried to apply non-linear filters to sample track. They can effect on the time and frequency domains of the signal. Also I investigated an approach to check non-lineary of the signal and got some interesting plots.
I had some problems with scilab plotting functions because of worst documentation but finally I solved it.
I think my solution can be improved with additional testing of another non-linear filter.
## Self-check questions
### What is the definition of an LSI system?
$$T(w1 * x1 + w2 * x2) = w1 * T(x1) + w2 * T(x2)$$
and
$$T(x1 + t1) = y(x1 + t1)$$