Magnitude and phase spectrum help us understand the strength and timing of different frequencies in a signal.
Magnitude and phase spectrum in Signal Processing
magnitude = np.abs(fft_result)
phase = np.angle(fft_result)fft_result is the output of the Fourier Transform on your signal.
np.abs() gives the magnitude (strength) of each frequency.
np.angle() gives the phase (timing) of each frequency.
import numpy as np signal = np.array([1, 0, -1, 0]) fft_result = np.fft.fft(signal) magnitude = np.abs(fft_result) phase = np.angle(fft_result)
import numpy as np signal = np.sin(2 * np.pi * 5 * np.linspace(0, 1, 100, endpoint=False)) fft_result = np.fft.fft(signal) magnitude = np.abs(fft_result) phase = np.angle(fft_result)
This program creates a signal with two frequencies (10 Hz and 20 Hz), computes its FFT, and prints the magnitude and phase for the first 10 frequency components.
import numpy as np import matplotlib.pyplot as plt # Create a signal with two frequencies fs = 100 # Sampling frequency t = np.linspace(0, 1, fs, endpoint=False) signal = 3 * np.sin(2 * np.pi * 10 * t) + 2 * np.cos(2 * np.pi * 20 * t) # Compute FFT fft_result = np.fft.fft(signal) # Calculate magnitude and phase magnitude = np.abs(fft_result) phase = np.angle(fft_result) # Frequency axis freq = np.fft.fftfreq(len(signal), 1/fs) # Print first 10 values of magnitude and phase for i in range(10): print(f"Freq: {freq[i]:.1f} Hz, Magnitude: {magnitude[i]:.2f}, Phase: {phase[i]:.2f} radians")
The magnitude shows how strong each frequency is in the signal.
The phase tells where each frequency wave starts in time.
Magnitude and phase together fully describe the frequency content of a signal.
Magnitude spectrum shows the strength of frequencies.
Phase spectrum shows the timing of frequencies.
Both are found by applying Fourier Transform and then using np.abs() and np.angle().
Practice
Solution
Step 1: Understand magnitude spectrum meaning
The magnitude spectrum shows how strong or intense each frequency is in a signal.Step 2: Differentiate from phase spectrum
The phase spectrum shows timing, not strength, so it is not the magnitude.Final Answer:
The strength or amplitude of different frequency components -> Option AQuick Check:
Magnitude spectrum = strength [OK]
- Confusing magnitude with phase
- Thinking magnitude shows time values
- Mixing magnitude with noise level
F?Solution
Step 1: Recall phase extraction function
In Python's NumPy,np.angle()returns the phase angles of complex numbers.Step 2: Differentiate from magnitude and other functions
np.abs()returns magnitude,np.fft()is not a valid function, andnp.real()returns real parts only.Final Answer:
np.angle(F) -> Option BQuick Check:
Phase spectrum = np.angle() [OK]
- Using np.abs() instead of np.angle() for phase
- Trying to use np.fft() which is invalid
- Confusing real part with phase
import numpy as np x = np.array([1, 0, -1, 0]) F = np.fft.fft(x) magnitude = np.abs(F) phase = np.angle(F) print(np.round(magnitude, 2)) print(np.round(phase, 2))
What is the printed magnitude and phase arrays?
Solution
Step 1: Compute FFT of x
The FFT of [1, 0, -1, 0] is [0+0j, 2+0j, 0+0j, 2+0j].Step 2: Calculate magnitude and phase arrays
Magnitude is [2.00 2.00 2.00 2.00]. Phase angles are [0.00 -1.57 3.14 1.57].Final Answer:
[2.00 2.00 2.00 2.00] [ 0.00 -1.57 3.14 1.57] -> Option CQuick Check:
Magnitude = [2 2 2 2], Phase = approx [-pi/2, pi, pi/2] [OK]
- Confusing magnitude with real part
- Not rounding phase correctly
- Mixing up signs of phase angles
import numpy as np x = np.array([1, 2, 3, 4]) F = np.fft.fft(x) magnitude = np.angle(F) phase = np.abs(F) print(magnitude) print(phase)
What is the error?
Solution
Step 1: Check function usage for magnitude and phase
Magnitude is the absolute value, so np.abs() should be used for magnitude, not np.angle(). Phase is angle, so np.angle() is for phase.Step 2: Identify swapped assignments
The code assigns magnitude = np.angle(F) and phase = np.abs(F), which is reversed.Final Answer:
Magnitude and phase are swapped; np.angle() should be for phase, np.abs() for magnitude -> Option DQuick Check:
Magnitude = abs(), Phase = angle() [OK]
- Swapping magnitude and phase functions
- Thinking FFT input must be complex
- Confusing FFT and IFFT usage
F?Solution
Step 1: Compute magnitude and create mask
Calculate magnitude with np.abs(F). Create a boolean mask where magnitude > 5 to keep strong frequencies.Step 2: Apply mask to original FFT data
Multiply the complex FFT array F by the mask to zero out weak frequencies, preserving phase and magnitude of strong ones.Final Answer:
Calculate magnitude = np.abs(F), create mask = magnitude > 5, then set F_filtered = F * mask -> Option AQuick Check:
Filter by magnitude > 5, multiply mask with F [OK]
- Filtering by phase instead of magnitude
- Using magnitude < 5 to keep frequencies
- Multiplying mask with magnitude only, losing phase info
Ready to prove it?
Pass the test and this pattern moves unknown
