---
# System prepended metadata

title: Non-Linear Transformations
tags: [Non-linear, DSP, FFT, SciLab, Distortion, Clipping]

---

# Non-Linear Transformations
## Artemii Bykov, DS-01

###### tags: `DSP`, `SciLab`, `FFT`, `Clipping`, `Distortion`, `Non-linear`

[ToC]
### State the problem
In this task we will apply two filters: clipping and distortion to somehow add a new effect to origin signal.
### Task 1
This filter introduces a certain threshold, which limits the amplitude of the original signal. All amplitudes above or below this threshold are clipped, i.e., replaced with $\alpha$ (in our case $\alpha = 0.05$)
![](https://i.imgur.com/xYQ4avu.png)
```
clf
clear

fontsize = 3

function result = clipping_filter(x, alpha)
    result(1:length(x)) = 0
    for i=1:length(x)
        if abs(x(i)) > alpha then
            result(i) = alpha * sign(x(i))
        else
            result(i) = x(i)
        end
    end
endfunction

[s, Fs, _] = wavread("guitar.wav")
s = s(1, :)
s_len = length(s)
frequencies = (0:s_len-1)/s_len * Fs;


clipped_s = clipping_filter(s, 0.05)


figure(1),
subplot(221),
plot2d(1:s_len, s, color("blue"))
xlabel('Time T', 'fontsize', fontsize)
ylabel('Amplitude', 'fontsize', fontsize)
title('Origin track', 'fontsize', fontsize)
subplot(222)
plot2d(frequencies, log(abs(fft(s))), color("blue"))
xlabel('Frequency F', 'fontsize', fontsize)
ylabel('Magnitude', 'fontsize', fontsize)
title('FFT from origin track', 'fontsize', fontsize)


subplot(223),
plot2d(1:s_len, clipped_s, color("blue"))
xlabel('Time T', 'fontsize', fontsize)
ylabel('Amplitude', 'fontsize', fontsize)
title('Track after applying clipping filter', 'fontsize', fontsize)
subplot(224)
plot2d(frequencies, log(abs(fft(clipped_s))), color("blue"))
xlabel('Frequency F', 'fontsize', fontsize)
ylabel('Magnitude', 'fontsize', fontsize)
title('FFT from filtered track', 'fontsize', fontsize)

savewave('clipped_guitar.wav', clipped_s, Fs)
```
$$
\text{To proof filter is non-linear} \\
\text{we should just find a case when the main linear property does not comply} \\
\alpha y_{1}(t)+\beta y_{2}(t)=H\left\{\alpha x_{1}(t)+\beta x_{2}(t)\right\} \\
\text{Let`s take } \alpha x_1 > \text{threshold and}\ \beta x_2 < \text{threshold}, \text{but}\ x_1 < \text{threshold and}\ x_2 < \text{threshold}\\
clip(\alpha x_1 + \beta x_2)\stackrel{?}{=} \alpha clip(x_1) + \beta clip (x_2) \\
clip(\alpha x_1 + \beta x_2) = a + \beta x_2 \\
\alpha clip(x_1) + \beta clip (x_2) = \alpha x_1 + \beta x_2 \\
a + \beta x_2 \neq \alpha x_1 + \beta x_2 \\
\textbf{The clipping filter is non-linear}
$$
### Task 2
 The original signal is used as an input to this function and then multiplied by a gain factor. Here $\alpha$ ($\alpha = 5$) is a constant that will control the amplitude and consequently the volume of the output. $\beta$ ($\beta = 10$) is a gain constant that will change the amount of distortion.
![](https://i.imgur.com/y1ubreo.png)
```
clf
clear

fontsize = 3

function result = distortion_filter(x, alpha, beta)
    result(1:length(x)) = 0
    for i=1:length(x)
        result(i) = alpha *atan(beta * x(i))
    end
endfunction

[s, Fs, _] = wavread("guitar.wav")
s = s(1, :)
s_len = length(s)
frequencies = (0:s_len-1)/s_len * Fs;


distortion_s = distortion_filter(s, 5, 10)


figure(1),
subplot(221),
plot2d(1:s_len, s, color("blue"))
xlabel('Time T', 'fontsize', fontsize)
ylabel('Amplitude', 'fontsize', fontsize)
title('Origin track', 'fontsize', fontsize)
subplot(222)
plot2d(frequencies, log(abs(fft(s))), color("blue"))
xlabel('Frequency F', 'fontsize', fontsize)
ylabel('Log from magnitude', 'fontsize', fontsize)
title('FFT from origin track', 'fontsize', fontsize)


subplot(223),
plot2d(1:s_len, distortion_s, color("blue"))
xlabel('Time T', 'fontsize', fontsize)
ylabel('Amplitude', 'fontsize', fontsize)
title('Track after applying distortion filter', 'fontsize', fontsize)
subplot(224)
plot2d(frequencies, log(abs(fft(distortion_s))), color("blue"))
xlabel('Frequency F', 'fontsize', fontsize)
ylabel('Log from magnitude', 'fontsize', fontsize)
title('FFT from filtered track', 'fontsize', fontsize)

savewave('distortion_guitar.wav', distortion_s, Fs)

```
$$
\text{To proof filter is non-linear} \\
\text{we should just find a case when the main linear property does not comply} \\
\alpha y_{1}(t)+\beta y_{2}(t)=H\left\{\alpha x_{1}(t)+\beta x_{2}(t)\right\} \\
dist(x) = \alpha\ atan(\beta x) \\
dist(\alpha x_1 + \beta x_2)\stackrel{?}{=} \alpha dist(x_1) + \beta dist(x_2) \\
dist(\alpha x_1 + \beta x_2) = a\ atan(b (\alpha x_1 + \beta x_2)) \\
\alpha dist(x_1) + \beta dist(x_2) = \alpha a\ atan(bx_1) + \beta a\ atan(bx_2) \\
a\ atan(b (\alpha x_1 + \beta x_2)) \neq \alpha a\ atan(bx_1) + \beta a\ atan(bx_2) \\
\text{Because of arctan is non-linear function} \\
\textbf{Distortion filter is non-linear}
$$
### Conclusion
We can see that the both filters change the same range of frequencies. Moreover, distortion performs more smooth transition unlike clipping filter. Clipping filter performs hard cut 


