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 (i.e., 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).
How to create custom model fields in django, and how does it works
'UnboundLocalError: local variable 'lower' referenced before assignment'
How to save the result of a button's command in tkinter?
I'm lost and confused! regarding functions and scope of returned objects. Why is the object returned from the first function changed?
How to remove quotes and brackets from a dict that has two keys to one value in a jinja template?
I am looking at each individual date in a Pandas dataframe and adjusting one column (weight), based on condition on another column for each date
For loop in Python working, but not doing it's job
Python error 'int' object has no attribute 'penup' (python turtle)
custome user serializer giving error on is_valid() - django
Dividing a graph into 2 disconnected subgraphs
use boto3 on GAE for Python
boto command for describing an Auto Scaling Group?
get sets of index values, grouped by column year
How to use a tensorflow model extracted from a trained keras model
How to query with many tables
python-shell on linux system indentation error