#include "stdafx.h"
#include
#include
#include
#include
using namespace std;
/***************************
**matlab smooth -> C++ code*
******Z[]为输出的数组,*******
*******start为滤波起点*******
*******end为滤波终点*******
******D为原数组,k为窗宽******
***s为指定输出到数组Z的起点***
***************************/
bool Smooth(int s, float Z[], int start, int end, float D[], int k){
int b = (k - 1) / 2;
if ((end-start+1)>k)
{
for (int i = start; i<=end; i++)
{
if ((i - start) <= b)//第一段
{
Z[i - start + s] = 0;
int j = start;
for (; j<= 2* i-start; j++)
{
Z[i - start + s] += D[j];
}
Z[i - start + s] = Z[i - start + s] / ((i - start) * 2 + 1);
}
else if (((i - start)>b)&&((end - i)>b))//第二段
{
Z[i - start + s] = 0;
for (int j = i - b; j <= i + b; j++)
{
Z[i - start + s] += D[j];
}
Z[i - start + s] = Z[i - start + s] / k;
}
else//第三段
{
Z[i - start + s] = 0;
for (int j = 2*i - end; j <= end; j++)
{
Z[i - start + s] += D[j];
}
Z[i - start + s] = Z[i - start + s] / (2 * (end - i) + 1);
}
}
}
return true;
}
int main(){
float D[] = { 1, 2, 1, 1, 4, 1, 6, 1, 1, 6, 1};
float Z1[10];
Smooth(3, Z1, 2, 8, D, 1);
for (int i = 0; i < 10; i++)printf("%f ", Z1[i]);
return(0);
}
经测试,与 matlab 里结果一样!!