以下是84个值,它们是ColFWHM功能的输入。基于此值,函数计算单个值。但是,出于某种原因,我得到了" 0"为结果。我认为这是浮点/双处之间的类型转换的问题,无法弄清楚在哪里。任何建议解决这个问题表示赞赏。
数据 -
double[] datay ={0.0431373, 0.0470588, 0.054902, 0.0627451, 0.0745098, 0.0862745, 0.109804, 0.133333, 0.156863, 0.192157, 0.227451, 0.254902, 0.270588, 0.294118, 0.309804, 0.321569, 0.333333, 0.333333, 0.32549, 0.32549, 0.317647, 0.301961, 0.282353, 0.254902, 0.227451, 0.2, 0.176471, 0.152941, 0.129412, 0.113725, 0.0941176, 0.0784314, 0.0627451, 0.0470588, 0.0392157, 0.0352941, 0.027451, 0.0235294, 0.0196078, 0.0156863, 0.0156863, 0.0117647, 0.00784314, 0.00784314, 0.00784314, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00784314, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.0, 0.0, 0.0, 0.00392157, 0.00392157, 0.00392157, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.00392157, 0.00392157, 0.00392157, 0.00392157, 0.0, 0.0, 0.0, 0.00392157};
ColFWHM
/// <summary>
/// Input: 1D array; Return: FWHM value.
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static double ColFWHM(double[] data)
{
//double[] datax;
//double[] datay;
//Console.WriteLine(data.Length);
int L;
int Mag = 4;
double PP = 2.2;
int CI = 0;
//int y;
double Interp;
double Tlead;
double Ttrail;
double fwhm = 0;
L = data.Length;
double[] datay = new double[data.Length];
double max = data.Max();
CI = data.ToList().IndexOf(max);//Push index of peak to CI
Console.WriteLine("CI :" + CI);
//Create datay and normalize to max[data]
for (int f = 0; f < data.Length; f++)
{
datay[f] = data[f]/max;
}
Console.WriteLine("datay[] Length :" + datay.Length);
//create datax
int[] datax = new int[data.Length];
// Create datax as index for the number of elemts in data from 1-Length(data).
for (int i = 0; i < data.Length; i++)//[0,1,2]
{
datax[i] = (i + 1);
}
Console.WriteLine("datax[] Length :" + datax.Length);
//Console.WriteLine(datax.GetValue);
// Start to search lead
int k = 2;
while (Math.Sign(datay[k] - 0.5) == Math.Sign(datay[k - 1] - 0.5))
{
k = k + 1;
//Console.WriteLine("K:"+k);
}
Interp = (0.5 - datay[k - 1]) / (datay[k] - datay[k - 1]);
Console.WriteLine("Interp" + Interp);
Tlead = datax[k - 1] + Interp * (datax[k] - datax[k - 1]);
Console.WriteLine("Tlead" + Tlead);
CI = CI + 1;
// Start search for the trail
while (Math.Sign(datay[k] - 0.5) == Math.Sign(datay[k - 1] - 0.5) && (k <= L - 1))// less than
{
k = k + 1;
}
if (k != L)
{
Interp = (0.5 - datay[k - 1]) / (datay[k] - datay[k - 1]);
Ttrail = datax[k - 1] + Interp * (datax[k] - datax[k - 1]);
Console.WriteLine("Ttrail" + Ttrail);
fwhm = ((Ttrail - Tlead) / Mag) * PP;
}
Console.WriteLine("FWHM: " + fwhm);
return fwhm;
}//end main
答案 0 :(得分:2)
您可以在此处搜索&#34;线索&#34;:
int k = 2;
while (Math.Sign(datay[k] - 0.5) == Math.Sign(datay[k - 1] - 0.5))
{
k = k + 1;
//Console.WriteLine("K:"+k);
}
然后你会对跟踪进行完全相同的搜索:
// Start search for the trail
while ((Math.Sign(datay[k] - 0.5) == Math.Sign(datay[k - 1] - 0.5)) && (k <= L - 1))// less than
{
k = k + 1;
}
这个搜索对我来说似乎是多余的,因为你没有在任何地方重置k
的值,所以它永远不会实际循环,因为它满足了在第一次搜索中打破循环的条件。
这意味着您的Ttrail
和Tlead
是相同的,因此您的最终结果将计算为(9.2777667641639709 - 9.2777667641639709) / Mag * PP
,它始终为零。
即使将k
重置为2,也会导致与第一次搜索完全相同的行为,结果会相同。
答案 1 :(得分:0)
我不是100%肯定你的算法是什么,所以我不打算评论它的实现。
但是,您的方法返回0的原因是这一行:
fwhm = ((Ttrail - Tlead) / Mag) * PP;
对于您的示例数据,此时代码Ttrail - Tlead
中的值为零。这两个变量都是9.2777667641639709
。
这就是fwhm
为零的原因。