⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 基2FFT
💻 C
字号:
#include <windows.h>

#include "main.h"
#include "fft.h"

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

/* Paint Function by xinwang 
 */
void paint(HDC hdc);

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

/* Some GUI interface hwnd 
 */
HWND hGroupWFrm;    /* wave form */
HWND hRadioWSqu;
HWND hRadioWSin;
HWND hRadioWTri;    int  wfsel[3] = {1,0,0};

HWND hGroupWAtr;    /* wave attribute */
HWND hLaberWCyc;
HWND hEditWCyc;     int cycle = 100;
HWND hLaberWSco;
HWND hEditWSco;     int scope = 50;
HWND hLaberWPar;
HWND hEditWPar;     int parse = 0;

HWND hGroupSamp;    /* sample control */
HWND hLaberSAlt;
HWND hEditSAlt;     int smp_alter = 1;
HWND hLaberSNum;
HWND hEditSNum;     int smp_num = 512;

HWND hButtonOK;     /* Action button */

/* Userful variable */
char strtmp[] = "";
char* strstop;
int nmax;

/* wave form */
enum {SQU, SIN, TRI} wave;

/* This instance handle 
 */
HINSTANCE hInst;

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 (hThisInstance, "mico");
    wincl.hIconSm = LoadIcon (hThisInstance, "mico");
    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 */
           "Windows App",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           600,                 /* 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);
    hInst = hThisInstance;
    /* 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)
{
    /* for message wm_paint */
    HDC hdc;
    PAINTSTRUCT ptstr;
    
    switch (message)                  /* handle the messages */
    {
        case WM_CREATE:
            /* Add display control.
             */
            hGroupWFrm = CreateWindow("BUTTON", 
                "波形",
                WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
                FRX, FRY, 130, 110,
                hwnd, (HMENU)IDG_WFRM,
                hInst, NULL
                );
            hRadioWSqu = CreateWindow("BUTTON",
                "方波",
                WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,
                FRX+30, FRY+25, 80, 20,
                hwnd, (HMENU)IDR_WSQU,
                hInst, NULL
                );
            hRadioWSin = CreateWindow("BUTTON",
                "正弦波",
                WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,
                FRX+30, FRY+50, 80, 20,
                hwnd, (HMENU)IDR_WSIN,
                hInst, NULL
                );
            hRadioWTri = CreateWindow("BUTTON",
                "三角波",
                WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,
                FRX+30, FRY+75, 80, 20,
                hwnd, (HMENU)IDR_WTRI,
                hInst, NULL
                );

            hGroupWAtr = CreateWindow("BUTTON", 
                "波形属性",
                WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
                FRX+150, FRY, 180, 110,
                hwnd, (HMENU)IDG_WATR,
                hInst, NULL
                );
            hLaberWCyc = CreateWindow("STATIC",
                "周期",
                WS_CHILD|WS_VISIBLE,
                FRX+180, FRY+25, 40, 20,
                hwnd, (HMENU)IDS_WCYC,
                hInst, NULL
                );
            hEditWCyc = CreateWindow("EDIT",
                "100",
                WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT,
                FRX+220, FRY+25, 80, 20,
                hwnd, (HMENU)IDE_WCYC,
                hInst, NULL
                );
            hLaberWSco = CreateWindow("STATIC",
                "幅值",
                WS_CHILD|WS_VISIBLE,
                FRX+180, FRY+50, 40, 20,
                hwnd, (HMENU)IDS_WSCO,
                hInst, NULL
                );
            hEditWSco = CreateWindow("EDIT",
                "50",
                WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT,
                FRX+220, FRY+50, 80, 20,
                hwnd, (HMENU)IDE_WSCO,
                hInst, NULL
                );
            hLaberWPar = CreateWindow("STATIC",
                "相位",
                WS_CHILD|WS_VISIBLE,
                FRX+180, FRY+75, 40, 20,
                hwnd, (HMENU)IDS_WPAR,
                hInst, NULL
                );
            hEditWPar = CreateWindow("EDIT",
                "0",
                WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT,
                FRX+220, FRY+75, 80, 20,
                hwnd, (HMENU)IDE_WPAR,
                hInst, NULL
                );
                
            hGroupSamp = CreateWindow("BUTTON", 
                "取样",
                WS_CHILD|WS_VISIBLE|BS_GROUPBOX,
                FRX+350, FRY, 200, 110,
                hwnd, (HMENU)IDG_SAMP,
                hInst, NULL
                );
            hLaberSAlt = CreateWindow("STATIC",
                "取样间隔",
                WS_CHILD|WS_VISIBLE,
                FRX+380, FRY+25, 80, 20,
                hwnd, (HMENU)IDS_SALT,
                hInst, NULL
                );
            hEditSAlt = CreateWindow("EDIT",
                "1",
                WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT,
                FRX+450, FRY+25, 80, 20,
                hwnd, (HMENU)IDE_SALT,
                hInst, NULL
                );
            hLaberSNum = CreateWindow("STATIC",
                "取样数",
                WS_CHILD|WS_VISIBLE,
                FRX+380, FRY+50, 80, 20,
                hwnd, (HMENU)IDS_SNUM,
                hInst, NULL
                );
            hEditSNum = CreateWindow("EDIT",
                "512",
                WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT,
                FRX+450, FRY+50, 80, 20,
                hwnd, (HMENU)IDE_SNUM,
                hInst, NULL
                );
            hButtonOK = CreateWindow("BUTTON",
                "OK",
                WS_CHILD|WS_VISIBLE,
                FRX+450, FRY+80, 80, 20,
                hwnd, (HMENU)IDC_OK,
                hInst, NULL
                );
            SendMessage(hRadioWSqu, BM_SETCHECK, 1, 0);
            break;
        case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;
        case WM_PAINT:
            hdc = BeginPaint(hwnd, &ptstr);
            paint(hdc);
            EndPaint(hwnd, &ptstr);
            break;
        case WM_COMMAND:
            switch (LOWORD(wParam))
            {
                case IDR_WSQU:
                    wfsel[0]=1; wfsel[1]=0; wfsel[2]=0;
                    SendMessage(hRadioWSqu, BM_SETCHECK, wfsel[0], 0);
                    SendMessage(hRadioWSin, BM_SETCHECK, wfsel[1], 0);
                    SendMessage(hRadioWTri, BM_SETCHECK, wfsel[2], 0);
                    break;
                case IDR_WSIN:
                    wfsel[0]=0; wfsel[1]=1; wfsel[2]=0;
                    SendMessage(hRadioWSqu, BM_SETCHECK, wfsel[0], 0);
                    SendMessage(hRadioWSin, BM_SETCHECK, wfsel[1], 0);
                    SendMessage(hRadioWTri, BM_SETCHECK, wfsel[2], 0);
                    break;
                case IDR_WTRI:
                    wfsel[0]=0; wfsel[1]=0; wfsel[2]=1;
                    SendMessage(hRadioWSqu, BM_SETCHECK, wfsel[0], 0);
                    SendMessage(hRadioWSin, BM_SETCHECK, wfsel[1], 0);
                    SendMessage(hRadioWTri, BM_SETCHECK, wfsel[2], 0);
                    break;
                case IDC_OK:
                    if (wfsel[0]==1)
                        wave = SQU;
                    if (wfsel[1]==1)
                        wave = SIN;
                    if (wfsel[2]==1)
                        wave = TRI;
                        
                    nmax = GetWindowTextLength(hEditWCyc)+1;
                    GetWindowText(hEditWCyc, strtmp, nmax);
                    cycle = strtol(strtmp, &strstop, 10);
                    
                    nmax = GetWindowTextLength(hEditWSco)+1;
                    GetWindowText(hEditWSco, strtmp, nmax);
                    scope = strtol(strtmp, &strstop, 10);
                    
                    nmax = GetWindowTextLength(hEditWPar)+1;
                    GetWindowText(hEditWPar, strtmp, nmax);
                    parse = strtol(strtmp, &strstop, 10);
                    
                    nmax = GetWindowTextLength(hEditSAlt)+1;
                    GetWindowText(hEditSAlt, strtmp, nmax);
                    smp_alter = strtol(strtmp, &strstop, 10);
                    
                    nmax = GetWindowTextLength(hEditSNum)+1;
                    GetWindowText(hEditSNum, strtmp, nmax);
                    smp_num = strtol(strtmp, &strstop, 10);
                    
                    InvalidateRect(hwnd, NULL, TRUE);
                    break;
            }
            break;
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}

