Bird
Raised Fist0
Signal Processingdata~5 mins

Power spectral density estimation in Signal Processing

Choose your learning style10 modes available
Introduction

Power spectral density (PSD) estimation helps us understand how the power of a signal is spread across different frequencies. It shows which frequencies carry the most energy.

Analyzing sound signals to find dominant tones or noise.
Studying heart rate signals to detect irregular rhythms.
Checking vibration signals in machines to find faults.
Examining brain waves to identify different brain states.
Understanding radio signals to improve communication quality.
Syntax
Signal Processing
frequencies, psd_values = signal.welch(signal_data, fs=sampling_rate, nperseg=segment_length)

signal_data is your input signal array.

fs is the sampling rate (how many samples per second).

nperseg is the length of each segment used for estimation.

Examples
Estimate PSD of data sampled at 1000 Hz with default segment length.
Signal Processing
frequencies, psd = signal.welch(data, fs=1000)
Estimate PSD with 500 Hz sampling rate and segments of 256 samples.
Signal Processing
frequencies, psd = signal.welch(data, fs=500, nperseg=256)
Use a Hann window to reduce spectral leakage during PSD estimation.
Signal Processing
frequencies, psd = signal.welch(data, fs=2000, nperseg=512, window='hann')
Sample Program

This program creates a signal with two sine waves at 50 Hz and 120 Hz plus some noise. It then estimates the power spectral density using Welch's method and prints the first few frequency and PSD values. Finally, it plots the PSD on a logarithmic scale to show the power distribution across frequencies.

Signal Processing
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# Create a sample signal: 2 sine waves + noise
fs = 1000  # Sampling frequency 1000 Hz
T = 1.0    # seconds
N = int(T * fs)  # Number of samples

t = np.linspace(0, T, N, endpoint=False)
sig = 0.5 * np.sin(2 * np.pi * 50 * t) + 0.3 * np.sin(2 * np.pi * 120 * t)
sig += 0.1 * np.random.normal(size=N)  # Add noise

# Estimate power spectral density
frequencies, psd = signal.welch(sig, fs=fs, nperseg=256)

# Print first 5 frequency and PSD values
for f, p in zip(frequencies[:5], psd[:5]):
    print(f"Frequency: {f:.3f} Hz, PSD: {p:.6f}")

# Plot PSD
plt.semilogy(frequencies, psd)
plt.title('Power Spectral Density Estimation')
plt.xlabel('Frequency (Hz)')
plt.ylabel('PSD (V**2/Hz)')
plt.grid(True)
plt.show()
OutputSuccess
Important Notes

Welch's method splits the signal into overlapping segments to get a smoother PSD estimate.

Choosing segment length (nperseg) affects frequency resolution and variance of the estimate.

Window functions like Hann reduce spectral leakage but change the PSD shape slightly.

Summary

Power spectral density shows how signal power spreads over frequencies.

Welch's method is a common way to estimate PSD by averaging segments.

Adjust sampling rate and segment length to get better frequency details.

Practice

(1/5)
1. What does the Power Spectral Density (PSD) of a signal represent?
easy
A. How the power of a signal is distributed over different frequencies
B. The total energy of the signal in the time domain
C. The phase shift of the signal over time
D. The amplitude of the signal at a single point in time

Solution

  1. Step 1: Understand the definition of PSD

    PSD shows how the power of a signal is spread across frequencies, not just total energy or amplitude.
  2. 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.
  3. Final Answer:

    How the power of a signal is distributed over different frequencies -> Option A
  4. Quick Check:

    PSD = power distribution over frequency [OK]
Hint: PSD shows power spread by frequency, not time or amplitude [OK]
Common Mistakes:
  • Confusing PSD with total signal energy
  • Thinking PSD shows signal amplitude at a time
  • Mixing PSD with phase information
2. Which Python function from the scipy.signal library is commonly used to estimate the Power Spectral Density using Welch's method?
easy
A. scipy.signal.spectrogram()
B. scipy.signal.fft()
C. scipy.signal.correlate()
D. scipy.signal.welch()

Solution

  1. Step 1: Identify Welch's method function

    Welch's method for PSD estimation is implemented in scipy.signal.welch().
  2. Step 2: Differentiate from other functions

    fft() computes Fourier transform, correlate() computes correlation, spectrogram() shows time-frequency but not PSD by Welch.
  3. Final Answer:

    scipy.signal.welch() -> Option D
  4. Quick Check:

    Welch PSD = scipy.signal.welch() [OK]
Hint: Welch PSD uses scipy.signal.welch() function [OK]
Common Mistakes:
  • Using fft() instead of welch() for PSD
  • Confusing spectrogram with PSD estimation
  • Trying correlate() for PSD calculation
3. Given the following Python code, what will be the length of the frequency array f returned by scipy.signal.welch?
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, nperseg=256)
medium
A. 1024
B. 129
C. 512
D. 256

Solution

  1. Step 1: Understand nperseg effect on frequency bins

    Welch returns frequency bins of length nperseg/2 + 1 for real signals.
  2. Step 2: Calculate frequency array length

    nperseg=256, so length = 256/2 + 1 = 129.
  3. Final Answer:

    129 -> Option B
  4. Quick Check:

    Frequency bins = nperseg/2 + 1 = 129 [OK]
Hint: Frequency bins length = nperseg/2 + 1 for real signals [OK]
Common Mistakes:
  • Assuming frequency length equals nperseg
  • Using full signal length instead of segment length
  • Confusing frequency bins with time samples
4. Identify the error in this code snippet estimating PSD with Welch's method:
import numpy as np
from scipy.signal import welch

fs = 500
x = np.sin(2 * np.pi * 50 * np.arange(1000) / fs)
f, Pxx = welch(x, fs=fs, nperseg=2000)
medium
A. Sampling frequency fs is too low
B. Signal x is not a numpy array
C. nperseg is larger than the signal length
D. Missing window parameter in welch()

Solution

  1. Step 1: Check nperseg vs signal length

    nperseg=2000 is greater than signal length 1000, which causes a warning or error.
  2. Step 2: Verify other parameters

    fs=500 is valid, x is numpy array, window parameter is optional with default.
  3. Final Answer:

    nperseg is larger than the signal length -> Option C
  4. Quick Check:

    nperseg > signal length causes error [OK]
Hint: nperseg must not exceed signal length [OK]
Common Mistakes:
  • Setting nperseg larger than signal length
  • Assuming window parameter is mandatory
  • Confusing sampling frequency with segment length
5. You have a noisy signal sampled at 2000 Hz. To get a smoother PSD estimate with better frequency resolution using Welch's method, which combination of parameters should you choose?
hard
A. Increase segment length (nperseg) and use overlapping segments
B. Decrease segment length and avoid overlapping
C. Use no window and set nperseg to 1
D. Increase sampling frequency beyond 2000 Hz

Solution

  1. Step 1: Understand effect of segment length and overlap

    Longer segments improve frequency resolution; overlapping segments reduce variance (smoother PSD).
  2. Step 2: Evaluate options for smoothing and resolution

    Increase segment length (nperseg) and use overlapping segments correctly suggests increasing nperseg and using overlap; others reduce resolution or are impractical.
  3. Final Answer:

    Increase segment length (nperseg) and use overlapping segments -> Option A
  4. Quick Check:

    Longer segments + overlap = smoother, better PSD [OK]
Hint: Longer segments + overlap = smoother PSD estimate [OK]
Common Mistakes:
  • Using very short segments losing frequency detail
  • Ignoring overlap reduces averaging benefits
  • Thinking sampling rate increase smooths PSD
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard