Python中的Parseval定理 [英] Parseval's theorem in Python

查看:108
本文介绍了Python中的Parseval定理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试掌握Python的fft功能,而我偶然发现的一件奇怪的事情是 Parseval定理似乎不适用,因为它现在相差约50,而应为0.

I'm trying to get some grip on Python's fft functionality, and one of the weird things that I've stumbled on is that Parseval's theorem doesn't seem to apply, as it gives a difference of about 50 now, while it should be 0.

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack as fftpack

pi = np.pi

tdata = np.arange(5999.)/300
dt = tdata[1]-tdata[0]

datay = np.sin(pi*tdata)+2*np.sin(pi*2*tdata)
N = len(datay)

fouriery = abs(fftpack.rfft(datay))/N

freqs = fftpack.rfftfreq(len(datay), d=(tdata[1]-tdata[0]))

df = freqs[1] - freqs[0]

parceval = sum(datay**2)*dt - sum(fouriery**2)*df
print parceval

plt.plot(freqs, fouriery, 'b-')
plt.xlim(0,3)
plt.show()

我很确定这是一个归一化因素,但是我似乎找不到它,因为我可以找到的有关此功能的所有信息都是

I'm pretty sure that it's a normalisation factor, but I don't seem to be able to find it, as all the information I can find about this function is the scipy.fftpack.rfft documentation.

推荐答案

您的归一化因子来自试图将Parseval定理应用于将连续信号进行傅立叶变换到离散序列的过程.在有关离散傅里叶变换的Wikipedia文章的侧面板上,对傅里叶变换,傅立叶级数,离散傅立叶变换和狄拉克梳子的采样.

Your normalization factor is coming from trying to apply Parseval's theorem for the Fourier transform of a continuous signal to a discrete sequence. On the side panel of the wikipedia article on the Discrete Fourier transform there is some discussion on the relationship of the Fourier transform, the Fourier series, the Discrete Fourier Transform and sampling with Dirac combs.

长话短说, Parseval定理在应用于DFT时不需要积分,但求和:将dtdf相乘即可创建2*pi.

To make a long story short, Parseval's theorem, when applied to DFTs, doesn't require integration, but summation: a 2*pi you are creating by multipliying by dt and df your summations.

还请注意,因为您使用的是 scipy.fftpack.rfft ,您得到的不是正确的数据DFT,而只是数据的正一半,因为负与它是对称的.因此,由于您只添加了一半的数据,加上DC术语中的0,所以缺少了2来获取@unutbu找到的4*pi.

Note also that, because you are using scipy.fftpack.rfft, what you are getting is not properly the DFT of your data, but only the positive half of it, since the negative would be symmetric to it. So since you are only adding half the data, plus the 0in the DC term, there's the missing 2 to get to the 4*pi that @unutbu found.

无论如何,如果datay保留您的序列,则可以按以下方式验证Parseval定理:

In any case, if datay holds your sequence, you can verify Parseval's theorem as follows:

fouriery = fftpack.rfft(datay)
N = len(datay)
parseval_1 = np.sum(datay**2)
parseval_2 = (fouriery[0]**2 + 2 * np.sum(fouriery[1:]**2)) / N
print parseval_1 - parseval_2

使用 scipy.fftpack.fft numpy.fft.fft 第二次求和不需要采取这种怪异的形式:

Using scipy.fftpack.fft or numpy.fft.fft the second summation does not need to take on such a weird form:

fouriery_1 = fftpack.fft(datay)
fouriery_2 = np.fft.fft(datay)
N = len(datay)
parseval_1 = np.sum(datay**2)
parseval_2_1 = np.sum(np.abs(fouriery_1)**2) / N
parseval_2_2 = np.sum(np.abs(fouriery_2)**2) / N
print parseval_1 - parseval_2_1
print parseval_1 - parseval_2_2

这篇关于Python中的Parseval定理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