Open In App

Matplotlib.pyplot.csd() in Python

Last Updated : 31 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

csd() method in Matplotlib is used to compute and plot the Cross Spectral Density (CSD) of two signals. It helps analyze the frequency domain relationship between two time series, revealing how the power of one signal is distributed relative to the other signal across frequencies. It's key features include:

  • Plots the magnitude squared coherence between the signals as a function of frequency.
  • Returns the frequency and cross spectral density values for further analysis.
  • Useful in signal processing and time series analysis to identify common frequency components.

Example:

Python
import numpy as np
import matplotlib.pyplot as plt

t = np.linspace(0, 1, 500, endpoint=False)
x = np.sin(2 * np.pi * 50 * t) + np.random.randn(t.size) * 0.5
y = np.sin(2 * np.pi * 50 * t) + np.random.randn(t.size) * 0.5

Pxy, freqs = plt.csd(x, y, NFFT=256, Fs=500)
plt.title('Cross Spectral Density')
plt.show()

Output

Output
Using Matplotlib.pyplot.csd()

Explanation:

  • np.linspace(0, 1, 500, endpoint=False) creates a 500-point time array at 500 Hz sampling.
  • x and y are noisy 50 Hz sine waves simulating real signals.
  • plt.csd(x, y, NFFT=256, Fs=500) computes the Cross Spectral Density with 256-point FFT blocks.
  • Returns Pxy (CSD) and freqs (frequencies) for frequency analysis.

Syntax of Matplotlib.pyplot.csd()

matplotlib.pyplot.csd(x, y, NFFT=None, Fs=None, detrend=None, window=None, noverlap=None, pad_to=None, scale_by_freq=None, sides='default', hold=None, data=None, **kwargs)

Parameters:

Parameter

Description

x, y

Input time series data arrays to compute the CSD between.

NFFT

Number of data points used in each FFT block.

Fs

float Sampling frequency of the time series.

detrend

Specifies detrending method applied to each segment ('constant' subtracts the mean).

window

Window function applied to each segment (e.g., 'hann' for Hanning window).

noverlap

Number of points to overlap between segments.

pad_to

Number of points to zero-pad the data segment before FFT.

scale_by_freq

Whether to scale the resulting spectrum by the frequency resolution.

sides

Which sides of the spectrum to return.

hold

Whether to hold the current plot (deprecated in newer Matplotlib versions).

data

An alternative to passing x and y directly,allows selecting columns from a DataFrame.

kwargs

Passed to the plot function for customizing the plot (e.g., color, linestyle).

Returns:

  • Pxy (ndarray ): The cross spectral density of x and y.
  • frequencies (ndarray): The frequencies corresponding to the CSD values.

Examples

Example 1: We compute the cross spectral density of two noisy cosine signals using a Hanning window and 50% overlap between segments to reduce spectral leakage and smooth the result.

Python
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.mlab import window_hanning

t = np.linspace(0, 1, 1000, endpoint=False)
x = np.cos(2 * np.pi * 20 * t) + np.random.randn(t.size) * 0.3
y = np.cos(2 * np.pi * 20 * t) + np.random.randn(t.size) * 0.3

Pxy, freqs = plt.csd(x, y, NFFT=128, Fs=1000, window=window_hanning, noverlap=64)
plt.show()

Output


Output
Using Matplotlib.pyplot.csd()

Explanation:

  • np.linspace(0, 1, 1000, endpoint=False) creates a 1000-point time array at 1000 Hz sampling.
  • x and y are noisy 20 Hz cosine waves simulating real signals.
  • plt.csd(x, y, NFFT=128, Fs=1000, window=window_hanning, noverlap=64) computes the Cross Spectral Density using 128-point FFT blocks with a Hanning window and 64-point overlap to reduce spectral leakage.

Example 2: We compute the cross spectral density of two noisy sine signals using frequency scaling and a one-sided spectrum to highlight how signal power varies with frequency.

Python
t = np.linspace(0, 2, 1000, endpoint=False)
x = np.sin(2 * np.pi * 10 * t) + np.random.randn(t.size) * 0.2
y = np.sin(2 * np.pi * 10 * t) + np.random.randn(t.size) * 0.2

Pxy, freqs = plt.csd(x, y, NFFT=256, Fs=500, scale_by_freq=True, sides='onesided')
plt.title('CSD with Frequency Scaling and One-sided Spectrum')
plt.show()

Output

Output
Using Matplotlib.pyplot.csd()

Explanation:

  • np.linspace(0, 2, 1000, endpoint=False) creates a 1000-point time array over 2 seconds.
  • x and y are noisy 10 Hz sine waves simulating real signals.
  • plt.csd() computes Cross Spectral Density with 256-point FFT blocks, scales by frequency, and returns a one-sided spectrum for clearer frequency analysis.

Example 3: We compute the cross spectral density of two noisy sine signals using detrending to remove DC offset and zero padding to enhance frequency resolution.

Python
t = np.linspace(0, 1, 512, endpoint=False)
x = np.sin(2 * np.pi * 40 * t) + 0.4 * np.random.randn(t.size)
y = np.sin(2 * np.pi * 40 * t) + 0.4 * np.random.randn(t.size)

Pxy, freqs = plt.csd(x, y, NFFT=256, Fs=512, detrend='constant', pad_to=512)
plt.title('CSD with Detrending and Zero Padding')
plt.show()

Output

Output
Using Matplotlib.pyplot.csd()

Explanation:

  • np.linspace(0, 1, 512, endpoint=False) creates a 512-point time array at 512 Hz sampling.
  • x and y are noisy 40 Hz sine waves simulating real signals.
  • plt.csd() computes Cross Spectral Density with 256-point FFT blocks, detrends by removing the mean, and zero-pads to improve frequency resolution.

Next Article
Article Tags :
Practice Tags :

Similar Reads