// see https://code.google.com/p/jstk/source/browse/trunk/jstk/src/de/fau/cs/jstk/?r=154#jstk%2Fvc public void drawSpectrogram1(String prefix, String filename, float[][] data) { VB6Spectrogram vb6Spectrogram = new VB6Spectrogram(); vb6Spectrogram.ComputeColorPalette(); double numberOfSamplesX = data.Length; double numberOfSamplesY = data[0].Length; int width = (int)numberOfSamplesX; int height = (int)numberOfSamplesY; String filenameToSave = String.Format("C:\\{0}-{1}x{2}-{3}.png", prefix, width, height, System.IO.Path.GetFileNameWithoutExtension(filename)); System.Console.Out.WriteLine("Writing " + filenameToSave); int maxYIndex = height - 1; double horizontalScaleFactor = (double)width / numberOfSamplesX; double verticalScaleFactor = (double)height / numberOfSamplesY; // Now, you need to figure out the incremental jump between samples to adjust for the scale factor. This works out to be: int incrementX = (int)(numberOfSamplesX / (numberOfSamplesX * horizontalScaleFactor)); if (incrementX == 0) { incrementX = 1; } int incrementY = (int)(numberOfSamplesY / (numberOfSamplesY * verticalScaleFactor)); if (incrementY == 0) { incrementY = 1; } // prepare the data: double maxVal = double.MinValue; double minVal = double.MaxValue; for (int x = 0; x < data.Length; x++) { for (int y = 0; y < data[x].Length; y++) { if (data[x][y] > maxVal) { maxVal = data[x][y]; } if (data[x][y] < minVal) { minVal = data[x][y]; } } } double minIntensity = Math.Abs(minVal); double maxIntensity = maxVal + minIntensity; /* Create the image for displaying the data. */ Bitmap png = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(png); Rectangle rect = new Rectangle(0, 0, width, height); g.FillRectangle(Brushes.White, rect); /* Set scaleFactor so that the maximum value, after removing * the offset, will be 0xff. */ float scaleFactor = (float)(0xff / maxIntensity); for (int i = 0; i < numberOfSamplesX; i += incrementX) { for (int j = 0; j < numberOfSamplesY; j += incrementY) { int x = (int)MathUtils.RoundDown(i * horizontalScaleFactor, 0); int y = (int)MathUtils.RoundDown(j * verticalScaleFactor, 0); float f = data[i][j]; double d = (f + minIntensity) * scaleFactor; Color c = Color.White; int RangedB = 100; int RangePaletteIndex = 256; double indexDouble = VB6Spectrogram.MapToPixelIndex(f, RangedB, RangePaletteIndex); byte vb6Index = (byte)indexDouble; c = vb6Spectrogram.LevelPaletteDictionary[vb6Index]; png.SetPixel(x, maxYIndex - y, c); } } png.Save(filenameToSave); g.Dispose(); }
public void drawSpectrogram3(String prefix, String filename, float[][] data) { float minDb = -65.0f; float maxDb = 0.0f; double numberOfSamplesX = data.Length; double numberOfSamplesY = data[0].Length; // set width and height int width = (int)numberOfSamplesX; int height = (int)numberOfSamplesY; String filenameToSave = String.Format("C:\\{0}-{1}x{2}-{3}.png", prefix, width, height, System.IO.Path.GetFileNameWithoutExtension(filename)); System.Console.Out.WriteLine("Writing " + filenameToSave); double horizontalScaleFactor = (double)width / numberOfSamplesX; double verticalScaleFactor = (double)height / numberOfSamplesY; int maxYIndex = height - 1; // Now, you need to figure out the incremental jump between samples to adjust for the scale factor. This works out to be: int incrementX = (int)(numberOfSamplesX / (numberOfSamplesX * horizontalScaleFactor)); if (incrementX == 0) { incrementX = 1; } int incrementY = (int)(numberOfSamplesY / (numberOfSamplesY * verticalScaleFactor)); if (incrementY == 0) { incrementY = 1; } // prepare the data // retrieve the highest and lowest value double maxVal = double.MinValue; double minVal = double.MaxValue; for (int x = 0; x < data.Length; x++) { for (int y = 0; y < data[x].Length; y++) { if (data[x][y] > maxVal) { maxVal = data[x][y]; } if (data[x][y] < minVal) { minVal = data[x][y]; } } } float maxValdB = MathUtils.AmplitudeToDecibel((float)maxVal, minDb, maxDb); float minValdB = MathUtils.AmplitudeToDecibel((float)minVal, minDb, maxDb); double minIntensity = Math.Abs(minVal); double maxIntensity = maxVal + minIntensity; System.Console.Out.WriteLine("min value: {0}", minVal); System.Console.Out.WriteLine("max value: {0}", maxVal); System.Console.Out.WriteLine("min value: {0} dB", minValdB); System.Console.Out.WriteLine("max value: {0} dB", maxValdB); System.Console.Out.WriteLine("min intensity: {0}", minIntensity); System.Console.Out.WriteLine("max intensity: {0}", maxIntensity); // Create the image for displaying the data. Bitmap png = new Bitmap(width, height, PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(png); Rectangle rect = new Rectangle(0, 0, width, height); g.FillRectangle(Brushes.White, rect); for (int i = 0; i < numberOfSamplesX; i += incrementX) { for (int j = 0; j < numberOfSamplesY; j += incrementY) { int x = (int)MathUtils.RoundDown(i * horizontalScaleFactor, 0); int y = (int)MathUtils.RoundDown(j * verticalScaleFactor, 0); float amplitude = data[i][j]; float dB = MathUtils.AmplitudeToDecibel(amplitude, minDb, maxDb); int color = (int)MathUtils.ConvertAndMainainRatio(dB, minValdB, maxValdB, 0, 256); //Color c = VB6Spectrogram.PaletteValueColor(color, 256); Color c = VB6Spectrogram.GreyPaletteValueColor(color, 256); png.SetPixel(x, maxYIndex - y, c); } } png = ColorUtils.Colorize(png, 255, ColorUtils.ColorPaletteType.SOX); png.Save(filenameToSave); g.Dispose(); }
// see https://code.google.com/p/jstk/source/browse/trunk/jstk/src/de/fau/cs/jstk/?r=154#jstk%2Fvc public void drawSpectrogram1(String prefix, String filename, float[][] data) { VB6Spectrogram vb6Spectrogram = new VB6Spectrogram(); vb6Spectrogram.ComputeColorPalette(); double numberOfSamplesX = data.Length; double numberOfSamplesY = data[0].Length; int width = (int) numberOfSamplesX; int height = (int) numberOfSamplesY; String filenameToSave = String.Format("C:\\{0}-{1}x{2}-{3}.png", prefix, width, height, System.IO.Path.GetFileNameWithoutExtension(filename)); System.Console.Out.WriteLine("Writing " + filenameToSave); int maxYIndex = height - 1; double horizontalScaleFactor = (double) width / numberOfSamplesX; double verticalScaleFactor = (double) height/ numberOfSamplesY; // Now, you need to figure out the incremental jump between samples to adjust for the scale factor. This works out to be: int incrementX = (int) (numberOfSamplesX / (numberOfSamplesX * horizontalScaleFactor)); if (incrementX == 0) incrementX = 1; int incrementY = (int) (numberOfSamplesY / (numberOfSamplesY * verticalScaleFactor)); if (incrementY == 0) incrementY = 1; // prepare the data: double maxVal = double.MinValue; double minVal = double.MaxValue; for(int x = 0; x < data.Length; x++) { for(int y = 0; y < data[x].Length; y++) { if (data[x][y] > maxVal) maxVal = data[x][y]; if (data[x][y] < minVal) minVal = data[x][y]; } } double minIntensity = Math.Abs(minVal); double maxIntensity = maxVal + minIntensity; /* Create the image for displaying the data. */ Bitmap png = new Bitmap(width, height, PixelFormat.Format32bppArgb ); Graphics g = Graphics.FromImage(png); Rectangle rect = new Rectangle(0, 0, width, height); g.FillRectangle(Brushes.White, rect); /* Set scaleFactor so that the maximum value, after removing * the offset, will be 0xff. */ float scaleFactor = (float)(0xff / maxIntensity); for (int i = 0; i < numberOfSamplesX; i += incrementX) { for (int j = 0; j < numberOfSamplesY; j += incrementY) { int x = (int) MathUtils.RoundDown(i*horizontalScaleFactor,0); int y = (int) MathUtils.RoundDown(j*verticalScaleFactor,0); float f = data[i][j]; double d = (f + minIntensity) * scaleFactor; Color c = Color.White; int RangedB = 100; int RangePaletteIndex = 256; double indexDouble = VB6Spectrogram.MapToPixelIndex(f, RangedB, RangePaletteIndex); byte vb6Index = (byte) indexDouble; c = vb6Spectrogram.LevelPaletteDictionary[vb6Index]; png.SetPixel(x, maxYIndex - y, c); } } png.Save(filenameToSave); g.Dispose(); }