Skip to content

Commit 6f341bb

Browse files
authored
Merge pull request #66 from swharden/numerics
FftSharp.Complex → System.Numerics.Complex
2 parents e01aee0 + 535e906 commit 6f341bb

28 files changed

+1407
-1273
lines changed

README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ var window = new FftSharp.Windows.Hanning();
1515
window.ApplyInPlace(signal);
1616

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

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

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

3637
// plot the sample audio
37-
var plt = new ScottPlot.Plot(400, 200);
38-
plt.AddSignal(signal, sampleRate / 1000.0);
38+
ScottPlot.Plot plt = new();
39+
plt.AddSignal(signal, samplePeriod);
3940
plt.YLabel("Amplitude");
40-
plt.Margins(0);
4141
plt.SaveFig("time-series.png");
4242
```
4343

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

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

6566
// plot the sample audio
66-
var plt = new ScottPlot.Plot(400, 200);
67+
ScottPlot.Plot plt = new ScottPlot.Plot();
6768
plt.AddScatterLines(freq, psd);
6869
plt.YLabel("Power (dB)");
6970
plt.XLabel("Frequency (Hz)");
70-
plt.Margins(0);
7171
plt.SaveFig("periodogram.png");
7272
```
7373

@@ -82,12 +82,12 @@ plt.SaveFig("periodogram.png");
8282
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:
8383

8484
```cs
85-
Complex[] buffer =
85+
System.Numerics.Complex[] buffer =
8686
{
87-
new Complex(42, 0),
88-
new Complex(96, 0),
89-
new Complex(13, 0),
90-
new Complex(99, 0),
87+
new(real: 42, imaginary: 12),
88+
new(real: 96, imaginary: 34),
89+
new(real: 13, imaginary: 56),
90+
new(real: 99, imaginary: 78),
9191
};
9292

9393
FftSharp.Transform.FFT(buffer);

src/FftSharp.Demo/FftSharp.Demo.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
2121
<PackageReference Include="NAudio" Version="1.10.0" />
2222
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="5.8.64" />
23-
<PackageReference Include="ScottPlot" Version="4.1.27" />
24-
<PackageReference Include="ScottPlot.WinForms" Version="4.1.27" />
23+
<PackageReference Include="ScottPlot" Version="4.1.63" />
24+
<PackageReference Include="ScottPlot.WinForms" Version="4.1.63" />
2525
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
26-
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
26+
<PackageReference Include="System.Drawing.Common" Version="*" />
2727
</ItemGroup>
2828
</Project>

src/FftSharp.Demo/FormAudio.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using ScottPlot.Drawing.Colormaps;
2+
using System;
23
using System.Collections.Generic;
34
using System.ComponentModel;
45
using System.Data;
@@ -117,7 +118,8 @@ private void UpdateWindowed(double[] audio)
117118

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

123125
plotFFT.Plot.Clear();

src/FftSharp.Demo/FormMicrophone.Designer.cs

Lines changed: 118 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)