# In-phase and Quadrature demodulation

I'm new with signal processing and have started to learn it for physics research I am involved in. I have a signal which I want to process to find the amplitude of one frequency of. To do this, I'm multiplying the signal numerically by sin(2πft) and cos(2πft), so Y = signal * sin(2πft), X = signal * cos(2πft). then taking the square root of the sum of these squared for the amplitude and arctan of Y/X for the phase over time. What does the plot of arctan(Y/X) actually show me? For an example, I have a code in Python I'm using for testing this.

from matplotlib.pyplot import *
from numpy import *
from scipy.signal import butter, lfilter

def butter_lowpass_filter(data, cutoff, fs, order=5):
'''Creates a lowpass filter for the data'''
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False) #filters data
y = lfilter(b, a, data) #filters data
return y

def sin_stat(time, f0, method = 'sin'):
'''Creates sine and cosine data to be used for manipulating data'''
if method == 'sin':
return [2.0 * np.sin(2*pi*f0 * t) for t in time] #2 in front to cancel out 1/2 later
if method == 'cos':
return [2.0 * np.cos(2*pi*f0 * t) for t in time]

def stats(time, signal,f0,sample_freq, cutoff = 1): #cutoff is in hz
''' first creates list of sine and cosine data to numerically multiply all
data by. Then Makes X, Y, which are data times cosine data and sine data
respectively. '''
C = sin_stat(time, f0, method = 'cos')
S = sin_stat(time, f0, method = 'sin')

X = [d * c for d,c in zip(signal,C)]
Y = [d * s for d,s in zip(signal,S)]

X_low = butter_lowpass_filter(X, cutoff, sample_freq) #lowpass each
Y_low = butter_lowpass_filter(Y, cutoff, sample_freq)

amp = [np.sqrt(x**2 + y**2) for x,y in zip(X_low, Y_low)]
phase = [np.arctan(y/x) for y,x in zip(Y_low,X_low)]

phase = [2 * (x + np.pi/2) for x in phase]
phase = np.unwrap(phase)
phase = [x/2.0 for x in phase]
return (amp,phase)

sample_freq = 100 #hz
end = 1000
t = linspace(0,end,end*sample_freq)
s = 5*sin(2*pi*10*t + 2) + 2*sin(2*pi*7*t + 1) + sin(2*pi*4*t +.5) + .2*sin(2*pi*2.3*t+.7)
plot(t,s)
show()
close()
amp, phase = stats(t,s,2.3,sample_freq,.5)
plot(t,amp)
show()
close()
plot(t,phase)
show()
close()


The amplitude plot shows the amplitude converge to the amplitude I define in the signal, but the phase plot does not. If my guess for the frequency in my In-phase and Quadrature signals is the same as in the simulated signal with a low-pass cutoff of .5 hz, I receive this: However, if I change the guess frequency to something not exactly the frequency, say 2.4 Hz, I receive this: Could someone explain what I am actually seeing? What does the slope of this represent? In my real data, I always have a slope of some sort due to not knowing the exact frequency of what I am looking for, only an approximation. I would like to calculate frequency vs time to perform an allan deviation plot, but am unsure of how to calculate frequency from this. I feel I am on the right track, but am a little confused. I've been using this for understanding a little of what I'm doing: http://www.thinksrs.com/downloads/PDFs/ApplicationNotes/AboutLIAs.pdf

• The second plot has a title mentioning the same guess frequency as the first plot, I guess it's a typo ? – Loufylouf Jun 3 '15 at 13:22
• Oops, yeah that was supposed to be 2.4. I'll fix it – TheStrangeQuark Jun 3 '15 at 13:58

## 1 Answer

Changes in frequency are equivalent to a linear phase sweep, so you're seeing exactly what you'd expect. Consider a reference sin wave with frequency $\omega_1$, and a signal with frequency $\omega_2=\omega_1+\Delta\omega$. Well, \begin{align} \sin\omega_2 t &= \sin((\omega_1 + \Delta\omega)t) \\ &= \sin(\omega_1t+\Delta\omega t) \\ &= \sin(\omega_1 t+\phi(t)) \end{align} Where $\phi(t) = \Delta\omega t$. This is the line you're seeing in the unwrapped phase.

So if you do lock in at $\omega_1$, your're going to pick up that frequency component, but the frequency offset will be seen in the phase varying linearly with time.