/* paint function */
void paint(HDC hdc)
{   
    int L=0, N=1;
    while (N<smp_num)
    {
        ++L;
        N = 1<<L;
    }
    /* initialize data */
    Complex *cin  = (Complex *)malloc(sizeof(Complex)*N);
    int i;
    for (i=0; i<N; i++)
    {
        cin[i].rex = cin[i].imx = 0;
    }
    Complex *cout = (Complex *)malloc(sizeof(Complex)*N);
    float   *dis  = (float *)malloc(sizeof(float)*N);
    /* check the waveform */
    const int T  = cycle, 
              P  = parse,
              A  = scope,
              Ts = smp_alter;
    const int SN = smp_num;

    switch (wave)
    {
        case SQU:    /* square wave */
            for (i=0; i<SN; i++)
            {
                double pos = (Ts*i)%T;
                if (pos<(T/2))
                {
                    cin[i].rex = A;
                }
                else
                {
                    cin[i].rex = -A;
                }
            }
            break;
        case SIN:    /* sine wave */
            for (i=0; i<SN; i++)
            {
                cin[i].rex = A * sin(2*PI*(Ts*i)/T + P);
            }
            break;
        case TRI:    /* triangular wave */
            for (i=0; i<SN; i++)
            {
                double pos = (Ts*i)%T;
                if (pos<(T/2))
                {
                    cin[i].rex = (pos)*4*A/T-A;
                }
                else
                {
                    cin[i].rex = (T-pos)*4*A/T-A;
                }            
            }
            break;
    }
    /* calculate data */
    cfftr2(L, cin, cout);
    //dft(N, cin, cout);
    //cifftr2(L, cout, cout);
    module(cout, dis, N, 100);
    
    /* display input data */
    MoveToEx(hdc, GAX, GAY-cin[0].rex, NULL);
    for (i=0; i<N; i++)
    {
        LineTo(hdc, GAX+i, GAY-cin[i].rex);
        MoveToEx(hdc, GAX+i, GAY-cin[i].rex, NULL);
    }
    /* display output data */
    MoveToEx(hdc, GBX, GBY-dis[0], NULL);
    for (i=0; i<N; i++)
    {
        LineTo(hdc, GBX+i, GBY-dis[i]);
        MoveToEx(hdc, GBX+i, GBY-dis[i], NULL);
    }
    
    free(cin);
    free(cout);
    free(dis);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -