In [5]:
def plotspec(x, Ts):
fig = figure()
ax1.plot(x)

q = fft.fft(x)
ax2.plot(fft.fftfreq(len(x), Ts), abs(q))

def arbspec(s, time, Ts):
t = linspace(0.0, time, time/Ts)
x = s(t)
plotspec(x, Ts)
In [4]:
# specdelta.py
time = 2.0
Ts = 1.0/100.0
t = linspace(0, time, time/Ts)
x = zeros(len(t))
x[0] = 1
plotspec(x, Ts)

4.3. Mimic the code in specdelta.m to find the spectrum of the discrete delta function when:

1. delta doesn't occur at the start of x. Try at 10, 100, and 110. how do the spectra differe? Can you use the time shift property to explain what you see?
2. The delta changes magnitude x. Try x[1]=20, x[10] = 3, and x[110] = 0.1. How do the xpectra differ? Can you use the linearity property to explain what you see?
In [9]:
def deltaAt(dt, dx):
def s(t):
x = zeros(len(t))
x[dt] = dx
return x

return s

arbspec( deltaAt( 10,1), 2.0, 1.0/100.0)
arbspec( deltaAt(100,1), 2.0, 1.0/100.0)
arbspec( deltaAt(110,1), 2.0, 1.0/100.0)
In [11]:
arbspec( deltaAt(  1, 20),   2.0, 1.0/100.0)
arbspec( deltaAt( 10,  3),   2.0, 1.0/100.0)
arbspec( deltaAt(100,  0.1), 2.0, 1.0/100.0)

4.4. Mimic the code in specdelta to find the spectrum of a signal containing two delta functions when:

1. The deltas are located at the start and the ened
2. The deltas are located symmetrically from the start and the end
3. The deltas are located arbitrarily (eg at 33 and 120)
In [14]:
def TwoDeltaAt(dt1, dt2):
def s(t):
x = zeros(len(t))
x[dt1] = 1
x[dt2] = 1
return x

return s

arbspec(TwoDeltaAt(0,-1), 2.0, 1/100.0)
arbspec(TwoDeltaAt(89,-90), 2.0, 1/100.0)
arbspec(TwoDeltaAt(33, 120), 2.0, 1/100.0)

4.5. Mimic the code in specdelta to find the spectrum of a train of equally spaced pulses.

1. Can you predict how far apart the resulting pulses in the spectrum will be? 2,3. Other analytical questions about the DFT
In [*]:
def pulsetrain(skip):
def s(t):
x = zeros(len(t))
i = 0
while i < len(t):
x[i] = 1
x += skip

return x
return s

arbspec(pulsetrain(20), 2.0, 1.0/100.0)
arbspec(pulsetrain(25), 2.0, 1.0/100.0)