Challenge - 5 Problems
Power Spectral Density Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of PSD calculation with scipy.signal.welch
What is the shape of the frequency and power spectral density arrays returned by scipy.signal.welch for a signal of length 1024 with default parameters?
SciPy
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) print(f.shape, Pxx.shape)
Attempts:
2 left
💡 Hint
The default nperseg is 256, which affects the output length.
✗ Incorrect
The default nperseg in scipy.signal.welch is 256. The output frequency and PSD arrays have length nperseg//2 + 1 = 129.
❓ data_output
intermediate2:00remaining
Power spectral density peak frequency detection
Given a signal composed of two sine waves at 50 Hz and 120 Hz sampled at 500 Hz, which frequency corresponds to the highest peak in the PSD computed by scipy.signal.welch?
SciPy
import numpy as np from scipy.signal import welch fs = 500 T = 1.0 t = np.linspace(0, T, int(T*fs), endpoint=False) x = 0.5*np.sin(2*np.pi*50*t) + 0.3*np.sin(2*np.pi*120*t) f, Pxx = welch(x, fs=fs, nperseg=100) peak_freq = f[np.argmax(Pxx)] print(peak_freq)
Attempts:
2 left
💡 Hint
The sine wave with larger amplitude usually produces the higher peak in PSD.
✗ Incorrect
The signal has two sine waves: 50 Hz with amplitude 0.5 and 120 Hz with amplitude 0.3. The PSD peak corresponds to the frequency with the largest power, which is 50 Hz.
❓ visualization
advanced3:00remaining
Interpreting PSD plot of noisy signal
You compute the PSD of a noisy sine wave using scipy.signal.welch and plot it. Which feature in the plot indicates the sine wave frequency?
SciPy
import numpy as np import matplotlib.pyplot as plt from scipy.signal import welch fs = 1000 T = 2.0 t = np.linspace(0, T, int(T*fs), endpoint=False) x = np.sin(2*np.pi*60*t) + 0.5*np.random.randn(len(t)) f, Pxx = welch(x, fs=fs) plt.semilogy(f, Pxx) plt.xlabel('Frequency (Hz)') plt.ylabel('PSD (V**2/Hz)') plt.title('Power Spectral Density') plt.show()
Attempts:
2 left
💡 Hint
A sine wave shows as a peak at its frequency in the PSD plot.
✗ Incorrect
The sine wave at 60 Hz appears as a sharp peak near 60 Hz in the PSD plot. Noise contributes to a broad background.
🧠 Conceptual
advanced2:00remaining
Effect of segment length on PSD resolution
How does increasing the segment length (nperseg) in scipy.signal.welch affect the frequency resolution and variance of the PSD estimate?
Attempts:
2 left
💡 Hint
Longer segments give finer frequency bins but fewer averages leading to higher variance.
✗ Incorrect
Increasing nperseg improves frequency resolution (df = fs/nperseg decreases) but results in fewer segments for averaging, increasing the variance of the PSD estimate.
🔧 Debug
expert3:00remaining
Identifying error in PSD computation code
What error does the following code raise when computing PSD with scipy.signal.welch?
import numpy as np
from scipy.signal import welch
fs = 1000
x = np.random.randn(500)
f, Pxx = welch(x, fs=fs, nperseg=1024)
print(f.shape, Pxx.shape)
SciPy
import numpy as np from scipy.signal import welch fs = 1000 x = np.random.randn(500) f, Pxx = welch(x, fs=fs, nperseg=1024) print(f.shape, Pxx.shape)
Attempts:
2 left
💡 Hint
nperseg cannot be larger than the signal length.
✗ Incorrect
The parameter nperseg cannot be larger than the length of the input signal. Here, nperseg=1024 but signal length is 500, so scipy.signal.welch raises a ValueError.