在 Mathematica 中使用插值函数进行卷积

发布于 2024-09-24 17:10:27 字数 1188 浏览 7 评论 0原文

我正在使用 Mathematica 7。

我有一个插值函数,这里有一个例子:

pressures = 
  WeatherData["Chicago", "Pressure", {2010, 8}] // 
     DeleteCases[#, {_, _Missing}] & // 
    Map[{AbsoluteTime[#[[1]]], #[[2]]} &, #] & // Interpolation;

我想计算它的导数,这很简单:

dpressures = D[pressures[x], x]

现在,如果你绘制这个函数

Plot[3600*dpressures, {x, AbsoluteTime[{2010, 8, 2}], AbsoluteTime[{2010, 8, 30}]}]

(抱歉,不知道如何发布图像来自 Mathematica 内部,并且没有时间去弄清楚。)您会发现它非常嘈杂。所以,我想把它弄平。我的第一个想法是使用 Convolve,并将其与高斯核集成,如下所示:

a = Convolve[PDF[NormalDistribution[0, 5], x], 3600*dpressures, x, y]

Returns

360 Sqrt[2/\[Pi]] Convolve[E^(-(x^2/50)), InterpolatingFunction[{{3.48961266 10^9, 3.49228746 10^9}},<>], ][x], x, y]

这对我来说看起来很合理。不幸的是,我相信我在某个地方犯了错误,因为我得到的结果似乎无法评估。那就是:

a /. y -> AbsoluteTime[{2010, 8, 2}]

Returns

360 Sqrt[2/\[Pi]] Convolve[E^(-(x^2/50)), InterpolatingFunction[{{3.48961266 10^9, 3.49228746 10^9}},<>][x], x, 3489696000]]

这不是我想要的,我期待一个 -1 到 1 之间的数字。

I'm using Mathematica 7.

I have an interpolated function, here's an example:

pressures = 
  WeatherData["Chicago", "Pressure", {2010, 8}] // 
     DeleteCases[#, {_, _Missing}] & // 
    Map[{AbsoluteTime[#[[1]]], #[[2]]} &, #] & // Interpolation;

I'd like to compute it's derivative, which is straight forward:

dpressures = D[pressures[x], x]

Now, If you plot this funciton

Plot[3600*dpressures, {x, AbsoluteTime[{2010, 8, 2}], AbsoluteTime[{2010, 8, 30}]}]

(sorry, don't know how to post the image from within Mathematica, and don't have time to figure it out.) You'll find that it's very noisy. So, I'd like to smooth it out. My first thought was to use Convolve, and integrate it against a Gaussian kernel, something like the following:

a = Convolve[PDF[NormalDistribution[0, 5], x], 3600*dpressures, x, y]

Returns

360 Sqrt[2/\[Pi]] Convolve[E^(-(x^2/50)), InterpolatingFunction[{{3.48961266 10^9, 3.49228746 10^9}},<>], ][x], x, y]

Which looks reasonable to me. Unfortunately, I believe I have made a mistake somewhere, because the result I get back does not seem to be evaluatable. That is:

a /. y -> AbsoluteTime[{2010, 8, 2}]

Returns

360 Sqrt[2/\[Pi]] Convolve[E^(-(x^2/50)), InterpolatingFunction[{{3.48961266 10^9, 3.49228746 10^9}},<>][x], x, 3489696000]]

Which just ain't what I was looking for I'm expecting a number between -1 and 1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云不回头 2024-10-01 17:10:27

卷积寻求卷积的闭合形式。您可以尝试数值卷积,从类似的内容开始

NConvolve[f_, g_, x_, y_?NumericQ] := 
 NIntegrate[f (g /. x -> y - x), {x, -Infinity, Infinity}]

。但是,对于这个嘈杂的非平滑函数,数值积分将很困难。 (它无法使用默认设置,并且即使仔细选择设置也会很慢。)

我建议您直接对基础数据进行操作,而不是对噪声数据进行插值。

时间范围的界限:

In[89]:= {lower = Min[First[pressures]], upper = Max[First[pressures]]}    
Out[89]= {3.48961*10^9, 3.49229*10^9}

使用插值每小时获取样本*:

data = Table[pressures[x], {x, lower, upper, 3600}];

现在

ListLinePlot[Differences[data]]

与 5 小时窗口内的平滑版本进行比较:

ListLinePlot[GaussianFilter[Differences[data], 5]]
  • 您可能需要使用 InterpolationOrder -> 1 对于噪声数据。

Convolve seeks a closed form for the convolution. You could try a numerical convolution, starting with something like

NConvolve[f_, g_, x_, y_?NumericQ] := 
 NIntegrate[f (g /. x -> y - x), {x, -Infinity, Infinity}]

However, for this noisy non-smooth function the numerical integration will struggle. (It won't work with default settings, and would be slow even with carefully chosen settings.)

I suggest you operate directly on the underlying data instead of interpolating the noisy data.

The bounds of your time range:

In[89]:= {lower = Min[First[pressures]], upper = Max[First[pressures]]}    
Out[89]= {3.48961*10^9, 3.49229*10^9}

Use your interpolation to get samples every hour*:

data = Table[pressures[x], {x, lower, upper, 3600}];

Now compare

ListLinePlot[Differences[data]]

with smoothed version over 5 hour window:

ListLinePlot[GaussianFilter[Differences[data], 5]]
  • You may want to use InterpolationOrder -> 1 for noisy data.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文