Skip to content

Commit

Permalink
Merge pull request #66 from swharden/numerics
Browse files Browse the repository at this point in the history
FftSharp.Complex → System.Numerics.Complex
  • Loading branch information
swharden authored May 17, 2023
2 parents e01aee0 + 535e906 commit 6f341bb
Show file tree
Hide file tree
Showing 28 changed files with 1,407 additions and 1,273 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ var window = new FftSharp.Windows.Hanning();
window.ApplyInPlace(signal);

// Calculate the FFT as an array of complex numbers
Complex[] fftRaw = FftSharp.Transform.FFT(signal);
System.Numerics.Complex[] spectrum = FftSharp.FFT.Forward(signal);

// or get the magnitude (units²) or power (dB) as real numbers
double[] fftMag = FftSharp.Transform.FFTmagnitude(signal);
double[] fftPwr = FftSharp.Transform.FFTpower(signal);
double[] magnitude = FftSharp.FFT.Magnitude(spectrum);
double[] power = FftSharp.FFT.Power(spectrum);
```

Signal | Windowed Signal | FFT
Expand All @@ -32,12 +32,12 @@ Signal | Windowed Signal | FFT
// sample audio with tones at 2, 10, and 20 kHz plus white noise
double[] signal = FftSharp.SampleData.SampleAudio1();
int sampleRate = 48_000;
double samplePeriod = sampleRate / 1000.0;

// plot the sample audio
var plt = new ScottPlot.Plot(400, 200);
plt.AddSignal(signal, sampleRate / 1000.0);
ScottPlot.Plot plt = new();
plt.AddSignal(signal, samplePeriod);
plt.YLabel("Amplitude");
plt.Margins(0);
plt.SaveFig("time-series.png");
```

Expand All @@ -59,15 +59,15 @@ double[] signal = FftSharp.SampleData.SampleAudio1();
int sampleRate = 48_000;

// calculate the power spectral density using FFT
double[] psd = FftSharp.Transform.FFTpower(signal);
double[] freq = FftSharp.Transform.FFTfreq(sampleRate, psd.Length);
System.Numerics.Complex[] spectrum = FftSharp.FFT.Forward(audio);
double[] psd = FftSharp.FFT.Power(spectrum);
double[] freq = FftSharp.FFT.FrequencyScale(psd.Length, sampleRate);

// plot the sample audio
var plt = new ScottPlot.Plot(400, 200);
ScottPlot.Plot plt = new ScottPlot.Plot();
plt.AddScatterLines(freq, psd);
plt.YLabel("Power (dB)");
plt.XLabel("Frequency (Hz)");
plt.Margins(0);
plt.SaveFig("periodogram.png");
```

Expand All @@ -82,12 +82,12 @@ plt.SaveFig("periodogram.png");
If you are writing a performance application or just enjoy working with real and imaginary components of complex numbers, you can build your own complex array perform FFT operations on it in place:

```cs
Complex[] buffer =
System.Numerics.Complex[] buffer =
{
new Complex(42, 0),
new Complex(96, 0),
new Complex(13, 0),
new Complex(99, 0),
new(real: 42, imaginary: 12),
new(real: 96, imaginary: 34),
new(real: 13, imaginary: 56),
new(real: 99, imaginary: 78),
};

FftSharp.Transform.FFT(buffer);
Expand Down
6 changes: 3 additions & 3 deletions src/FftSharp.Demo/FftSharp.Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="NAudio" Version="1.10.0" />
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="5.8.64" />
<PackageReference Include="ScottPlot" Version="4.1.27" />
<PackageReference Include="ScottPlot.WinForms" Version="4.1.27" />
<PackageReference Include="ScottPlot" Version="4.1.63" />
<PackageReference Include="ScottPlot.WinForms" Version="4.1.63" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="System.Drawing.Common" Version="*" />
</ItemGroup>
</Project>
6 changes: 4 additions & 2 deletions src/FftSharp.Demo/FormAudio.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using ScottPlot.Drawing.Colormaps;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
Expand Down Expand Up @@ -117,7 +118,8 @@ private void UpdateWindowed(double[] audio)

private void UpdateFFT(double[] audio)
{
double[] ys = cbLog.Checked ? Transform.FFTpower(audio) : Transform.FFTmagnitude(audio);
System.Numerics.Complex[] spectrum = FftSharp.FFT.Forward(audio);
double[] ys = cbLog.Checked ? FftSharp.FFT.Power(spectrum) : FftSharp.FFT.Magnitude(spectrum);
string yLabel = cbLog.Checked ? "Power (dB)" : "Magnitude (RMS²)";

plotFFT.Plot.Clear();
Expand Down
226 changes: 118 additions & 108 deletions src/FftSharp.Demo/FormMicrophone.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 6f341bb

Please sign in to comment.