gpt4 book ai didi

wolfram-mathematica - 使用傅里叶变换获得频率和相位

转载 作者:行者123 更新时间:2023-12-04 08:16:50 27 4
gpt4 key购买 nike

假设我有一个周期函数的样本,从中获取频率和相位信息的好方法是什么?

特别是,我想得到一个像

a+b Cos[c x + d]

这是样本的一部分
{255,255,255,249,64,0,0,0,0,0,0,0,0,0,0,0,0,233,255,255,255,255,255,255,255,255,255,209,0,0,0,0,0,0,0,0,0,0,0,0,118,255,255,255,255,255,255,255,255,255,255,132,0,0,0,0,0,0,0,0,0,0,0,0,200,255,255,255,255,255,255,255,255,255,239,19,0,0,0,0,0,0,0,0,0,0,0,46,245,255,255,255,255,255,255,255,255,255,186,0}

最佳答案

使用自相关和 FindFit[ ]

(*Your list*)
ListPlot@l

enter image description here
(*trim the list*)
l1 = Drop[l, (First@Position[l, 0])[[1]] - 1];
l2 = Drop[l1, Length@l1 - (Last@Position[l1, 0])[[1]] - 1];
(*autocorrelate*)
ListLinePlot@(ac = ListConvolve[l2, l2, {1, 1}])

enter image description here
(*Find Period by taking means of maxs and mins spacings*)
period = Mean@
Join[
Differences@(maxs = Table[If[ac[[i - 1]] < ac[[i]] > ac[[i + 1]], i,
Sequence @@ {}], {i, 2, Length@ac - 1}]),
Differences@(mins = Table[If[ac[[i - 1]] > ac[[i]] < ac[[i + 1]], i,
Sequence @@ {}], {i, 2, Length@ac - 1}])];

(*Show it*)
Show[ListLinePlot[(ac = ListConvolve[l2, l2, {1, 1}]),
Epilog ->
Inset[Framed[Style["Mean Period = " <> ToString@N@period, 20],
Background -> LightYellow]]],
Graphics[Join[{Arrowheads[{-.05, .05}]}, {Red},
Sequence @@@ Arrow[{{{#[[1]], Min@ac}, {#[[2]], Min@ac}}}] & /@
Partition[mins, 2, 1], {Blue},
Sequence @@@ Arrow[{{{#[[1]], Max@ac}, {#[[2]], Max@ac}}}] & /@
Partition[maxs, 2, 1]]]]

enter image description here
(*Now let's fit the Cos[ ] to find the phase*)
model = a + b Cos[x (2 Pi)/period + phase];
ff = FindFit[l, model, {a, b, phase}, x,
Method -> NMinimize, MaxIterations -> 100];

(*Show results*)
Show[ListPlot[l, PlotRange -> All,
Epilog ->
Inset[Framed[Style["Phase = " <> ToString@N@(phase /. ff), 20],
Background -> LightYellow]]], Plot[model /. ff, {x, 1, 100}]]

enter image description here

关于wolfram-mathematica - 使用傅里叶变换获得频率和相位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6653369/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com