# Doing analysis using IQ demodulation in Python

I'm trying to doing an analysis using IQ demodulation. I'm doing this by taking a noisy signal which has a desired signal in it. The desired signal has a frequency that is almost stable, which I'm simulating to be 5 hz + a small amount of random fluctuations in instantaneous frequency. I'm then mixing it with a cosine and sine function of 5 hz, then applying a lowpass filter. Then I'm taking the square root of these two filtered signals squared to retrieve the amplitude, which I simulate to be decaying exponentially. I'm not sure where my problem is, but the amplitude I extract is not stable. I'm not sure if it is a misunderstanding of something fundamentally or a code problem, but I don't think it is a code problem. I am self-teaching some DSP, and would not consider myself an expert at all. Here is the code:

import numpy as np
from scipy.signal import butter, lfilter, freqz
import matplotlib.pyplot as plt

pi = np.pi

def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a

def butter_lowpass_filter(data, cutoff, fs, order=5):
b, a = butter_lowpass(cutoff, fs, order=order)
y = lfilter(b, a, data)
return y

t = np.linspace(0,20,20001)
sample_freq = 1/(t[1] - t[0])

f1 = [3 + .2*np.random.rand() for x in t]
Amp1 = np.exp(-2*t)
cos1 = Amp1*np.array([np.cos(2*pi*y*x) for y,x in zip(f1,t)])

f2 = [5 + 0.1*np.random.rand() for x in t]
Amp2 = 5*np.exp(-t)
cos2 = Amp2*np.array([np.cos(2*pi*y*x) for y,x in zip(f2,t)])

f3 = [8 + .2*np.random.rand() for x in t]
Amp3 = np.exp(-t/2)
cos3 = Amp3*np.array([np.cos(2*pi*y*x) for y,x in zip(f3,t)])

f4 = [.2 + .05*np.random.rand() for x in t]
Amp4 = 0.1
cos4 = Amp4*np.array([np.cos(2*pi*y*x) for y,x in zip(f4,t)])

noisy = (cos1+cos2+cos3+cos4)

cos_mix = np.cos(5*t)
sin_mix = np.sin(5*t)

X = noisy * cos_mix

Y = noisy * sin_mix

cutoff = .5

X_low = butter_lowpass_filter(X, cutoff, sample_freq)
Y_low = butter_lowpass_filter(Y, cutoff, sample_freq)

Amplitude = 2* np.sqrt(X_low**2 + Y_low**2)

plt.plot(t, Amplitude)

Which plots an image like this:

• I don't understand what you're trying to achieve, but I get the feeling that you haven't fully understood quadradure (de)modulation yet. I'd suggest reading (at least) chapter 5 of the book linked here. – MBaz Dec 2 '15 at 14:22
• I'm trying to extract a certain signal from a noisy wave. The frequency of the signal I'm trying to extract is about 5 Hz. I want to extract the amplitude of the signal, which is decreasing with time. Neither the Amplitude nor the frequency are constant, but the frequency will only fluctuate small amounts. – TheStrangeQuark Dec 2 '15 at 16:02
• In your code, what is the "certain signal" you want to extract, and what is the noise? – MBaz Dec 2 '15 at 16:10
• f2,Amp2, cos2 is the signal I'm trying to extract. The noise on this signal is the 0.1*random on this signal. The other cosine waves added in would be background noise on the carrier wave – TheStrangeQuark Dec 2 '15 at 16:13
• OK. It is not possible (AFAIK) to extract that signal from noisy using quadrature demodulation. – MBaz Dec 2 '15 at 16:28