# Correct way of derivating in frequency domain with FFT

I believe I am very close to the answer and only need a small nudge to get to the answer.

What I want:
I want to take a signal, use FFT to transform it to the frequency domain (FD), multiply it by $$jw$$, and then transform it back to the time domain (TD).

Why do I want it:
Because it's something that I should be able to solve in my position, I want to know how to do it properly. And because it would be fun to make a very low-end spectrum analyzer with an Arduino and instead of sending a pulse I can send a step instead, derive the output to get an impulse response. I know that I also can send square waves and use a Goertzel filter and look for the fundamental sine in the square wave, but... as I said earlier. I want to know how to do this properly.

What I've tried:
I thought I'd go for the simplest case first, a square wave and a triangle wave.

The fourier series for a square wave is: $$\text{square_wave}(x) = \sum_{n=0}^\infty\frac{\sin\biggl(x(2n+1)\biggr)}{2n+1}$$

The fourier series for a triangle wave is:

$$\text{triangle_wave}(x) = \sum_{n=0}^\infty\frac{\sin\biggl(x(2n+1)-\frac{\pi}{2}\biggr)}{(2n+1)^2}$$

The derivative of a triangle wave is a square wave, from the equation above it's clear that multiplying by $$jw$$ phase shifts each sine and amplifies each sine correctly to get rid of one $$(2n+1)$$ factor.

I know that the derivative of the 0 Hz component is 0, so in the FD I know all values that I need to multiply my signal with, except for the highest frequency component, but I'll set that one to 0 too because I don't know what it should be.

I also know that the frequency spectrum is mirrored around the middle for real valued inputs, and that the second half of the spectrum is just the conjugate of the first half.

This means that the vector I need to multiply the signal with in the FD for a 32 bin FFT looks something like this in matlab/octave code:

derivative = [0,i*1:15,0,-i*15:-1:1]
%0 = 0
%1:15 = 1,2...14,15
%15:-1:1 = 15,14...2,1

derived_signal = ifft(fft(signal).*derivative)


My make-a-sense-o-meter says that it makes sense, yet when I multiply my signal with it I get something that is very close to the derivative, but not 100% correct. I get a better result if I multiply by the FFT of [0,-1,zeros(1,29),1] which is a first order derivative approximation. See figures below for octave plots.

Here is the code I used in case anyone else wants to mess around:

triangle=[linspace(-1,1,16),linspace(1,-1,16)];

subplot(1,2,1)

plot(0:31,triangle)

xticks([0 7 15 23 31])
axis([-1 32 -1.1 1.1])

title("32 point triangle wave")
grid

subplot(1,2,2)
hold on

dt_jw=real(ifft(fft(triangle).*[0,i*(1:15),0,-i*(15:-1:1)]));
plot(0:31,dt_jw)

dt_convolution=real(ifft(fft(triangle).*fft([0,-1,zeros(1,29),1])));
plot(0:31,dt_convolution)

axis([-1 32 -1.1 1.1])
xticks([0 7 15 23 31])
yticks([1 dt_jw(floor(length(dt_jw)/4)) dt_convolution(floor(length(dt_jw)/4)) 0 -1])

title("d/dt(triangle)")
legend("fft(tri)*jw","convolution with [-1,0,1]")

grid
hold off


I feel like I'm very close to the answer and that I'm missing something very obvious.

So why does multiplying by $$jw$$ not derive my triangle wave into a nice square wave? Because so far it makes more sense to convolve my triangle wave by a 1st order derivative approximation.

• What do you mean by "nice" square wave? if you want to have it smoother than you should extend your signal (right now you are 32 samples, try it with more and see what happens), however, there is the limiting factor of the Gibbs phenomenon which will result in overshoots. – Irreducible Apr 29 at 7:50
• @Irreducible A "nice" square wave, being an actual square wave, like $n = a, a, a, a, -a, -a, -a, -a$, values that actually correspond to the derivative of the triangle wave. where a is the amplitude. - At $n=13$ there is a tip at the $jw$ square wave, this value is oscillating, like the Gibbs phenomenon, while the triangle wave's derivative is not. – Harry Svensson Apr 29 at 8:12
• I found this paper very helpful as it compares different first derivatives. It includes both approaches and explains how they relate to each other. As far as I understood it, your first order approximation is similar to a tapered version with 3 coefficients which results in a different frequency response – Irreducible Apr 29 at 11:21
• @Irreducible Woaw, that's very interesting. It's kind of my entire question + it's answer in a paper. So I suppose my question required some more research. – Harry Svensson Apr 29 at 18:04