0
0
SciPydata~10 mins

Power spectral density in SciPy - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compute the power spectral density (PSD) of the signal using scipy.

SciPy
from scipy.signal import [1]
import numpy as np

fs = 1000  # Sampling frequency
signal = np.sin(2 * np.pi * 50 * np.linspace(0, 1, fs))
f, Pxx = [1](signal, fs)
print(f[:5], Pxx[:5])
Drag options to blanks, or click blank then click option'
Aperiodogram
Bfind_peaks
Cspectrogram
Dwelch
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'spectrogram' instead of 'welch' which returns a spectrogram, not PSD directly.
Using 'find_peaks' which is for peak detection, not PSD.
Using 'periodogram' which also computes PSD but is less robust than 'welch'.
2fill in blank
medium

Complete the code to generate a noisy sine wave signal before computing its PSD.

SciPy
import numpy as np
fs = 500
T = 1
n = int(T * fs)
time = np.linspace(0, T, n, endpoint=False)
signal = np.sin(2 * np.pi * 60 * time) + [1]
Drag options to blanks, or click blank then click option'
Anp.random.normal(0, 0.5, n)
Bnp.random.uniform(0, 1, n)
Cnp.random.binomial(10, 0.5, n)
Dnp.random.poisson(5, n)
Attempts:
3 left
💡 Hint
Common Mistakes
Using uniform noise which has a different distribution.
Using discrete distributions like Poisson or binomial which are not typical for noise.
3fill in blank
hard

Fix the error in the code to correctly compute the PSD with the correct sampling frequency parameter.

SciPy
from scipy.signal import welch
import numpy as np
fs = 1000
signal = np.sin(2 * np.pi * 100 * np.linspace(0, 1, fs))
f, Pxx = welch(signal, [1]=fs)
print(f[:5], Pxx[:5])
Drag options to blanks, or click blank then click option'
Afs
Bsampling_rate
Cfreq
Drate
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter names like 'sampling_rate' or 'freq' causes errors.
Omitting the sampling frequency leads to wrong frequency axis.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.

SciPy
words = ['data', 'science', 'is', 'fun']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword.startswith('s')
Dword == 'fun'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Using unrelated conditions like checking the first letter.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.

SciPy
words = ['Data', 'AI', 'ML', 'Science']
result = { [1]: [2] for word in words if [3] }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 2
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase instead of uppercase for keys.
Using incorrect conditions that do not filter by length.