# Adding noise to data

**URL:** https://community.freefem.org/t/adding-noise-to-data/673
**Category:** General Discussion
**Created:** [December 1, 2020, 9:58pm UTC](https://community.freefem.org/t/adding-noise-to-data/673 "2020-12-01T21:58:10Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)
#### Post date: [December 1, 2020, 9:58pm UTC](https://community.freefem.org/t/adding-noise-to-data/673/1 "2020-12-01T21:58:10Z")

</div>

Dear users,

I would like to add noise to data in my code using FF++,  
Can anyone send please an example of adding noise?

Thanks in advance,  
Mordechai.

---

<div class="post-metadata">

### Author: ![julienG](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/julieng/32/222_2.png) [@julienG](https://community.freefem.org/u/julienG)
#### Post date: [December 2, 2020, 7:53am UTC](https://community.freefem.org/t/adding-noise-to-data/673/2 "2020-12-02T07:53:16Z")

</div>

Depends what kind of noise. If you want simple white noise,

> Vh toto = sin(x)\*(1+0.2\*randreal1());  
> plot(toto,wait=1,value=1,fill=1);

should do the job

---

<div class="post-metadata">

### Author: ![mordechaiy](https://avatars.discourse-cdn.com/v4/letter/m/cc9497/32.png) [@mordechaiy](https://community.freefem.org/u/mordechaiy)
#### Post date: [December 2, 2020, 10:49am UTC](https://community.freefem.org/t/adding-noise-to-data/673/3 "2020-12-02T10:49:23Z")

</div>

Thank you Julien!

I would like to add a Gaussian noise to my data in the following way:  
suppose u(x,t) is my data with no noise,  
define  
u\_with\_noise=(1+coeff\*randn)\*u(x,t).  
Where, “coeff” is the level of noise and “randn” is a centered normal law.

I found a code for randn which uses the Box-Muller transform

func real randn()  
{  
real U1, U2;  
U1 = randreal3();  
U2 = randreal3();  
return sqrt(-2_log(U1))cos(2pi_U2);  
}

I defined in my code that  
real coeff=0.2; (20% level of noise)  
u is calculated in the code and I used the piece of code that generates “randn”  
thus,  
u\_with\_noise=(1+coeff\*randn)\*u(x,t);

Is it correct?

Thanks in advance,  
Mordechai.
