#include "iostream"
#include "vector"
using namespace std;
double db4[8]={-0.0106 ,0.0329 ,0.0308,-0.1870,-0.0280,0.6309,0.7148, 0.2304};
double db4l[8]={ -0.2304,0.7148,-0.6309,-0.0280,0.1870,0.0308,-0.0329,-0.0106};
#define DB4 4
class Wavelet
{
public:
Wavelet();
~Wavelet();
void initialize(vector<double> *data);
void WaveDecompose(int Nnmber);
void iWaveDecompose(int Nnmber);
int GetLength();
void GetFilter(double Low[],double High[],int Length);
void conv(double *data,int WL,double *filter,int FL,double *cov,int &cL);//
void reverse(double *odata,int size,double *rdata);
void CutData(double *data,int Begin,int End,double *cdata);
void AbstractTow(double *data,int size,double *Adata);
void DenseTow(double *data,int size,double*Ddata);
protected:
double *pData;
int Datalength;
double *pFilterL;
double *pFilterH;
int FilterLength;
double *c,**d;
int DNumber,*dL,cL;
};
Wavelet::~Wavelet()
{
if (pData!=NULL)delete []pData;
if (pFilterL!=NULL)delete []pFilterL;
if (pFilterH!=NULL)delete []pFilterH;
}
Wavelet::Wavelet()
{
pData=NULL;
pFilterH=NULL;
pFilterL=NULL;
dL=NULL;
cL=0;
}
void Wavelet::WaveDecompose(int Nnmber)
{
DNumber=Nnmber;//分解次数
int WL=Datalength,cl;
double *p=pData;
double *covedata,*cutd;
double *RpH,*RpL;
RpL=new double[FilterLength];
reverse(pFilterL,FilterLength,RpL);//分解时将滤波系数反转
RpH=new double[FilterLength];
reverse(pFilterH,FilterLength,RpH);//分解时将滤波系数反转
//c=new double*[Nnmber];//低频部分系数
d=new double*[Nnmber];//高频部分系数
dL=new int[Nnmber];
for (int i=0;i<Nnmber;i++)//分解级数
{
//低通
covedata=new double[WL+FilterLength-1];
conv(p,WL,RpL,FilterLength,covedata,cl);//第i层低通卷积
c=new double[cl/2];
AbstractTow(covedata,cl,c);//第i层低频下采样系数
//高通
conv(p,WL,RpH,FilterLength,covedata,cl);//第i层高通卷积
d[i]=new double[cl/2];
dL[i]=cl/2;//第i层高频系数长度
AbstractTow(covedata,cl,d[i]);//第i层高频下采样系数
p=c;
WL=cl/2;
}
cL=WL;//最后低频系数长度
}
//小波的重构,
void Wavelet::iWaveDecompose(int Nnmber)
{
int cl,j;
double *DCA,*CA,*DDA,*P,*ca;
// P=c[Nnmber-1];
P=new double[cL];//分解后的最后低频部分系数
memcpy(P,c,sizeof(double)*cL);
for (int i=DNumber-1;i>=(DNumber-Nnmber);i--)//重构级数
{
//低通
DCA=new double[2*cL-1];
DenseTow(P,cL,DCA);//上采样
CA=new double[2*cL-2+FilterLength];
conv(DCA,2*cL-1,pFilterL,FilterLength,CA,cl);//卷积
//高通
DDA=new double[2*dL[i]-1];
DenseTow(d[i],dL[i],DDA);//对应低频部分高频系数的上采样
ca=new double[2*cL-2+FilterLength];
conv(DDA,2*dL[i]-1,pFilterH,FilterLength,ca,cl);//高频卷积
P=new double[cl];
memset(P,0,sizeof(double)*cl);
for (j=0;j<cl;j++)
{
P[j]=CA[j]+ca[j];//重构上一层低频系数
cout<<P[j]<<endl;
}
cout<<"DDD"<<endl;
cL=cl;//上一层低频系数的长度
}
}
//返回数据的长度
int Wavelet::GetLength()
{
return Datalength;
}
//初使化,数据的输入
void Wavelet::initialize(vector<double> *data)
{
Datalength=data->size();
pData=new double[Datalength];
for (int i=0;i<Datalength;i++)
{
pData[i]=data->at(i);
}
}
//输入滤波器系数
void Wavelet::GetFilter(double *Low,double *High,int Length)
{
pFilterH=new double[Length];
pFilterL=new double[Length];
memcpy(pFilterL,Low,Length*sizeof(double));
memcpy(pFilterH,High,Length*sizeof(double));
FilterLength=Length;//滤波器长度
}
//卷积函数,
void Wavelet::conv(double *data,int WL,double *filter,int FL,double *cov,int &cL)
{
//cov=new double[WL+FL-1];
cL=WL+FL-1;
int i,j,t;
double ck;
//当滤波移动超过了数据长度时的卷积
for(i=0;i<WL+FL-1;i++)
{
ck=0.0;
if (i>=WL)
{
for (j=0;j<(FL-(i-(WL-1)));j++)
{
t=WL-(FL-(i-(WL-1)));
ck+=data[t+j]*filter[j];
}
}
else//当滤波移动没有超过了数据长度时的卷积
{
for (j=0;(j<=i)&&(j< FL);j++)
{
ck+=data[i-j]*filter[ FL-1-j];
}
}
cov[i]=ck;
}
}
//反转数据
void Wavelet::reverse(double *odata,int size,double *rdata)
{
//rdata=new double[size];
for (int i=0;i<size;i++)
{
rdata[size-1-i]=odata[i];
}
}
//提取出Begin到End的数据从data中,放到cdata
void Wavelet::CutData(double *data,int Begin,int End,double *cdata)
{
//cdata=new double[End-Begin+1];
for (int i=0;i<=End-Begin;i++)
{
cdata[i]=data[Begin+i];
}
}
//函数 Y=DOWMSPL(X) 对输入序列进行下抽样,输出序列 Y。
//下抽样是对输入序列取其偶数位,舍弃奇数位。例如 x=[x1,x2,x3,x4,x5],则 y=[x2,x4].
void Wavelet::AbstractTow(double *data,int size,double *Adata)
{
//Adata=new double[int(size/2)];
for (int i=0;i<int(size/2);i++)
{
Adata[i]=data[2*i+1];
}
}
// 函数 Y = UPSPL(X) 对输入的一维序列x进行上抽样,即对序列x每个元素之间
// 插零,例如 x=[x1,x2,x3,x4],上抽样后为 y=[x1,0,x2,0,x3,0,x4];
void Wavelet::DenseTow(double *data,int size,double*Ddata)
{
// Ddata=new double[2*size-1];
for (int i=0;i<2*size-1;i++)
{
if (i%2)
{
Ddata[i]=0;
}
else Ddata[i]=data[i/2];
}
}
void main()
{
double x[18]={1, 0.1, -1 ,0.1,1,2,3,4,1,3,9,11,44,55,7,1,44,11};
double L[2]={0.7071,0.7071};
double h[2]={-0.7071,0.7071},
a[7];
int cL,i,j,t,xl=9,hl=4;
double ck,c[9];
double *p=new double[4];
vector<double> da;
for (i=0;i<18;i++)
{
da.push_back(x[i]);
}
Wavelet ww;
double *Adata;
//ww.DenseTow(h,5,c);
for (i=0;i<10;i++)
{
//cout<<c[i]<<endl;
}
//ww.DenseTow(h,4,a);
ww.initialize(&da);
ww.GetFilter(L,h,2);
ww.WaveDecompose(4);
ww.iWaveDecompose(3);
/* for(i=0;i<xl+hl-1;i++)
{
ck=0.0;
if (i>=xl)
{
for (j=0;j<(hl-(i-(xl-1)));j++)
{
t=xl-(hl-(i-(xl-1)));
ck+=x[t+j]*h1[j];
}
}
else
{
for (j=0;(j<=i)&&(j<hl);j++)
{
ck+=x[i-j]*h1[hl-1-j];
}
}
c[i]=ck;
cout<<ck<<endl;
}*/
/* int n,f;
int i,l=0,j,lx=5,lh=4;
double ck,dk,c[4];
for (i=0;i<lx;)
{ ck=0.0;
dk=0.0;
for (j=0;j<lh;j++)
{
f=i+j;
ck+=x[f%lx]*h1[j];
// dk+=x[f%lx]*h[l];
}
c[l++]=ck;
cout<<ck<<endl;
i=i+1;
}
*/
}
评论0