public static Bitmap BitmapFromFFTs(float[][] ffts, Settings.DisplaySettings displaySettings) { if (ffts == null || ffts.Length == 0) { throw new ArgumentException("ffts must contain float arrays"); } Bitmap bmp = new Bitmap(ffts.Length, displaySettings.height, PixelFormat.Format8bppIndexed); ApplyColormap(bmp, displaySettings.colormap); var rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bitmapData = bmp.LockBits(rect, ImageLockMode.ReadOnly, bmp.PixelFormat); byte[] pixels = new byte[bitmapData.Stride * bmp.Height]; for (int col = 0; col < bmp.Width; col++) { if (col >= bmp.Width) { continue; } if (col == displaySettings.highlightColumn) { for (int row = 0; row < bmp.Height; row++) { int bytePosition = (bmp.Height - 1 - row) * bitmapData.Stride + col; pixels[bytePosition] = 255; } continue; } if (ffts[col] == null) { continue; } for (int row = 0; row < bmp.Height; row++) { int bytePosition = (bmp.Height - 1 - row) * bitmapData.Stride + col; float pixelValue; pixelValue = ffts[col][row + displaySettings.pixelLower]; if (displaySettings.decibels) { pixelValue = (float)(Math.Log10(pixelValue) * 20); } pixelValue = (pixelValue * displaySettings.brightness); pixelValue = Math.Max(0, pixelValue); pixelValue = Math.Min(255, pixelValue); pixels[bytePosition] = (byte)(pixelValue); } } Marshal.Copy(pixels, 0, bitmapData.Scan0, pixels.Length); bmp.UnlockBits(bitmapData); return(bmp); }
public Spectrogram(int sampleRate = 8000, int fftSize = 1024, int? step = null) { if (step == null) step = sampleRate; fftSettings = new Settings.FftSettings(sampleRate, fftSize, (int)step); displaySettings = new Settings.DisplaySettings(); displaySettings.fftResolution = fftSettings.fftResolution; displaySettings.freqHigh = fftSettings.maxFreq; }