当前位置:首页 > 代码 > 正文

小波逆变换c语言代码(小波变换函数)

admin 发布:2022-12-19 20:36 99


今天给各位分享小波逆变换c语言代码的知识,其中也会对小波变换函数进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

C语言怎么实现小波变换

#include stdio.h

#include stdlib.h

#define LENGTH 512//信号长度

/******************************************************************

* 一维卷积函数

*

* 说明: 循环卷积,卷积结果的长度与输入信号的长度相同

*

* 输入参数: data[],输入信号; core[],卷积核; cov[],卷积结果;

*  n,输入信号长度; m,卷积核长度.

*

* 李承宇, lichengyu2345@126.com

*

*  2010-08-18  

******************************************************************/

void Covlution(double data[], double core[], double cov[], int n, int m)

{

int i = 0;

int j = 0;

int k = 0;

//将cov[]清零

for(i = 0; i  n; i++)

{

cov[i] = 0;

}

//前m/2+1行

i = 0;

for(j = 0; j  m/2; j++, i++)

{

for(k = m/2-j; k  m; k++ )

{

cov[i] += data[k-(m/2-j)] * core[k];//k针对core[k]

}

for(k = n-m/2+j; k  n; k++ )

{

cov[i] += data[k] * core[k-(n-m/2+j)];//k针对data[k]

}

}

//中间的n-m行

for( i = m/2; i = (n-m)+m/2; i++)

{

for( j = 0; j  m; j++)

{

cov[i] += data[i-m/2+j] * core[j];

}

}

//最后m/2-1行

i = (n - m) + m/2 + 1;

for(j = 1; j  m/2; j++, i++)

{

for(k = 0; k  j; k++)

{

cov[i] += data[k] * core[m-j-k];//k针对data[k]

}

for(k = 0; k  m-j; k++)

{

cov[i] += core[k] * data[n-(m-j)+k];//k针对core[k]

}

}

}

/******************************************************************

* 一维小波变换函数

*

* 说明: 一维小波变换,只变换一次

*

* 输入参数: input[],输入信号; output[],小波变换结果,包括尺度系数和

* 小波系数两部分; temp[],存放中间结果;h[],Daubechies小波基低通滤波器系数;

* g[],Daubechies小波基高通滤波器系数;n,输入信号长度; m,Daubechies小波基紧支集长度.

*

* 李承宇, lichengyu2345@126.com

*

*  2010-08-19  

******************************************************************/

void DWT1D(double input[], double output[], double temp[], double h[], 

   double g[], int n, int m)

{

// double temp[LENGTH] = {0};//?????????????

int i = 0;

/*

//尺度系数和小波系数放在一起

Covlution(input, h, temp, n, m);

for(i = 0; i  n; i += 2)

{

output[i] = temp[i];

}

Covlution(input, g, temp, n, m);

for(i = 1; i  n; i += 2)

{

output[i] = temp[i];

}

*/

//尺度系数和小波系数分开

Covlution(input, h, temp, n, m);

for(i = 0; i  n; i += 2)

{

output[i/2] = temp[i];//尺度系数

}

Covlution(input, g, temp, n, m);

for(i = 1; i  n; i += 2)

{

output[n/2+i/2] = temp[i];//小波系数

}

}

void main()

{

double data[LENGTH];//输入信号

double temp[LENGTH];//中间结果

double data_output[LENGTH];//一维小波变换后的结果

int n = 0;//输入信号长度

int m = 6;//Daubechies正交小波基长度

int i = 0; 

char s[32];//从txt文件中读取一行数据

static double h[] = {.332670552950, .806891509311, .459877502118, -.135011020010, 

-.085441273882, .035226291882};

static double g[] = {.035226291882, .085441273882, -.135011020010, -.459877502118,

.806891509311, -.332670552950};

//读取输入信号

FILE *fp;

fp=fopen("data.txt","r");

if(fp==NULL) //如果读取失败

{

printf("错误!找不到要读取的文件/"data.txt/"/n");

exit(1);//中止程序

}

while( fgets(s, 32, fp) != NULL )//读取长度n要设置得长一点,要保证读到回车符,这样指针才会定位到下一行?回车符返回的是零值?是,非数字字符经过atoi变换都应该返回零值

{

// fscanf(fp,"%d", data[count]);//一定要有""啊!!!最后读了个回车符!适应能力不如atoi啊

data[n] = atof(s);

n++;

}

//一维小波变换

DWT1D(data, data_output, temp, h, g, n, m);

//一维小波变换后的结果写入txt文件

fp=fopen("data_output.txt","w");

//打印一维小波变换后的结果

for(i = 0; i  n; i++)

{

printf("%f/n", data_output[i]);

fprintf(fp,"%f/n", data_output[i]);

}

//关闭文件

fclose(fp);

}

