import numpy as np
import matplotlib.pyplot as plt
= np.fft.fft
fft = 0, 1
fmin, fmax #fft = lambda *args: np.fft.fftshift(np.fft.fft(args))
#fmin, fmax = -0.5, 0.5
2 DTFT and DFT
2.1 Example: a sinusoidal signal
Consider a cosine signal: \[x(t) = \cos(2 \pi f n)\] with \(f = 0.01\)
This is how the signal looks like:
# Create the signal
= 0.02
f = 30
N #fmax = fmax * (N-10)/N
= np.arange(N)
n = np.cos(2*np.pi*f*n)
x
# Plot the signal
=(6, 3))
plt.figure(figsize'-o')
plt.plot(x, 'The signal $x(t) = \cos(2 \pi f n)$')
plt.title('Discrete time $n$')
plt.xlabel('Signal $x[n]$')
plt.ylabel( plt.show()
Now let’s compute the Discrete-Time Fourier transform. This assumes that the signal is infinitely long.
If the cosine signal would be infinitely long, the DTFT contains only two Dirac impulses at the corresponding frequency.
# Regenerate the signal so that it fits in one period
= 10000*f
period = np.arange(period)
ninf = np.cos(2*np.pi*f*ninf)
xinf
# Compute the DTFT
= fft(xinf)
Sinf
# Create the frequency axis
= np.linspace(fmin, fmax, len(Sinf))
freqinf
# Plot the magnitude of the DTFT
=(6, 3))
plt.figure(figsize'DTFT of infinitely-long periodic signal')
plt.title(abs(Sinf), linefmt='b')
plt.stem(freqinf, np.'Frequency')
plt.xlabel('Magnitude')
plt.ylabel( plt.show()
If the signal is assumed to be only the segment we defined, and is surrounded by infinitely-long zeros, i.e. a truncated cosine, then the spectrum is convoluted with the spectrum of a rectangular window, and the DTFT looks as follows:
# Compute the DTFT
= 10000*n.size
FFT_points = fft(x, FFT_points)
S1
# Create the frequency axis
= np.linspace(fmin, fmax, len(S1))
freq1
# Plot the magnitude of the DTFT
=(6, 3))
plt.figure(figsize'DTFT of windowed signal')
plt.title(abs(S1), 'b')
plt.plot(freq1, np.#plt.stem(freqinf, np.abs(Sinf), 'b')
'Frequency')
plt.xlabel('Magnitude')
plt.ylabel( plt.show()
When computing the Discrete Fourier Transform (DFT), this assumes that the given piece of the signal is would be repeated periodically. The DFT is not continous, it is discrete.
# Compute the DFT
= fft(x)
S2
# Create the frequency axis
= np.linspace(fmin, fmax, len(S2))
freq2 #freq2 = np.fft.fftfreq(x.size)
# Plot the magnitude of the DTFT
=(6, 3))
plt.figure(figsize'Its DFT')
plt.title(abs(S2), linefmt='ro')
plt.stem(freq2, np.'Frequency')
plt.xlabel('Magnitude')
plt.ylabel( plt.show()
The DFT is just sampled from the DTFT:
# Plot the DTFT and DFT overlaid
= np.linspace(fmin, fmax, len(S2)+1)
freq2 =(6, 3))
plt.figure(figsizeabs(S1), 'b')
plt.plot(freq1, np.-1], np.abs(S2), linefmt='ro')
plt.stem(freq2[:'The DFT is just sampled from the DTFT')
plt.title('Frequency')
plt.xlabel('Magnitude')
plt.ylabel( plt.show()
2.2 Example: rectangle pulse
Consider a rectangle pulse signal as below:
# Create the signal
= 100
len_1 = 100
len_0 = np.hstack((np.ones(len_1), np.zeros(len_1)))
x = np.hstack((x, x))
x
# Plot the signal
plt.figure()
plt.plot(x)'Discrete time $n$')
plt.xlabel('Signal $x[n]$')
plt.ylabel( plt.show()
The DTFT is:
# Compute the DTFT of the rectangle window
= 2000
FFT_points = fft(x, FFT_points)
W
# Create the frequency axis
= np.linspace(fmin, fmax, len(W))
freq
# Plot the magnitude of the DTFT
plt.figure()abs(W))
plt.plot(freq, np.'Frequency')
plt.xlabel('Magnitude')
plt.ylabel( plt.show()