How do I compute the cross-spectral density of two signals?  Can you explain with an example? Thank you
May 27, 2019 in Python 903 views

## 1 answer to this question.

Hey @Akash, if your just looking for an example code, this should work:

```import numpy as np
import matplotlib.pyplot as plt

fig, (ax1, ax2) = plt.subplots(2, 1)
# make a little extra space between the subplots

dt = 0.01
t = np.arange(0, 30, dt)

# Fixing random state for reproducibility
np.random.seed(19680801)

nse1 = np.random.randn(len(t))                 # white noise 1
nse2 = np.random.randn(len(t))                 # white noise 2
r = np.exp(-t / 0.05)

cnse1 = np.convolve(nse1, r, mode='same') * dt   # colored noise 1
cnse2 = np.convolve(nse2, r, mode='same') * dt   # colored noise 2

# two signals with a coherent part and a random part
s1 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse1
s2 = 0.01 * np.sin(2 * np.pi * 10 * t) + cnse2

ax1.plot(t, s1, t, s2)
ax1.set_xlim(0, 5)
ax1.set_xlabel('time')
ax1.set_ylabel('s1 and s2')
ax1.grid(True)

cxy, f = ax2.csd(s1, s2, 256, 1. / dt)
ax2.set_ylabel('CSD (db)')
plt.show()```
answered May 27, 2019 by Kavya

## How do you get the logical xor of two variables in Python?

What i found is that you can use ...READ MORE

## In NumPy how do I get the maximum of subsets? Python

You can use np.maximum.reduceat: >>> _, idx = np.unique(g, ...READ MORE

## Example code for creating broken barh graph using matplotlib

Try this, it should work well. import matplotlib.pyplot ...READ MORE

## How to use categorical variables in Matplotlib?

Many times you want to create a ...READ MORE

## An example showing how to plot the coherence of two signals

This should work well: import numpy as np import ...READ MORE

## How do I access a user by his/her ID using django.contrib.auth.models.User in the latest release of Django?

Hi, there is only that way and ...READ MORE

## How do I obtain the index list in a NumPy Array of all the NaN values present using Python?

Hi, it is pretty simple, to be ...READ MORE