分类: WINDOWS
2010-03-06 17:22:05
在做实时采样处理时,经常会碰到一些干扰波形,用示波器无法抓得到,虽然在程序里面可以把这样采样点数据
保存下来,但无法看到直观的波形图,于是写了一个小程序,用于把采样点数据还原为波形图。还的波形图如下:
代码如下(使用GCC 3.4.5编译通过):
/*******************************************************************************
*
* COPYRIGHT (C) 2010 By SHENZHEN ARTEL TECHNOLOGY CO., LTD. All Rights Reserved.
*
* File Name : adc2wave.cpp
*
* Author : mok
*
* First Issued: 2010-3-6
*
* Description :
*
*******************************************************************************
* Modify Record:
* Version Date(YY/MM/DD) Author Note
* V0.10 2010-3-6 mok 第一版
*
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include
/* Private typedef -----------------------------------------------------------*/
typedef struct
{
unsigned char b;
unsigned char g;
unsigned char r;
}color;
/* Private define ------------------------------------------------------------*/
//模块预定义
/* Private macro -------------------------------------------------------------*/
#define HEIGHT 260
#define WIDTH 500
/* Private function prototypes -----------------------------------------------*/
void SaveBmp(char *filename,int height,int width,unsigned char *data);
/* Private variables ---------------------------------------------------------*/
unsigned int adc_value[WIDTH] =
{
1985,2164,2304,2420,2584,2700,2859,3000,3119,3235,
3300,3385,3445,3526,3616,3663,3728,3739,3782,3807,
3795,3816,3834,3817,3829,3844,3831,3853,3831,3856,
3865,3839,3856,3856,3821,3777,3660,3506,3394,3197,
2991,2824,2698,2488,2209,1951,1685,1409,1160, 877,
600, 359, 154, 0, 178, 355, 550, 688, 829,1046,
1251,1417,1631,1817,1973,2155,2293,2413,2577,2695,
2854,2991,3113,3238,3297,3380,3448,3520,3608,3664,
3727,3752,3776,3804,3804,3812,3830,3826,3828,3846,
3837,3854,3837,3852,3869,3845,3851,3860,3828,3785,
3668,3514,3406,3227,3001,2826,2711,2507,2229,1972,
1706,1428,1180, 892, 622,3748,4095,4095,4095, 355,
550, 688, 829,1046,1251,1417,1631,1817,1973,2155,
2293,2413,2577,2695,2854,2991,3113,3238,3297,3380,
3448,3520,3608,3664,3727,3752,3776,3804,3804,3812,
3830,3826,3828,3846,3837,3854,3837,3852,3869,3845,
3851,3860,3828,3785,3668,3514,3406,3227,3001,2826,
2711,2507,2229,1972,1706,1428,1180, 892, 622,3748,
4095,4095,4095,4095,4095,4095,4095,3977,3856,3749,
3621,3527,3437,3307,1900, 460, 496, 403, 365, 319,
264, 263, 229, 186, 187, 175, 151, 134, 104, 112,
95, 62, 82, 76, 28, 67, 83, 23, 59, 57,
43, 25, 1, 10, 14, 0, 0, 0, 0, 7,
0, 0, 12, 0, 0, 4, 0, 0, 10, 0,
4, 28, 105, 112, 128, 108, 104,4095,4095,4095,
4095,3977,3856,3749,3621,3527,3437,3307,1900, 460,
496, 403, 365, 319, 264, 263, 229, 186, 187, 175,
151, 134, 104, 112, 95, 62, 82, 76, 28, 67,
83, 23, 59, 57, 43, 25, 1, 10, 14, 0,
0, 0, 0, 7, 0, 0, 12, 0, 0, 4,
0, 0, 10, 0, 4, 28, 105, 112, 128, 108,
104, 103, 62, 71, 52, 17, 38, 39, 17, 58,
33, 2, 0, 0, 2, 0, 11, 11, 1, 21,
20, 1, 30, 12, 31, 30, 15, 39, 10, 9,
38, 10, 4, 0, 20,3478,3689,3796,3815,3845,
3862,3850,3860,3872,3852,3820,3702,3600,3497,3380,
3303,3078,2969,2803,2556,2330,2070,1800,1566,1270,
1028, 120, 6, 193, 362, 103, 62, 71, 52, 17,
38, 39, 17, 58, 33, 2, 0, 0, 2, 0,
11, 11, 1, 21, 20, 1, 30, 12, 31, 30,
15, 39, 10, 9, 38, 10, 4, 0, 20,3478,
3689,3796,3815,3845,3862,3850,3860,3872,3852,3820,
3702,3600,3497,3380,3303,3078,2969,2803,2556,2330,
2070,1800,1566,1270,1028, 120, 6, 193, 362, 519,
713, 817, 948,1146,1329,1511,1710,1892,2047,2210,
2322,2469,2607,2711,2854,2988,3091,3206,3273,3320,
3400,3478,3572,3626,3675,3732,3740,3759,3790,3781,
3790,3814,3810,3826,3817,3827,3843,3828,3835,3847,
3832,3837,3837,3794,3687,3561,3452,3339,3209,2939,
2841,2649,2392,2157,1876,1636,1360,1069, 843, 0,
};
/*******************************************************************************
* Function : main().main function.
*
* Arguments: None
*
* Inputs : None
*
* Outputs : None
*
* Returns : None
******************************************************************************/
int main(void)
{
int i, j;
color buffer[HEIGHT][WIDTH];
//底色填充为白色
for (i = 0; i < HEIGHT; i++)
{
for (j = 0; j < WIDTH; j++)
{
buffer[i][j].b = 0xFF;
buffer[i][j].g = 0xFF;
buffer[i][j].r = 0xFF;
}
}
//采样点坐标(x=采样点序号,y=采样点数据)填充为黑色
for (i = 0; i < WIDTH; i++)
{
//采样数据过大,缩小40倍
buffer[adc_value[i] / 40][i].b = 0x00;
buffer[adc_value[i] / 40][i].g = 0x00;
buffer[adc_value[i] / 40][i].r = 0x00;
}
SaveBmp("wave.bmp", HEIGHT, WIDTH, (unsigned char*) buffer);
return 0;
}
/*******************************************************************************
* Function : SaveBmp().
*
* Arguments: @*filename->文件名
* @height ->图像高度
* @width ->图像宽度
* @*data ->图像内容
*
* Inputs : None
*
* Outputs : None
*
* Returns : None
******************************************************************************/
void SaveBmp(char *filename,int height,int width,unsigned char *data)
{
unsigned int size = width * height * 3 + 54;
unsigned short bmp_head[] =
{
0x4D42, // 1. 424Dh="BM",表示是Windows支持的BMP格式。
size % 0x10000, // 2. 文件大小低位
size / 0x10000, // 3. 文件大小高位
0x0000, // 4. 保留,必须为0
0x0000, // 5. 保留,必须为0
0x0036, // 6. 从文件开始到位图数据之间的偏移量.低位
0x0000, // 7. 从文件开始到位图数据之间的偏移量.高位
0x0028, // 8. 位图图信息头长度。
0x0000, // 9. 位图图信息头长度。
width % 0x10000, //10. 位图宽度,以像素为单位。
width / 0x10000, //11.
height % 0x10000, //12. 位图高度,以像素为单位。
height / 0x10000, //13.
0x0100, //14. 位图的位面数,该值总是1。
0x0018, //15. 每个像素的位数。有1(单色),4(16色),8(256色),16(64K色,高彩色),24(16M色,真彩色),32(4096M色,增强型真彩色)。
0x0000, //16. 压缩说明:有0(不压缩).
0x0000, //17.
0x0000, //18. 用字节数表示的位图数据的大小,该数必须是4的倍数,数值上等于位图宽度×位图高度×每个像素位数。
0x0000, //19.
0x0000, //20. 用象素/米表示的水平分辨率。
0x0000, //21.
0x0000, //22. 用象素/米表示的垂直分辨率。
0x0000, //23.
0x0000, //24. 位图使用的颜色索引数。
0x0000, //25.
0x0000, //26. 对图象显示有重要影响的颜色索引的数目。
0x0000 //27.
};
FILE* fp = fopen(filename, "wb");
if (!fp) return;
fwrite(bmp_head, 1, sizeof(bmp_head), fp);
fwrite(data, 1, size, fp);
fclose(fp);
}
/************************* END OF FILE ****************************************/