#pragma comment(linker, "/subsystem:Windows")
#include <windows.h>
const long c_width = 700, c_height = 500;
const int max_len = 512;
bool g_mouseDown = false;
int g_value = 0;
int g_buffer[max_len] = { 0 };
double g_filter[max_len] = { 0.0 };
typedef double FILTER_FUNC();
int g_pick_count = 0;
int g_func_index = -1;
FILTER_FUNC *g_filter_func = NULL;
void sort(int buffer[], int count)
{
for (int j = count - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
if (buffer[i] > buffer[i + 1])
{
int temp = buffer[i];
buffer[i] = buffer[i + 1];
buffer[i + 1] = temp;
}
}
}
}
double filter_amplitude_limit() // 1. 限幅滤波法
{
const char A = 13;
if (g_value - g_filter[1] > A || g_filter[1] - g_value > A)
{
return g_filter[1];
}
return g_value;
}
double filter_median_value() // 2. 中位值滤波法
{
const char N = 5;
++g_pick_count;
if (g_pick_count < N)
{
return g_filter[1];
}
g_pick_count = 0;
int buffer[N];
for (int i = 0; i < N; i++)
{
buffer[i] = g_buffer[i];
}
sort (buffer, N);
return buffer[N / 2];
}
double filter_arithmetic_average() // 3. 算术平均滤波法
{
const char N = 5;
++g_pick_count;
if (g_pick_count < N)
{
return g_filter[1];
}
g_pick_count = 0;
double sum = 0.0;
for (int i = 0; i < N; i++)
{
sum += g_buffer[i];
}
return sum / N;
}
double filter_recurrence_average() // 4. 递推平均滤波法
{
const char N = 5;
double sum = 0.0;
for (int i = 0; i < N; i++)
{
sum += g_buffer[i];
}
return sum / N;
}
double filter_median_average() // 5. 中位值平均滤波法
{
const char N = 5;
++g_pick_count;
if (g_pick_count < N)
{
return g_filter[1];
}
g_pick_count = 0;
int buffer[N];
for (int i = 0; i < N; i++)
{
buffer[i] = g_buffer[i];
}
sort (buffer, N);
double sum = 0.0;
for (int j = 1; j < N - 1; j++)
{
sum += buffer[j];
}
return sum / (N - 2);
}
double filter_limiting_mean() // 6. 限幅平均滤波法
{
const char A = 13;
const char N = 5;
if (g_value - g_filter[1] > A || g_filter[1] - g_value > A)
{
return g_filter[1];
}
static int s_buffer[N];
for (int i = N - 1; i > 0; i--)
{
s_buffer[i] = s_buffer[i - 1];
}
s_buffer[0] = g_value;
++g_pick_count;
if (g_pick_count < N)
{
return g_filter[1];
}
double sum = 0.0;
for (int j = 0; j < N; j++)
{
sum += s_buffer[j];
}
return sum / N;
}
double filter_first_order_lag() // 7. 一阶滞后滤波法
{
const char a = 1, b = 9;
return (g_value * a + g_filter[1] * b) / (a + b);
}
double filter_weighted_average_recursive() // 8. 加权递推平均滤波法
{
const char N = 13;
char coe[N] = {1,2,3,4,5,6,7,8,9,10,11,12,13};
char sum_coe = 1+2+3+4+5+6+7+8+9+10+11+12+13;
char i;
double sum=0;
for (i=0;i<N;i++)
{
sum += g_buffer[i] * coe[i];
}
return sum / sum_coe;
}
double filter_elimination_buffeting() // 9. 消抖滤波法
{
const char N = 5;
char i;
int value = (int) g_filter[1];
for (i = 0; i < N; i++)
{
if (value == g_buffer[N - i])
{
return g_filter[1];
}
value = g_buffer[N - i];
}
return value;
}
double filter_limiting_elimination_buffeting() // 10. 限幅消抖滤波法
{
const char N = 5;
const char A = 13;
if (g_value - g_filter[1] > A || g_filter[1] - g_value > A)
{
return g_filter[1];
}
char i;
int value = (int) g_filter[1];
for (i = 0; i < N; i++)
{
if (value == g_buffer[N - i])
{
return g_filter[1];
}
value = g_buffer[N - i];
}
return value;
}
void Update()
{
int i;
for (i = max_len - 1; i > 0; i--)
{
g_buffer[i] = g_buffer[i - 1];
g_filter[i] = g_filter[i - 1];
}
g_buffer[0] = g_value;
g_filter[0] = g_filter_func ? g_filter_func () : g_value;
}
void Render(HDC hdc)
{
int i;
HPEN hPen = CreatePen (PS_SOLID, 1, RGB (255, 85, 51));
SelectObject (hdc, hPen);
MoveToEx (hdc, 600, 213 - g_value, NULL);
LineTo (hdc, 613, 213 - g_value);
LineTo (hdc, 621, 209 - g_value);
LineTo (hdc, 621, 217 - g_value);
LineTo (hdc, 613, 213 - g_value);
DeleteObject (hPen);
SelectObject (hdc, GetStockObject (BLACK_PEN));
SelectObject (hdc, GetStockObject (NULL_BRUSH));
Rectangle (hdc, 600, 12, 614, 215);
Rectangle (hdc, 20, 12, 533, 215);
MoveToEx (hdc, 532, 213 - g_buffer[0], NULL);
for (i = 1; i < max_len; i++)
{
LineTo (hdc, 532 - i, 213 - g_buffer[i]);
}
SetPixel (hdc, 533 - max_len, 213 - g_buffer[max_len - 1], RGB (0, 0, 0));
Rectangle (hdc, 20, 240, 533, 443);
MoveToEx (hdc, 532, int (441 - g_filter[0]), NULL);
for (i = 1; i < max_len; i++)
{
LineTo (hdc, 532 - i, int (441 - g_filter[i]));
}
SetPixel (hdc, 533 - max_len, int (441 - g_filter[max_len - 1]), RGB (0, 0, 0));
HFONT hFont = CreateFont (13, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "宋体");
SelectObject (hdc, hFont);
SetTextColor (hdc, RGB (0, 0, 0));
SetBkMode (hdc, TRANSPARENT);
TextOut (hdc, 540, 240, "限幅滤波法 ", 20);
TextOut (hdc, 540, 260, "中位值滤波法 ", 20);
TextOut (hdc, 540, 280, "算术平均滤波法 ", 20);
TextOut (hdc, 540, 300, "递推平均滤波法 ", 20);
TextOut (hdc, 540, 320, "中位值平均滤波法 ", 20);
TextOut (hdc, 540, 340, "限幅平均滤波法 ", 20);
TextOut (hdc, 540, 360, "一阶滞后滤波法 ", 20);
TextOut (hdc, 540, 380, "加权递推平均滤波法 ", 20);
TextOut (hdc, 540, 400, "消抖滤波法 ", 20);
TextOut (hdc, 540, 420, "限幅消抖滤波法 ", 20);
DeleteObject (hFont);
if (g_func_index >= 0)
{
Rectangle (hdc, 537, 237 + 20 * g_func_index, 673, 256 + 20 * g_func_index);
}
}
BOOL CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_COMMAND:
{
WORD wID = LOWORD (wParam);
if (wID == IDCANCEL)
{
PostQuitMessage (0);
return TRUE;
}
}
return FALSE;
case WM_LBUTTONDOWN:
{
short xPos = LOWORD (lParam);
short yPos = HIWORD (lParam);
if (yPos >= 13 && yPos < 213)
{
if (xPos >= 600 && xPos < 613
|| xPos >= 613 && xPos < 621 && yPos >= 209 - g_value && yPos < 217 - g_value)
{
g_mouseDown = true;
g_value = 213 - yPos;
SetCapture (hwndDlg);
}
}
if (xPos > 540 && xPos < 670 && yPos >= 240 && yPos < 440)
{
if ((yPos - 240) % 20 < 12)
{
FILTER_FUNC *g_func[] = {
filter_amplitude_limit,
filter_median_value,
filter_arithmetic_average,
filter_recurrence_average,
filter_median_average,
filter_limiting_mean,
filter_first_order_lag,
filter_weighted_average_recursive,
filter_elimination_buffeting,
filter_limiting_elimination_buffeting
};
g_pick_count = 0;
g_func_index = (yPos - 240) / 20;
g_filter_func = g_func[g_func_index];
}
}
}
InvalidateRect (hwndDlg, NULL, FALSE);
return TRUE;
case WM_MOUSEMOVE:
if (g_mouseDown)
{
short xPos = LOWORD (lParam);
short yPos = HIWORD (lParam);
int temp = 213 - yPos;
if (temp < 0)
{
temp = 0;
}
if (temp > 200)
{
temp = 200;
}
g_value = temp;
}
InvalidateRect (hwndDlg, NULL, FALSE);
return TRUE;
case WM_LBUTTONUP:
g_mouseDown = false;
ReleaseCapture ();
return TRUE;
case WM_PAINT:
{
static const BITMAPINFO bmi =
{
sizeof (BITMAPINFOHEADER),
c_width,
c_height,
1,
32,
0,
c_width * c_height * 4
};
PAINTSTRUCT ps;
HDC hdc = BeginPaint (hwndDlg, &ps);
HDC buffer = CreateCompatibleDC (hdc);
void *ppv;
评论0
最新资源