matlab 小波包生成数据利用c#画出热力图 代码实现
时间: 2023-06-13 18:06:49 浏览: 78
以下是使用 Matlab 生成小波包数据,并且使用 C# 画出热力图的代码实现:
Matlab 代码:
```
% 小波包生成数据
t = linspace(0, 1, 1024);
x = sin(32*pi*t) + 5 * cos(64*pi*t) + 2 * randn(size(t));
wpt = wpdec(x, 4, 'db4');
% 将小波包系数转换为矩阵
wpmtx = wp2mat(wpt);
% 保存小波包系数矩阵
save('wpmtx.mat', 'wpmtx');
```
C# 代码:
```
using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace HeatMap
{
public partial class Form1 : Form
{
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);
[DllImport("kernel32.dll")]
static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern int GetShortPathName(
[MarshalAs(UnmanagedType.LPTStr)]
string path,
[MarshalAs(UnmanagedType.LPTStr)]
StringBuilder shortPath,
int shortPathLength
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LoadLibraryEx(
[MarshalAs(UnmanagedType.LPTStr)]
string lpFileName,
IntPtr hFile,
uint dwFlags
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LoadResource(
IntPtr hModule,
IntPtr hResInfo
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr LockResource(
IntPtr hResData
);
[DllImport("kernel32.dll", SetLastError = true)]
static extern uint SizeofResource(
IntPtr hModule,
IntPtr hResInfo
);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr GetDC(
IntPtr hWnd
);
[DllImport("user32.dll", SetLastError = true)]
static extern int ReleaseDC(
IntPtr hWnd,
IntPtr hDC
);
[DllImport("gdi32.dll", SetLastError = true)]
static extern IntPtr CreateCompatibleDC(
IntPtr hDC
);
[DllImport("gdi32.dll", SetLastError = true)]
static extern IntPtr CreateCompatibleBitmap(
IntPtr hDC,
int nWidth,
int nHeight
);
[DllImport("gdi32.dll", SetLastError = true)]
static extern IntPtr SelectObject(
IntPtr hDC,
IntPtr hObject
);
[DllImport("gdi32.dll", SetLastError = true)]
static extern bool BitBlt(
IntPtr hDCDest,
int nXDest,
int nYDest,
int nWidth,
int nHeight,
IntPtr hDCSrc,
int nXSrc,
int nYSrc,
uint dwRop
);
[DllImport("gdi32.dll", SetLastError = true)]
static extern bool DeleteDC(
IntPtr hDC
);
[DllImport("gdi32.dll", SetLastError = true)]
static extern bool DeleteObject(
IntPtr hObject
);
private const int IMAGE_BITMAP = 0;
private const int LR_LOADFROMFILE = 0x10;
private const int LR_CREATEDIBSECTION = 0x2000;
private const uint SRCCOPY = 0xCC0020;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// 获取当前工作目录
string curDir = Directory.GetCurrentDirectory();
// 获取 Matlab Runtime 的安装目录
string mcrDir = GetShortPathName(Path.Combine(curDir, @"mcr"));
// 加载 Matlab Runtime 的依赖库
LoadLibrary(Path.Combine(mcrDir, @"bin\win64\mclmcrrt8_2.dll"));
LoadLibrary(Path.Combine(mcrDir, @"bin\win64\mclmcr.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\kernel32.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\user32.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\gdi32.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\advapi32.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\msvcrt.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\version.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\ole32.dll"));
LoadLibrary(Path.Combine(mcrDir, @"sys\os\win64\oleaut32.dll"));
// 从 Matlab 生成的小波包系数矩阵文件中读取数据
double[,] wpmtx;
using (var reader = new BinaryReader(File.OpenRead("wpmtx.mat")))
{
reader.ReadChar();
reader.ReadChar();
reader.ReadChar();
reader.ReadChar();
reader.ReadChar();
reader.ReadChar();
reader.ReadChar();
reader.ReadChar();
uint version = reader.ReadUInt32();
uint endian = reader.ReadUInt32();
uint arrayFlags = reader.ReadUInt32();
uint dimensions = reader.ReadUInt32();
uint rows = reader.ReadUInt32();
uint cols = reader.ReadUInt32();
reader.ReadBytes(8);
wpmtx = new double[cols, rows];
for (int j = 0; j < rows; j++)
{
for (int i = 0; i < cols; i++)
{
wpmtx[i, j] = reader.ReadDouble();
}
}
}
// 绘制热力图
DrawHeatmap(wpmtx);
}
private void DrawHeatmap(double[,] data)
{
// 获取绘图区域的宽度和高度
int width = pictureBox1.Width;
int height = pictureBox1.Height;
// 创建 Bitmap 对象,并且设置其分辨率
Bitmap bmp = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
bmp.SetResolution(96.0f, 96.0f);
// 创建 GDI+ 绘图对象
Graphics g = Graphics.FromImage(bmp);
// 获取绘图区域的句柄
IntPtr hdc = GetDC(pictureBox1.Handle);
// 创建兼容的 GDI 绘图对象
IntPtr hdcMem = CreateCompatibleDC(hdc);
// 创建兼容的 Bitmap 对象
IntPtr hBitmap = bmp.GetHbitmap(Color.FromArgb(0));
// 将 Bitmap 对象选择进 GDI 绘图对象
SelectObject(hdcMem, hBitmap);
// 计算数据的最大值和最小值
double maxData = 0.0;
double minData = 0.0;
for (int j = 0; j < data.GetLength(1); j++)
{
for (int i = 0; i < data.GetLength(0); i++)
{
if (data[i, j] > maxData)
{
maxData = data[i, j];
}
if (data[i, j] < minData)
{
minData = data[i, j];
}
}
}
// 计算每个像素对应的数据值
double[,] pixelData = new double[width, height];
double xScale = (double)data.GetLength(0) / (double)width;
double yScale = (double)data.GetLength(1) / (double)height;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int x0 = (int)Math.Floor(x * xScale);
int y0 = (int)Math.Floor(y * yScale);
int x1 = (int)Math.Ceiling((x + 1) * xScale);
int y1 = (int)Math.Ceiling((y + 1) * yScale);
double sum = 0.0;
int count = 0;
for (int j = y0; j < y1; j++)
{
for (int i = x0; i < x1; i++)
{
if (i < data.GetLength(0) && j < data.GetLength(1))
{
sum += data[i, j];
count += 1;
}
}
}
pixelData[x, y] = sum / count;
}
}
// 绘制热力图
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
double value = pixelData[x, y];
double ratio = (value - minData) / (maxData - minData);
int color = (int)Math.Floor(ratio * 255.0);
g.FillRectangle(new SolidBrush(Color.FromArgb(255, color, color, color)), x, y, 1, 1);
}
}
// 将绘制好的热力图拷贝到绘图区域
BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
// 释放资源
DeleteObject(hBitmap);
DeleteDC(hdcMem);
ReleaseDC(pictureBox1.Handle, hdc);
// 显示热力图
pictureBox1.Image = bmp;
}
}
}
```
注意:上述代码中使用到了 Matlab Runtime,需要事先安装并且将其安装目录添加到系统 PATH 中。另外,Matlab 生成的小波包系数矩阵文件需要使用 BinaryReader 读取。