我正在尝试使用傅立叶变换滤除信号噪声。使用Accord.net中的函数我能够对输入信号应用FFT并重建它。但是我无法获得正确的信号相位。经过几个小时的谷歌搜索,我发现了很多类似的问题,但非特定的问题足以帮我解决这个问题。
以下是我得到的代码。提前感谢您的建议。
void Test()
{
var testData = new List<double>();
var stepSize = (2.0 * Math.PI * 2)/1024;
for ( int i = 0; i < RESOLUTION; i++ )
{
var scaledI = i * stepSize;
testData.Add(
5.0 * Math.Sin( scaledI + 2) +
( rnd.NextDouble() - 0.5 ) * 2.2 // Add some noise
);
}
var filteredData = ApplyFilter(testData, stepSize);
}
public static List<double> ApplyFilter( List<double> orderedData, double stepSize)
{
var result = new List<double>();
var transformedData = orderedData.Select( d => new Complex( d, 0 )).ToArray();
FourierTransform2.FFT( transformedData, FourierTransform.Direction.Forward );
var firstPart = transformedData.Take(orderedData.Count/2).ToArray();
var freqThreshold = firstPart.Max( f => Complex.Abs(f)) * 0.1;
for ( int i = 0; i < orderedData.Count; i++ )
{
var synthesizedOutput = Synthesize( firstPart, i * stepSize, orderedData.Count, freqThreshold );
result.Add( synthesizedOutput );
}
return result;
}
static public double Synthesize( Complex[] frequencyBins, double x, int dataCount, double freqThreshold )
{
double result = 0;
for ( int i = 0; i < frequencyBins.Length; i++ )
{
var freq = frequencyBins[i];
if ( Complex.Abs( freq ) < freqThreshold )
continue;
var phase = freq.Phase;
var mangitude = freq.Magnitude * 2 / dataCount;
result += mangitude * Math.Sin( i * x * 2.0 * Math.PI / dataCount + phase );
}
return result;
}