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

📄 gauss_gen.c

📁 用中心极限定理产生高斯白噪声
💻 C
字号:
//该程序产生M个均值为MeanNeed,方差为SigmaNeed平方的高斯随机数
#include<windows.h>
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
#define M 1000 //产生256*256个点
#define N_perpoint 50  //每产生1000个高斯点中的一个点需要50个均匀分布的随机数。这个数越大越精确
#define MeanNeed 1   //MeanNeed  : the expected value of mean of generated gauss series
#define SigmaNeed sqrt(2)  //SigmaNeed : the expected value of sigma of generated gauss series

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("LineDemo") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
     
     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = WndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
     
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"), 
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
     
     hwnd = CreateWindow (szAppName, TEXT ("均值为1,方差为2的1000个高斯白噪声序列。用VC编写。bugzhao"),
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          CW_USEDEFAULT, CW_USEDEFAULT,
                          NULL, NULL, hInstance, NULL) ;
     
     ShowWindow (hwnd, iCmdShow) ;
     UpdateWindow (hwnd) ;
     
     while (GetMessage (&msg, NULL, 0, 0))
     {
          TranslateMessage (&msg) ;
          DispatchMessage (&msg) ;
     }
     return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
     static int  cxClient, cyClient;
     HDC         hdc;
     PAINTSTRUCT ps;
	 int         n,i,stime;
     long        ltime;
     float       x[N_perpoint],gauss[M],mean=0,sigma=0;
    
	 for(i=0;i<M;i++)
   	       gauss[i]=0;
     for(i=0;i<M;i++)
	 {
	   ltime=time(NULL);
       stime=(unsigned int)ltime;
	   stime=stime+i;
       srand(stime);
	   for(n=0;n<N_perpoint;n++)
	      x[n]=0;
       for(n=0;n<N_perpoint;n++)
	   {  x[n]=(float)rand()/RAND_MAX;
          gauss[i]=gauss[i]+(float)sqrt((float)12/N_perpoint)*x[n];
	   }
	   gauss[i]=gauss[i]-(float)sqrt((float)12/N_perpoint)*(N_perpoint/2);
	   gauss[i]=(float)(MeanNeed+SigmaNeed*gauss[i]);
	   mean=mean+gauss[i]/M;
	 }
    for(i=0;i<M;i++)
       sigma=sigma+(gauss[i]-mean)*(gauss[i]-mean)/M;
       
     switch (message)
     {
	 case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
          return 0 ;
          
     case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
          for(i=0;i<M;i++)     
          {
			  MoveToEx(hdc,i+10,cyClient/2,NULL);
              LineTo(hdc,i+10,cyClient/2+(int)((gauss[i]-mean)*60));
          }
		  EndPaint (hwnd, &ps) ;
          return 0 ;
          
     case WM_DESTROY:
          PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

⌨️ 快捷键说明

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