2

I am trying to filter out signal noise using Fourier Transform. Using functions from Accord.net I was able to apply FFT on an input signal and reconstruct it. However I am unable get the correct phase of the signal. After hours of Googling, I found many similar questions out there, but non were specific enough to help me figure this out.

Below is the code I got. Thanks in advance for the advice.

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;
}
  • Welcome to SO! Did you know there is a stack exchange site [just for signal processing](https://dsp.stackexchange.com/)? You might want to post this there instead. – John Wu May 22 '18 at 02:15

0 Answers0