Bird
Raised Fist0
Signal Processingdata~20 mins

Power spectral density estimation in Signal Processing - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PSD Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of PSD estimation using Welch's method
What is the shape of the output array psd after running Welch's method on a 1D signal of length 1024 with nperseg=1024?
Signal Processing
import numpy as np
from scipy.signal import welch

fs = 1000  # Sampling frequency
signal = np.random.randn(1024)
frequencies, psd = welch(signal, fs=fs, nperseg=1024)
print(psd.shape)
A(513,)
B(512,)
C(1024,)
D(256,)
Attempts:
2 left
💡 Hint
Welch's method returns half the FFT length plus one for the frequency bins.
❓ data_output
intermediate
2:00remaining
Number of frequency bins in periodogram
Given a signal of length 2048 sampled at 500 Hz, what is the number of frequency bins returned by the periodogram method with default FFT length?
Signal Processing
import numpy as np
from scipy.signal import periodogram

fs = 500
signal = np.random.randn(2048)
frequencies, psd = periodogram(signal, fs=fs)
print(len(frequencies))
A2048
B2049
C1025
D1024
Attempts:
2 left
💡 Hint
Periodogram returns nfft/2 + 1 frequency bins for real signals.
❓ visualization
advanced
3:00remaining
Identifying the correct PSD plot
Which plot correctly shows the power spectral density of a noisy sine wave at 50 Hz using Welch's method?
Signal Processing
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import welch

fs = 500
T = 2
t = np.linspace(0, T, int(T*fs), endpoint=False)
signal = np.sin(2*np.pi*50*t) + 0.5*np.random.randn(len(t))
frequencies, psd = welch(signal, fs=fs, nperseg=256)

plt.figure(figsize=(8,4))
plt.semilogy(frequencies, psd)
plt.xlabel('Frequency (Hz)')
plt.ylabel('PSD (V**2/Hz)')
plt.title('Power Spectral Density using Welch')
plt.grid(True)
plt.show()
AA plot with a peak near 100 Hz instead of 50 Hz
BA flat line with no peaks
CMultiple peaks at random frequencies with no dominant peak
DA plot with a clear peak near 50 Hz and noise floor elsewhere
Attempts:
2 left
💡 Hint
The sine wave frequency should appear as a peak in the PSD.
🧠 Conceptual
advanced
2:00remaining
Effect of segment length on PSD resolution
How does increasing the segment length (nperseg) in Welch's method affect the frequency resolution and variance of the PSD estimate?
AIncreases frequency resolution but increases variance
BDecreases frequency resolution and decreases variance
CIncreases frequency resolution and decreases variance
DDecreases frequency resolution but increases variance
Attempts:
2 left
💡 Hint
Longer segments give finer frequency bins but fewer averages.
🔧 Debug
expert
2:00remaining
Identifying error in PSD calculation code
What error will this code raise when calculating PSD using Welch's method?
import numpy as np
from scipy.signal import welch

fs = 1000
signal = np.random.randn(500)
frequencies, psd = welch(signal, fs=fs, nperseg=1024)
print(psd)
ANo error, prints PSD array
BValueError: nperseg must not be greater than input length
CIndexError: index out of range
DTypeError: unsupported operand type(s) for /: 'int' and 'str'
Attempts:
2 left
💡 Hint
Check if segment length is valid for the signal length.

Practice

(1/5)
1. What does the Power Spectral Density (PSD) of a signal represent?
easy
A. How the power of a signal is distributed over different frequencies
B. The total energy of the signal in the time domain
C. The phase shift of the signal over time
D. The amplitude of the signal at a single point in time

Solution

  1. Step 1: Understand the definition of PSD

    PSD shows how the power of a signal is spread across frequencies, not just total energy or amplitude.
  2. Step 2: Compare options with PSD meaning

    Only How the power of a signal is distributed over different frequencies correctly describes PSD as power distribution over frequencies.
  3. Final Answer:

    How the power of a signal is distributed over different frequencies -> Option A
  4. Quick Check:

    PSD = power distribution over frequency [OK]
