📖 Scenario: You have recorded a simple signal from a sensor measuring vibrations. You want to analyze the signal to find out how its power is distributed across different frequencies. This helps in understanding the main frequency components of the signal.
🎯 Goal: Build a Python program that calculates the power spectral density (PSD) of a given signal using SciPy's signal.welch function.
📋 What You'll Learn
Create a signal as a list of numbers representing sensor data.
Set the sampling frequency of the signal.
Use SciPy's signal.welch function to calculate the PSD.
Print the frequency array and the corresponding PSD values.
💡 Why This Matters
🌍 Real World
Power spectral density analysis is used in engineering and science to find dominant frequencies in signals like vibrations, sound, or electrical signals.
💼 Career
Understanding PSD helps in roles like signal processing engineer, data analyst, and research scientist to analyze and interpret sensor data.
Progress0 / 4 steps
1
Create the signal data
Create a list called signal_data with these exact values: 0, 1, 0, -1, 0, 1, 0, -1 representing the sensor measurements.
SciPy
Hint
Use square brackets to create a list and separate values with commas.
2
Set the sampling frequency
Create a variable called sampling_freq and set it to 4, representing 4 samples per second.
SciPy
Hint
Assign the number 4 to the variable sampling_freq.
3
Calculate the power spectral density
Import signal from scipy. Then use signal.welch with signal_data and sampling_freq to calculate the frequency array freqs and power spectral density array psd.
SciPy
Hint
Use from scipy import signal to import. Then call signal.welch(signal_data, fs=sampling_freq) and unpack the result into freqs and psd.
4
Print the frequency and PSD arrays
Print the variables freqs and psd on separate lines to display the frequency values and their corresponding power spectral density values.
SciPy
Hint
Use two print statements: one for freqs and one for psd.
Practice
(1/5)
1. What does the power spectral density (PSD) represent in signal analysis?
easy
A. The phase shift of a signal at various points
B. The total energy of a signal over time
C. How the power of a signal is distributed over different frequencies
D. The amplitude of a signal at a single point in time
Solution
Step 1: Understand the definition of PSD
Power spectral density shows how the power of a signal is spread across frequencies, not just total energy or amplitude at one time.
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.
Final Answer:
How the power of a signal is distributed over different frequencies -> Option C
Quick Check:
PSD = power distribution over frequency [OK]
Hint: PSD shows power spread by frequency, not time or phase [OK]
Common Mistakes:
Confusing PSD with total signal energy
Thinking PSD shows amplitude at one time
Mixing PSD with phase information
2. Which of the following is the correct way to import the Welch method for PSD calculation from scipy?
easy
A. from scipy.signal import welch
B. import scipy.welch
C. from scipy import welch
D. import scipy.signal.welch as welch
Solution
Step 1: Recall scipy.signal module structure
The Welch method is inside scipy.signal, so we import it from there.
Step 2: Check import syntax correctness
Only 'from scipy.signal import welch' is valid Python syntax for this function.
Final Answer:
from scipy.signal import welch -> Option A
Quick Check:
Correct import = from scipy.signal import welch [OK]
Hint: Welch is in scipy.signal, import directly from there [OK]
Common Mistakes:
Trying to import welch directly from scipy
Using incorrect import syntax
Assuming welch is a standalone module
3. What will be the output type of the following code snippet?
from scipy.signal import welch
import numpy as np
fs = 100
x = np.sin(2 * np.pi * 10 * np.arange(1000) / fs)
freqs, psd = welch(x, fs)
print(type(freqs), type(psd))
medium
A. (, )
B. (, )
C. (, )
D. (, )
Solution
Step 1: Understand welch output types
The welch function returns two numpy arrays: frequencies and power spectral density values.
Step 2: Check printed types
Printing type(freqs) and type(psd) will show numpy.ndarray for both.
Final Answer:
(<class 'numpy.ndarray'>, <class 'numpy.ndarray'>) -> Option D
Quick Check:
welch returns arrays = numpy.ndarray [OK]
Hint: Welch returns frequency and PSD as numpy arrays [OK]
Common Mistakes:
Expecting lists instead of numpy arrays
Thinking output is a tuple of floats
Confusing output with single float values
4. Identify the error in this code snippet that calculates PSD:
from scipy.signal import welch
import numpy as np
fs = 50
x = np.random.randn(500)
freqs, psd = welch(x)
print(freqs)
medium
A. The print statement should be print(psd) instead
B. Missing sampling frequency (fs) argument in welch call
C. Random data cannot be used with welch
D. Import statement for welch is incorrect
Solution
Step 1: Check welch function call parameters
The welch function needs the sampling frequency (fs) to correctly compute frequencies.
Step 2: Identify missing argument
Here, fs=50 is defined but not passed to welch, causing incorrect frequency calculation.
Final Answer:
Missing sampling frequency (fs) argument in welch call -> Option B
Quick Check:
Pass fs to welch to get correct frequencies [OK]
Hint: Always pass fs to welch for correct frequency output [OK]
Common Mistakes:
Forgetting to pass fs argument
Assuming random data is invalid input
Confusing print output with error
5. You have a noisy signal sampled at 200 Hz. You want to find the dominant frequency component using scipy's Welch method. Which parameter adjustment helps improve frequency resolution in the PSD estimate?
hard
A. Increase the segment length (nperseg) to use longer data chunks
B. Decrease the sampling frequency (fs) to reduce data size
C. Use a smaller window type to reduce computation
D. Set detrend to False to keep original signal shape
Solution
Step 1: Understand frequency resolution in Welch method
Frequency resolution improves with longer segments (nperseg) because it provides finer frequency bins.
Step 2: Evaluate options for improving resolution
Increasing nperseg increases segment length, improving resolution. Decreasing fs reduces max frequency but not resolution. Window type affects leakage, not resolution. Detrend affects baseline, not resolution.
Final Answer:
Increase the segment length (nperseg) to use longer data chunks -> Option A
Quick Check:
Longer nperseg = better frequency resolution [OK]
Hint: Longer segments (nperseg) give finer frequency detail [OK]
Common Mistakes:
Reducing sampling frequency to improve resolution
Confusing window type with resolution effect
Ignoring segment length impact on frequency bins
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.