小波变换C语言实现代码.docx
《小波变换C语言实现代码.docx》由会员分享,可在线阅读,更多相关《小波变换C语言实现代码.docx(24页珍藏版)》请在冰豆网上搜索。
![小波变换C语言实现代码.docx](https://file1.bdocx.com/fileroot1/2023-1/24/ec03de47-1829-43f4-bd9b-e58ec7d9b03e/ec03de47-1829-43f4-bd9b-e58ec7d9b03e1.gif)
小波变换C语言实现代码
#include
#include
#defineheight256
#definewidth256
voidtwoDDWT(doubleorg[][width],doubledwt[][width],intNumofBand);voidoneDDWT(intflag,doubleorg[][width],doubledwt[][width]);
voidtwoDIDWT(doubledwt[][width],doublerec[][width],intNumofBand);voidoneDIDWT(intflag,doubledwt[][width],doublerec[][width]);
intmain()
{
inti,j;
doubleorg[height][width],dwt[height][width],rec[height][width];
FILE*lena;
FILE*lena3;
FILE*lena4;
lena=fopen("c:
\\lena.raw","rb");
lena3=fopen("c:
\\lena3.raw","wb");
lena4=fopen("c:
\\lena4.raw","wb");
for(i=0;i{
for(j=0;j{
org[i][j]=fgetc(lena);
}
}
twoDDWT(org,dwt,4);
for(i=0;i{
for(j=0;j{
org[i][j]=dwt[i][j];
}
}
twoDDWT(org,dwt,7);
for(i=0;i{
for(j=0;j{
fputc(dwt[i][j],lena3);
}
}
twoDIDWT(dwt,rec,7);
for(i=0;i{
for(j=0;j{
dwt[i][j]=rec[i][j];
}
}
twoDIDWT(dwt,rec,4);
for(i=0;i{
for(j=0;j{
fputc(rec[i][j],lena4);
}
}return0;
}
voidtwoDDWT(doubleorg[][width],doubledwt[][width],intNumofBand)
{
if(NumofBand==4)
{
oneDDWT(0,org,dwt);
oneDDWT(1,org,dwt);
}
if(NumofBand==7)
{
oneDDWT(2,org,dwt);
oneDDWT(3,org,dwt);
}
}
voidoneDDWT(intflag,doubleorg[][width],doubledwt[][width])
{
inti,j;
doubletemp[height][width],temp1[height][width];
doublef_LPF[]={-0.125,0.25,0.75,0.25,-0.125};
doublef_HPF[]={-0.5,1,-0.5};
if(flag==0)//verticaldirection
{
for(i=0;i{
for(j=0;j{if(j==0)
temp[i][j]=f_LPF[0]*org[i][j+2]+f_LPF[1]*org[i][j+1]+f_LPF[2]*org[i][j]+f_LPF[3]
*org[i][j+1]+f_LPF[4]*org[i][j+2];
elseif(j==1)
temp[i][j]=f_LPF[0]*org[i][j]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j+2];
elseif(j>1&&jtemp[i][j]=f_LPF[0]*org[i][j-2]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j+2];
elseif(j==height-2)
temp[i][j]=f_LPF[0]*org[i][j-2]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j];
elseif(j==height-1)
temp[i][j]=f_LPF[0]*org[i][j-2]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j-1]+f_LPF[4]*org[i][j-2];
}
}
for(i=0;i{
for(j=0;j{
dwt[i][j]=temp[i][j*2+1];
}
}
for(i=0;i{
for(j=0;j{if(j==0)temp[i][j]=f_HPF[0]*org[i][j+1]+f_HPF[1]*org[i][j]+f_HPF[2]*org[i][j+1];
elseif(j>0&&jtemp[i][j]=f_HPF[0]*org[i][j-1]+f_HPF[1]*org[i][j]+f_HPF[2]*org[i][j+1];
elseif(j==height-1)temp[i][j]=f_HPF[0]*org[i][j-1]+f_HPF[1]*org[i][j]+f_HPF[2]*org[i][j-1];
}
}
for(i=0;i{
for(j=0;j{
dwt[i][j+width/2]=temp[i][j*2];
}
}
}
if(flag==1)//horizontaldirection
{
for(i=0;i{
for(j=0;j{
if(i==0)temp[i][j]=f_LPF[0]*dwt[i+2][j]+f_LPF[1]*dwt[i+1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i==1)temp[i][j]=f_LPF[0]*dwt[i][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i>1&&i*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i==width-2)temp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i][j];
elseif(i==width-1)temp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i-1][j]+f_LPF[4]*dwt[i-2][j];
}
}
for(i=0;i{for(j=0;jtemp1[i][j]=temp[i*2+1][j];
}
}
for(i=0;i{for(j=0;jif(i==0)temp[i][j]=f_HPF[0]*dwt[i+1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i+1][j];
elseif(i>0&&ielseif(i==width-1)temp[i][j]=f_HPF[0]*dwt[i-1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i-1][j];
}
}
for(i=0;i{for(j=0;jtemp1[i][j+width/2]=temp[i*2][j];
}
for(i=0;i{
for(j=width/2;j{if(i==0)temp[i][j]=f_LPF[0]*dwt[i+2][j]+f_LPF[1]*dwt[i+1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i==1)
temp[i][j]=f_LPF[0]*dwt[i][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i>1&&ielseif(i==height-2)temp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]
*dwt[i+1][j]+f_LPF[4]*dwt[i][j];
elseif(i==height-1)temp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]
*dwt[i-1][j]+f_LPF[4]*dwt[i-2][j];
}
}
for(i=0;i{
for(j=width/2;j{temp1[i+height/2][j-width/2]=temp[i*2+1][j];
}
}
for(i=0;i{
for(j=width/2;j{if(i==0)temp[i][j]=f_HPF[0]*dwt[i+1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i+1][j];elseif(i>0&&itemp[i][j]=f_HPF[0]*dwt[i-1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i-1][j];
}
}
for(i=0;i{
for(j=width/2;j{
temp1[i+height/2][j]=temp[i*2][j];
}
}
for(i=0;i{
for(j=0;j{
dwt[i][j]=temp1[i][j];
}
}
if(flag==2)//verticaldirection
{
for(i=0;i{
for(j=0;j{if(j==0)
temp[i][j]=f_LPF[0]*org[i][j+2]+f_LPF[1]*org[i][j+1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j+2];
elseif(j==1)
temp[i][j]=f_LPF[0]*org[i][j]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j+2];
elseif(j>1&&jtemp[i][j]=f_LPF[0]*org[i][j-2]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j+2];
elseif(j==height/2-2)
temp[i][j]=f_LPF[0]*org[i][j-2]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j+1]+f_LPF[4]*org[i][j];
elseif(j==height/2-1)
temp[i][j]=f_LPF[0]*org[i][j-2]+f_LPF[1]*org[i][j-1]+f_LPF[2]*org[i][j]+f_LPF[3]*org[i][j-1]+f_LPF[4]*org[i][j-2];
}
}
for(i=0;i{
for(j=0;j{dwt[i][j]=temp[i][j*2+1];
}
}
for(i=0;i{
for(j=0;j{if(j==0)
temp[i][j]=f_HPF[0]*org[i][j+1]+f_HPF[1]*org[i][j]+f_HPF[2]*org[i][j+1];elseif(j>0&&jtemp[i][j]=f_HPF[0]*org[i][j-1]+f_HPF[1]*org[i][j]+f_HPF[2]*org[i][j+1];elseif(j==height-1)
temp[i][j]=f_HPF[0]*org[i][j-1]+f_HPF[1]*org[i][j]+f_HPF[2]*org[i][j-1];
}
}
for(i=0;i{
for(j=0;j{
dwt[i][j+width/4]=temp[i][j*2];
}
}
}
if(flag==3)//horizontaldirection
{
for(i=0;i{
for(j=0;j{if(i==0)
temp[i][j]=f_LPF[0]*dwt[i+2][j]+f_LPF[1]*dwt[i+1][j]+f_LPF[2]*dwt[i][j]+
f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];elseif(i==1)
temp[i][j]=f_LPF[0]*dwt[i][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i>1&&itemp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]
*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];elseif(i==height/2-2)
temp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i][j];
elseif(i==height/2-1)
temp[i][j]=f_LPF[0]*dwt[i-2][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i-1][j]+f_LPF[4]*dwt[i-2][j];
}
}
for(i=0;i{
for(j=0;j{temp1[i][j]=temp[i*2+1][j];
}
}
for(i=0;i{
for(j=0;j{if(i==0)temp[i][j]=f_HPF[0]*dwt[i+1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i+1][j];elseif(i>0&&itemp[i][j]=f_HPF[0]*dwt[i-1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i+1][j];elseif(i==height/2-1)
temp[i][j]=f_HPF[0]*dwt[i-1][j]+f_HPF[1]*dwt[i][j]+f_HPF[2]*dwt[i-1][j];}
}
for(i=0;i{
for(j=0;j{temp1[i+height/4][j]=temp[i*2][j];
}
}
for(i=0;i{
for(j=width/4;j{if(i==0)temp[i][j]=f_LPF[0]*dwt[i+2][j]+f_LPF[1]*dwt[i+1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
elseif(i==1)
temp[i][j]=f_LPF[0]*dwt[i][j]+f_LPF[1]*dwt[i-1][j]+f_LPF[2]*dwt[i][j]+f_LPF[3]*dwt[i+1][j]+f_LPF[4]*dwt[i+2][j];
else