0
0
SciPydata~20 mins

Power spectral density in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Power Spectral Density Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A(1024,) (1024,)
B(512,) (512,)
C(129,) (129,)
D(256,) (256,)
Attempts:
2 left
💡 Hint
The default nperseg is 256, which affects the output length.
data_output
intermediate
2: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)
A120.0
B50.0
C85.0
D0.0
Attempts:
2 left
💡 Hint
The sine wave with larger amplitude usually produces the higher peak in PSD.
visualization
advanced
3: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()
AA sharp peak near 60 Hz
BA peak near 0 Hz
CA flat line across all frequencies
DMultiple peaks at random frequencies
Attempts:
2 left
💡 Hint
A sine wave shows as a peak at its frequency in the PSD plot.
🧠 Conceptual
advanced
2: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?
ADecreases frequency resolution and decreases variance
BDecreases frequency resolution and increases variance
CIncreases frequency resolution and decreases variance
DIncreases frequency resolution but increases variance
Attempts:
2 left
💡 Hint
Longer segments give finer frequency bins but fewer averages leading to higher variance.
🔧 Debug
expert
3: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)
AValueError: nperseg must not be greater than input length
BTypeError: unsupported operand type(s) for /: 'int' and 'str'
CIndexError: index out of range
DNo error, runs successfully
Attempts:
2 left
💡 Hint
nperseg cannot be larger than the signal length.