-1

I'm trying to use FFT filter on a list of points ( each point has x and y coordinates) i should get in return a list<Complex>.

when testing the code below (on a list containing 12 points) i get this error

System.ArgumentException : The given array is too small. It must be at least 14 long. at MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal(Double[] data, Int32 n, FourierOptions options)

I'm actually using Math.net MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal(buffer, buffer.Length, FourierOptions.Matlab);

This is my class's code

        // inputs is a List of Point(List<Point> inputs)
        var buffer=inputs.Select(p => (p.Y)).ToArray();
        try
        {
            MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal(buffer, buffer.Length, FourierOptions.Matlab);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }

Any idea how to fix it ? thank you :)

AMI
  • 97
  • 12
  • 5
    "It must be at least 14 long." and "on a list containing 12 points" seem to explain the problem... What type of help you are looking here? – Alexei Levenkov May 23 '17 at 16:45
  • I already tried with a list with 14 points it's always the same problem ( System.ArgumentException : The given array is too small. It must be at least 16 long. at MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal(Double[] data, Int32 n, FourierOptions options) – AMI May 23 '17 at 17:44

2 Answers2

2

Based on the fine manual the buffer needs to be 2 items longer than data you have.

https://numerics.mathdotnet.com/api/MathNet.Numerics.IntegralTransforms/Fourier.htm#ForwardReal

void ForwardReal(Double[] data, int n, FourierOptions options)

... The data array needs to be N+2 (if N is even) or N+1 (if N is odd) long in order to support such a packed spectrum.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
1

when i read the code on github, the ideas are much clearer now. So this is the rectification of my code,

        var inputsCount = inputs.Count;
        var buffer =new double[inputsCount % 2 == 0 ? inputsCount + 2 : inputsCount + 1];
        int j = 0;

        foreach (var point in inputs)
        {
            buffer[j] = point.Y;
            j++;
        }

        try
        {
            MathNet.Numerics.IntegralTransforms.Fourier.ForwardReal(buffer, inputsCount, FourierOptions.Matlab);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
AMI
  • 97
  • 12