1 package JSci.maths.wavelet; 2 3 import JSci.maths.*; 4 import JSci.maths.wavelet.*; 5 6 11 public final class FWTPacketCoef extends Object implements NumericalConstants, Cloneable { 12 protected double[][] coefs; 13 protected boolean[] StandardChoice; 14 final static double normalisation=1.0/SQRT2; 15 16 public FWTPacketCoef () { 17 } 18 19 21 public FWTPacketCoef (double[][] v, boolean[] b) { 22 coefs=v; 23 StandardChoice=b; 24 if(b.length!=v.length-1) { 25 throw new IllegalArgumentException ("boolean[].length must be exactly double[][].length -1: boolean[].length="+b.length+" and double[][].length="+v.length); 26 } 27 } 28 31 public Object clone() { 32 try { 33 FWTPacketCoef fwtp=(FWTPacketCoef) super.clone(); 34 if(coefs!=null) 35 fwtp.coefs=ArrayMath.copy(coefs); 36 if(StandardChoice!=null) { 37 fwtp.StandardChoice = new boolean [StandardChoice.length]; 38 System.arraycopy(StandardChoice,0,fwtp.StandardChoice,0,StandardChoice.length); 39 } 40 return(fwtp); 41 } catch (CloneNotSupportedException cnse) { 42 throw new InternalError (); 43 } 44 } 45 46 48 public int getJ () { 49 return(coefs.length); 50 } 51 52 53 54 56 public int dimension(int i) { 57 if ((i<0)||(i>=coefs.length)) { 58 throw new IllegalArgumentException ("This scale doesn't exist : "+i+", "+coefs.length); 59 } 60 return(coefs[i].length); 61 } 62 63 65 public double[][] getCoefs() { 66 return(coefs); 67 } 68 69 73 public double[] norm() { 74 double[] ans=new double[coefs.length]; 75 for(int j=0;j<coefs.length;j++) { 76 ans[j]=ArrayMath.norm(coefs[j]); 77 } 78 return(ans); 79 } 80 81 85 public double norm(int i) { 86 if((i<0)||(i>=coefs.length)) { 87 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 88 } 89 double ans=ArrayMath.norm(coefs[i]); 90 return(ans); 91 } 92 93 94 98 private double[] sumSquares() { 99 double[] ans=new double[coefs.length]; 100 for(int j=0;j<coefs.length;j++) { 101 ans[j]=ArrayMath.sumSquares(coefs[j]); 102 } 103 return(ans); 104 } 105 106 110 public double sumSquares(int i) { 111 if((i<0)||(i>=coefs.length)) { 112 throw new IllegalArgumentException ("The integer parameter "+i+" must be between 0 et "+(coefs.length-1)); 113 } 114 double ans=ArrayMath.sumSquares(coefs[i]); 115 return(ans); 116 } 117 118 120 public double mass(int i) { 121 if((i<0)||(i>=coefs.length)) { 122 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 123 } 124 double ans=ArrayMath.mass(coefs[i]); 125 return(ans); 126 } 127 128 130 private double[] variance() { 131 double[] ans=new double[coefs.length]; 132 for(int j=0;j<coefs.length;j++) { 133 ans[j]=ArrayMath.variance(coefs[j]); 134 } 135 return(ans); 136 } 137 138 140 public double variance(int i) { 141 if((i<0)||(i>=coefs.length)) { 142 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 143 } 144 double ans=ArrayMath.variance(coefs[i]); 145 return(ans); 146 } 147 148 149 public double sumEnergies() { 150 if(coefs.length<=1) { 151 throw new IllegalArgumentException ("No wavelet coefficients!"); 152 } 153 double[] energies=sumSquares(); 154 double ans=0.0; 155 for(int k=0;k<energies.length;k++) { 156 ans+=energies[k]; 157 } 158 return(ans); 159 } 160 161 public double entropy() { 162 if(coefs.length<=1) { 163 throw new IllegalArgumentException ("No wavelet coefficients!"); 164 } 165 double sumEnergies=sumEnergies(); 166 int nombreDeCoefficients=0; 167 for(int k=0;k<coefs.length;k++) { 168 nombreDeCoefficients+=coefs[k].length; 169 } 170 double[] pourcentageDEnergie=new double[nombreDeCoefficients]; 171 int pos=0; 172 for(int k=0;k<coefs.length;k++) { 173 for(int l=0;l<coefs[k].length;l++) { 174 pourcentageDEnergie[pos]=coefs[k][l]*coefs[k][l]/sumEnergies; 175 pos++; 176 } 177 } 178 return(EngineerMath.icf(pourcentageDEnergie)); 179 } 180 181 public double sumVariance() { 182 if(coefs.length<=1) { 183 throw new IllegalArgumentException ("No wavelet coefficients!"); 184 } 185 double[] variances=variance(); 186 double ans=0.0; 187 for(int k=0;k<variances.length;k++) { 188 ans+=variances[k]; 189 } 190 return(ans); 191 } 192 193 public double energyRatio(int i) { 194 if(coefs.length<=1) { 195 throw new IllegalArgumentException ("No wavelet coefficients!"); 196 } 197 if((i<0)||(i>=coefs.length)) { 198 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 199 } 200 if(sumEnergies()==0) { 201 if(coefs.length!=0) { 202 return(1/coefs.length); 203 } else { 204 throw new IllegalArgumentException ("No energy!"); 205 } 206 } 207 return(sumSquares(i)/sumEnergies()); 208 } 209 210 211 212 public double varianceRatio(int i) { 213 if(coefs.length<=1) { 214 throw new IllegalArgumentException ("No wavelet coefficients!"); 215 } 216 if((i<0)||(i>=coefs.length)) { 217 throw new IllegalArgumentException ("The integer parament "+i+" should be between 0 and "+(coefs.length-1)); 218 } 219 if(sumVariance()==0) { 220 if(coefs.length!=0) { 221 return(1/coefs.length); 222 } else { 223 throw new IllegalArgumentException ("No variance!"); 224 } 225 } 226 return(variance(i)/sumVariance()); 227 } 228 229 232 public double icf() { 233 if(coefs.length<=1) { 234 throw new IllegalArgumentException ("No wavelet coefficients!"); 235 } 236 double[] pe=new double[coefs.length-1]; 237 for(int j=0;j<coefs.length;j++) { 238 pe[j]=energyRatio(j); 239 } 240 return(EngineerMath.icf(pe)); 241 } 242 243 public double varianceICF() { 244 if(coefs.length<=1) { 245 throw new IllegalArgumentException ("No wavelet coefficients!"); 246 } 247 double[] pv=new double[coefs.length-1]; 248 for(int j=0;j<coefs.length;j++) { 249 pv[j]=varianceRatio(j); 250 } 251 return(EngineerMath.icf(pv)); 252 } 253 254 public void setCoefs(double[][] v) { 255 coefs=v; 256 } 257 258 262 public void setPacket(boolean[] b) { 263 StandardChoice=b; 264 } 265 266 public void setCoefs(double[] v, int i) { 267 if((i<0)||(i>=coefs.length)) { 268 throw new IllegalArgumentException ("The integer parameter "+i+" should be between 0 and "+(coefs.length-1)); 269 } 270 coefs[i]=v; 271 } 272 273 public void synthesize(Filter filtreprimaire, double[] param) { 274 if(coefs.length<=1) { 275 throw new IllegalArgumentException ("No synthesis left to do: "+coefs.length); 276 } 277 double[] V0,W0; 278 if(StandardChoice[StandardChoice.length-1]) { 279 V0=filtreprimaire.lowpass(coefs[coefs.length-1],param); 280 V0=ArrayMath.scalarMultiply(normalisation,V0); 282 W0=filtreprimaire.highpass(coefs[coefs.length-2],param); 283 } else { 284 V0=filtreprimaire.lowpass(coefs[coefs.length-2],param); 285 V0=ArrayMath.scalarMultiply(normalisation,V0); 287 W0=filtreprimaire.highpass(coefs[coefs.length-1],param); 288 } 289 if(V0.length!=W0.length) { 290 throw new IllegalArgumentException ("Synthesis is impossible : bad data/multiresolution? "+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length); 291 } 292 V0=ArrayMath.add(V0,W0); 293 double[][] c=new double[coefs.length-1][]; 294 for(int j=0;j<coefs.length-2;j++) { 295 c[j]=coefs[j]; 296 } 297 c[coefs.length-1]=V0; 298 coefs=c; 299 boolean[] b=new boolean[c.length-1]; 300 for(int j=0;j<b.length;j++) { 301 b[j]=StandardChoice[j]; 302 } 303 StandardChoice=b; 304 } 305 306 public void synthesize(Filter filtreprimaire, double[] param, int jmax) { 307 if ((jmax<0) || (jmax>coefs.length-1)) { 308 throw new IllegalArgumentException ("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1)); 309 } 310 for(int j=0;j<jmax;j++) { 311 synthesize(filtreprimaire, param); 312 } 313 } 314 315 public void synthesizeTout(Filter filtreprimaire,double[] param) { 316 synthesize(filtreprimaire, param, coefs.length-1); 317 } 318 319 public void synthesize(Filter filtreprimaire) { 320 if(coefs.length<=1) { 321 throw new IllegalArgumentException ("No synthesis left to do: "+coefs.length); 322 } 323 double[] V0,W0; 324 if(StandardChoice[StandardChoice.length-1]) { 325 V0=filtreprimaire.lowpass(coefs[coefs.length-1]); 326 V0=ArrayMath.scalarMultiply(normalisation,V0); 328 W0=filtreprimaire.highpass(coefs[coefs.length-2]); 329 } else { 330 V0=filtreprimaire.lowpass(coefs[coefs.length-2]); 331 V0=ArrayMath.scalarMultiply(normalisation,V0); 333 W0=filtreprimaire.highpass(coefs[coefs.length-1]); 334 } 335 if(V0.length!=W0.length) { 336 throw new IllegalArgumentException ("Synthesis is impossible : bad data/multiresolution? "+coefs[0].length+", "+coefs[coefs.length-1].length+", "+V0.length+", "+W0.length); 337 } 338 V0=ArrayMath.add(V0,W0); 339 double[][] c=new double[coefs.length-1][]; 340 for(int j=0;j<coefs.length-2;j++) { 341 c[j]=coefs[j]; 342 } 343 c[coefs.length-1]=V0; 344 coefs=c; 345 boolean[] b=new boolean[c.length-1]; 346 for(int j=0;j<b.length;j++) { 347 b[j]=StandardChoice[j]; 348 } 349 StandardChoice=b; 350 } 351 352 public void synthesize(Filter filtreprimaire, int jmax) { 353 if ((jmax<0) || (jmax>coefs.length-1)) { 354 throw new IllegalArgumentException ("The integer parameter "+jmax+" must be between 0 and "+(coefs.length-1)); 355 } 356 for(int j=0;j<jmax;j++) { 357 synthesize(filtreprimaire); 358 } 359 } 360 361 public void synthesizeAll(Filter filtreprimaire) { 362 synthesize(filtreprimaire, coefs.length-1); 363 } 364 365 public Signal rebuildSignal(Filter filtreprimaire) { 366 FWTCoef fwt=new FWTCoef(coefs); fwt.synthesizeAll(filtreprimaire); 368 return(new Signal(fwt.getCoefs()[0])); 369 } 370 371 public Signal rebuildSignal(Filter filtreprimaire, double[] param) { 372 FWTCoef fwt=new FWTCoef(coefs); fwt.synthesizeAll(filtreprimaire,param); 374 return(new Signal(fwt.getCoefs()[0])); 375 } 376 377 381 public void denoise(double p) { 382 for(int k=1;k<coefs.length;k++) { 383 coefs[k]=denoise(coefs[k],p); 384 } 385 } 386 391 public void denoise(double p, int k) { 392 coefs[k]=denoise(coefs[k],p); 393 } 394 395 400 public static double[] denoise(double[] v, double p) { 401 if(p==0.0) return(v); 402 double[] ans=v; 403 double seuil=ArrayMath.percentile(ArrayMath.abs(ans),1-p); 404 for(int k=0;k<ans.length;k++) { 405 if(Math.abs(ans[k])>=seuil) { 406 ans[k]=0.0; 407 } 408 } 409 return(ans); 410 } 411 412 416 public void compress(double p) { 417 for(int k=0;k<coefs.length;k++) { 418 coefs[k]=compress(coefs[k],p); 419 } 420 } 421 426 public void compress(double p, int k) { 427 coefs[k]=compress(coefs[k],p); 428 } 429 430 435 public static double[] compress(double[] v, double p) { 436 if(p==0.0) return(v); 437 double[] ans=v; 438 double seuil=ArrayMath.percentile(ArrayMath.abs(ans),p); 439 for(int k=0;k<ans.length;k++) { 440 if(Math.abs(ans[k])<=seuil) { 441 ans[k]=0.0; 442 } 443 } 444 return(ans); 445 } 446 447 448 452 public void denoiseHard(double p) { 453 for(int k=0;k<coefs.length;k++) { 454 coefs[k]=denoiseHard(coefs[k],p); 455 } 456 } 457 462 public void denoiseHard(double p, int k) { 463 coefs[k]=denoiseHard(coefs[k],p); 464 } 465 466 471 public static double[] denoiseHard(double[] v, double seuil) { 472 if(seuil<0.0) { 473 throw new IllegalArgumentException ("The cutoff value must be positive."); 474 } 475 double[] ans=v; 476 for(int k=0;k<ans.length;k++) { 477 if(Math.abs(ans[k])>=seuil) { 478 ans[k]=0.0; 479 } 480 } 481 return(ans); 482 } 483 484 488 public void compressHard(double p) { 489 for(int k=0;k<coefs.length;k++) { 490 coefs[k]=compressHard(coefs[k],p); 491 } 492 } 493 498 public void compressHard(double p, int k) { 499 coefs[k]=compressHard(coefs[k],p); 500 } 501 502 507 public static double[] compressHard(double[] v, double seuil) { 508 if(seuil<0.0) { 509 throw new IllegalArgumentException ("The cutoff value must be positive."); 510 } 511 double[] ans=v; 512 for(int k=0;k<ans.length;k++) { 513 if(Math.abs(ans[k])<=seuil) { 514 ans[k]=0.0; 515 } 516 } 517 return(ans); 518 } 519 } 520 521
| Popular Tags
|