Bird
Raised Fist0
Signal Processingdata~20 mins

Magnitude and phase spectrum in Signal Processing - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spectrum Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of magnitude spectrum calculation
What is the output of the following Python code that computes the magnitude spectrum of a signal?
Signal Processing
import numpy as np
signal = np.array([1, 2, 3, 4])
fft_result = np.fft.fft(signal)
magnitude = np.abs(fft_result)
print(np.round(magnitude, 2))
A[10. 2.83 2. 2.83]
B[10. 2.83 2.83 2.83]
C[10. 2. 2. 2. ]
D[10. 3. 2. 3. ]
Attempts:
2 left
💡 Hint
Recall that magnitude is the absolute value of the FFT complex output.
❓ data_output
intermediate
2:00remaining
Phase spectrum values from FFT
Given the signal array and its FFT, what is the phase spectrum output (in radians) from the code below?
Signal Processing
import numpy as np
signal = np.array([0, 1, 0, -1])
fft_result = np.fft.fft(signal)
phase = np.angle(fft_result)
print(np.round(phase, 2))
A[ 0. -1.57 3.14 1.57]
B[ 0. 0. 0. 0. ]
C[ 0. 1.57 3.14 -1.57]
D[ 0. -3.14 1.57 3.14]
Attempts:
2 left
💡 Hint
Phase is the angle of the complex FFT output, use np.angle.
❓ visualization
advanced
3:00remaining
Identify the correct magnitude spectrum plot
Which plot correctly shows the magnitude spectrum of a 1-second 5 Hz sine wave sampled at 50 Hz?
Signal Processing
import numpy as np
import matplotlib.pyplot as plt
fs = 50
f = 5
t = np.arange(0, 1, 1/fs)
signal = np.sin(2 * np.pi * f * t)
fft_result = np.fft.fft(signal)
magnitude = np.abs(fft_result) / len(signal)
freq = np.fft.fftfreq(len(signal), 1/fs)
plt.stem(freq[:len(freq)//2], magnitude[:len(magnitude)//2])
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.title('Magnitude Spectrum')
plt.show()
AA flat plot with no peaks
BA plot with a single peak at 5 Hz and zero elsewhere
CA plot with peaks at 5 Hz and 45 Hz
DA plot with multiple peaks evenly spaced every 5 Hz
Attempts:
2 left
💡 Hint
Remember FFT frequency bins include negative frequencies represented as high positive frequencies.
🧠 Conceptual
advanced
2:00remaining
Effect of zero-padding on magnitude spectrum resolution
What is the main effect of zero-padding a time-domain signal before computing its FFT magnitude spectrum?
AIt changes the actual frequency content of the signal
BIt removes noise from the signal
CIt decreases the amplitude of all frequency components
DIt increases the frequency resolution by interpolating the spectrum
Attempts:
2 left
💡 Hint
Think about how zero-padding affects the FFT length and frequency bins.
🔧 Debug
expert
2:00remaining
Identify the error in phase spectrum calculation code
What error will the following code produce when calculating the phase spectrum of a signal?
Signal Processing
import numpy as np
signal = np.array([1, 0, -1, 0])
fft_result = np.fft.fft(signal)
phase = np.angle(fft_result.real)
print(phase)
ASyntaxError due to incorrect np.angle usage
BThe phase array will be all zeros, losing phase information
CTypeError because fft_result.real is not a complex array
DCorrect phase spectrum output
Attempts:
2 left
💡 Hint
Check what np.angle expects as input and what fft_result.real contains.

Practice

(1/5)
1. What does the magnitude spectrum represent in signal processing?
easy
A. The strength or amplitude of different frequency components
B. The timing or phase shift of frequency components
C. The original time-domain signal values
D. The noise level in the signal

Solution

  1. Step 1: Understand magnitude spectrum meaning

    The magnitude spectrum shows how strong or intense each frequency is in a signal.
  2. Step 2: Differentiate from phase spectrum

    The phase spectrum shows timing, not strength, so it is not the magnitude.
  3. Final Answer:

    The strength or amplitude of different frequency components -> Option A
  4. Quick Check:

    Magnitude spectrum = strength [OK]
Hint: Magnitude = strength of frequencies, phase = timing [OK]
Common Mistakes:
  • Confusing magnitude with phase
  • Thinking magnitude shows time values
  • Mixing magnitude with noise level
2. Which Python function is used to get the phase spectrum from a Fourier Transform result stored in variable F?
easy
A. np.abs(F)
B. np.angle(F)
C. np.fft(F)
D. np.real(F)

Solution

  1. Step 1: Recall phase extraction function

    In Python's NumPy, np.angle() returns the phase angles of complex numbers.
  2. Step 2: Differentiate from magnitude and other functions

    np.abs() returns magnitude, np.fft() is not a valid function, and np.real() returns real parts only.
  3. Final Answer:

    np.angle(F) -> Option B
  4. Quick Check:

    Phase spectrum = np.angle() [OK]
Hint: Use np.angle() for phase, np.abs() for magnitude [OK]
Common Mistakes:
  • Using np.abs() instead of np.angle() for phase
  • Trying to use np.fft() which is invalid
  • Confusing real part with phase
3. Given the code:
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?
medium
A. [4.00 0.00 0.00 0.00] [0.00 0.00 0.00 0.00]
B. [0.00 2.00 0.00 2.00] [0.00 0.00 0.00 0.00]
C. [2.00 2.00 2.00 2.00] [ 0.00 -1.57 3.14 1.57]
D. [2.00 0.00 2.00 0.00] [0.00 1.57 3.14 -1.57]

Solution

  1. Step 1: Compute FFT of x

    The FFT of [1, 0, -1, 0] is [0+0j, 2+0j, 0+0j, 2+0j].
  2. 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].
  3. Final Answer:

    [2.00 2.00 2.00 2.00] [ 0.00 -1.57 3.14 1.57] -> Option C
  4. Quick Check:

    Magnitude = [2 2 2 2], Phase = approx [-pi/2, pi, pi/2] [OK]
Hint: FFT magnitude is abs(), phase is angle() in radians [OK]
Common Mistakes:
  • Confusing magnitude with real part
  • Not rounding phase correctly
  • Mixing up signs of phase angles
4. The following code attempts to compute magnitude and phase but has an error:
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?
medium
A. No error; code runs correctly
B. np.fft.fft() is used incorrectly; should be np.fft.ifft()
C. The input array x must be complex numbers
D. Magnitude and phase are swapped; np.angle() should be for phase, np.abs() for magnitude

Solution

  1. 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.
  2. Step 2: Identify swapped assignments

    The code assigns magnitude = np.angle(F) and phase = np.abs(F), which is reversed.
  3. Final Answer:

    Magnitude and phase are swapped; np.angle() should be for phase, np.abs() for magnitude -> Option D
  4. Quick Check:

    Magnitude = abs(), Phase = angle() [OK]
Hint: Remember: abs() for magnitude, angle() for phase [OK]
Common Mistakes:
  • Swapping magnitude and phase functions
  • Thinking FFT input must be complex
  • Confusing FFT and IFFT usage
5. You have a noisy signal and want to keep only frequencies with magnitude above 5. Which steps correctly apply this filter using FFT results stored in F?
hard
A. Calculate magnitude = np.abs(F), create mask = magnitude > 5, then set F_filtered = F * mask
B. Calculate phase = np.angle(F), create mask = phase > 5, then set F_filtered = F * mask
C. Calculate magnitude = np.abs(F), create mask = magnitude < 5, then set F_filtered = F * mask
D. Calculate magnitude = np.abs(F), create mask = magnitude > 5, then set F_filtered = np.abs(F) * mask

Solution

  1. Step 1: Compute magnitude and create mask

    Calculate magnitude with np.abs(F). Create a boolean mask where magnitude > 5 to keep strong frequencies.
  2. 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.
  3. Final Answer:

    Calculate magnitude = np.abs(F), create mask = magnitude > 5, then set F_filtered = F * mask -> Option A
  4. Quick Check:

    Filter by magnitude > 5, multiply mask with F [OK]
Hint: Filter frequencies by magnitude mask, multiply mask with FFT [OK]
Common Mistakes:
  • Filtering by phase instead of magnitude
  • Using magnitude < 5 to keep frequencies
  • Multiplying mask with magnitude only, losing phase info
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard