#include <math.h>
#include "otdr_const.h"
#include "haar.h"
#include "otdr_bufferfilter.h"

WORD32 windowsize=200;
WORD32 backnum=900;
extern void filter(double *a, double *b, CurveData *data, int n);
void ReverseCurveData(CurveData *pCurveDataIn,CurveData *pCurveDataOut,WORD32 len)
{
WORD32 i;
i=len;
while(i--)
pCurveDataOut[len-(i+1)].y=pCurveDataIn[i].y;
}

void CurveMinusData(CurveData *pCurveDataIn,double data, WORD32 len)
{
WORD32 i=0;
for(i=0;i<len;i++)
pCurveDataIn[i].y=pCurveDataIn[i].y-data;
}

void FilterCurve(WORD32 plsWid, CurveData *pCurveData,const WORD32 len)
{
double a[2] = {0}, b[2] = {0};

NOISE_LPF_BUTTER(plsWid,a,b);
filter(a, b, pCurveData, len);
}

void CurveAddData(CurveData *pCurveDataIn,double data, WORD32 len)
{
WORD32 i=0;
for(i=0;i<len;i++)
pCurveDataIn[i].y=pCurveDataIn[i].y+data;
}

double getMean(CurveData *pCurveDataIn, WORD32 start,WORD32 n)
{
WORD32 i=0;
double ymean=0.0;
for(i=start;i<start+n;i++)
ymean+=pCurveDataIn[i].y/(double)n;
return ymean;
}

double getStd(CurveData *pCurveDataIn,WORD32 n)
{
WORD32 i=0;
double ymean=0.0,var=0.0,std=0.0;
for(;i<n;i++)
ymean+=pCurveDataIn[i].y/(double)n; /*getMean*/

for(i=0;i<n;i++)
var+=((pCurveDataIn[i].y-ymean)*(pCurveDataIn[i].y-ymean));

var/=(float)n;
std=sqrt(var);
return std;
}

WORD32 foundDataNo(CurveData *CurveDataIn,double ymean,double y2std,WORD32 found,WORD32 len)
{
int i=0;
double ymn=0.0;
for(i=0;i<len/windowsize-1;i++)
{
ymn=getMean(CurveDataIn,i*windowsize,windowsize);

if((ymn-ymean)>6*y2std)
{
if(i<(len/windowsize-1))
{
ymn=getMean(CurveDataIn,(i+1)*windowsize,windowsize);

if(ymn-ymean>3*y2std)
{
found=i;
break;
}
}
}
else if(ymn<ymean)
{
ymean=ymn;
}
}

return found;
}

void CaculateNoise(CurveData *pCurveDataIn1,CurveData *pCurveDataIn2,WORD32 found,WORD32 len,double *noise)
{
WORD32 j=0;
WORD32 k=found*windowsize-backnum ;
for(j=0;j<found*windowsize-backnum;j++)
{
noise[j]=pCurveDataIn1[len+backnum-found*windowsize+j].y - pCurveDataIn2[k--].y;
}
}

void ReconstructData(CurveData *pCurveDataIn,WORD32 found,WORD32 len,double *noise,double ymn,CurveData *pCurveDataOut)
{
WORD32 j = 0;
for(j = 0;j <=(len-found*windowsize+backnum-1);j++)
{
/* if(j>500 || j>found*windowsize-backnum)
break;*/
pCurveDataOut[j].y=pCurveDataIn[j].y-ymn;
}

for(j = (len-found*windowsize+backnum);j<len;j++)
{
pCurveDataOut[j].y=noise[j-(len-found*windowsize+backnum)];
}
for(j=0;j<len;j++)
{
pCurveDataIn[j].y=pCurveDataOut[j].y;
}
}

WORD32 NoiseProcess(Curve *pCurveOut,WORD32 sampinterval, WORD32 sampperiod, WORD32 len)
{
CurveData *data1=NULL;
CurveData *data2=NULL;
double ymean=0.0;
double y2std=0.0;
double ymn=0.0;
double *noise=NULL;
WORD32 found=0xff;
double endata = 0.0;

data1=(CurveData *)malloc(len * sizeof(CurveData));
if(NULL==data1)
{
return ERROR;
}
memset(data1,0,len);

data2=(CurveData *)malloc(len* sizeof(CurveData));
if(NULL==data2)
{
free(data1);
return ERROR;
}
memset(data2,0,len);

ReverseCurveData(pCurveOut->data,data1,len);
endata = getMean(pCurveOut,len-100,100);
CurveMinusData(data1,endata,len);

FilterCurve(pCurveOut->config.comm.plsWid,data1,len);

CurveAddData(data1,endata,len);

if(len<windowsize || len<500)
return ERROR;
ymean=getMean(data1,0,windowsize);
y2std=getStd(data1,500);

found=foundDataNo(data1,ymean,y2std,found,len);
printf("found:%d\n",found);
if((found*windowsize-backnum-30)<0 || found == 0xff)
{
return ERROR;
}

ymn=getMean(data1,found*windowsize-backnum-30,30);

noise=(double *)malloc((found*windowsize-backnum)* sizeof(double));
if(NULL==noise)
{
free(data1);
free(data2);
return ERROR;
}
memset(noise,0,found*windowsize-backnum);

CaculateNoise(pCurveOut->data,data1,found,len,noise);

ReconstructData(pCurveOut->data,found,len,noise,ymn,data2);

free(data1);
free(data2);
free(noise);

return OK;

}
//没有专门的求任意底数对数的函数,不过可以用
//log(x)/log(y)表示log y x

noise_process.c的更多相关文章

  1. 套题T7

    P4712 铺瓷砖 时间: 1000ms / 空间: 65536KiB / Java类名: Main   描述

随机推荐

  1. window服务器上搭建git服务,window server git!!!

    先给大家看一个高大上的,这是我给我公司配置的,小伙伴们都说好! 阿里云的2012server 基于这篇大神的教程,我把服务端搭建好了. 传送门,当然我还是自己做个笔记的好. 1.下载java,并安装 ...

  2. 自动化CI构建工具

    hudson/maven jenkins,bamboo, hudson

  3. Session机制二(简易购物车案例)

    一:案例一(简易购物车) 1.目录结构 2.step1.jsp <%@ page language="java" contentType="text/html; c ...

  4. WebLogic和Tomcat的区别

    J2ee开发主要是浏览器和服务器进行交互的一种结构.逻辑都是在后台进行处理,然后再把结果传输回给浏览器.可以看出服务器在这种架构是非常重要的. 这几天接触到两种Java的web服务器,做项目用的Tom ...

  5. Ace-editor 输入内容时光标闪动,定位错乱的解决方案

    请尝试将字体设置成12PX或者14px(偶数),避免设置成13px. 应该就可以解决. 同时向大家推荐一款可直接生成文档的API调试.管理工具(中文PostMAN):https://www.apipo ...

  6. 初识Spring——Spring核心容器

    一. IOC和DI基础 IOC-Inversion of Control,译为控制反转,是一种遵循依赖倒置原则的代码设计思想. 所谓依赖倒置,就是把原本的高层建筑依赖底层建筑“倒置”过来,变成底层建筑 ...

  7. awk 提取列

    awk '{OFS="";print(substr($0,1,6),substr($0,74,18),substr($0,15,3),substr($0,18,8))}' inpu ...

  8. 理解事件(Event)

    Overview 在前几章,我们已经对委托有了一个完整的了解了,本章将会对事件进行一下介绍: 相对于委托,事件再是我们更加频繁的接触的,比如 鼠标的click 事件,键盘的 keydown 事件等等. ...

  9. [leetcode tree]107. Binary Tree Level Order Traversal II

    Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...

  10. shellcode在栈溢出中的利用与优化

    0x00 前言 在<Windows Shellcode学习笔记——shellcode的提取与测试>中介绍了如何对shellcode作初步优化,动态获取Windows API地址并调用,并通 ...