使用STFT(短时傅里叶变换)后,输出是表示3d图的矩阵,好像(A[X, Y] = M)
A是输出矩阵,X是时间,Y是频率,第三维M是像素颜色强度所示的幅度,如下图所示:
如何使用C#中的图片中的渐变颜色绘制输出矩阵A?
是否有一个包含C#频谱图控件的库?
更新:
在对给定算法进行一些修改后,我可以绘制光谱图,我没有改变调色板,除了第一种颜色变为黑色,但我不知道它为什么会褪色!
这个代表声音
Bye Bye
这是pure sine wave
中的一个,所以它的频率几乎都是一样的
Pure sine wave Spectrogram
输出被接受它表示输入信号的频率如预期的那样,但我认为有一种方法可以使光谱图像示例中的那样进行说明,你能看一下我的代码并建议修改吗? / p>
这是事件处理程序:
private void SpectrogramButton_Click(object sender, EventArgs e)
{
Complex[][] SpectrogramData = Fourier_Transform.STFT(/*signal:*/ samples, /*windowSize:*/ 512, /*hopSize:*/ 512);
SpectrogramBox.Image = Spectrogram.DrawSpectrogram(SpectrogramData, /*Interpolation Factor:*/ 1000, /*Height:*/ 256);
}
这是我修改后的绘图功能:
public static Bitmap DrawSpectrogram(Complex[][] Data, int InterpolationFactor, int Height)
{
// target size:
Size sz = new Size(Data.GetLength(0), Height);
Bitmap bmp = new Bitmap(sz.Width, sz.Height);
// the data array:
//double[,] data = new double[222, 222];
// step sizes:
float stepX = 1f * sz.Width / Data.GetLength(0);
float stepY = 1f * sz.Height / Data[0].GetLength(0);
// create a few stop colors:
List<Color> baseColors = new List<Color>(); // create a color list
baseColors.Add(Color.Black);
baseColors.Add(Color.LightSkyBlue);
baseColors.Add(Color.LightGreen);
baseColors.Add(Color.Yellow);
baseColors.Add(Color.Orange);
baseColors.Add(Color.Red);
// and the interpolate a larger number of grdient colors:
List<Color> colors = interpolateColors(baseColors, InterpolationFactor);
// a few boring test data
//Random rnd = new Random(1);
//for (int x = 0; x < data.GetLength(0); x++)
// for (int y = 0; y < data.GetLength(1); y++)
// {
// //data[x, y] = rnd.Next((int)(300 + Math.Sin(x * y / 999) * 200)) +
// // rnd.Next(x + y + 111);
// data[x, y] = 0;
// }
// now draw the data:
float Max = Complex.Max(Data);
using (Graphics G = Graphics.FromImage(bmp))
for (int x = 0; x < Data.GetLength(0); x++)
for (int y = 0; y < Data[0].GetLength(0); y++)
{
int Val = (int)Math.Ceiling((Data[x][y].Magnitude / Max) * (InterpolationFactor - 1));
using (SolidBrush brush = new SolidBrush(colors[(int)Val]))
G.FillRectangle(brush, x * stepX, (Data[0].GetLength(0) - y) * stepY, stepX, stepY);
}
// and display the result
return bmp;
}
我真的不理解你在答案中谈论的log
事情,对不起我的小知识。
的更新
这是将log10
添加到幅度(忽略负值)后的输出:
我认为这个输出是可以接受的,它与我在开始时带来的例子有所不同,但我认为它更好。
答案 0 :(得分:1)
不,我知道没有开箱即用的控制。当然,你可以购买外面的图书馆,但是嘘,你不能就这些图书询问他们......
从理论上讲,你可以使用,或者我想我应该说滥用一个Chart
控件。但由于DataPoints
是相当昂贵的对象,或者至少比它们看起来更贵,所以这似乎不可取。
相反,您只需将图表绘制成Bitmap
自己。
第一步是决定颜色的渐变。有关此示例,请参阅interpolateColors function here!
然后,您只需使用floats
对步骤和像素大小对数据进行双循环,并在那里执行Graphics.FillRectangle
。
以下是使用GDI+
创建Bitmap
和Winforms PictureBox
进行展示的简单示例。它不会向图形添加任何轴并完全填充它。
它首先创建一些样本数据和一种1000
颜色的渐变。然后它绘制成Bitmap
并显示结果:
private void button6_Click(object sender, EventArgs e)
{
// target size:
Size sz = pictureBox1.ClientSize;
Bitmap bmp = new Bitmap(sz.Width, sz.Height);
// the data array:
double[,] data = new double[222, 222];
// step sizes:
float stepX = 1f * sz.Width / data.GetLength(0);
float stepY = 1f * sz.Height / data.GetLength(1);
// create a few stop colors:
List<Color> baseColors = new List<Color>(); // create a color list
baseColors.Add(Color.RoyalBlue);
baseColors.Add(Color.LightSkyBlue);
baseColors.Add(Color.LightGreen);
baseColors.Add(Color.Yellow);
baseColors.Add(Color.Orange);
baseColors.Add(Color.Red);
// and the interpolate a larger number of grdient colors:
List<Color> colors = interpolateColors(baseColors, 1000);
// a few boring test data
Random rnd = new Random(1);
for (int x = 0; x < data.GetLength(0); x++)
for (int y = 0; y < data.GetLength(1); y++)
{
data[x, y] = rnd.Next( (int) (300 + Math.Sin(x * y / 999) * 200 )) +
rnd.Next( x + y + 111);
}
// now draw the data:
using (Graphics G = Graphics.FromImage(bmp))
for (int x = 0; x < data.GetLength(0); x++)
for (int y = 0; y < data.GetLength(1); y++)
{
using (SolidBrush brush = new SolidBrush(colors[(int)data[x, y]]))
G.FillRectangle(brush, x * stepX, y * stepY, stepX, stepY);
}
// and display the result
pictureBox1.Image = bmp;
}
以下是链接中的功能:
List<Color> interpolateColors(List<Color> stopColors, int count)
{
SortedDictionary<float, Color> gradient = new SortedDictionary<float, Color>();
for (int i = 0; i < stopColors.Count; i++)
gradient.Add(1f * i / (stopColors.Count - 1), stopColors[i]);
List<Color> ColorList = new List<Color>();
using (Bitmap bmp = new Bitmap(count, 1))
using (Graphics G = Graphics.FromImage(bmp))
{
Rectangle bmpCRect = new Rectangle(Point.Empty, bmp.Size);
LinearGradientBrush br = new LinearGradientBrush
(bmpCRect, Color.Empty, Color.Empty, 0, false);
ColorBlend cb = new ColorBlend();
cb.Positions = new float[gradient.Count];
for (int i = 0; i < gradient.Count; i++)
cb.Positions[i] = gradient.ElementAt(i).Key;
cb.Colors = gradient.Values.ToArray();
br.InterpolationColors = cb;
G.FillRectangle(br, bmpCRect);
for (int i = 0; i < count; i++) ColorList.Add(bmp.GetPixel(i, 0));
br.Dispose();
}
return ColorList;
}
您可能希望使用标签等绘制轴。您可以使用Graphics.DrawString
或TextRenderer.DrawText
来执行此操作。只需在绘图区域周围留出足够的空间!
我使用转换为int
的数据值作为颜色表的直接指针。
根据您的数据,您需要缩小它们甚至使用日志转换。第一个图像显示对数比例从100到20k,第二个看起来线性从0到100。
如果您向我们展示您的数据结构,我们可以为您提供进一步的提示,以便如何调整代码以便使用它。
答案 1 :(得分:0)
您可以根据其他答案创建位图。使用颜色查找表将FFT对数幅度转换为用于每个像素或小矩形的颜色也很常见。