简体   繁体   English

在绘制给定数据集的功率谱密度时需要abs()方法

[英]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. 大家好,我是数据科学的新手,想知道使用abs()函数的重要性,并将接收到的值作为python的scipy的fft()函数的输出。 fftpack library, used while trying to plot a power spectral density for a dataset. fftpack库,在尝试绘制数据集的功率谱密度时使用。 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. 我发现绘制功率谱密度的许多代码示例都使用abs(),然后将之后获得的值平方。 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. 我们不能直接在python的scipy中直接绘制从fft()函数获得的值。 fftpack library? fftpack库?

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. 通用FFT消耗复值数据(即实数和虚数)并返回复值数据。 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. 即使您的输入是真实的,我熟悉的所有FFT例程(FFTW,Numpy的FFT,Scipy的FFTPACK,Matlab等)都有fft()返回复值数据。

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! 一种选择是分别绘制真实和成像元件,但通常不像幅度/ abs (实平方加上图像平方)那样有趣:真实与图像可以告诉我们信号相位的行为,这对于实际信号通常是随机的和无趣的,而幅度结合了真实和成像组件,并以直接的方式告诉我们给定频率箱中的能量有用!

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. 一些附注:如果您的数据是真实的,那么实际到复杂的FFT将运行得更快。 It's called rfft but it's output is a little confusing: it returns the complex output formatted as [real, imag, real, imag, …]. 它被称为rfft但它的输出有点令人困惑:它返回格式为[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. (社区已经在这个Scipy问题中引起了对FFTPACK的这种不寻常和非标准约定的担忧。)如果可能的话,我通常会尝试使用numpy.fft.rfft因为它会像人们期望的那样返回复值数据。 (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.) (这种从实际到复杂的rfft返回的复数值输出的数量是复杂到复杂fft 一半 ,这是运行时改进的来源。)

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). 考虑下次在http://dsp.stackexchange.com上提出这样的问题(尽管你在这里没有大问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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