---
# System prepended metadata

title: DSP ass 5

---

# DSP ass 5

## Task 1

Simple virgin DFT:
```
function [output] = dft_my(data)
    N = length(data);
    for k = 0:N-1
        s = 0.;
        for n = 0:N-1
            s = s + data(n+1) * exp(-2 * %i * %pi * n * k / N);
        end
        output(k+1) = s;
    end
endfunction
```

Cool chad FFT (yeeey 1 bonus point so hard oof):
```
function [output] = fft_my(data)
    output = conj(fftReq(data)');
endfunction

function [output] = fft_req(data)
    N = length(data);
    if N == 1 then
        output = data;
    else
        data_even = fft_req(data(1:2:N));
        data_odd = fft_req(data(2:2:N));
        fact = exp(-2 * %i * %pi / N * (0:N-1));
        left = [data_even + fact(1:N/2) .* data_odd];
        right = [data_even + fact(N/2+1:N) .* data_odd];
        output = [left, right];
    end
endfunction
```

## Task 2

First lets create a component without leakage:

```
fm1 = 10;
fs1 = 125;
m1 = 2;
t1 = 0.0001 : 1/fs1 : 0.2;
x1 = cos(2 * %pi * fm1 * t1);
```
![](https://i.imgur.com/wvkHOEI.png)


Then create first component with spectral leakage. The difference with the leakageless component is that signal frequnecy is higher.
```
fm2 = 12;
fs2 = 125;
m2 = 2
t2 = 0.0001 : 1/fs2 : 0.2;
x2 = cos(2 * %pi * fm2 * t2)
```
![](https://i.imgur.com/KwpEFBB.png)

In the third component we increase frequency even more:
```
fm3 = 14;
fs3 = 125;
m3 = 2;
t3 = 0.0001 : 1/fs3 : 0.2;
x3 = cos(2 * %pi * fm3 * t3);
```
![](https://i.imgur.com/j93ea4P.png)

Now we can combine 3 components and get a signal with spectral leakage:
```
x = x1 + x2 + x3
```
![](https://i.imgur.com/UI3fUaP.png)


The idea is that increasing the amplitude of the signal we increase the magnitude in frequency domain. We don't see it here because I'm changing frequency, not amplitude.


## Task 3
For three signals above we now will calculate spectral interpolation.

For each signal we will plot abs padded signal and interpolation, where `padded_signal = resize_matrix(signal, 1, 64)` and `interpolation = abs(fft(padded_signal))`. Lets proceed:

Signal 1:
![](https://i.imgur.com/p7k20Y1.png)

Signal 2:
![](https://i.imgur.com/PEmlLqC.png)

Signal 3:
![](https://i.imgur.com/V4WLlZf.png)


The interesting thing here is that interpolations are different. As we increase frequency, interpolation decreases.


## Task 4

First lets create signals and plot them in time and frequency domains (abs(fft(signal)) just in case you forgot):

![](https://i.imgur.com/cxkRrd2.png)


We see that frequency domains are kinda the same. But that should not be the case, because signals are different (in amplitude and frequency).

The problem is in sampling frequency. As we know from Nyquist theorem: <i>sampling frequency should be atleast twice the highest frequency of the signal</i>. But in the first signal we have Fs = 200, while F = 190! Lets make Fs = 2F and see if it is correct:

![](https://i.imgur.com/aIJ9wuc.png)

Yeey, we are good.


## Code here:
https://github.com/fatawesome/dsp-ass5