Skip to content

Commit 435047c

Browse files
committed
improve mel scale tests and quickstart
1 parent 2d27308 commit 435047c

7 files changed

Lines changed: 16 additions & 9 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,9 @@ Analytical spectrograms aimed at achieving maximum frequency resolution are pres
132132

133133
**To visualize frequency in a way that mimics human perception** we create a spectrogram that represents lower frequencies using a large portion of the image, and condense higher frequency ranges into smaller rows of pixels toward the top of the image. The [Mel Scale](https://en.wikipedia.org/wiki/Mel_scale) is commonly used to represent power spectral density this way, and the resulting _Mel Spectrogram_ has greatly reduced vertical resolution but is a better representation of human frequency perception.
134134

135-
Cropped Linear Scale (0-1kHz) | Full Linear Scale (0-22 kHz) | Mel Scale (0-22 kHz)
135+
Cropped Linear Scale (0-3kHz) | Mel Scale (0-22 kHz)
136136
---|---|---
137-
![](dev/graphics/halMelLinearCropped.png)|![](dev/graphics/halMelLinearFull.png)|![](dev/graphics/halMel.png)
137+
![](dev/graphics/halMel-LinearCropped.png)|![](dev/graphics/halMel-MelScale.png)
138138

139139
Amplitude perception in humans, like frequency perception, is logarithmic. Therefore, Mel spectrograms typically display log-transformed spectral power and are presented using Decibel units.
140140

@@ -145,10 +145,10 @@ Amplitude perception in humans, like frequency perception, is logarithmic. There
145145
// Create a traditional (linear) Spectrogram with dB units
146146
var spec = new Spectrogram(sampleRate, fftSize: 4096, stepSize: 500, maxFreq: 3000);
147147
spec.Add(audio);
148-
spec.SaveImage("hal.png", intensity: 4, dB: true);
148+
spec.SaveImage("hal.png", intensity: 4);
149149

150150
// Create a Mel Spectrogram with dB units
151-
Bitmap bmp = spec.GetBitmapMel(melSizePoints: 250, intensity: 4, dB: true);
151+
Bitmap bmp = spec.GetBitmapMel(melSizePoints: 250, intensity: 4);
152152
bmp.Save("halMel.png", ImageFormat.Png);
153153
```
154154

90.3 KB
Loading

dev/graphics/halMel-MelScale.png

30.6 KB
Loading

dev/graphics/halMel.png

-35.6 KB
Binary file not shown.
-168 KB
Binary file not shown.

dev/graphics/halMelLinearFull.png

-78.7 KB
Binary file not shown.

src/Spectrogram.Tests/Mel.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,23 @@ namespace Spectrogram.Tests
1010
class Mel
1111
{
1212
[Test]
13-
public void Test_Mel_Spectrogram()
13+
public void Test_MelSpectrogram_MelScale()
1414
{
1515
(double[] audio, int sampleRate) = TestTools.ReadWavWithNAudio("../../../../../data/cant-do-that-44100.wav");
1616
int fftSize = 4096;
1717
var spec = new Spectrogram(sampleRate, fftSize, stepSize: 500);
1818
spec.Add(audio);
19-
//spec.SaveImage("../../../../../dev/graphics/halNotMel.png", 2_000, true);
20-
21-
Bitmap bmp = spec.GetBitmapMel(250, 2_000, true);
22-
bmp.Save("../../../../../dev/graphics/halMel.png", ImageFormat.Png);
19+
20+
Bitmap bmpMel = spec.GetBitmapMel(250, 8_000);
21+
bmpMel.Save("../../../../../dev/graphics/halMel-MelScale.png", ImageFormat.Png);
22+
23+
Bitmap bmpRaw = spec.GetBitmap(8_000);
24+
Bitmap bmpCropped = new Bitmap(bmpRaw.Width, bmpMel.Height);
25+
using (Graphics gfx = Graphics.FromImage(bmpCropped))
26+
{
27+
gfx.DrawImage(bmpRaw, 0, bmpMel.Height - bmpRaw.Height);
28+
}
29+
bmpCropped.Save("../../../../../dev/graphics/halMel-LinearCropped.png", ImageFormat.Png);
2330
}
2431

2532
[Test]

0 commit comments

Comments
 (0)