Complete the code to compute the power spectral density (PSD) of the signal using 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])
The welch function from scipy.signal computes the power spectral density (PSD) of a signal.
Complete the code to generate a noisy sine wave signal before computing its PSD.
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]
Adding Gaussian noise with mean 0 and standard deviation 0.5 simulates a noisy sine wave.
Fix the error in the code to correctly compute the PSD with the correct sampling frequency parameter.
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])
The correct parameter name for sampling frequency in welch is fs.
Fill both blanks to create a dictionary comprehension that maps each word to its length only if the length is greater than 3.
words = ['data', 'science', 'is', 'fun'] lengths = {word: [1] for word in words if [2]
The dictionary comprehension maps each word to its length (len(word)) only if the length is greater than 3 (len(word) > 3).
Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 2.
words = ['Data', 'AI', 'ML', 'Science'] result = { [1]: [2] for word in words if [3] }
The comprehension maps the uppercase version of each word (word.upper()) to its length (len(word)) only if the length is greater than 2 (len(word) > 2).