Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals

noise.c

Go to the documentation of this file.
00001 /************************************************************************* 00002 * $Id: noise.c,v 1.6 2005/09/23 09:09:18 yougz Exp $ 00003 ************************************************************************** 00004 This software is governed by the CeCILL license under French law and 00005 abiding by the rules of distribution of free software. You can use, 00006 modify and/ or redistribute the software under the terms of the CeCILL 00007 license as circulated by CEA, CNRS and INRIA at the following URL 00008 "http://www.cecill.info". 00009 00010 As a counterpart to the access to the source code and rights to copy, 00011 modify and redistribute granted by the license, users are provided only 00012 with a limited warranty and the software's author, the holder of the 00013 economic rights, and the successive licensors have only limited 00014 liability. 00015 00016 In this respect, the user's attention is drawn to the risks associated 00017 with loading, using, modifying and/or developing or reproducing the 00018 software by the user in light of its specific status of free software, 00019 that may mean that it is complicated to manipulate, and that also 00020 therefore means that it is reserved for developers and experienced 00021 professionals having in-depth computer knowledge. Users are therefore 00022 encouraged to load and test the software's suitability as regards their 00023 requirements in conditions enabling the security of their systems and/or 00024 data to be ensured and, more generally, to use and operate it in the 00025 same conditions as regards security. 00026 00027 The fact that you are presently reading this means that you have had 00028 knowledge of the CeCILL license and that you accept its terms. 00029 00030 Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de 00031 l'Image). All rights reserved. See License.txt for details. 00032 00033 Version 1.0 05/09/2005 00034 *************************************************************************/ 00035 00036 #include "noise.h" 00037 #include <math.h> 00038 #include <idseq.h> 00039 #include <stdlib.h> 00040 00041 #define PI 3.14159265358979 00042 00051 double RandGaussian(double s, double m, int initseed) 00052 { 00053 double r1,r2; 00054 double x; 00055 00056 if (initseed!=0) srand((unsigned)(initseed*time(NULL))); 00057 00058 r1=rand()/(double)RAND_MAX; 00059 r2=rand()/(double)RAND_MAX; 00060 00061 x= (s*sqrt(-2.0*log(r1))*cos(2*PI*r2)) + m; 00062 00063 return(x); 00064 } 00065 00073 double RandUniform(int initseed) 00074 { 00075 double x; 00076 00077 if (initseed!=0) srand((unsigned)(initseed*time(NULL))); 00078 00079 x=rand()/(double)RAND_MAX; 00080 00081 return(x); 00082 } 00083 00091 void AddGaussianNoiseToRFVolume(PPPVOLUME_COMPLEX_DOUBLE volrf,double s) 00092 { 00093 int i,j,k; 00094 double x,y,z; 00095 double r1,r2; 00096 00097 x= (double) IdVolDimX(volrf); 00098 y= (double) IdVolDimY(volrf); 00099 z= (double) IdVolDimZ(volrf); 00100 00101 for(k=0;k<z;k++) for(j=0;j<y;j++) 00102 { 00103 r1=RandGaussian(s,0,k*j); /* to change seed beetween each 1D signal */ 00104 for(i=0;i<x;i++) /* 1 signal RF 1D */ 00105 { 00106 r1=RandGaussian(s,0,0); 00107 r2=RandGaussian(s,0,0); 00108 volrf[k][j][i].re=volrf[k][j][i].re + r1; 00109 volrf[k][j][i].im=volrf[k][j][i].im + r2; 00110 } 00111 } 00112 } 00113 00121 void AddGaussianNoisePercentToRFVolume(PPPVOLUME_COMPLEX_DOUBLE volrf,double p) 00122 { 00123 int i,j,k; 00124 double x,y,z; 00125 double r1,r2; 00126 double m,s; 00127 m=0.0; 00128 00129 x= (double) IdVolDimX(volrf); 00130 y= (double) IdVolDimY(volrf); 00131 z= (double) IdVolDimZ(volrf); 00132 00133 for(k=0;k<z;k++) for(j=0;j<y;j++) for(i=0;i<x;i++) 00134 { 00135 if (volrf[k][j][i].re >m) m=volrf[k][j][i].re; 00136 if (volrf[k][j][i].im >m) m=volrf[k][j][i].im; 00137 } 00138 s = m*p; 00139 00140 for(k=0;k<z;k++) for(j=0;j<y;j++) 00141 { 00142 r1=RandGaussian(s,0,k*j); /* to change seed beetween each 1D signal */ 00143 for(i=0;i<x;i++) /* 1 signal RF 1D */ 00144 { 00145 r1=RandGaussian(s,0,0); 00146 r2=RandGaussian(s,0,0); 00147 volrf[k][j][i].re=volrf[k][j][i].re + r1; 00148 volrf[k][j][i].im=volrf[k][j][i].im + r2; 00149 } 00150 } 00151 } 00152 00160 void AddNoiseToObject(OBJECT3D *object,double s) 00161 { 00162 int i,j,k,n; 00163 double x,y,z; 00164 double r0,r1,r2; 00165 int nbparam; 00166 00167 x=(double) object->x; 00168 y=(double) object->y; 00169 z=(double) object->z; 00170 nbparam = object->nbparam; 00171 00172 r0=RandGaussian(1,0,1); /* Initialize seed */ 00173 00174 for (n=0;n<object->nbcomponent;n++) 00175 { 00176 for(k=0;k<z;k++) for(j=0;j<y;j++) for(i=0;i<x;i++) 00177 { 00178 r0=RandGaussian(s,0,0); 00179 r1=RandGaussian(s,0,0); 00180 r2=RandGaussian(s,0,0); 00181 if (object->values[nbparam*n+0][k][j][i]!=0) 00182 object->values[nbparam*n+0][k][j][i] += (short) r0; 00183 object->values[nbparam*n+1][k][j][i] += (short) r1; 00184 object->values[nbparam*n+2][k][j][i] += (short) r2; 00185 } 00186 } 00187 } 00188 00196 void AddNoisePercentToObject(OBJECT3D *object,double p) 00197 { 00198 int i,j,k,n; 00199 double x,y,z; 00200 double r0,r1,r2,sro,st1,st2; 00201 short maxro,maxt1,maxt2; 00202 int nbparam; 00203 00204 x=(double) object->x; 00205 y=(double) object->y; 00206 z=(double) object->z; 00207 nbparam = object->nbparam; 00208 00209 r0=RandGaussian(1,0,1); /* Initialize seed */ 00210 00211 for (n=0;n<object->nbcomponent;n++) 00212 { 00213 maxro=0;maxt1=0;maxt2=0; 00214 for(k=0;k<z;k++) for(j=0;j<y;j++) for(i=0;i<x;i++) 00215 { 00216 if (object->values[nbparam*n+0][k][j][i]>maxro) 00217 maxro=object->values[nbparam*n+0][k][j][i]; 00218 if (object->values[nbparam*n+1][k][j][i]>maxro) 00219 maxt1=object->values[nbparam*n+1][k][j][i]; 00220 if (object->values[nbparam*n+2][k][j][i]>maxro) 00221 maxt2=object->values[nbparam*n+2][k][j][i]; 00222 } 00223 sro = maxro*p; st1 = maxt1*p; st2 = maxt2*p; 00224 for(k=0;k<z;k++) for(j=0;j<y;j++) for(i=0;i<x;i++) 00225 { 00226 r0=RandGaussian(sro,0,0); 00227 r1=RandGaussian(st1,0,0); 00228 r2=RandGaussian(st2,0,0); 00229 if (object->values[nbparam*n+0][k][j][i]!=0) 00230 object->values[nbparam*n+0][k][j][i] += (short) r0; 00231 object->values[nbparam*n+1][k][j][i] += (short) r1; 00232 object->values[nbparam*n+2][k][j][i] += (short) r2; 00233 } 00234 } 00235 00236 } 00237 00245 void ApplyMeanToObject(OBJECT3D *object,int N) 00246 { 00247 int x,y,z; 00248 int p,q,r; 00249 int i,j,k; 00250 int n,n2; 00251 int nbparam; 00252 unsigned short val0,val1,val2; 00253 00254 if (N%2 == 0) 00255 { 00256 printf("Only an odd length mean average filter is supported !\n"); 00257 exit(1); 00258 } 00259 00260 x=object->x; 00261 y=object->y; 00262 z=object->z; 00263 nbparam = object->nbparam; 00264 00265 n2 = (N-1)/2; 00266 00267 for (n=0;n<object->nbcomponent;n++) 00268 { 00269 if ((x!=1)&&(y!=1)&&(z!=1)) /*3D*/ 00270 { 00271 N = N*N*N; 00272 for(k=n2;k<z-n2;k++) for(j=n2;j<y-n2;j++) for(i=n2;i<x-n2;i++) 00273 { 00274 val0=0; val1=0; val2=0; 00275 for(r=-n2;(r<=n2)&&(r!=0);r++) for(q=-n2;(q<=n2)&&(q!=0);q++) for(p=-n2;(p<=n2)&&(p!=0);p++) 00276 { 00277 val0 += object->values[nbparam*n+0][k+r][j+q][i+p]; 00278 val1 += object->values[nbparam*n+1][k+r][j+q][i+p]; 00279 val2 += object->values[nbparam*n+2][k+r][j+q][i+p]; 00280 } 00281 object->values[nbparam*n+0][k][j][i]= val0 / N; 00282 object->values[nbparam*n+1][k][j][i]= val1 / N; 00283 object->values[nbparam*n+2][k][j][i]= val2 / N; 00284 00285 } 00286 } 00287 if ((x!=1)&&(y!=1)&&(z==1)) /*2D*/ 00288 { 00289 N = N*N; 00290 for(k=0;k<z;k++) for(j=n2;j<y-n2;j++) for(i=n2;i<x-n2;i++) 00291 { 00292 val0=0; val1=0; val2=0; 00293 for(q=-n2;q<=n2;q++) for(p=-n2;p<=n2;p++) 00294 { 00295 val0 += object->values[nbparam*n+0][k][j+q][i+p]; 00296 val1 += object->values[nbparam*n+1][k][j+q][i+p]; 00297 val2 += object->values[nbparam*n+2][k][j+q][i+p]; 00298 } 00299 object->values[nbparam*n+0][k][j][i]= val0 / N; 00300 object->values[nbparam*n+1][k][j][i]= val1 / N; 00301 object->values[nbparam*n+2][k][j][i]= val2 / N; 00302 } 00303 } 00304 if ((x!=1)&&(y==1)&&(z==1)) /*1D*/ 00305 { 00306 for(k=0;k<z;k++) for(j=0;j<y;j++) for(i=n2;i<x-n2;i++) 00307 { 00308 val0=0; val1=0; val2=0; 00309 for(p=-n2;p<=n2;p++) 00310 { 00311 val0 += object->values[nbparam*n+0][k][j][i+p]; 00312 val1 += object->values[nbparam*n+1][k][j][i+p]; 00313 val2 += object->values[nbparam*n+2][k][j][i+p]; 00314 } 00315 object->values[nbparam*n+0][k][j][i]= val0 / N; 00316 object->values[nbparam*n+1][k][j][i]= val1 / N; 00317 object->values[nbparam*n+2][k][j][i]= val2 / N; 00318 } 00319 } 00320 } 00321 }

Generated on Wed Oct 19 09:28:34 2005 for SIMRI3D by doxygen 1.3.7