0
0
SciPydata~10 mins

Power spectral density in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Power spectral density
Start with time signal
Apply Fourier Transform
Calculate magnitude squared
Normalize by frequency
Output Power Spectral Density (PSD)
The process starts with a time signal, transforms it to frequency domain, computes power at each frequency, normalizes it, and outputs the PSD.
Execution Sample
SciPy
import numpy as np
from scipy.signal import welch

fs = 1000
x = np.sin(2*np.pi*50*np.arange(1000)/fs)
freqs, psd = welch(x, fs)
This code computes the power spectral density of a 50 Hz sine wave sampled at 1000 Hz using Welch's method.
Execution Table
StepActionInput/VariableOutput/Result
1Create time arraynp.arange(1000)Array of 1000 time points from 0 to 999
2Generate sine wave50 Hz sine with fs=1000Array x with sine values
3Call welch(x, fs)x array, fs=1000Compute PSD using Welch's method
4Split signal into segmentsx arraySegments of length 256 (default)
5Apply window and FFT to each segmentSegmentsFFT of each segment
6Calculate magnitude squared and averageFFT resultsEstimate power at each frequency
7Normalize by sampling frequencyPower valuesPSD values
8Return frequencies and PSDfreqs, psd arraysfreqs from 0 to 500 Hz, psd values
9Plot or analyze PSDfreqs, psdVisual or numerical insight into signal power distribution
10EndPSD computed successfully
💡 Welch method completes after processing all segments and returns frequency and PSD arrays.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 6Final
xundefinedArray of sine wave valuesSame array passed to welchUsed in FFT segmentsUnchanged
freqsundefinedundefinedCalculated frequenciesCalculated frequenciesArray from 0 to 500 Hz
psdundefinedundefinedPower values per freqPower values per freqNormalized PSD values
Key Moments - 3 Insights
Why does the PSD output only show frequencies up to half the sampling rate?
Because of the Nyquist theorem, the highest frequency that can be represented is half the sampling rate. The execution_table row 8 shows freqs from 0 to 500 Hz when fs=1000 Hz.
Why do we split the signal into segments before FFT in Welch's method?
Splitting into segments reduces noise in the PSD estimate by averaging. Execution_table row 4 shows segmenting, and row 6 shows averaging power across segments.
What does the normalization step in PSD calculation do?
Normalization scales the power values to correct units considering sampling frequency. Execution_table row 7 describes normalizing power values to get PSD.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 8, what is the maximum frequency in the freqs array?
A1000 Hz
B50 Hz
C500 Hz
D256 Hz
💡 Hint
Refer to execution_table row 8 where freqs range is described.
At which step does the code average the power from multiple segments?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Check execution_table row 6 about calculating magnitude squared and averaging.
If the sampling frequency fs is doubled, how does the PSD frequency range change?
AMaximum frequency halves
BMaximum frequency doubles
CMaximum frequency stays the same
DPSD values double
💡 Hint
Refer to Nyquist limit explained in key_moments and execution_table row 8.
Concept Snapshot
Power Spectral Density (PSD) shows how signal power is distributed over frequency.
Use scipy.signal.welch(x, fs) to compute PSD.
Welch splits signal, applies FFT, averages power, and normalizes.
Output frequencies go from 0 to fs/2 (Nyquist frequency).
PSD helps analyze signal frequency content and noise.
Full Transcript
Power spectral density (PSD) analysis starts with a time-domain signal. We apply Fourier transform to convert it to frequency domain. Then we calculate the squared magnitude of the transform to get power at each frequency. Welch's method splits the signal into overlapping segments, applies windowing and FFT to each, averages the power spectra, and normalizes by sampling frequency. The output is frequency values from zero up to half the sampling rate (Nyquist frequency) and corresponding PSD values. This helps us understand how signal power is distributed across frequencies. The example code generates a 50 Hz sine wave sampled at 1000 Hz and computes its PSD using scipy.signal.welch. The execution table traces each step from signal creation, segmentation, FFT, power calculation, normalization, to final PSD output. Key moments clarify why frequencies only go up to half the sampling rate, why segmentation and averaging reduce noise, and the role of normalization. The visual quiz tests understanding of frequency range, averaging step, and effect of changing sampling frequency.