Skip to content

Commit 577d9d2

Browse files
authored
Merge pull request #45 from swharden/1.1.6
Spectrogram 1.1.6
2 parents f647f09 + 1ee81a9 commit 577d9d2

File tree

7 files changed

+25
-154
lines changed

7 files changed

+25
-154
lines changed

README.md

-52
Original file line numberDiff line numberDiff line change
@@ -153,58 +153,6 @@ Bitmap bmp = sg.GetBitmapMel(melSizePoints: 250);
153153
bmp.Save("halMel.png", ImageFormat.Png);
154154
```
155155

156-
## Spectrogram File Format (SFF)
157-
158-
The Spectrogram library has methods which can read and write SFF files, a file format specifically designed for storing spectrogram data. SFF files contain 2D spectrogram data (repeated FFTs) with a [small header](dev/sff) describing the audio and FFT settings suitable for deriving scale information.
159-
160-
SFF files store `double` values (8-byte floating-point data) which is far superior to saving spectrograms as indexed color images (which represent intensity with a single `byte` per pixel).
161-
162-
SFF files be saved using `Complex` data format (with real and imaginary values for each point) to faithfully represent the FFT output, or `double` format to represent magnitude (with an optional pre-conversion to Decibels to represent power).
163-
164-
### Create SFF Files with C#
165-
166-
This example creates a spectrogram but saves it using the SFF file format instead of saving it as an image. The SFF file can then be read in any language.
167-
168-
```cs
169-
(double[] audio, int sampleRate) = ReadWavMono("hal.wav");
170-
var sg = new SpectrogramGenerator(sampleRate, fftSize: 4096, stepSize: 700, maxFreq: 2000);
171-
sg.Add(audio);
172-
sg.SaveData("hal.sff");
173-
```
174-
175-
### Display SFF Files with C#
176-
Spectrogram data can be loaded from SFF files to facilitate rapid recall of data which can otherwise be resource-intensive to calculate. Spectrogram's `SFF` module facilitates this operation and has methods which can directly convert spectrograms to Bitmaps with options to customize the colormap, intensity, and Decibel scaling.
177-
178-
![](dev/sff/SffViewer/screenshot.png)
179-
180-
A simple SFF file viewer has been added to [dev/sff](dev/sff) and serves as a demonstration of how the `SFF` module can be used to generate spectrogram images from SFF files.
181-
182-
### Read SFF Files with Python
183-
A Python module to read SFF files has been created (in [dev/sff/python](dev/sff/python)) which allows Spectrograms created by this library and stored in SFF format to be loaded as 2D numpy arrays in Python.
184-
185-
This example demonstrates how the SFF file created in the previous C# example can be loaded into Python and displayed with matplotlib. This example has a few lines related to styling omitted for brevity, but the full Python demo can be found in [dev/sff/python](dev/sff/python).
186-
187-
```python
188-
import matplotlib.pyplot as plt
189-
import sffLib
190-
191-
# load spectrogram data as a 2D numpy array
192-
sf = sffLib.SpectrogramFile("hal.sff")
193-
194-
# display the spectrogram as a pseudocolor mesh
195-
plt.pcolormesh(freqs, times, sf.values)
196-
plt.colorbar()
197-
plt.show()
198-
```
199-
200-
![](dev/sff/python/hal.sff.png)
201-
202-
## Resources
203-
* [FftSharp](https://github.com/swharden/FftSharp) - the module which actually performs the FFT and related transformations
204-
* [MP3Sharp](https://github.com/ZaneDubya/MP3Sharp) - a library I use to read MP3 files during testing
205-
* [FSKview](https://github.com/swharden/FSKview) - a real-time spectrogram for viewing frequency-shift-keyed (FSK) signals from audio transmitted over radio frequency.
206-
* [NAudio](https://github.com/naudio/NAudio) - an open source .NET library which makes it easy to get samples from the microphone or sound card in real time
207-
208156
## Read data from a WAV File
209157

210158
You should customize your file-reading method to suit your specific application. I frequently use the NAudio package to read data from WAV and MP3 files. This function reads audio data from a mono WAV file and will be used for the examples on this page.

src/Spectrogram.MicrophoneDemo/Spectrogram.Demo.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ProjectReference Include="..\Spectrogram\Spectrogram.csproj" />
1111
</ItemGroup>
1212
<ItemGroup>
13-
<PackageReference Include="FftSharp" Version="1.1.2" />
13+
<PackageReference Include="FftSharp" Version="1.1.6" />
1414
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
1515
<PackageReference Include="NAudio" Version="1.10.0" />
1616
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />

src/Spectrogram.Tests/FileFormat.cs

-97
This file was deleted.

src/Spectrogram/SFF.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
namespace Spectrogram
1010
{
11-
// Spectrogram File Format reader/writer
11+
[Obsolete("The SFF file format is obsolete. " +
12+
"Users are encouraged to write their own IO routines specific to their application. "+
13+
"To get a copy of the original SFF reader/writer see https://github.com/swharden/Spectrogram/issues/44",
14+
error: true)]
1215
public class SFF
1316
{
1417
public readonly byte VersionMajor = 1;

src/Spectrogram/Spectrogram.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>1.4.4</Version>
5+
<Version>1.5.0</Version>
66
<Description>A .NET Standard library for creating spectrograms</Description>
77
<Authors>Scott Harden</Authors>
88
<Company>Harden Technologies, LLC</Company>
@@ -29,10 +29,10 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="FftSharp" Version="1.1.2" />
32+
<PackageReference Include="FftSharp" Version="1.1.6" />
3333
<PackageReference Include="System.Drawing.Common" Version="4.6.1" />
3434
<PackageReference Include="runtime.osx.10.10-x64.CoreCompat.System.Drawing" Version="5.8.64" Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'" />
35-
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
35+
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
3636
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3737
<PrivateAssets>all</PrivateAssets>
3838
</PackageReference>

src/Spectrogram/SpectrogramGenerator.cs

+4
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ public Bitmap GetBitmapMax(double intensity = 1, bool dB = false, double dBScale
350350
return Image.GetBitmap(ffts2, Colormap, intensity, dB, dBScale, roll, NextColumnIndex);
351351
}
352352

353+
[Obsolete("The SFF file format is obsolete. " +
354+
"Users are encouraged to write their own IO routines specific to their application. " +
355+
"To get a copy of the original SFF reader/writer see https://github.com/swharden/Spectrogram/issues/44",
356+
error: true)]
353357
/// <summary>
354358
/// Export spectrogram data using the Spectrogram File Format (SFF)
355359
/// </summary>

src/Spectrogram/Tools.cs

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ namespace Spectrogram
77
{
88
public static class Tools
99
{
10+
[Obsolete("The SFF file format is obsolete. " +
11+
"Users are encouraged to write their own IO routines specific to their application. " +
12+
"To get a copy of the original SFF reader/writer see https://github.com/swharden/Spectrogram/issues/44",
13+
error: true)]
1014
/// <summary>
1115
/// Collapse the 2D spectrogram into a 1D array (mean power of each frequency)
1216
/// </summary>
@@ -31,6 +35,11 @@ public static double[] SffMeanFFT(SFF sff, bool dB = false)
3135
return mean;
3236
}
3337

38+
39+
[Obsolete("The SFF file format is obsolete. " +
40+
"Users are encouraged to write their own IO routines specific to their application. " +
41+
"To get a copy of the original SFF reader/writer see https://github.com/swharden/Spectrogram/issues/44",
42+
error: true)]
3443
/// <summary>
3544
/// Collapse the 2D spectrogram into a 1D array (mean power of each time point)
3645
/// </summary>
@@ -48,6 +57,10 @@ public static double[] SffMeanPower(SFF sff, bool dB = false)
4857
return power;
4958
}
5059

60+
[Obsolete("The SFF file format is obsolete. " +
61+
"Users are encouraged to write their own IO routines specific to their application. " +
62+
"To get a copy of the original SFF reader/writer see https://github.com/swharden/Spectrogram/issues/44",
63+
error: true)]
5164
public static double GetPeakFrequency(SFF sff, bool firstFftOnly = false)
5265
{
5366
double[] freqs = firstFftOnly ? sff.Ffts[0] : SffMeanFFT(sff, false);

0 commit comments

Comments
 (0)