Skip to content

Commit 2284099

Browse files
committed
update test that generates readme header spectrogram
1 parent 435047c commit 2284099

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
[![](https://img.shields.io/azure-devops/build/swharden/swharden/9?label=Build&logo=azure%20pipelines)](https://dev.azure.com/swharden/swharden/_build/latest?definitionId=9&branchName=master)
44
[![Nuget](https://img.shields.io/nuget/v/Spectrogram?label=NuGet&logo=nuget)](https://www.nuget.org/packages/Spectrogram/)
55

6-
**Spectrogram** is a .NET library for creating spectrograms from pre-recorded signals or live audio from the sound card. Spectrogram uses FFT algorithms and window functions provided by the [FftSharp](https://github.com/swharden/FftSharp) project, and it targets .NET Standard 2.0 so it can be used in .NET Framework and .NET Core projects.
6+
**Spectrogram** is a .NET library for creating spectrograms from pre-recorded signals or live audio from the sound card. Spectrogram uses FFT algorithms and window functions provided by the [FftSharp](https://github.com/swharden/FftSharp) project, and it targets .NET Standard so it can be used in .NET Framework and .NET Core projects.
77

88
<div align="center">
99

10-
![](dev/spectrogram.png)
10+
![](dev/graphics/hal-spectrogram.png)
1111

1212
_"I'm sorry Dave... I'm afraid I can't do that"_
1313

dev/graphics/hal-spectrogram.png

81.5 KB
Loading

src/Spectrogram.Tests/Quickstart.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public void Test_Quickstart_Hal()
3333
Console.WriteLine(spec);
3434
}
3535

36+
[Test]
37+
public void Test_Readme_HeaderImage()
38+
{
39+
(double[] audio, int sampleRate) = TestTools.ReadWavWithNAudio("../../../../../data/cant-do-that-44100.wav");
40+
int fftSize = 2048;
41+
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 400, maxFreq: 6000);
42+
spec.Add(audio);
43+
spec.SaveImage("../../../../../dev/graphics/hal-spectrogram.png", intensity: 10, dB: true, dBScale: 500);
44+
45+
Console.WriteLine(spec);
46+
}
47+
3648
[Test]
3749
public void Test_Quickstart_Handel()
3850
{

src/Spectrogram/Image.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ namespace Spectrogram
1010
{
1111
public static class Image
1212
{
13-
public static Bitmap GetBitmap(List<double[]> ffts, Colormap cmap, double intensity = 1, bool dB = false, bool roll = false, int rollOffset = 0)
13+
public static Bitmap GetBitmap(
14+
List<double[]> ffts,
15+
Colormap cmap,
16+
double intensity = 1,
17+
bool dB = false,
18+
double dBScale = 1,
19+
bool roll = false,
20+
int rollOffset = 0)
1421
{
1522
if (ffts.Count == 0)
1623
throw new ArgumentException("This Spectrogram contains no FFTs (likely because no signal was added)");
@@ -40,7 +47,7 @@ public static Bitmap GetBitmap(List<double[]> ffts, Colormap cmap, double intens
4047
{
4148
double value = ffts[sourceCol][row];
4249
if (dB)
43-
value = 20 * Math.Log10(value + 1);
50+
value = 20 * Math.Log10(value * dBScale + 1);
4451
value *= intensity;
4552
value = Math.Min(value, 255);
4653
int bytePosition = (Height - 1 - row) * stride + col;

src/Spectrogram/Spectrogram.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,22 @@ public List<double[]> GetMelFFTs(int melBinCount)
136136
throw new InvalidOperationException("cannot get Mel spectrogram unless minimum frequency is 0Hz");
137137

138138
var fftsMel = new List<double[]>();
139-
foreach(var fft in ffts)
139+
foreach (var fft in ffts)
140140
fftsMel.Add(FftSharp.Transform.MelScale(fft, SampleRate, melBinCount));
141141

142142
return fftsMel;
143143
}
144144

145-
public Bitmap GetBitmap(double intensity = 1, bool dB = false, bool roll = false) =>
146-
Image.GetBitmap(ffts, cmap, intensity, dB, roll, NextColumnIndex);
145+
public Bitmap GetBitmap(double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false) =>
146+
Image.GetBitmap(ffts, cmap, intensity, dB, dBScale, roll, NextColumnIndex);
147147

148-
public Bitmap GetBitmapMel(int melBinCount = 25, double intensity = 1, bool dB = false, bool roll = false) =>
149-
Image.GetBitmap(GetMelFFTs(melBinCount), cmap, intensity, dB, roll, NextColumnIndex);
148+
public Bitmap GetBitmapMel(int melBinCount = 25, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false) =>
149+
Image.GetBitmap(GetMelFFTs(melBinCount), cmap, intensity, dB, dBScale, roll, NextColumnIndex);
150150

151151
[Obsolete("use SaveImage()", true)]
152152
public void SaveBitmap(Bitmap bmp, string fileName) { }
153153

154-
public void SaveImage(string fileName, double intensity = 1, bool dB = false, bool roll = false)
154+
public void SaveImage(string fileName, double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false)
155155
{
156156
if (ffts.Count == 0)
157157
throw new InvalidOperationException("Spectrogram contains no data. Use Add() to add signal data.");
@@ -170,10 +170,10 @@ public void SaveImage(string fileName, double intensity = 1, bool dB = false, bo
170170
else
171171
throw new ArgumentException("unknown file extension");
172172

173-
Image.GetBitmap(ffts, cmap, intensity, dB, roll, NextColumnIndex).Save(fileName, fmt);
173+
Image.GetBitmap(ffts, cmap, intensity, dB, dBScale, roll, NextColumnIndex).Save(fileName, fmt);
174174
}
175175

176-
public Bitmap GetBitmapMax(double intensity = 1, bool dB = false, bool roll = false, int reduction = 4)
176+
public Bitmap GetBitmapMax(double intensity = 1, bool dB = false, double dBScale = 1, bool roll = false, int reduction = 4)
177177
{
178178
List<double[]> ffts2 = new List<double[]>();
179179
for (int i = 0; i < ffts.Count; i++)
@@ -185,7 +185,7 @@ public Bitmap GetBitmapMax(double intensity = 1, bool dB = false, bool roll = fa
185185
d2[j] = Math.Max(d2[j], d1[j * reduction + k]);
186186
ffts2.Add(d2);
187187
}
188-
return Image.GetBitmap(ffts2, cmap, intensity, dB, roll, NextColumnIndex);
188+
return Image.GetBitmap(ffts2, cmap, intensity, dB, dBScale, roll, NextColumnIndex);
189189
}
190190

191191
public void SaveData(string filePath, int melBinCount = 0)

0 commit comments

Comments
 (0)