您的位置:首页 > 其它

波形峰值查找的简单模拟 (使用WinAPI)

2008-11-23 20:31 369 查看


预览图/*
 * 波形峰值查找的简单模拟 GUI Version 
 * Coder: blackboy 
 * Date:  2008-1-15
 * Tools: Dev-C++, C, Win32 API
 */ 

#include <windows.h>
#include <stdio.h>
#include <string.h>

#define MAX_POINTS  70   // 样本值大小 
#define JUDGE_WIN_X 1    // 用于判断的x轴距离 
#define JUDGE_RATE  1.0  // 用于判断的斜率 

// 样本值 
const int value[MAX_POINTS] = 
{
    19,20,20,20,20,19,19,19,20,19,
    18,17,16,19,21,19,16,17,18,19,
    19,20,20,20,20,20,21,20,20,20,
    20,20,20,23,26,23,20,21,21,20,
    20,20,20,20,20,19,19,18,17,16,
    15,14,13,15,17,15,12,11,10,9,
    8, 7, 6, 5, 4, 3, 2, 1, 0, 1
};
    
// 判断标志 
// 趋势   增大:1, 减小:2, 峰值?:3, 保持:0, 无效:-1
int tag[MAX_POINTS] = {-1};

// 扫描每个点 
void Scan(int tag[MAX_POINTS])
{
    int i;
    
    tag[0] = 0;
    // 第一遍扫描 
    for(i=1; i<MAX_POINTS; i++)
    {
        if(value[i]>value[i-1])
            tag[i] = 1;
        else if(value[i]<value[i-1])
            tag[i] = 2;
        else
            tag[i] = 0;
    }
    
    // 第二遍扫描 
    for(i=1; i<MAX_POINTS-1; i++)
    {
        if(tag[i-1]==1 && tag[i+1]==2)
        {
            if( (value[i]-value[i-1])/JUDGE_WIN_X > JUDGE_RATE )
                tag[i] = 3;
        }
    }
}

////////////////////  以下Windows界面框架主要是自动生成的 //////////////// 

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "峰值查找模拟 - coded by blackboy",       /* Title Text */
           //WS_OVERLAPPEDWINDOW, /* default window */
           WS_DLGFRAME | WS_SYSMENU,
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           700,                 /* The programs width */
           500,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int i;
    int ix = 0, iy = 1;
    char str[10];
    static int  cxClient, cyClient;
    static int cxChar, cyChar;
    static HWND hwndButton;        // 按钮 
    PAINTSTRUCT ps;
    HDC hdc;
    static POINT pt[MAX_POINTS];
    
    
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            cxChar = LOWORD(GetDialogBaseUnits());
            cyChar = HIWORD(GetDialogBaseUnits());
            // 画按钮 
            hwndButton = CreateWindow( 
                "button",
                "查找峰值",
                WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                250,
                400,
                20*cxChar,
                7*cyChar/4,
                hwnd,
                (HMENU) 1,/* Control ID: 1 */
                ((LPCREATESTRUCT) lParam)->hInstance,
                NULL
                );
            return 0;  
        case WM_SIZE:
            cxClient = LOWORD (lParam);
            cyClient = HIWORD (lParam);
            return 0 ;
        case WM_PAINT:
            // 画坐标及波形 
            hdc = BeginPaint(hwnd, &ps);
            SetMapMode(hdc, MM_LOMETRIC);
            SetViewportOrgEx(hdc, 30, cyClient-150, NULL);
            SetBkMode(hdc, TRANSPARENT);            
            //TextOut(hdc, 1000, 700, "Waveform Simulator", strlen("Waveform Simulator"));
            LineTo(hdc, 1500, 0);
            MoveToEx(hdc, 0, 0, NULL);
            LineTo(hdc, 0, 700);
            
            for(i=0; i<MAX_POINTS; i++)
            {
                pt[i].x = i*20;
                pt[i].y = value[i]*20;
                if(i%10==0)
                {
                    MoveToEx(hdc, pt[i].x, 0, NULL);
                    LineTo(hdc, pt[i].x, 20);
                    TextOut(hdc, pt[i].x-20, -10, str, sprintf(str, "%2d", ix*10));
                    ix++;
                }
                // 如果已经按下"查找峰值"按钮, 则在波形中标出 
                if(tag[i]==3)   Rectangle(hdc, pt[i].x+20, pt[i].y+20, pt[i].x-20, pt[i].y-20);
            }
            
            for(i=0; i<700; i++)
            {
                if(i>0 && i%200==0)
                {
                    MoveToEx(hdc, 0, i, NULL);
                    LineTo(hdc, 20, i);
                    TextOut(hdc, -60, i+20, str, sprintf(str, "%2d", iy*10));
                    iy++;
                }
            }
            Polyline(hdc, pt, MAX_POINTS);
            
            EndPaint(hwnd, &ps);
            return 0;
        case WM_COMMAND:
            // 对按下"查找峰值"按钮的响应 
            if (LOWORD(wParam) == 1 &
                HIWORD(wParam) == BN_CLICKED &
                (HWND) lParam == hwndButton)
            {
                Scan(tag);
                InvalidateRect(hwnd, NULL, FALSE);
            }
            return 0;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: