//stochastique //simulation autour de la Resonance stochastique // ©Jimmy Roussel //http://perso.ensc-rennes.fr/jimmy.roussel //Oct 2009 // Mise à jour pour processing.js : Juin 2012 // Dernière mise à jour : Avril 2016 /* @pjs pauseOnBlur="true";preload="./img/NB.jpg,./img/couleur1.jpg,./img/gris.jpg,./img/couleur2.jpg"; */ //****** Déclaration ******** PImage img,img0,img1,img2,img3; float bruit;//niveau de bruit float seuilFiltrage;//seuil du filtre NL int w = 200; //largeur de chaque image //******** Initialisation *************** void setup() { size(600,450); frameRate(10); seuilFiltrage=1; bruit=0.5; img0 = loadImage("./img/NB.jpg");//image en N&B img2 = loadImage("./img/couleur1.jpg");//image en couleur img1 = loadImage("./img/gris.jpg");//image en niveau de gris img3 = loadImage("./img/couleur2.jpg");//image en couleur img=img0; text("",0,0); // force Processing to load a font textAlign(CORNER); textSize(12); colorMode(RGB, 1.0) } void draw() { background(#FFFFFF); image(img,10,height/2-100); loadPixels(); StochastiqueGris(img,(width-w-10),10,bruit,seuilFiltrage); NonStochastique(img,(width-w-10),height-w-10,seuilFiltrage); updatePixels(); dessinFiltre(); copyright(); } //**** Procédures ***** void StochastiqueGris(PImage imgg, int xx, int yy, float a, float seuil){ float alea; float s; for (int x =0; x < imgg.width; x++) { for (int y =0; y < imgg.height; y++ ) { int pos = x + imgg.width*y; s=0; float r = red(imgg.pixels[pos]); r+=random(-a,a); s=fonctionNL(r,seuil,0.05); color c1 = color(s,s,s); int locx=x+xx; int locy=y+yy; int loc1 = locx + locy*width; pixels[loc1] = c1; } } } void StochastiqueCouleur(PImage imgg, int xx, int yy, float a, float seuil){ for (int x =0; x < imgg.width; x++) { for (int y =0; y < imgg.height; y++ ) { int pos = x + imgg.width*y; float r = red(imgg.pixels[pos]); float g = green(imgg.pixels[pos]); float b = blue(imgg.pixels[pos]); r+=random(-a,a); g+=random(-a,a); b+=random(-a,a); r= fonctionNL(r,seuil,0.05); g= fonctionNL(g,seuil,0.05); b= fonctionNL(b,seuil,0.05); color c1 = color(r,g,b); int locx=x+xx; int locy=y+yy; int loc1 = locx + locy*width; pixels[loc1] = c1; } } } //calcul de la sortie sans ajout de bruit void NonStochastique(PImage imgg, int xx, int yy, float seuil){ for (int x =0; x < imgg.width; x++) { for (int y =0; y < imgg.height; y++ ) { int pos = x + imgg.width*y; float r = red(imgg.pixels[pos]); float g = green(imgg.pixels[pos]); float b = blue(imgg.pixels[pos]); r= fonctionNL(r,seuil,0.05); g= fonctionNL(g,seuil,0.05); b= fonctionNL(b,seuil,0.05); color c1 = color(r,g,b); int locx=x+xx; int locy=y+yy; int loc1 = locx + locy*width; pixels[loc1] = c1; } } } //procédure de tracé du schéma bloc void dessinFiltre(){ pushMatrix(); translate(width/2,height/2);//centrage de l'origine du repère rectMode(CENTER); stroke(0); fill(1); line(0,100,0,-100); line(0,100,100,100); line(0,-100,100,-100); line(-100,20,0,20); ellipse(-70,-20,10,10); line(-100,-20,0,-20); line(-70,-40,-70,-15); rect(0,0,70,70); fill(0); text("bruit",-75,-45); text("+",-80,-30); noFill(); beginShape(); vertex(-25,25); vertex(0,25); vertex(0,-25); vertex(25,-25); endShape(); popMatrix(); rectMode(CORNER); } void copyright(){ pushMatrix(); translate(10,height-10); fill(0); text("©Jimmy ROUSSEL",0,0); popMatrix(); } //réponse non linéaire de la chaîne de transmission. Il s'agit d'une fonction arctangente. plus la largeur est petite polus la non linéarité est importante. float fonctionNL(float x,float seuil,float largeur) { float y; y=atan((2*(x-seuil)/largeur))/PI+0.5; return(y); } void changeSeuil(int x){seuilFiltrage=0.01*x;} void changeBruit(int x){bruit=0.01*x;} void setImage(int n){ if(n==1){img=img0}; if(n==2){img=img1}; if(n==3){img=img2}; if(n==4){img=img3};}