Wolfram mathematica 用傅里叶变换求频率和相位

Wolfram mathematica 用傅里叶变换求频率和相位,wolfram-mathematica,Wolfram Mathematica,假设我有一个周期函数的样本,从中获取频率和相位信息的好方法是什么 特别是,我想得到一个像 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,

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

特别是,我想得到一个像

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}

请看
FourierDCT

您的数据看起来非常像
SquareWave
函数。通过手动检查,您的数据似乎符合
SquareWave[{025},(x+5)/23]

使用自相关和FindFit[]

(*使用最大和最小间隔查找周期*)
周期=平均值@
加入[
差异@(maxs=表[If[ac[[i-1]]ac[[i+1]],i,
序列@@{}],{i,2,Length@ac - 1}]), 
差异@(分钟=表[If[ac[[i-1]]>ac[[i]]
插图[框架[样式[“平均周期=”ToString@N@期间,20],
背景->浅黄色]]],
图形[Join[{箭头[{-.05,05}]},{Red},
序列@@Arrow[{{{#[[1]],Min@ac}, {#[[2]], Min@ac}}}] & /@ 
分区[mins,2,1],{Blue},
序列@@Arrow[{{{#[[1]],Max@ac}, {#[[2]], Max@ac}}}] & /@ 
分区[maxs,2,1]]]

(*现在让我们拟合Cos[]以找到阶段*)
模型=a+b Cos[x(2π)/周期+相位];
ff=FindFit[l,model,{a,b,phase},x,
方法->最小化,最大迭代->100];
(*显示结果*)
显示[ListPlot[l,PlotRange->All,
结语->
插入[框架[样式[“阶段=”ToString@N@(相位/.ff),20],
背景->浅黄色]],绘图[model/.ff,{x,1100}]

很高兴再次见到你!你的问题:为什么你想在那里安装一个
Cos[]
?好吧,只是想弄清楚如何获得信号的相位。它来自书本扫描中的校准图片,你可以对频率进行自相关,然后检测最大值和最小值。如果你修剪两边的点,自相关效果会更好。@Yaro很重要的一点是你不能一次拟合trig函数,你需要先找到频率。我不记得确切的原因,但它发生在我之前,发现这两个步骤的程序好得多
(*Your list*)
ListPlot@l
(*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}])
(*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]]]]
(*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}]]