# FM demodulation with arctan

I am trying to demodulate a FM signal with a RTL-SDR and python using the arctan method. From here I know that $$\frac{d}{dt}\arctan(\frac{q(t)}{i(t)}) = \frac{i(t)\frac{d}{dt}q(t)-q(t)\frac{d}{dt}i(t)}{[i(t)]^2}$$

and so far I have successfully been able to demodulate using the RHS and the following python code

 def discrim(x): X=np.real(x) # X is the real part of the received signal Y=np.imag(x) # Y is the imaginary part of the received signal b=np.array([1, -1]) # filter coefficients for discrete derivative a=np.array([1, 0]) # filter coefficients for discrete derivative derY=signal.lfilter(b,a,Y) # derivative of Y, derX=signal.lfilter(b,a,X) # " X, disdata=(X*derY-Y*derX)/(X**2+Y**2) return disdata 

However I would also like to get the same results using the LHS and naively assumed that python code as follows would do that

 def discrim_bad(x): X=np.real(x) # X is the real part of the received signal Y=np.imag(x) # Y is the imaginary part of the received signal b=np.array([1, -1]) # filter coefficients for discrete derivative a=np.array([1, 0]) # filter coefficients for discrete derivative x = np.arctan2(Y,X) der=signal.lfilter(b,a,x) # derivative return der 

Unfortunately this is not the case. I assume it has something to do with the discretization and most likely the discrete derivative approximations but I am not quite able to figure it out.

The $\arctan2()$ function has a jump at $\pm\pi$ which will cause problems when trying to compute a derivative.

Since you're using the first difference discrete derivative approximation to compute $d\phi/dt$, there's a simple way to avoid the jumps.

First note that your signal samples can be written polar form:

$$s[n] = i[n]+jq[n] = r[n]\cdot e^{\phi[n]}$$

so you can use multiplication by a complex conjugate to perfom angle subtraction before taking the $\arctan2()$:

$$s[n]\cdot s^*[n-1] = (i[n] +jq[n])(i[n-1]-jq[n-1]) = r[n]e^{\phi[n]}r[n-1]e^{-\phi[n-1]}=r[n] r[n-1] e^{\phi[n]-\phi[n-1]}$$

Thus, to get the discrete derivative you want, just take:

$$\mathrm{Arg}[(i[n]+jq[n])(i[n-1]-jq[n-1])]$$