swharden/FftSharp

How to use FFTfreqPeriod?

MimmoGhi opened this issue · 1 comments

Hello
scuasa I can't find the FFTfreqPeriod procedure in FFTSharp the development tool tells me that it doesn't exist
What can I use to replace it?

I wrote so taking a vs example

double [] paddedAudio = FftSharp.Pad.ZeroPad (AudioValues);
double [] fftMag = FftSharp.Transform.FFTpower (paddedAudio);
Array.Copy (fftMag, FftValues, fftMag.Length);

// find the frequency peak
int peakIndex = 0;
for (int i = 0; i <fftMag.Length; i ++)
{
  if (fftMag [i]> fftMag [peakIndex])
  peakIndex = i;
}
double fftPeriod = FftSharp.Transform.FFTfreqPeriod (SampleRate, fftMag.Length);
double peakFrequency = fftPeriod * peakIndex;

I have to find the frequency to which a parsona sings
Thank you

Mimmo

Hi @MimmoGhi, this works for me. Where do you get stuck?

// create sample data
int sampleRate = 8000;
double[] sampleData = new double[32768];
double toneFrequency = 500;
for (int i = 0; i < sampleData.Length; i++)
    sampleData[i] = Math.Sin(i * toneFrequency / sampleRate * 2 * Math.PI);

// plot the raw data
ScottPlot.Plot plt1 = new(400, 300);
plt1.AddSignal(sampleData, sampleRate);
plt1.SetAxisLimitsX(0, .01);
plt1.SaveFig("data.png");

// get FFT and FFT frequencies
double[] fft = FftSharp.Transform.FFTpower(sampleData);
double fftFreqPeriod = FftSharp.Transform.FFTfreqPeriod(sampleRate, fft.Length);
double fftFreqSpacing = 1.0 / fftFreqPeriod;

// plot the FFT
ScottPlot.Plot plt2 = new(400, 300);
plt2.AddSignal(fft, fftFreqSpacing);
plt2.SaveFig("fft.png");
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="fftsharp" Version="1.1.6" />
    <PackageReference Include="scottplot" Version="4.1.58" />
  </ItemGroup>

</Project>

data

fft