简体   繁体   中英

Need of abs () method while plotting a power spectral density for a given dataset

Hello Everyone, I am a newbie in data science and would like to know the significance of using the abs () function and squaring the values received as an output of fft () function of python's scipy. fftpack library, used while trying to plot a power spectral density for a dataset. I have found that many of code examples to plot a power spectral density do use an abs () and then square the values obtained thereafter. Can anyone please provide me a reason for doing so? Can't we just directly plot the values obtained from fft () function in python's scipy. fftpack library?

Here is the code I have written till now to plot a power spectral density by referring some of the code examples,

import scipy.io as sio
import numpy as np
Import matplotlib.pyplot as plt
import pandas as pd

df = pd.read_csv("denoised.csv")
data = df.values
x = data[:,0]

from scipy.fftpack import fft,fftfreq
dft= fft(data)        
PSD = np.abs(dft) ** 2

The general-purpose FFT consumes complex-valued data (ie, real and imaginary) and returns complex-valued data. Even if your input is real-only, all FFT routines I'm familiar with (FFTW, Numpy's FFT, Scipy's FFTPACK, Matlab, etc.) have fft() that returns complex-valued data.

So. To plot a complex-valued vector, we have to somehow convert it to real. One option is to plot the real & imag components separately but that's usually not as interesting as the magnitude/ abs (real-squared plus imag-squared): real versus imag can tell us the behavior of the phase of the signal, which for real signals is usually random and uninteresting, whereas the magnitude combines the real and imag components and tells us in a straightforward way the amount of energy in a given frequency bin—useful!

If the magnitude of a complex number is its energy, the magnitude-squared is its power. Often engineers like to see magnitude-squared because they can cross-reference that number with, say, the power ratings of the hardware they're working with. It's just a convention.

Some side-notes: if your data is real, a real-to-complex FFT will run faster. It's called rfft but it's output is a little confusing: it returns the complex output formatted as [real, imag, real, imag, …]. (The community has raised concerns about this unusual and non-standard convention by FFTPACK in this Scipy issue .) If possible, I usually try and use numpy.fft.rfft because it returns complex-valued data as one would expect. (This real-to-complex rfft returns half as many complex-valued outputs as the complex-to-complex fft , that's where the runtime improvement comes from.)

Another side-note: this question isn't really related to data science, just digital signal processing. Consider asking such questions on http://dsp.stackexchange.com next time (no big deal that you asked it here though).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM