Mercurial > hg > smallbox
comparison util/Rice Wavelet Toolbox/denoise.m @ 78:f69ae88b8be5
added Rice Wavelet Toolbox with my modification, so it can be compiled on newer systems.
author | Ivan Damnjanovic lnx <ivan.damnjanovic@eecs.qmul.ac.uk> |
---|---|
date | Fri, 25 Mar 2011 15:27:33 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
76:d052ec5b742f | 78:f69ae88b8be5 |
---|---|
1 function [xd,xn,option] = denoise(x,h,type,option) | |
2 % [xd,xn,option] = denoise(x,h,type,option); | |
3 % | |
4 % DENOISE is a generic program for wavelet based denoising. | |
5 % The program will denoise the signal x using the 2-band wavelet | |
6 % system described by the filter h using either the traditional | |
7 % discrete wavelet transform (DWT) or the linear shift invariant | |
8 % discrete wavelet transform (also known as the undecimated DWT | |
9 % (UDWT)). | |
10 % | |
11 % Input: | |
12 % x : 1D or 2D signal to be denoised | |
13 % h : Scaling filter to be applied | |
14 % type : Type of transform (Default: type = 0) | |
15 % 0 --> Discrete wavelet transform (DWT) | |
16 % 1 --> Undecimated DWT (UDWT) | |
17 % option : Default settings is marked with '*': | |
18 % *type = 0 --> option = [0 3.0 0 0 0 0] | |
19 % type = 1 --> option = [0 3.6 0 1 0 0] | |
20 % option(1) : Whether to threshold low-pass part | |
21 % 0 --> Don't threshold low pass component | |
22 % 1 --> Threshold low pass component | |
23 % option(2) : Threshold multiplier, c. The threshold is | |
24 % computed as: | |
25 % thld = c*MAD(noise_estimate)). | |
26 % The default values are: | |
27 % c = 3.0 for the DWT based denoising | |
28 % c = 3.6 for the UDWT based denoising | |
29 % option(3) : Type of variance estimator | |
30 % 0 --> MAD (mean absolute deviation) | |
31 % 1 --> STD (classical numerical std estimate) | |
32 % option(4) : Type of thresholding | |
33 % 0 --> Soft thresholding | |
34 % 1 --> Hard thresholding | |
35 % option(5) : Number of levels, L, in wavelet decomposition. By | |
36 % setting this to the default value '0' a maximal | |
37 % decomposition is used. | |
38 % option(6) : Actual threshold to use (setting this to | |
39 % anything but 0 will mean that option(3) | |
40 % is ignored) | |
41 % | |
42 % Output: | |
43 % xd : Estimate of noise free signal | |
44 % xn : The estimated noise signal (x-xd) | |
45 % option : A vector of actual parameters used by the | |
46 % program. The vector is configured the same way as | |
47 % the input option vector with one added element | |
48 % option(7) = type. | |
49 % | |
50 % HERE'S AN EASY WAY TO RUN THE EXAMPLES: | |
51 % Cut-and-paste the example you want to run to a new file | |
52 % called ex.m, for example. Delete out the % at the beginning | |
53 % of each line in ex.m (Can use search-and-replace in your editor | |
54 % to replace it with a space). Type 'ex' in matlab and hit return. | |
55 % | |
56 % Example 1: | |
57 % h = daubcqf(6); [s,N] = makesig('Doppler'); n = randn(1,N); | |
58 % x = s + n/10; % (approximately 10dB SNR) | |
59 % figure;plot(x);hold on;plot(s,'r'); | |
60 % | |
61 % %Denoise x with the default method based on the DWT | |
62 % [xd,xn,opt1] = denoise(x,h); | |
63 % figure;plot(xd);hold on;plot(s,'r'); | |
64 % | |
65 % %Denoise x using the undecimated (LSI) wavelet transform | |
66 % [yd,yn,opt2] = denoise(x,h,1); | |
67 % figure;plot(yd);hold on;plot(s,'r'); | |
68 % | |
69 % Example 2: (on an image) | |
70 % h = daubcqf(6); load lena; | |
71 % noisyLena = lena + 25 * randn(size(lena)); | |
72 % figure; colormap(gray); imagesc(lena); title('Original Image'); | |
73 % figure; colormap(gray); imagesc(noisyLena); title('Noisy Image'); | |
74 % Denoise lena with the default method based on the DWT | |
75 % [denoisedLena,xn,opt1] = denoise(noisyLena,h); | |
76 % figure; colormap(gray); imagesc(denoisedLena); title('denoised Image'); | |
77 % | |
78 % | |
79 % See also: mdwt, midwt, mrdwt, mirdwt, SoftTh, HardTh, setopt | |
80 % | |
81 | |
82 %File Name: denoise.m | |
83 %Last Modification Date: 04/15/97 10:44:28 | |
84 %Current Version: denoise.m 2.4 | |
85 %File Creation Date: Mon Feb 20 08:33:15 1995 | |
86 %Author: Jan Erik Odegard <odegard@ece.rice.edu> | |
87 % | |
88 %Copyright (c) 2000 RICE UNIVERSITY. All rights reserved. | |
89 %Created by Jan Erik Odegard, Department of ECE, Rice University. | |
90 % | |
91 %This software is distributed and licensed to you on a non-exclusive | |
92 %basis, free-of-charge. Redistribution and use in source and binary forms, | |
93 %with or without modification, are permitted provided that the following | |
94 %conditions are met: | |
95 % | |
96 %1. Redistribution of source code must retain the above copyright notice, | |
97 % this list of conditions and the following disclaimer. | |
98 %2. Redistribution in binary form must reproduce the above copyright notice, | |
99 % this list of conditions and the following disclaimer in the | |
100 % documentation and/or other materials provided with the distribution. | |
101 %3. All advertising materials mentioning features or use of this software | |
102 % must display the following acknowledgment: This product includes | |
103 % software developed by Rice University, Houston, Texas and its contributors. | |
104 %4. Neither the name of the University nor the names of its contributors | |
105 % may be used to endorse or promote products derived from this software | |
106 % without specific prior written permission. | |
107 % | |
108 %THIS SOFTWARE IS PROVIDED BY WILLIAM MARSH RICE UNIVERSITY, HOUSTON, TEXAS, | |
109 %AND CONTRIBUTORS AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
110 %BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
111 %FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RICE UNIVERSITY | |
112 %OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
113 %EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
114 %PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
115 %OR BUSINESS INTERRUPTIONS) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
116 %WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
117 %OTHERWISE), PRODUCT LIABILITY, OR OTHERWISE ARISING IN ANY WAY OUT OF THE | |
118 %USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
119 % | |
120 %For information on commercial licenses, contact Rice University's Office of | |
121 %Technology Transfer at techtran@rice.edu or (713) 348-6173 | |
122 % | |
123 %Change History: Fixed output of function and an error in the computation | |
124 % of the threshold for redundant denoising. | |
125 % <Jan Erik Odegard> <Mon Jul 31, 1995> | |
126 % | |
127 % This code is composed of several of our old codes for | |
128 % wavelet based denoising. In an effort to make the mess | |
129 % more manageable we decided to create on code that would | |
130 % handle all the various wavelet based denoising methods. | |
131 % However, only time will show (as we discover new and | |
132 % improved forms of denoising) if we can succeed in our goals. | |
133 % <Jan Erik Odegard> <Thu May 11, 1995> | |
134 % | |
135 | |
136 if(nargin < 2) | |
137 error('You need to provide at least 2 inputs: x and h'); | |
138 end; | |
139 if(nargin < 3), | |
140 type = 0; | |
141 option = []; | |
142 elseif(nargin < 4) | |
143 option = []; | |
144 end; | |
145 if(isempty(type)), | |
146 type = 0; | |
147 end; | |
148 if(type == 0), | |
149 default_opt = [0 3.0 0 0 0 0]; | |
150 elseif(type == 1), | |
151 default_opt = [0 3.6 0 1 0 0]; | |
152 else, | |
153 error(['Unknown denoising method',10,... | |
154 'If it is any good we need to have a serious talk :-)']); | |
155 end; | |
156 option = setopt(option,default_opt); | |
157 [mx,nx] = size(x); | |
158 dim = min(mx,nx); | |
159 if(dim == 1), | |
160 n = max(mx,nx); | |
161 else, | |
162 n = dim; | |
163 end; | |
164 if(option(5) == 0), | |
165 L = floor(log2(n)); | |
166 else | |
167 L = option(5); | |
168 end; | |
169 if(type == 0), % Denoising by DWT | |
170 xd = mdwt(x,h,L); | |
171 if (option(6) == 0), | |
172 tmp = xd(floor(mx/2)+1:mx,floor(nx/2)+1:nx); | |
173 if(option(3) == 0), | |
174 thld = option(2)*median(abs(tmp(:)))/.67; | |
175 elseif(option(3) == 1), | |
176 thld = option(2)*std(tmp(:)); | |
177 else | |
178 error('Unknown threshold estimator, Use either MAD or STD'); | |
179 end; | |
180 else, | |
181 thld = option(6); | |
182 end; | |
183 if(dim == 1) | |
184 ix = 1:n/(2^L); | |
185 ykeep = xd(ix); | |
186 else | |
187 ix = 1:mx/(2^L); | |
188 jx = 1:nx/(2^L); | |
189 ykeep = xd(ix,jx); | |
190 end; | |
191 if(option(4) == 0), | |
192 xd = SoftTh(xd,thld); | |
193 elseif(option(4) == 1), | |
194 xd = HardTh(xd,thld); | |
195 else, | |
196 error('Unknown threshold rule. Use either Soft (0) or Hard (1)'); | |
197 end; | |
198 if (option(1) == 0), | |
199 if(dim == 1), | |
200 xd(ix) = ykeep; | |
201 else, | |
202 xd(ix,jx) = ykeep; | |
203 end; | |
204 end; | |
205 xd = midwt(xd,h,L); | |
206 elseif(type == 1), % Denoising by UDWT | |
207 [xl,xh] = mrdwt(x,h,L); | |
208 if(dim == 1), | |
209 c_offset = 1; | |
210 else, | |
211 c_offset = 2*nx + 1; | |
212 end; | |
213 if (option(6) == 0), | |
214 tmp = xh(:,c_offset:c_offset+nx-1); | |
215 if(option(3) == 0), | |
216 thld = option(2)*median(abs(tmp(:)))/.67; | |
217 elseif(option(3) == 1), | |
218 thld = option(2)*std(tmp(:)); | |
219 else | |
220 error('Unknown threshold estimator, Use either MAD or STD'); | |
221 end; | |
222 else, | |
223 thld = option(6); | |
224 end; | |
225 if(option(4) == 0), | |
226 xh = SoftTh(xh,thld); | |
227 if(option(1) == 1), | |
228 xl = SoftTh(xl,thld); | |
229 end; | |
230 elseif(option(4) == 1), | |
231 xh = HardTh(xh,thld); | |
232 if(option(1) == 1), | |
233 xl = HardTh(xl,thld); | |
234 end; | |
235 else, | |
236 error('Unknown threshold rule. Use either Soft (0) or Hard (1)'); | |
237 end; | |
238 xd = mirdwt(xl,xh,h,L); | |
239 else, % Denoising by unknown method | |
240 error(['Unknown denoising method',10,... | |
241 'If it is any good we need to have a serious talk :-)']); | |
242 end; | |
243 option(6) = thld; | |
244 option(7) = type; | |
245 xn = x - xd; |