1 package JSci.maths.wavelet; 2 3 import JSci.maths.wavelet.*; 4 import JSci.maths.*; 5 6 10 public final class FWTCoef extends Object implements NumericalConstants, Cloneable { 11 protected double[][] coefs; 12 final static double normalisation=1.0/SQRT2; 13 14 public FWTCoef () { 15 } 16 17 19 public FWTCoef (double[][] v) { 20 coefs=v; 21 22 } 23 26 public Object clone() { 27 try { 28 FWTCoef fwt=(FWTCoef) super.clone(); 29 if(coefs!=null) 30 fwt.coefs=ArrayMath.copy(coefs); 31 return(fwt); 32 } catch (CloneNotSupportedException cnse) { 33 throw new InternalError (); 34 } 35 } 36 37 39 public int getJ () { 40 return(coefs.length); 41 } 42 43 44 45 47 public int dimension(int i) { 48 if ((i<0)||(i>=coefs.length)) { 49 throw new IllegalArgumentException ("This dimension doesn't exist : "+i+", "+coefs.length); 50 } 51 return(coefs[i].length); 52 } 53 54 56 public double[][] getCoefs() { 57 return(coefs); 58 } 59 60 64 public double[] norm() { 65 double[] ans=new double[coefs.length]; 66 for(int j=0;j<coefs.length;j++) { 67 ans[j]=ArrayMath.norm(coefs[j]); 68 } 69 return(ans); 70 } 71 72 81 public double norm(int i) { 82 if((i<0)||(i>=coefs.length)) { 83 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 84 } 85 double ans=ArrayMath.norm(coefs[i]); 86 return(ans); 87 } 88 89 90 94 private double[] sumSquares() { 95 double[] ans=new double[coefs.length]; 96 for(int j=0;j<coefs.length;j++) { 97 ans[j]=ArrayMath.sumSquares(coefs[j]); 98 } 99 return(ans); 100 } 101 102 106 public double sumSquares(int i) { 107 if((i<0)||(i>=coefs.length)) { 108 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 109 } 110 double ans=ArrayMath.sumSquares(coefs[i]); 111 return(ans); 112 } 113 114 116 public double mass(int i) { 117 if((i<0)||(i>=coefs.length)) { 118 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 119 } 120 double ans=ArrayMath.mass(coefs[i]); 121 return(ans); 122 } 123 124 126 private double[] variance() { 127 double[] ans=new double[coefs.length]; 128 for(int j=0;j<coefs.length;j++) { 129 ans[j]=ArrayMath.variance(coefs[j]); 130 } 131 return(ans); 132 } 133 134 136 public double variance(int i) { 137 if((i<0)||(i>=coefs.length)) { 138 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 139 } 140 double ans=ArrayMath.variance(coefs[i]); 141 return(ans); 142 } 143 144 145 147 public double sumEnergies() { 148 if(coefs.length<=1) { 149 throw new IllegalArgumentException ("No wavelet coefficients!"); 150 } 151 double[] energies=sumSquares(); 152 double ans=0; 153 for(int k=1;k<energies.length;k++) { 154 ans+=energies[k]; 155 } 156 return(ans); 157 } 158 159 161 public double entropy() { 162 if(coefs.length<=1) { 163 throw new IllegalArgumentException ("No wavelet coefficients!"); 164 } 165 double se=sumEnergies(); 166 int nombreDeCoefficients=0; 167 for(int k=1;k<coefs.length;k++) { 168 nombreDeCoefficients+=coefs[k].length; 169 } 170 double[] er=new double[nombreDeCoefficients]; 171 int pos=0; 172 for(int k=1;k<coefs.length;k++) { 173 for(int l=0;l<coefs[k].length;l++) { 174 er[pos]=coefs[k][l]*coefs[k][l]/se; 175 pos++; 176 } 177 } 178 return(EngineerMath.icf(er)); 179 } 180 181 183 public double sumVariance() { 184 if(coefs.length<=1) { 185 throw new IllegalArgumentException ("No wavelet coefficients!"); 186 } 187 double[] variances=variance(); 188 double ans=0; 189 for(int k=1;k<variances.length;k++) { 190 ans+=variances[k]; 191 } 192 return(ans); 193 } 194 195 197 public double energyRatio(int i) { 198 if(coefs.length<=1) { 199 throw new IllegalArgumentException ("No wavelet coefficients!"); 200 } 201 if((i<1)||(i>=coefs.length)) { 202 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 203 } 204 if(sumEnergies()==0) { 205 if(coefs.length!=0) { 206 return(1/coefs.length); 207 } else { 208 throw new IllegalArgumentException ("No energy!"); 209 } 210 } 211 return(sumSquares(i)/sumEnergies()); 212 } 213 214 216 public double varianceRatio(int i) { 217 if(coefs.length<=1) { 218 throw new IllegalArgumentException ("No wavelet coefficients!"); 219 } 220 if((i<1)||(i>=coefs.length)) { 221 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 222 } 223 if(sumVariance()==0) { 224 if(coefs.length!=0) { 225 return(1/coefs.length); 226 } else { 227 throw new IllegalArgumentException ("No energy!"); 228 } 229 } 230 return(variance(i)/sumVariance()); 231 } 232 233 236 public double icf() { 237 if(coefs.length<=1) { 238 throw new IllegalArgumentException ("No wavelet coefficients!"); 239 } 240 double[] pe=new double[coefs.length-1]; 241 for(int j=1;j<coefs.length;j++) { 242 pe[j-1]=energyRatio(j); 243 } 244 return(EngineerMath.icf(pe)); 245 } 246 247 249 public double varianceICF() { 250 if(coefs.length<=1) { 251 throw new IllegalArgumentException ("No wavelet coefficients!"); 252 } 253 double[] pv=new double[coefs.length-1]; 254 for(int j=1;j<coefs.length;j++) { 255 pv[j-1]=varianceRatio(j); 256 } 257 return(EngineerMath.icf(pv)); 258 } 259 260 262 public void setCoefs(double[][] v) { 263 coefs=v; 264 } 265 266 268 public void setCoefs(double[] v, int i) { 269 if((i<0)||(i>=coefs.length)) { 270 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 271 } 272 coefs[i]=v; 273 } 274 275 277 public void synthesize(Filter filtreprimaire, double[] param) { 278 if(coefs.length<=1) { 279 throw new IllegalArgumentException ("No synthesis possible : "+coefs.length); 280 } 281 double[] V0=filtreprimaire.lowpass(coefs[0],param); 282 double[] W0=filtreprimaire.highpass(coefs[coefs.length-1],param); 283 V0=ArrayMath.scalarMultiply(normalisation,V0); 284 if(V0.length!=W0.length) { 285 throw new IllegalArgumentException ("Synthesis impossible : bad data/multiresolution?"+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length); 286 } 287 V0=ArrayMath.add(V0,W0); 288 double[][] c=new double[coefs.length-1][]; 289 for(int j=1;j<coefs.length-1;j++) { 290 c[j]=coefs[j]; 291 } 292 c[0]=V0; 293 coefs=c; 294 } 295 296 298 public void synthesize(Filter filtreprimaire, double[] param, int jmax) { 299 if ((jmax<0) || (jmax>coefs.length-1)) { 300 throw new IllegalArgumentException ("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1)); 301 } 302 for(int j=0;j<jmax;j++) { 303 synthesize(filtreprimaire, param); 304 } 305 } 306 307 309 public void synthesizeAll(Filter filtreprimaire,double[] param) { 310 synthesize(filtreprimaire, param, coefs.length-1); 311 } 312 313 315 public void synthesize(Filter filtreprimaire) { 316 if(coefs.length<=1) { 317 throw new IllegalArgumentException ("No synthesis possible : "+coefs.length); 318 } 319 double[] V0=filtreprimaire.lowpass(coefs[0]); 320 double[] W0=filtreprimaire.highpass(coefs[coefs.length-1]); 321 V0=ArrayMath.scalarMultiply(normalisation,V0); 322 if(V0.length!=W0.length) { 323 throw new IllegalArgumentException ("Synthesis impossible : bad data/multiresolution?"+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length); 324 } 325 V0=ArrayMath.add(V0,W0); 326 double[][] c=new double[coefs.length-1][]; 327 for(int j=1;j<coefs.length-1;j++) { 328 c[j]=coefs[j]; 329 } 330 c[0]=V0; 331 coefs=c; 332 } 333 334 336 public void synthesize(Filter filtreprimaire, int jmax) { 337 if ((jmax<0) || (jmax>coefs.length-1)) { 338 throw new IllegalArgumentException ("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1)); 339 } 340 for(int j=0;j<jmax;j++) { 341 synthesize(filtreprimaire); 342 } 343 } 344 345 347 public void synthesizeAll(Filter filtreprimaire) { 348 synthesize(filtreprimaire, coefs.length-1); 349 } 350 351 353 public Signal rebuildSignal(Filter filtreprimaire) { 354 FWTCoef fwt=new FWTCoef(coefs); fwt.synthesizeAll(filtreprimaire); 356 return(new Signal(fwt.getCoefs()[0])); 357 } 358 359 361 public Signal rebuildSignal(Filter filtreprimaire, double[] param) { 362 FWTCoef fwt=new FWTCoef(coefs); fwt.synthesizeAll(filtreprimaire,param); 364 return(new Signal(fwt.getCoefs()[0])); 365 } 366 367 371 public void denoise(double p) { 372 for(int k=1;k<coefs.length;k++) { 373 coefs[k]=denoise(coefs[k],p); 374 } 375 } 376 381 public void denoise(double p, int k) { 382 coefs[k]=denoise(coefs[k],p); 383 } 384 385 390 public static double[] denoise(double[] v, double p) { 391 if(p==0) return(v); 392 double[] ans=v; 393 double seuil=ArrayMath.percentile(ArrayMath.abs(ans),1-p); 394 for(int k=0;k<ans.length;k++) { 395 if(Math.abs(ans[k])>=seuil) { 396 ans[k]=0; 397 } 398 } 399 return(ans); 400 } 401 402 406 public void compress(double p) { 407 for(int k=1;k<coefs.length;k++) { 408 coefs[k]=compress(coefs[k],p); 409 } 410 } 411 416 public void compress(double p, int k) { 417 coefs[k]=compress(coefs[k],p); 418 } 419 420 425 public static double[] compress(double[] v, double p) { 426 if(p==0) return(v); 427 double[] ans=v; 428 double seuil=ArrayMath.percentile(ArrayMath.abs(ans),p); 429 for(int k=0;k<ans.length;k++) { 430 if(Math.abs(ans[k])<=seuil) { 431 ans[k]=0; 432 } 433 } 434 return(ans); 435 } 436 437 438 442 public void denoiseHard(double p) { 443 for(int k=1;k<coefs.length;k++) { 444 coefs[k]=denoiseHard(coefs[k],p); 445 } 446 } 447 452 public void denoiseHard(double p, int k) { 453 coefs[k]=denoiseHard(coefs[k],p); 454 } 455 456 461 public static double[] denoiseHard(double[] v, double seuil) { 462 if(seuil<0) { 463 throw new IllegalArgumentException ("The cutoff value must be positive."); 464 } 465 double[] ans=v; 466 for(int k=0; k<ans.length; k++) { 467 if(Math.abs(ans[k]) >= seuil) { 468 ans[k]=0; 469 } 470 } 471 return(ans); 472 } 473 474 478 public void compressHard(double p) { 479 for(int k=1;k<coefs.length;k++) { 480 coefs[k]=compressHard(coefs[k],p); 481 } 482 } 483 488 public void compressHard(double p, int k) { 489 coefs[k]=compressHard(coefs[k],p); 490 } 491 492 497 public static double[] compressHard(double[] v, double seuil) { 498 if(seuil<0) { 499 throw new IllegalArgumentException ("The cutoff value must be positive."); 500 } 501 double[] ans=v; 502 for(int k=0; k<ans.length; k++) { 503 if(Math.abs(ans[k]) <= seuil) { 504 ans[k]=0; 505 } 506 } 507 return(ans); 508 } 509 } 510 511
| Popular Tags
|