`numpy.diff`和`scipy.fftpack.diff`在区分时给出不同的结果 [英] `numpy.diff` and `scipy.fftpack.diff` giving different results when differentiating

查看:154
本文介绍了`numpy.diff`和`scipy.fftpack.diff`在区分时给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试计算某些数据的导数,并且试图将有限差分的输出与频谱方法的输出进行比较.但是结果却大不相同,我无法弄清楚为什么.

I am trying to compute the derivative of some data and I was trying to compare the output of a finite difference and a spectral method output. But the results are very different and I can't figure out exactly why.

考虑以下示例代码

import numpy as np
from scipy import fftpack as sp
from matplotlib import pyplot as plt
x = np.arange(-100,100,1)
y = np.sin(x)

plt.plot(np.diff(y)/np.diff(x))
plt.plot(sp.diff(y))

plt.show()

这将输出以下结果

橙色输出为fftpack输出.没关系的微妙之处,这只是为了举例.

The orange output being the fftpack output. Nevermind the subtleties, this is just for the sake of an example.

那么,为什么它们如此不同?它们不应该(大约)相同吗?

So, why are they so different? Shouldn't they be (approximately) the same?

我很确定可以使用fftpack.diff的period关键字来校正不同的幅度,但是我无法确定哪个是正确的周期(我以为应该是period=1,但这是行不通的).

I'm pretty sure that the different amplitudes can be corrected with fftpack.diff's period keyword, but I can't figure which is the correct period (I thought it should be period=1 but that doesn't work).

此外,我如何使用numpy进行光谱区分?

Furthermore, how can I have my own spectral differentiation using numpy?

推荐答案

函数 scipy.fftpack.diff 计算导数,但假定输入是周期性的. period自变量给出输入序列的周期(即x间隔的总长度).

The function scipy.fftpack.diff computes the derivative, but it assumes that the input is periodic. The period argument gives the period (i.e. the total length of the x interval) of the input sequence.

在您的情况下,这是len(x)*dx,其中dx = x[1] - x[0].

In your case, this is len(x)*dx where dx = x[1] - x[0].

这里有一些代码使用period参数(红色)绘制简单(居中)的有限差(蓝色)和diff的结果.变量xy与代码中使用的变量相同:

Here's some code that plots the simple (centered) finite difference (in blue) and the result of diff using the period argument (in red). The variables x and y are the same as those used in your code:

In [115]: plt.plot(0.5*(x[1:]+x[:-1]), np.diff(y)/np.diff(x), 'b')
Out[115]: [<matplotlib.lines.Line2D at 0x1188d01d0>]

In [116]: plt.plot(x, sp.diff(y, period=len(x)*(x[1]-x[0])), 'r')
Out[116]: [<matplotlib.lines.Line2D at 0x1188fc9d0>]

In [117]: plt.xlabel('x')
Out[117]: <matplotlib.text.Text at 0x1157425d0>

请注意,如果您输入的内容实际上不是周期性的,则由diff计算的导数在区间末尾附近将不准确.

Note that if your input is not actually periodic, the derivative computed by diff will be inaccurate near the ends of the interval.

这是另一个示例,使用一个较短的序列,该序列在[0,1]间隔内仅包含一个完整的正弦函数周期:

Here's another example, using a shorter sequence that contains just one full period of a sine function in the interval [0, 1]:

In [149]: x = np.linspace(0, 1, 20, endpoint=False)

In [150]: y = np.sin(2*np.pi*x)

In [151]: plt.plot(0.5*(x[1:]+x[:-1]), np.diff(y)/np.diff(x), 'b')
Out[151]: [<matplotlib.lines.Line2D at 0x119872d90>]

In [152]: plt.plot(x, sp.diff(y, period=len(x)*(x[1]-x[0])), 'r')
Out[152]: [<matplotlib.lines.Line2D at 0x119c49090>]

In [153]: plt.xlabel('x')
Out[153]: <matplotlib.text.Text at 0x1197823d0>

这篇关于`numpy.diff`和`scipy.fftpack.diff`在区分时给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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