小波分解与合成的完整代码

% Load original 1D signal.

load leleccum; s = leleccum(1:3920);

ls = length(s);

% Perform one step decomposition of s using db1.

[ca1,cd1] = dwt(s,'db1');

% Perform one step reconstruction of ca1 and cd1.

a1 = upcoef('a',ca1,'db1',1,ls);

d1 = upcoef('d',cd1,'db1',1,ls);

% Now plot a1 + d1.

% Invert directly decomposition of s using coefficients.

a0 = idwt(ca1,cd1,'db1',ls);

% Perform decomposition at level 3 of s using db1.

[c,l] = wavedec(s,3,'db1')

% Extract approximation coefficients at level 3,

% from wavelet decomposition structure [c,l].

ca3 = appcoef(c,l,'db1',3);

% Extract detail coefficients at levels 1, 2 and 3,

% from wavelet decomposition structure [c,l].

cd3 = detcoef(c,l,3);

cd2 = detcoef(c,l,2);

cd1 = detcoef(c,l,1);

% Reconstruct approximation at level 3,

% from wavelet decomposition structure [c,l].

a3 = wrcoef('a',c,l,'db1',3);

% Reconstruct detail coefficients at levels 1,2 and 3,

% from the wavelet decomposition structure [c,l].

d3 = wrcoef('d',c,l,'db1',3);

d2 = wrcoef('d',c,l,'db1',2);

d1 = wrcoef('d',c,l,'db1',1);

% Reconstruct s from the wavelet decomposition structure [c,l].

a0 = waverec(c,l,'db1');

小波变换

我给你大概标注了一下,但是你的程序有问题,

% 小波图像压缩 - RGB 图像

clear all;

close all;

% 读取图像

im = input('输入图像');%输入图像名称,要加分号

X=imread(im);

% 输入要分解的小波层数和小波

n=input('输入要分解的小波层数');%输入所要分解的层数

wname = input('输入小波名称');%输入小波名称,也要加分号

x = double(X);

NbColors = 255;

map = gray(NbColors);

x = uint8(x);

%把RGB图像转换成灰度图

% x = double(X);

% xrgb = 0.2990*x(:,:,1) + 0.5870*x(:,:,2) + 0.1140*x(:,:,3);

% colors = 255;

% x = wcodemat(xrgb,colors);

% map = pink(colors);

% x = uint8(x);

% 对图像x进行n维小波分解

[c,s] = wavedec2(x,n,wname);

% 使用默认参数选择各层不同的阈值

alpha = 1.5; m = 2.7*prod(s(1,:));

[thr,nkeep] = wdcbm2(c,s,alpha,m)

% 使用上面的阈值和硬阈值处理进行图像压缩

[xd,cxd,sxd,perf0,perfl2] = wdencmp('lvd',c,s,wname,n,thr,'h');

disp('压缩效率');

disp(perf0);

% 重构(下面这个地方有问题,你这里是原始图像小波变换后进行重构,xd才是小波阀值压缩后重构的图像,cxd,sxd,是c,s经过阀值处理后得到的小波分解结构,也就是说xd=waverec2(cxd,sxd,wname);这个wdencmp函数不需要另外进行重构,你下面那些关于重构的都没用,而下面压缩后的图像才是重构后的图像,)

R = waverec2(c,s,wname);

rc = uint8(R);

% 显示原始图像和压缩图像

subplot(221), image(x);

colormap(map);

title('原始图像')

subplot(222), image(xd);

colormap(map);

title('压缩后的图像')

% 显示结果

xlab1 = ['图像压缩后保留能量百分比',num2str(perfl2)];

xlab2 = ['小波阀值压缩后置零系数百分比 ',num2str(perf0), ' %'];

xlabel([xlab1 xlab2]);

subplot(223), image(rc);

colormap(map);

title('重构图像');

%计算图像大小

disp('原始图像');

imwrite(x,'original.tif');%将图像x保存为original.tif,下同

imfinfo('original.tif')%显示图片original.tif详细信息,下同

disp('压缩后的图像');

imwrite(xd,'compressed.tif');

imfinfo('compressed.tif')

disp('重构后的图像');

imwrite(rc,'decompressed.tif');

imfinfo('decompressed.tif')

如何用C语言实现小波多层变换wavedec2

C语言标准库当中没有这种函数,去OpenCV库当中找找有没有类似函数,如果自己实现想是相当麻烦

小波逆变换c语言代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于小波变换函数、小波逆变换c语言代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/23121.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载