ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 20 Mar 2015 15:17:18 -0500Low pass filter for coherent demodulation?http://ask.sagemath.org/question/26255/low-pass-filter-for-coherent-demodulation/ Hello,
I'm trying to design a low-pass filter to recover a signal that was modulated using DSB-SC. I have produced the message signal and the modulated signal, but I can't seem to recover the original.
To modulate, I use a 2 kHz sinusoid: dsb = m(t) * cos(2*pi*2000*t)
To begin demodulation, I multiply that signal by another sinusoid of the same frequency: demod = m(t) * cos^2(2*pi*2000*t)
Finally, I (try to) use an FIR low-pass filter with cutoff frequency 100 Hz. Here is what I'm doing:
import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
# Message signal
def message(t):
m1 = (100*t + 2) * (unit_step(t+0.02) - unit_step(t+0.01))
m2 = unit_step(t+0.01) - unit_step(t-0.01)
m3 = (-100*t + 2) * (unit_step(t-0.01) - unit_step(t-0.02))
return m1 + m2 + m3
T = 1e-4 # sampling interval
fs = 1 / T # sampling rate
nyq_freq = fs / 2 # Nyquist frequency
fc = 2000 # carrier frequency
B = 100 # using 100 Hz as the bandwidth of the signal m(t)
t = var('t')
dsb = message(t) * cos(2*pi*fc*t)
dem = dsb(t) * cos(2*pi*fc*t)
t = np.arange(-0.04, 0.04, T) # redefine 't' as an array
m = map(message, t)
dem = map(dem, t) # make dem into an array representing its values
h = sig.firwin(40, B/nyq_freq)
a = np.ones(len(h))
rec = sig.lfilter(h, a, dem)
plt.plot(t, m, 'b')
plt.plot(t, rec, 'r')
Based on the examples I've seen to implement filters, this looks to be the way it is done? Am I approaching this incorrectly? Ideally, the plots should look quite similar, but these don't.Thu, 19 Mar 2015 10:12:23 -0500http://ask.sagemath.org/question/26255/low-pass-filter-for-coherent-demodulation/Answer by mpatton for <p>Hello,</p>
<p>I'm trying to design a low-pass filter to recover a signal that was modulated using DSB-SC. I have produced the message signal and the modulated signal, but I can't seem to recover the original.</p>
<p>To modulate, I use a 2 kHz sinusoid: dsb = m(t) * cos(2<em>pi</em>2000<em>t)
To begin demodulation, I multiply that signal by another sinusoid of the same frequency: demod = m(t) * cos^2(2</em>pi<em>2000</em>t)</p>
<p>Finally, I (try to) use an FIR low-pass filter with cutoff frequency 100 Hz. Here is what I'm doing:</p>
<pre><code>import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
# Message signal
def message(t):
m1 = (100*t + 2) * (unit_step(t+0.02) - unit_step(t+0.01))
m2 = unit_step(t+0.01) - unit_step(t-0.01)
m3 = (-100*t + 2) * (unit_step(t-0.01) - unit_step(t-0.02))
return m1 + m2 + m3
T = 1e-4 # sampling interval
fs = 1 / T # sampling rate
nyq_freq = fs / 2 # Nyquist frequency
fc = 2000 # carrier frequency
B = 100 # using 100 Hz as the bandwidth of the signal m(t)
t = var('t')
dsb = message(t) * cos(2*pi*fc*t)
dem = dsb(t) * cos(2*pi*fc*t)
t = np.arange(-0.04, 0.04, T) # redefine 't' as an array
m = map(message, t)
dem = map(dem, t) # make dem into an array representing its values
h = sig.firwin(40, B/nyq_freq)
a = np.ones(len(h))
rec = sig.lfilter(h, a, dem)
plt.plot(t, m, 'b')
plt.plot(t, rec, 'r')
</code></pre>
<p>Based on the examples I've seen to implement filters, this looks to be the way it is done? Am I approaching this incorrectly? Ideally, the plots should look quite similar, but these don't.</p>
http://ask.sagemath.org/question/26255/low-pass-filter-for-coherent-demodulation/?answer=26272#post-id-26272I found my mistake. In the lines
a = np.ones(len(h))
rec = sig.lfilter(h, a, dem)
I should not have passed an array of ones to `lfilter()`. The correct way to do this turned out to be:
rec = sig.lfilter(h, [1.0], dem)
This gives the correct output. On a side note, since the output of a linear system is effectively the convolution of its input and its unit impulse response, I could have used
rec = sig.convolve(dem, h, mode='same')
I recalled this after struggling with `lfilter()`. The convolution approach may even be better, because the output of `lfilter` results in the signal having a phase delay that must be accounted for (this is especially notable when it comes to plotting). `convolve()` has no such issue.Fri, 20 Mar 2015 15:17:18 -0500http://ask.sagemath.org/question/26255/low-pass-filter-for-coherent-demodulation/?answer=26272#post-id-26272