Skip to content

Commit

Permalink
Merge pull request #78 from swharden/73
Browse files Browse the repository at this point in the history
Fix RFFT_WithoutChecks overload
  • Loading branch information
swharden authored Aug 21, 2023
2 parents 6f4c322 + 18a4680 commit f12b9e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
18 changes: 17 additions & 1 deletion src/FftSharp.Tests/VsNumpy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void Test_VsNumpy_Fft_SystemNumericsComplex()
}

[Test]
public void Test_VsNumpy_Rfft()
public void Test_VsNumpy_ForwardRealDoubles()
{
System.Numerics.Complex[] rfft = FftSharp.FFT.ForwardReal(values);
System.Numerics.Complex[] numpyRfft = LoadData.Complex("fftReal.txt");
Expand All @@ -72,6 +72,22 @@ public void Test_VsNumpy_Rfft()
}
}

[Test]
public void Test_VsNumpy_ForwardRealComplex()
{
System.Numerics.Complex[] complexValues = values.Select(value => new System.Numerics.Complex(real: value, imaginary: 0)).ToArray();
System.Numerics.Complex[] rfft = FftSharp.FFT.ForwardReal(complexValues);
System.Numerics.Complex[] numpyRfft = LoadData.Complex("fftReal.txt");

Assert.AreEqual(numpyRfft.Length, rfft.Length);

for (int i = 0; i < rfft.Length; i++)
{
Assert.AreEqual(numpyRfft[i].Real, rfft[i].Real, 1e-10);
Assert.AreEqual(numpyRfft[i].Imaginary, rfft[i].Imaginary, 1e-10);
}
}

[Test]
public void Test_VsNumpy_FftMag()
{
Expand Down
21 changes: 15 additions & 6 deletions src/FftSharp/FftOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,21 @@ public static void RFFT_WithoutChecks(Span<System.Numerics.Complex> destination,
{
System.Numerics.Complex[] temp = ArrayPool<System.Numerics.Complex>.Shared.Rent(input.Length);

Span<System.Numerics.Complex> buffer = temp.AsSpan(0, input.Length);

FFT_WithoutChecks(buffer);
buffer.Slice(0, destination.Length).CopyTo(destination);

ArrayPool<System.Numerics.Complex>.Shared.Return(temp);
try
{
Span<System.Numerics.Complex> buffer = temp.AsSpan(0, input.Length);
input.CopyTo(buffer);
FFT.Forward(buffer);
buffer.Slice(0, destination.Length).CopyTo(destination);
}
catch (Exception ex)
{
throw new Exception("Could not calculate RFFT", ex);
}
finally
{
ArrayPool<System.Numerics.Complex>.Shared.Return(temp);
}
}

/// <summary>
Expand Down

0 comments on commit f12b9e0

Please sign in to comment.