The Discrete Fourier Transform (DFT) helps us find the different frequencies inside a signal. It changes a signal from time view to frequency view so we can understand its hidden patterns.
Discrete Fourier Transform (DFT) in Signal Processing
X[k] = \sum_{n=0}^{N-1} x[n] \cdot e^{-i 2 \pi k n / N} \text{ for } k = 0, 1, ..., N-1x[n] is the input signal at time n.
X[k] is the output frequency component at index k.
import numpy as np x = [1, 0, 0, 0] N = 4 X = [sum(x[n] * np.exp(-2j * np.pi * k * n / N) for n in range(N)) for k in range(N)]
import numpy as np x = np.array([1, 2, 3, 4]) X = np.fft.fft(x)
This program creates a signal made of two sine waves at 5 Hz and 20 Hz. It calculates the DFT to find these frequencies and prints the main ones. Then it shows a graph of the frequency strengths.
import numpy as np import matplotlib.pyplot as plt # Create a simple signal: sum of two sine waves fs = 100 # sampling frequency T = 1 # seconds N = fs * T # total samples t = np.linspace(0, T, N, endpoint=False) # Signal with 5 Hz and 20 Hz frequencies x = 0.7 * np.sin(2 * np.pi * 5 * t) + 0.3 * np.sin(2 * np.pi * 20 * t) # Compute DFT using numpy fft X = np.fft.fft(x) # Frequency bins freqs = np.fft.fftfreq(N, 1/fs) # Take only positive frequencies and their magnitudes pos_mask = freqs >= 0 freqs = freqs[pos_mask] magnitudes = np.abs(X[pos_mask]) # Print main frequency components for f, mag in zip(freqs, magnitudes): if mag > 10: print(f"Frequency: {f:.1f} Hz, Magnitude: {mag:.2f}") # Plot the magnitude spectrum plt.stem(freqs, magnitudes, use_line_collection=True) plt.xlabel('Frequency (Hz)') plt.ylabel('Magnitude') plt.title('DFT Magnitude Spectrum') plt.show()
The DFT output is complex numbers; magnitude shows strength, and angle shows phase.
DFT assumes the signal repeats, so edges can cause artifacts called spectral leakage.
For faster calculations on large data, use Fast Fourier Transform (FFT) algorithms.
DFT changes a signal from time to frequency view.
It helps find hidden frequencies in data like sound or vibrations.
Use libraries like numpy to compute DFT easily and visualize results.
Practice
Solution
Step 1: Understand signal domains
The time domain shows how a signal changes over time, while the frequency domain shows the signal's frequency components.Step 2: Role of DFT
DFT transforms a signal from the time domain into the frequency domain, revealing hidden frequencies.Final Answer:
Converts the signal from time domain to frequency domain -> Option AQuick Check:
DFT = Time to Frequency [OK]
- Thinking DFT amplifies signals
- Confusing DFT with noise filtering
- Assuming DFT compresses data
signal using numpy?Solution
Step 1: Identify numpy FFT function
In numpy, the function to compute the Discrete Fourier Transform isfftinside thefftmodule.Step 2: Check correct syntax
The correct call isnumpy.fft.fft(signal). Other options are either non-existent or incorrect function names.Final Answer:
numpy.fft.fft(signal) -> Option BQuick Check:
Use numpy.fft.fft() for DFT [OK]
- Using nonexistent numpy.dft() function
- Confusing module and function names
- Typing transform instead of fft
import numpy as np signal = np.array([1, 0, -1, 0]) dft_result = np.fft.fft(signal) print(np.round(dft_result, 2))
What is the output?
Solution
Step 1: Understand input signal
The signal is [1, 0, -1, 0], a simple 4-point sequence alternating positive and negative values.Step 2: Compute DFT manually or recall pattern
DFT of this sequence results in complex values showing frequency components. The output is [0.+0.j, 0.+2.j, 0.+0.j, 0.-2.j] after rounding.Final Answer:
[0.+0.j 0.+2.j 0.+0.j 0.-2.j] -> Option AQuick Check:
DFT of alternating signal = complex freq components [OK]
- Ignoring imaginary parts in output
- Confusing zero arrays with actual DFT
- Misreading numpy rounding output
import numpy as np signal = [1, 2, 3, 4] dft = np.fft.fft(signal, n=2) print(dft)
Solution
Step 1: Understand parameter n in np.fft.fft
The parameternsets the length of the DFT. If shorter than signal length, signal is truncated.Step 2: Effect of n=2 on signal length 4
Settingn=2truncates the signal from length 4 to 2, losing data and changing the DFT result unexpectedly.Final Answer:
The parameter n=2 truncates the DFT length incorrectly -> Option CQuick Check:
n parameter truncates or pads signal [OK]
- Assuming n pads signal always
- Thinking signal must be numpy array
- Believing np.fft.fft disallows n parameter
[1, 0, 1, 0, 1, 0, 1, 0]. You want to remove high-frequency noise by zeroing out the last 4 DFT coefficients and then reconstruct the signal using inverse DFT. Which Python code snippet correctly performs this?Solution
Step 1: Compute DFT of signal
Usenp.fft.fft(signal)to get frequency components.Step 2: Zero out last 4 coefficients to remove high frequencies
Since signal length is 8, last 4 coefficients aredft[4:]. Setting them to zero removes high-frequency noise.Step 3: Reconstruct signal with inverse DFT
Usenp.fft.ifft(dft).realto get cleaned time-domain signal.Final Answer:
dft = np.fft.fft(signal) dft[4:] = 0 clean_signal = np.fft.ifft(dft).real -> Option DQuick Check:
Zero last half DFT coefficients to filter high freq [OK]
- Zeroing first half instead of last half
- Zeroing wrong slice indices
- Forgetting to take real part after inverse DFT
Ready to prove it?
Pass the test and this pattern moves unknown
