-
Notifications
You must be signed in to change notification settings - Fork 52
Closed
Description
Could it be possible that there is some offset when computing the sample frequency by calling FftSharp.Transform.FFTfreq()?
These are the steps I've followed and the results obtained:
- Create a 1-cycle 2-Hz-sinus wave at a sampling rate of 16 Hz (see 16 points 2 Hz sinus .txt) (The offset is less noticeable the higher the sampling frequency).
- Compute the FFT using:
signalFFT = FftSharp.Transform.FFTmagnitude(signalWindowed);
freq = FftSharp.Transform.FFTfreq(sampleFreq, signalFFT.Length);
plotFFT.Plot.AddScatterLines(freq, signalFFT);Possible explanation:
- The array returned by
FftSharp.Transform.FFTmagnitude()has one more point than half its size:new double[input.Length / 2 + 1](2n-1 +1). - Inside function
FftSharp.Transform.FFTfreq(), the frequency is computed as:
if (OneSided)
double fftPeriodHz = sampleRate / pointCount / 2;
else
double fftPeriodHz = sampleRate / pointCount;- The problem is that
pointCountis passed as the length of the array returned byFftSharp.Transform.FFTmagnitude()(2n-1 +1), so it has one more point than what is required to computefftPeriodHz.
So, i can think of two options:
- Subtract 1 from the array's length when calling
FftSharp.Transform.FFTfreq(). This might be counterintuitive and not obvious. - Perform the subtraction inside
FftSharp.Transform.FFTfreq():
if (OneSided)
double fftPeriodHz = sampleRate / (pointCount-1) / 2;
else
double fftPeriodHz = sampleRate / (pointCount-1);This last option produces the correct result for the sinus wave mentioned at the beginning. I hesitated before doing any PR since I'm not sure which one would you consider more appropriate.

Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
