Skip to content

Commit

Permalink
FFT.FftShift()
Browse files Browse the repository at this point in the history
resolves #69 and extends  #68
  • Loading branch information
swharden committed Aug 21, 2023
1 parent 4afc97a commit 019e770
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions dev/python/fftshift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scipy.fft
import numpy as np

if __name__ == "__main__":

values = np.arange(7)
print(values)
print(scipy.fft.fftshift(values))

values = np.arange(8)
print(values)
print(scipy.fft.fftshift(values))
18 changes: 18 additions & 0 deletions src/FftSharp.Tests/FftTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,22 @@ public void Test_FFT_FrequencyScale()
Assert.AreEqual(fftFreq[i], fftFreq[i], 1e-10);
}
}

[Test]
public void Test_FftShift_Odd()
{
double[] values = { 0, 1, 2, 3, 4, 5, 6 };
double[] shifted = FFT.FftShift(values);
double[] expectedResult = { 4, 5, 6, 0, 1, 2, 3 };
Assert.AreEqual(expectedResult, shifted);
}

[Test]
public void Test_FftShift_Even()
{
double[] values = { 0, 1, 2, 3, 4, 5, 6, 7 };
double[] shifted = FFT.FftShift(values);
double[] expectedResult = { 4, 5, 6, 7, 0, 1, 2, 3 };
Assert.AreEqual(expectedResult, shifted);
}
}
14 changes: 14 additions & 0 deletions src/FftSharp/FFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,4 +216,18 @@ public static double[] Power(System.Numerics.Complex[] spectrum, bool positiveOn

return output;
}

/// <summary>
/// Return a copy of the given values with the zero frequency component shifted to the center.
/// </summary>
public static double[] FftShift(double[] values)
{
int shiftBy = (values.Length + 1) / 2;

double[] values2 = new double[values.Length];
for (int i = 0; i < values.Length; i++)
values2[i] = values[(i + shiftBy) % values.Length];

return values2;
}
}

0 comments on commit 019e770

Please sign in to comment.