第5章-小波变换的matlab实现
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第4章
小波变换的matlab实现
1
1.Matlab中小波种类 15种
经典类小波:Harr小波、Morlet小波、Mexican
hat小波、Gaussian小波
正交小波:db小波、对称小波、Coiflets小波、
Meyer小波
双正交小波 查看命令
wavemngr('read',1)
2
格式:
1. X=idwt(cA,cD,’wname’) 2. X=idwt(cA,cD,Lo_R, Hi_R)
3. X=idwt(cA,cD,’wname’,L)
4. X=idwt(cA,cD, Lo_R, Hi_R, L)
逆变换恢复信号
1
2
550 500 450
举例:A0=idwt(cA1,cD1,'db1',ls); 400
8
单尺度分解——dwt2 格式: 1. [cA1,cH1,cV1,cD1]=dwt2(X,’wname’) 2. [cA1,cH1,cV1,cD1]=dwt2(X,Lo_D,Hi_D)
cA1, cH1水平;cV1垂直;cD1对角 应用:
load wbarb; figure(1); image(X); colormap(map); colorbar; [cA1,cH1,cV1,cD1]=dwt2(X,’bior3.7’)
重构系数
1
9
A3=wrcoef('a',C,L,'db1',3);
D1=wrcoef('d',C,L,'db1',1); D2=wrcoef('d',C,L,'db1',2);
D3=wrcoef('d',C,L,'db1',3);
Approximation A3 600 40 20 0 200 -20 -40 Detail D1
1600 1400 1200 1000 800 600 400 200 0 -200
1
0 500 1000 1500 2000 2500 3000 3500 4000
5
命令:appcoef 格式:
1. A=appcoef(C,L,’wname’,N)
2. A=appcoef(C,L,’wname’) 3. A=appcoef(C,L,Lo_R,Hi_R,N) 4. A=appcoef(C,L, Lo_R,Hi_R)
1 0.5
46 43 40 37 34 31 28 25 22 19 16 13 10 7 4 1
0
小波分析示例
-0.5 -1 -1.5 0 100 200 300 400 500 600 700 800 900 1000
scales a
100
200
300
400 500 600 time (or space) b
scales a
4
图形接口方式(GUI)
命令:wavemenu
5
6
7
命令:dwt 格式:
[cA1,cD1]=dwt(X,’wname’)
[cA1,cD1]=dwt(X,Lo_D,Hi_D) 举例: load leleccum; s=leleccum(1:3920); 一维离散小波分解 ls=length(s); [cA1,cD1]=dwt(s,’db1’);
-100
命令:wrcoef 格式: 1. X=wrcoef(‘type’,C,L,’wname’,N) 2. X=wrcoef(‘type’,C,L,Lo_R,Hi_R,N) 3. X=wrcoef(‘type’,C,L,’wname’) 4. X=wrcoef(‘type’,C,L, Lo_R,Hi_R) type=‘a’ 低频, type=‘d’ 高频
350 300 250 200 150 100
0
500
1ቤተ መጻሕፍቲ ባይዱ00
1500
2000
2500
3000
3500
4000
1
3
命令:wavedec
格式:
[C, L]=wavedec(X,N,’wname’) [C, L]=wavedec(X,N,Lo_D,Hi_D)
多尺度一维分解
1
4
[C,L]=wavedec(s,3,'db1');
700
800
900
1000
3
C=cwt(noissin,2:2:128,’db4’,’plot’)
Absolute Values of Ca,b Coefficients for a = 2 4 6 8 10 ... 122 114 106 98 90 82 74 66 58 50 42 34 26 18 10 2 100 200 300 400 500 600 time (or space) b 700 800 900 1000
6. Y=upcoef(O,X,Lo_R,Hi_R)
O=‘a’ 低频,O=‘d’ 高频
系数重构
1
0
举例:
A1=upcoef('a','cA1','db1',1,ls); D1=upcoef('d','cD1','db1',1,ls); subplot(1,2,1);plot(A1);title('Approximation A1')
Approximation cA3 2000 1500 1000 500 0 40 20 0 -20 -40 Detail cD1
0
200
400
600
0
500
1000 Detail cD3
1500
2000
Detail cD2 50 50
0 0 -50
1
8
0 200 400 600
-50
0
500
1000
6. Y=upcoef2(O,X,Lo_R,Hi_R)
O:‘a’低频;‘h’水平;‘v’垂直;‘d’对角
重构系数
3
0
A1 H1 V1 D1
= upcoef2('a',cA1,'bior3.7',1); = upcoef2('h',cH1,'bior3.7',1); = upcoef2('v',cV1,'bior3.7',1); = upcoef2('d',cD1,'bior3.7',1);
400
0
0
1000
2000 Detail D2
3000
4000
0
1000
2000 Detail D3
3000
4000
40 20 0 -20 -40
40 20 0 -20 -40
2
0
0 1000 2000 3000 4000
0
1000
2000
3000
4000
命令:waverec 格式: 1.X=waverec(C,L,’wname’) 2.X=waverec(C,L,Lo_R,Hi_R)
2. 二维离散小波
2
9
命令:upcoef2 格式: 1. Y=upcoef2(O,X,’wname’,N,S) 2. Y=upcoef2(O,X,Lo_R,Hi_R,N,S) 3. Y=upcoef2(O,X,’wname’,N) 4. Y=upcoef2(O,X,Lo_R,Hi_R,N) 5. Y=upcoef2(O,X,’wname’)
1. [C, S]=wavedec2(X,N,’wname’) 2.[C, S]=wavedec2(X,N,Lo_D,Hi_D)
多尺度二维小波
3
4
[C,S] = wavedec2(X,2,'bior3.7');
%图像的多尺度二维小波分解
3
5
命令:appcoef2 格式:
1. A=appcoef2(C,S,’wname’,N)
8
550 500 450 400 350 300 250 200
原始信号
低频系数
800 700 600
150 100
0
500
1000
1500
2000
2500
3000
3500
4000
高频系数
30
20
10
500
0
400
-10
300
-20
200
9
0 200 400 600 800 1000 1200 1400 1600 1800 2000
A= detcoef2(‘type’,C,S,’wname’,N)
说明:
Type: ’h’ 水平;‘v’垂直;‘d’对角
cH2 = detcoef2('h',C,S,2);
cV2 = detcoef2('v',C,S,2);
cD2 = detcoef2('d',C,S,2); 提取高频系数 cH1 = detcoef2('h',C,S,1); cV1 = detcoef2('v',C,S,1);
一维连续小波 1. coefs = cwt(s,scale,’wname’) 2. coefs = cwt(s,scale,’wname’,’plot’)
1.5
Absolute Values of Ca,b Coefficients for a = 1 2 3 4 5 ...
c=cwt(noissin,1:48,'db4','plot');
3
1
title('Vertical Detail V1')subplot(2,2,4); image(wcodemat(D1,192));title('Diagonal
Approximation A1 50 100 150 200 250 50 100 150 200 250 Vertical Detail V1 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250
例子: A0=waverec(C,L,’db1’); 重构最大误差: Err=max(abs(s-A0))
重构原始信号
2
1
original
Level 3 Approximation
Original Approximation
2
2
图形接口方式(GUI)
2
3
2
4
2
5
2
6
2
7
2
100
-30
0
200
400
600
800
1000
1200
1400
1600
1800
2000
命令:upcoef 格式: 1. Y=upcoef(O,X,’wname’,N) 2. Y=upcoef(O,X,’wname’,N,L) 3. Y=upcoef(O,X,’Lo_R, Hi_R’,N) 4. Y=upcoef(O,X,’Lo_R,Hi_R’,N,L) 5. Y=upcoef(O,X,’wname’)
Horizontal Detail H1
50
100 150 200 250
Diagonal Detail D1
50
100 150 200 250
3
2
命令:idwt2 格式: 1. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7'); 2. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
figure(2);colormap(map);
subplot(2,2,1); image(wcodemat(A1,192)); title('Approximation A1') subplot(2,2,2); image(wcodemat(H1,192)); title('Horizontal Detail H1') subplot(2,2,3); image(wcodemat(V1,192));
2. A=appcoef2(C,S,’wname’) 3. A=appcoef2(C,S,Lo_R,Hi_R)
4. A=appcoef2(C,S,Lo_R,Hi_R,N)
提取低频系数
cA2 = appcoef2(C,S,'bior3.7',2); %从上面的C中提取第二层的低频系 数
3
6
命令:detcoef2 格式:
subplot(1,2,2);plot(D1);title('Detail D1')
Approximation A1 75 70 65 60 20 55 0 50 -20 45 40 35 30 -40 -60 -80 80 60 40 Detail D1
1
1
0 2 4 6
0
2
4
6
命令:idwt
3. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
4. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
应用:
Xsyn = idwt2(cA1,cH1,cV1,cD1,'bior3.7'); 二维逆变换
3
3
命令:wavedec2
格式:
低频系数提取
1
6
命令:detcoef
格式:
1. A=detcoef(C,L,N) 2. A=detcoef(C,L)
高频系数提取
1
7
举例
cA3=appcoef(C,L,'db1',3);
cD3=detcoef(C,L,3);
cD2=detcoef(C,L,2); cD1=detcoef(C,L,1);
小波变换的matlab实现
1
1.Matlab中小波种类 15种
经典类小波:Harr小波、Morlet小波、Mexican
hat小波、Gaussian小波
正交小波:db小波、对称小波、Coiflets小波、
Meyer小波
双正交小波 查看命令
wavemngr('read',1)
2
格式:
1. X=idwt(cA,cD,’wname’) 2. X=idwt(cA,cD,Lo_R, Hi_R)
3. X=idwt(cA,cD,’wname’,L)
4. X=idwt(cA,cD, Lo_R, Hi_R, L)
逆变换恢复信号
1
2
550 500 450
举例:A0=idwt(cA1,cD1,'db1',ls); 400
8
单尺度分解——dwt2 格式: 1. [cA1,cH1,cV1,cD1]=dwt2(X,’wname’) 2. [cA1,cH1,cV1,cD1]=dwt2(X,Lo_D,Hi_D)
cA1, cH1水平;cV1垂直;cD1对角 应用:
load wbarb; figure(1); image(X); colormap(map); colorbar; [cA1,cH1,cV1,cD1]=dwt2(X,’bior3.7’)
重构系数
1
9
A3=wrcoef('a',C,L,'db1',3);
D1=wrcoef('d',C,L,'db1',1); D2=wrcoef('d',C,L,'db1',2);
D3=wrcoef('d',C,L,'db1',3);
Approximation A3 600 40 20 0 200 -20 -40 Detail D1
1600 1400 1200 1000 800 600 400 200 0 -200
1
0 500 1000 1500 2000 2500 3000 3500 4000
5
命令:appcoef 格式:
1. A=appcoef(C,L,’wname’,N)
2. A=appcoef(C,L,’wname’) 3. A=appcoef(C,L,Lo_R,Hi_R,N) 4. A=appcoef(C,L, Lo_R,Hi_R)
1 0.5
46 43 40 37 34 31 28 25 22 19 16 13 10 7 4 1
0
小波分析示例
-0.5 -1 -1.5 0 100 200 300 400 500 600 700 800 900 1000
scales a
100
200
300
400 500 600 time (or space) b
scales a
4
图形接口方式(GUI)
命令:wavemenu
5
6
7
命令:dwt 格式:
[cA1,cD1]=dwt(X,’wname’)
[cA1,cD1]=dwt(X,Lo_D,Hi_D) 举例: load leleccum; s=leleccum(1:3920); 一维离散小波分解 ls=length(s); [cA1,cD1]=dwt(s,’db1’);
-100
命令:wrcoef 格式: 1. X=wrcoef(‘type’,C,L,’wname’,N) 2. X=wrcoef(‘type’,C,L,Lo_R,Hi_R,N) 3. X=wrcoef(‘type’,C,L,’wname’) 4. X=wrcoef(‘type’,C,L, Lo_R,Hi_R) type=‘a’ 低频, type=‘d’ 高频
350 300 250 200 150 100
0
500
1ቤተ መጻሕፍቲ ባይዱ00
1500
2000
2500
3000
3500
4000
1
3
命令:wavedec
格式:
[C, L]=wavedec(X,N,’wname’) [C, L]=wavedec(X,N,Lo_D,Hi_D)
多尺度一维分解
1
4
[C,L]=wavedec(s,3,'db1');
700
800
900
1000
3
C=cwt(noissin,2:2:128,’db4’,’plot’)
Absolute Values of Ca,b Coefficients for a = 2 4 6 8 10 ... 122 114 106 98 90 82 74 66 58 50 42 34 26 18 10 2 100 200 300 400 500 600 time (or space) b 700 800 900 1000
6. Y=upcoef(O,X,Lo_R,Hi_R)
O=‘a’ 低频,O=‘d’ 高频
系数重构
1
0
举例:
A1=upcoef('a','cA1','db1',1,ls); D1=upcoef('d','cD1','db1',1,ls); subplot(1,2,1);plot(A1);title('Approximation A1')
Approximation cA3 2000 1500 1000 500 0 40 20 0 -20 -40 Detail cD1
0
200
400
600
0
500
1000 Detail cD3
1500
2000
Detail cD2 50 50
0 0 -50
1
8
0 200 400 600
-50
0
500
1000
6. Y=upcoef2(O,X,Lo_R,Hi_R)
O:‘a’低频;‘h’水平;‘v’垂直;‘d’对角
重构系数
3
0
A1 H1 V1 D1
= upcoef2('a',cA1,'bior3.7',1); = upcoef2('h',cH1,'bior3.7',1); = upcoef2('v',cV1,'bior3.7',1); = upcoef2('d',cD1,'bior3.7',1);
400
0
0
1000
2000 Detail D2
3000
4000
0
1000
2000 Detail D3
3000
4000
40 20 0 -20 -40
40 20 0 -20 -40
2
0
0 1000 2000 3000 4000
0
1000
2000
3000
4000
命令:waverec 格式: 1.X=waverec(C,L,’wname’) 2.X=waverec(C,L,Lo_R,Hi_R)
2. 二维离散小波
2
9
命令:upcoef2 格式: 1. Y=upcoef2(O,X,’wname’,N,S) 2. Y=upcoef2(O,X,Lo_R,Hi_R,N,S) 3. Y=upcoef2(O,X,’wname’,N) 4. Y=upcoef2(O,X,Lo_R,Hi_R,N) 5. Y=upcoef2(O,X,’wname’)
1. [C, S]=wavedec2(X,N,’wname’) 2.[C, S]=wavedec2(X,N,Lo_D,Hi_D)
多尺度二维小波
3
4
[C,S] = wavedec2(X,2,'bior3.7');
%图像的多尺度二维小波分解
3
5
命令:appcoef2 格式:
1. A=appcoef2(C,S,’wname’,N)
8
550 500 450 400 350 300 250 200
原始信号
低频系数
800 700 600
150 100
0
500
1000
1500
2000
2500
3000
3500
4000
高频系数
30
20
10
500
0
400
-10
300
-20
200
9
0 200 400 600 800 1000 1200 1400 1600 1800 2000
A= detcoef2(‘type’,C,S,’wname’,N)
说明:
Type: ’h’ 水平;‘v’垂直;‘d’对角
cH2 = detcoef2('h',C,S,2);
cV2 = detcoef2('v',C,S,2);
cD2 = detcoef2('d',C,S,2); 提取高频系数 cH1 = detcoef2('h',C,S,1); cV1 = detcoef2('v',C,S,1);
一维连续小波 1. coefs = cwt(s,scale,’wname’) 2. coefs = cwt(s,scale,’wname’,’plot’)
1.5
Absolute Values of Ca,b Coefficients for a = 1 2 3 4 5 ...
c=cwt(noissin,1:48,'db4','plot');
3
1
title('Vertical Detail V1')subplot(2,2,4); image(wcodemat(D1,192));title('Diagonal
Approximation A1 50 100 150 200 250 50 100 150 200 250 Vertical Detail V1 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250 50 100 150 200 250
例子: A0=waverec(C,L,’db1’); 重构最大误差: Err=max(abs(s-A0))
重构原始信号
2
1
original
Level 3 Approximation
Original Approximation
2
2
图形接口方式(GUI)
2
3
2
4
2
5
2
6
2
7
2
100
-30
0
200
400
600
800
1000
1200
1400
1600
1800
2000
命令:upcoef 格式: 1. Y=upcoef(O,X,’wname’,N) 2. Y=upcoef(O,X,’wname’,N,L) 3. Y=upcoef(O,X,’Lo_R, Hi_R’,N) 4. Y=upcoef(O,X,’Lo_R,Hi_R’,N,L) 5. Y=upcoef(O,X,’wname’)
Horizontal Detail H1
50
100 150 200 250
Diagonal Detail D1
50
100 150 200 250
3
2
命令:idwt2 格式: 1. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7'); 2. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
figure(2);colormap(map);
subplot(2,2,1); image(wcodemat(A1,192)); title('Approximation A1') subplot(2,2,2); image(wcodemat(H1,192)); title('Horizontal Detail H1') subplot(2,2,3); image(wcodemat(V1,192));
2. A=appcoef2(C,S,’wname’) 3. A=appcoef2(C,S,Lo_R,Hi_R)
4. A=appcoef2(C,S,Lo_R,Hi_R,N)
提取低频系数
cA2 = appcoef2(C,S,'bior3.7',2); %从上面的C中提取第二层的低频系 数
3
6
命令:detcoef2 格式:
subplot(1,2,2);plot(D1);title('Detail D1')
Approximation A1 75 70 65 60 20 55 0 50 -20 45 40 35 30 -40 -60 -80 80 60 40 Detail D1
1
1
0 2 4 6
0
2
4
6
命令:idwt
3. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
4. X = idwt2(cA1,cH1,cV1,cD1,'bior3.7');
应用:
Xsyn = idwt2(cA1,cH1,cV1,cD1,'bior3.7'); 二维逆变换
3
3
命令:wavedec2
格式:
低频系数提取
1
6
命令:detcoef
格式:
1. A=detcoef(C,L,N) 2. A=detcoef(C,L)
高频系数提取
1
7
举例
cA3=appcoef(C,L,'db1',3);
cD3=detcoef(C,L,3);
cD2=detcoef(C,L,2); cD1=detcoef(C,L,1);