untode.su

Discrete Fourier Transform in Desmos

If you are not familiar with changing a signal’s domain from time to frequency, there’s plenty of information available online! Just click the right links!

NOTE: all the Desmos plots are clickable! If you want to see what happens if you change something, feel free to open the plot and fiddle with it!

Lets define our signal

As one may know, a discrete signal is basically an array of numbers, containing only bits of information that that were sampled at fixed time intervals. Lets define how that continious signal was converted into a discrete array:

\[s_{n}=s\left(\frac{n}{f_{s}}\right)\]

Now, what the hell does any of that mean? Let’s go through the formula step by step:

  1. The \(s_{n}\) is the \(n\)-th sample, \(n\) starts from zero as it’s an array index.

  2. The \(s\left(t\right)\) is the continious signal, it has value all the time, even between sample periods. In practice, this is the signal that is goes through, say tour microphone cable.

  3. The \(\frac{n}{f_{s}}\) defines the time value at which the signal is sampled. The \(f_{s}\) is the sample rate, it means how fast the sampling process is going.

Lets use a simple cosine wave signal as an example:

\[s\left(t\right)=\cos\left(2\pi f_{0}t\right)\]

Lets plot the continious signal alongside discrete. Continious should look like an envelope filled with vertical lines meaning each sample’s height. Let’s have a look:

Shifting gears into the frequency domain

Okay, well, how do you then turn this into a frequency spectrum? The answer is in the title: you perform a little magic ritual called Fourier Transform:

\[\dot{S}_{m}=\sum_{n=0}^{N-1}s_{n}\cdot\exp\left[-j\cdot\frac{2\pi mn}{N}\right]\]

Again, don’t be scared of all the math stuff, we’ll go through it step by step as well:

  1. The \(\dot{S}_{m}\) is the \(m\)-th sample of the signal in frequency domain. The \(m\) isn’t that different from \(n\), we just need to define different domains with different letters.

  2. The \(\exp\left[-j\cdot\frac{2\pi mn}{N}\right]\) is the Euler’s formula, we use it in this context to find out how much a specific frequency contributes to the signal’s shape.

Not so complex numbers

Desmos cannot handle complex numbers, but knowing how Euler’s formula is defined, we can split the formula into two arrays:

\[Re\left[\dot{S}_{m}\right]=\sum_{n=0}^{N-1}s_{n}\cdot\cos\left[\frac{2\pi mn}{N}\right]\]

\[Im\left[\dot{S}_{m}\right]=-1\cdot\sum_{n=0}^{N-1}s_{n}\cdot\sin\left[\frac{2\pi mn}{N}\right]\]

Now, we can combine the two and get just amplitude of each frequency by using a little bit of 8th grade geometry:

\[S_{m}=\sqrt{Re^2\left[\dot{S}_{m}\right]+Im^2\left[\dot{S}_{m}\right]}\]

Let’s plot that spectrum and see what it looks like:

Now would you look at that! It looks exactly how a real DFT spectrum would look like!

Making sure it’s real

Lets simulate spectral leakage. To do that we have to set the signal’s frequency to a value that doesn’t work well with other settings:

Now that the frequency value is set to something that really doesn’t want to cooperate with DFT, lets take a look how did the graph change:

That certainly looks like spectral leakage to me!

Common signals

Lets plot some commonly known signals in frequency domain:

  1. Square wave:

    \[s\left(t\right)=sign\left[\cos\left(2\pi f_{0}t\right)\right]\]

  2. Triangle wave:

    \[s\left(t\right)=2\cdot\frac{\arccos\left[\cos\left(2\pi f_{0}t\right)\right]}{\pi}-1\]

  3. Sawtooth wave:

    \[s\left(t\right)=2\cdot\left[\left(f_{0}\cdot t\right)\bmod 1\right]-1\]

Conclusion

So I guess you really can perform DFT in Desmos. It’s quite probable that you also can do other kinds of DSP (digital signal processing) there, I just haven’t tried it yet, but I sure would! Just, y’know, not right now!