-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from arthurits/main
FFTfreq: fix off-by-one error
- Loading branch information
Showing
8 changed files
with
164 additions
and
34 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using NUnit.Framework; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace FftSharp.Tests | ||
{ | ||
internal class FftFreqTests | ||
{ | ||
[Test] | ||
public void Test_FftFreq_KnownValues() | ||
{ | ||
// https://github.com/swharden/FftSharp/issues/49 | ||
|
||
// generate signal with 2 Hz sine wave | ||
int sampleCount = 16; | ||
double sampleRateHz = 16; | ||
double sinFrequencyHz = 2; | ||
double[] samples = Enumerable.Range(0, sampleCount) | ||
.Select(i => Math.Sin(2 * Math.PI * sinFrequencyHz * i / sampleRateHz)) | ||
.ToArray(); | ||
|
||
// get FFT | ||
double[] fft = FftSharp.Transform.FFTmagnitude(samples); | ||
double[] fftKnown = { 0, 0, 1, 0, 0, 0, 0, 0, 0 }; | ||
Assert.That(fft, Is.EqualTo(fftKnown).Within(1e-10)); | ||
|
||
// calculate FFT frequencies both ways | ||
double[] fftFreqKnown = { 0, 1, 2, 3, 4, 5, 6, 7, 8 }; | ||
double[] fftFreq = FftSharp.Transform.FFTfreq(sampleRateHz, fft.Length); | ||
double[] fftFreq2 = FftSharp.Transform.FFTfreq(sampleRateHz, fft); | ||
Assert.That(fftFreq, Is.EqualTo(fftFreqKnown)); | ||
Assert.That(fftFreq2, Is.EqualTo(fftFreqKnown)); | ||
|
||
ScottPlot.Plot plt1 = new(400, 200); | ||
plt1.AddSignal(samples, sampleRateHz); | ||
TestTools.SaveFig(plt1, "signal"); | ||
|
||
ScottPlot.Plot plt2 = new(400, 200); | ||
plt2.AddScatter(fftFreq, fft); | ||
plt2.AddVerticalLine(2, System.Drawing.Color.Red, style: ScottPlot.LineStyle.Dash); | ||
TestTools.SaveFig(plt2, "fft"); | ||
} | ||
|
||
[Test] | ||
public void Test_Freq_Lookup() | ||
{ | ||
/* Compare against values generated using Python: | ||
* | ||
* >>> numpy.fft.fftfreq(16, 1/16000) | ||
* array([ 0., 1000., 2000., 3000., 4000., 5000., 6000., 7000., | ||
* -8000., -7000., -6000., -5000., -4000., -3000., -2000., -1000.]) | ||
* | ||
*/ | ||
|
||
int sampleRate = 16000; | ||
int pointCount = 16; | ||
double[] signal = new double[pointCount]; | ||
double[] fft = FftSharp.Transform.FFTmagnitude(signal); | ||
|
||
double[] freqsFullKnown = { | ||
0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, | ||
-8000, -7000, -6000, -5000, -4000, -3000, -2000, -1000 | ||
}; | ||
double[] freqsFull = FftSharp.Transform.FFTfreq(sampleRate, signal, oneSided: false); | ||
double[] freqsFull2 = FftSharp.Transform.FFTfreq(sampleRate, signal.Length, oneSided: false); | ||
Assert.That(freqsFull, Is.EqualTo(freqsFullKnown)); | ||
Assert.That(freqsFull2, Is.EqualTo(freqsFullKnown)); | ||
|
||
double[] freqsHalfKnown = { | ||
0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000 | ||
}; | ||
double[] freqsHalf = FftSharp.Transform.FFTfreq(sampleRate, fft, oneSided: true); | ||
double[] freqsHalf2 = FftSharp.Transform.FFTfreq(sampleRate, fft.Length, oneSided: true); | ||
Assert.That(freqsHalf, Is.EqualTo(freqsHalfKnown)); | ||
Assert.That(freqsHalf2, Is.EqualTo(freqsHalfKnown)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters