detrend signal c source code
신호의 경향성을 없애는 detrend 함수의 소스 코드를 수록하였다.
신호처리/분석에서 FFT를 통해 신호의 주파수 성분을 분석할 때 먼저 detrend를
통해 경향성을 제거하는 것이 좋다. 신호의 경향성때문에 발생하는 저주파 성분이
신호 분석에 혼란을 주기도 한다.
아래 그림은 detrend 함수를 통해 신호의 경향성을 제거한 예이다.
detrend 함수의 소스 코드는 아래와 같다.
double detrend(double *pin, double *pout, int
n, int is_nomal_out)
{
double x = 0;
double y = 0;
double a = 0;
double b = 0;
double sy = 0.0;
double sxy = 0.0;
double sxx = 0.0;
int i = 0;
i = 0;
x = (-n / 2.0 + 0.5);
while (i<n)
{
y = pin[i];
sy += y;
sxy += x * y;
sxx += x * x;
i++;
x += 1.0;
}
a = sxy / sxx;
b = sy / n;
i = 0;
x = (-n / 2.0 + 0.5);
if (is_nomal_out)
{
while (i<n)
{
pout[i] = pin[i] - (b + a*x);
i++;
x += 1.0;
}
}
else
{
while (i<n)
{
pout[i] = pin[i] - (a*i);
i++;
x += 1.0;
}
}
return b;
}
댓글
댓글 쓰기