Hint: PSD shows power spread by frequency, not time or amplitude [OK]
Common Mistakes:
  • Confusing PSD with total signal energy
  • Thinking PSD shows signal amplitude at a time
  • Mixing PSD with phase information
2. Which Python function from the scipy.signal library is commonly used to estimate the Power Spectral Density using Welch's method?
easy
A. scipy.signal.spectrogram()
B. scipy.signal.fft()
C. scipy.signal.correlate()
D. scipy.signal.welch()

Solution

  1. Step 1: Identify Welch's method function

    Welch's method for PSD estimation is implemented in scipy.signal.welch().
  2. Step 2: Differentiate from other functions

    fft() computes Fourier transform, correlate() computes correlation, spectrogram() shows time-frequency but not PSD by Welch.
  3. Final Answer:

    scipy.signal.welch() -> Option D
  4. Quick Check:

    Welch PSD = scipy.signal.welch() [OK]
Hint: Welch PSD uses scipy.signal.welch() function [OK]
Common Mistakes:
  • Using fft() instead of welch() for PSD
  • Confusing spectrogram with PSD estimation
  • Trying correlate() for PSD calculation
3. Given the following Python code, what will be the length of the frequency array f returned by scipy.signal.welch?
import numpy as np
from scipy.signal import welch

fs = 1000  # Sampling frequency
x = np.random.randn(1024)  # Random signal
f, Pxx = welch(x, fs=fs, nperseg=256)
medium
A. 1024
B. 129
C. 512
D. 256

Solution

  1. Step 1: Understand nperseg effect on frequency bins

    Welch returns frequency bins of length nperseg/2 + 1 for real signals.
  2. Step 2: Calculate frequency array length

    nperseg=256, so length = 256/2 + 1 = 129.
  3. Final Answer:

    129 -> Option B
  4. Quick Check:

    Frequency bins = nperseg/2 + 1 = 129 [OK]
Hint: Frequency bins length = nperseg/2 + 1 for real signals [OK]
Common Mistakes:
  • Assuming frequency length equals nperseg
  • Using full signal length instead of segment length
  • Confusing frequency bins with time samples
4. Identify the error in this code snippet estimating PSD with Welch's method:
import numpy as np
from scipy.signal import welch

fs = 500
x = np.sin(2 * np.pi * 50 * np.arange(1000) / fs)
f, Pxx = welch(x, fs=fs, nperseg=2000)
medium
A. Sampling frequency fs is too low
B. Signal x is not a numpy array
C. nperseg is larger than the signal length
D. Missing window parameter in welch()

Solution

  1. Step 1: Check nperseg vs signal length

    nperseg=2000 is greater than signal length 1000, which causes a warning or error.
  2. Step 2: Verify other parameters

    fs=500 is valid, x is numpy array, window parameter is optional with default.
  3. Final Answer:

    nperseg is larger than the signal length -> Option C
  4. Quick Check:

    nperseg > signal length causes error [OK]
Hint: nperseg must not exceed signal length [OK]
Common Mistakes:
  • Setting nperseg larger than signal length
  • Assuming window parameter is mandatory
  • Confusing sampling frequency with segment length
5. You have a noisy signal sampled at 2000 Hz. To get a smoother PSD estimate with better frequency resolution using Welch's method, which combination of parameters should you choose?
hard
A. Increase segment length (nperseg) and use overlapping segments
B. Decrease segment length and avoid overlapping
C. Use no window and set nperseg to 1
D. Increase sampling frequency beyond 2000 Hz

Solution

  1. Step 1: Understand effect of segment length and overlap

    Longer segments improve frequency resolution; overlapping segments reduce variance (smoother PSD).
  2. Step 2: Evaluate options for smoothing and resolution

    Increase segment length (nperseg) and use overlapping segments correctly suggests increasing nperseg and using overlap; others reduce resolution or are impractical.
  3. Final Answer:

    Increase segment length (nperseg) and use overlapping segments -> Option A
  4. Quick Check:

    Longer segments + overlap = smoother, better PSD [OK]
Hint: Longer segments + overlap = smoother PSD estimate [OK]
Common Mistakes:
  • Using very short segments losing frequency detail
  • Ignoring overlap reduces averaging benefits
  • Thinking sampling rate increase smooths PSD
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