Notification texts go here Contact Us Buy Now!

How do I plot power spectral diagram in python?

The standard spectral diagram looks much like your last plot, though your function produces much more than 10**-4 in the pass band.

import matplotlib.pyplot as plt
import numpy as np


def a(t: np.ndarray) -> np.ndarray:
    return (
        5.4547990171221e-20 * np.sin(4.35893728452007e-5 * t)**2 
        - 6.80677962622999e-40 * np.sin(4.35893728452007e-5 * t) * np.cos(4.35893728452007e-5 * t)
        + 3.84259827520928e-19 * np.sin(2831.25294891184 * t)**2
        - 2.77555756156289e-17 * np.sin(2831.25294891184 * t) * np.cos(2831.25294891184 * t)
        + 5.45479901712209e-20 * np.cos(4.35893728452007e-5 * t)**2
        - 3.87725551857472e-22 * np.cos(2831.25294891184 * t)**2
        + 1391.74933087011 * np.exp(-40.0880015429485 * t) * np.sin(2831.25294891184 * t)
        + 9.5003864918335e-13 * np.exp(-40.0880015429485 * t) * np.cos(2831.25294891184 * t)
        - 0.000158807497479067 * np.exp(-9.500167e-15 * t) * np.sin(4.35893728452007e-5 * t)
        + 2094.39510239319 * np.exp(-9.500167e-15 * t) * np.cos(4.35893728452007e-5 * t)
    )


fsample = 1e5
tsample = 1/fsample
t = np.arange(0, 1, tsample)
a_t = a(t)
a_f = np.abs(np.fft.rfft(a_t))
f = np.fft.rfftfreq(n=t.size, d=tsample)

fig, ax = plt.subplots()
ax.loglog(f, a_f)
plt.show()

spectrum

another answer

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.