#include <windows.h>
#include <math.h>
#include <iostream.h>
void MyPaint(HWND);
int Nxt(float);
int Nyt(float);
float f(float);
float A(float);
float B(float);
float f1(float);
float f2(float);
float f3(float);
float f4(float);
void DrawGraph (HDC, float(*)(float), float x0, float xn, int N,float px1, float
py1, float px2, float py2);
float Integral(float(*)(float),float,float);
int Nxmin, Nymin, Nxmax, Nymax,k;
float Xmin, Ymin, Xmax, Ymax;
float T=10;
const int N=100;
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
char szClassName
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 */
"Proga Fidan", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
1024, /* The programs width */
768, /* 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, 500))
{
/* 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)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
case WM_PAINT:
MyPaint(hwnd);
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0;
}
//==================================================================================
int Nxt(float xt)
{
return (xt-Xmin)/(Xmax-Xmin)*(Nxmax-Nxmin)+Nxmin;
}
int Nyt(float yt)
{
return (yt-Ymin)/(Ymax-Ymin)*(Nymin-Nymax)+Nymax;
}
//==================================================================================
float f(float x)
{
float y;
if ((0<=x)&&(x<T/4)) y=1;
if ((T/4<=x)&&(x<3*T/4)) y=-4*x/T+2;
if ((3*T/4<=x)&&(x<=T)) y=-1;
return y;
}
//==================================================================================
void MyPaint (HWND hwnd)
{
PAINTSTRUCT ps;
HDC hdc;
hdc=BeginPaint(hwnd, &ps); /* N x1 y1 x2 y2 */
DrawGraph(hdc, f, 0.0,10.0, 1000, 10, 30, 310, 230);
DrawGraph(hdc, f1, 0.0,10.0, 1000, 350, 30, 650, 230);
DrawGraph(hdc, f2, 0.0,10.0, 1000, 690, 30, 990, 230);
DrawGraph(hdc, f3, 0.0,10.0, 800, 350, 300, 650, 500);
DrawGraph(hdc, f4, 0.0,10.0, 800, 690, 300, 990, 500);
EndPaint(hwnd, &ps);
}
//==================================================================================
void DrawGraph (HDC hdc, float(*f)(float), float x0, float xn, int N,
float px1, float py1, float px2, float py2)
{
int i;
float dx,xt,yt;
Ymax=1.0;
Ymin=-1.0;
Xmin=x0;
Xmax=xn;
Nxmin=px1;
Nxmax=px2;
Nymin=py1;
Nymax=py2;
HPEN hPen;
BOOL b;
hPen=CreatePen(PS_SOLID,1,RGB(0,13,12));
SelectObject(hdc,hPen);
b=Rectangle(hdc,px1-10,py1-20,px2+10,py2+20);
TextOut(hdc,10,400,"Прога {преобразования Фурье}",28);
DeleteObject(hPen);
if ((py1==30) && (py2==230))
{
MoveToEx(hdc,px1,120,NULL);
LineTo(hdc,px2,120);
}
if ((py1==300) && (py2==500))
{
MoveToEx(hdc,px1,400,NULL);
LineTo(hdc,px2,400);
};
dx=(xn-x0)/(N-1);
for (i=1; i<=N; i++)
{
xt=(i-1)*dx+x0;
yt=f(xt);
SetPixel(hdc,Nxt(xt),Nyt(yt),RGB(255,25,0));
}
}
//==========================================================================================
float A(float x)
{
return f(x)*cos((2*M_PI*k*x)/T);
}
//==========================================================================================
float B(float x)
{
return f(x)*sin((2*M_PI*k*x)/T);
}
//=========================================================================================
float Integral(float(*f)(float),float a,float b)
{
int i;
float sum=0.0, h;
h=(b-a)/N;
sum=h/2*(f(a)+f(b));
for (i=1; i<=N-1; i++)
{
sum+=h*f(a+i*h);
}
return sum;
}
//=========================================================================================
float f1(float x)
{
int j,nn;
const int n=11;
float a[n], b[n];
float sum=0.0, y;
for (k=0; k<=(n-1); k++)
{
a[k]=2/T*Integral(A,0,T);
b[k]=2/T*Integral(B,0,T);
}
for (j=1; j<=(n-1); j++)
{
sum+=(a[j]*cos((2*M_PI*j*x)/T)+b[j]*sin((2*M_PI*j*x)/T));
}
y=a[0]/2+sum;
return y;
}
//=========================================================================================
float f2(float x)
{
int j,nn;
const int n=11;
float a[n], b[n];
float sum=0.0, y;
for (k=0; k<=(n-1); k++)
{
a[k]=2/T*Integral(A,0,T);
b[k]=2/T*Integral(B,0,T);
}
for (j=1; j<=(n-1); j++)
{
sum+=(a[j]*cos((2*M_PI*j*x)/T)+b[j]*sin((2*M_PI*j*x)/T))*sin(j*M_PI/k)/(j*M_PI/k);
}
y=a[0]/2+sum;
return y;
}
//=========================================================================================
float f3(float x)
{
int j,nn;
const int n=31;
float a[n], b[n];
float sum=0.0, y;
for (k=0; k<=(n-1); k++)
{
a[k]=2/T*Integral(A,0,T);
b[k]=2/T*Integral(B,0,T);
}
for (j=1; j<=(n-1); j++)
{
sum+=(a[j]*cos((2*M_PI*j*x)/T)+b[j]*sin((2*M_PI*j*x)/T));
}
y=a[0]/2+sum;
return y;
}
//=========================================================================================
float f4(float x)
{
int j,nn;
const int n=31;
float a[n], b[n];
float sum=0.0, y;
for (k=0; k<=(n-1); k++)
{
a[k]=2/T*Integral(A,0,T);
b[k]=2/T*Integral(B,0,T);
}
for (j=1; j<=(n-1); j++)
{
sum+=(a[j]*cos((2*M_PI*j*x)/T)+b[j]*sin((2*M_PI*j*x)/T))*sin(j*M_PI/k)/(j*M_PI/k);
}
y=a[0]/2+sum;
return y;
}