Adding random noise to a gradient

In summary, the conversation is about creating a quilt with a gradient of six shades of blue and experimenting with adding noise to it. The person is exploring Photoshop to achieve this but is limited by the program's filters. They are looking for suggestions on how to randomize the gradient using a mathematical procedure. The conversation also includes a discussion about swapping pixels and using a spreadsheet to generate a random gradient. However, there are issues with creating a true random gradient and the person is seeking guidance on how to improve the process.
  • #1
DaveC426913
Gold Member
22,497
6,168
TL;DR Summary
I have a grid of tiles filled with exactly 6 colours in a gradient from 1 to 6. I want to add "noise" to it using some sort of randomization technique.
I am designing the pattern of a quilt my wife is making. The quilt is made of 15x20 squares of exactly six shades of blue - dark at one end to light at the other end.

The gradient will be "noisy". I want to experiment with that noise.

I am exploring Photoshop to do this visually, but it doesn't deal well with limited palettes. In particular any filters such as "add noise" are not available to an image with an indexed (i.e. less than 16-bit) palette.

Left: no noise:
Right: noise done manually:

1607815172250.png
1607814751742.png


I am thinking of some sort of mathematical procedure that spits out a value from 1 to 6 into every cell of a 15x20 table.

There are a few ways I can see this varying, but mostly it involves the "spread" - whether colour "4" is smeared across only bands with colours "3" and "5", or whether it is smeared all the way from, say band 2 to 6. That would presumably affect the parameters of the randomizer.

I am looking for suggestions on how to randomize this gradient above and beyond my fruitless attempts in Photoshop.
 
Last edited:
Mathematics news on Phys.org
  • #2
I would define a function f(x,y) for the average color at place x,y, then add a random number taken from e.g. a Gaussian, then round to the nearest integer. The width of the Gaussian is a free parameter.

f(x,y) is not limited to the range from 1 to 6. Values outside that range will give most/nearly all tiles the color 1 or 6.
 
  • #3
If you're able to play with the pixels why not just do swapping over a line crossing thru the gradients.

11111222223333344445555566666777778888899999
11112122232333434445455565666767778788898999

1121122122334334434455655665667787788788...

in my case I swapped digits across the borders 1111122222 1111212222...

you could make the distance between the two pixel swap within a range of 1-5 pixels and then choose a distance randomly.
 
  • #4
Quickly made in a spreadsheet with a width of 1:

colors.png


The first one looks more chaotic than most other things I got, not sure why. Maybe a typo in the width.
 
  • Like
Likes jim mcnamara
  • #5
jedishrfu said:
If you're able to play with the pixels why not just do swapping over a line crossing thru the gradients.

11111222223333344445555566666777778888899999
11112122232333434445455565666767778788898999

1121122122334334434455655665667787788788...

in my case I swapped digits across the borders 1111122222 1111212222...

you could make the distance between the two pixel swap within a range of 1-5 pixels and then choose a distance randomly.
On this scale, any non-randomness shows up as an obvious artifact.
I've tried some manual swapping but - as I have learned in several different modalities recently (online article leading to book purchase), humans are super-bad at randomness.

(Had my wife pretend to toss a coin 100 times, while I did it for real and compared the difference. Fascinating.)
 
  • Like
Likes jedishrfu
  • #6
mfb said:
Quickly made in a spreadsheet with a width of 1:

View attachment 274192

The first one looks more chaotic than most other things I got, not sure why. Maybe a typo in the width.
That's very cool and exactly what I'm looking for!

Can you walk me through it again? (don't know how to do that in Excel OO-Calc) (Also, is the colour rendered formulaically, or added?)

I'm not sure what you mean by "width of 1". There's nine columns, so are each ... 1/9th?
The quilt is actually 15 tiles wide.
 
Last edited:
  • #7
Steps in LibreOffice:

Select an area as large as the quilt. In my sheet it's A3 to O22.
Sheet -> Fill Cells -> Random Number, distribution -> normal, mean -> 0, standard deviation -> personal preference. This is the width I mentioned. Making the random numbers wider creates patterns with more contrast. Now the area is filled with random numbers.

Somewhere else prepare the left column from 1 to 20 or whatever, in my sheet it's Q3 to Q22. Next to that (R3) I used the formula =MIN(MAX(ROUND($Q3/3+A3,0),1),6). This can be adjusted, of course, to make the overall gradient faster/slower or to shift it.
This formula can be filled out to an area as large as the random number field (e.g. by dragging the little box in the lower right corner), here until AF22.

Mark the area that's now filled with numbers from 1 to 6.
Format -> Conditional Formatting -> Color Scale
Color Scale (2 entries)
Define starting color (1) and final color (6).
You can also define 6 individual colors by adding more conditions if you prefer that.

Dislike this particular pattern? Just mark the first area and repeat the first step.
 
  • #8
mfb said:
Steps in LibreOffice:
:cry:
...sigh... What we do for science...
 
  • #9
You can probably do something like

=ROUNDUP(Row()/3,0)+ROUND((Rand()-0.5)*1.3,0)

And tweak that 1.3 to be whatever you want. This might not quite compile as written.
 
  • #10
mfb said:
Somewhere else prepare the left column from 1 to 20 or whatever, in my sheet it's Q3 to Q22. Next to that (R3) ...
Sorry, I don't know what "prepare" means here.
Your next instruction involves column R, so what did you do to column Q?
mfb said:
This can be adjusted, of course, to make the overall gradient faster/slower or to shift it.
Sorry, there is no gradient in my numbers. It is simply a random grid of 1 to 6.
I obviously missed a step.
 
  • #11
DaveC426913 said:
:cry:
...sigh... What we do for science...
OpenOffice should be similar but I don't use it, in Excel I would have to look up how to produce the normal distribution but apart from that it's the same.
DaveC426913 said:
Sorry, I don't know what "prepare" means here.
Just write in the numbers. You can also put in 1 and 2 in the first two cells, mark them, and then drag down. Or put =Q3+1 in Q4 and drag that formula down.
 
  • #12
Sorry, I m totally not getting the expected results.

For one, there's no gradient. (Nor can I see anything in your instructions that I might have missed would create a gradient).

For two, they're mostly 1's. (Perhaps because the random numbers are often negative - which rounds to 1?)
1607830614464.png
 
Last edited:
  • #13
DaveC426913 said:
On this scale, any non-randomness shows up as an obvious artifact.
I've tried some manual swapping but - as I have learned in several different modalities recently (online article leading to book purchase), humans are super-bad at randomness.

(Had my wife pretend to toss a coin 100 times, while I did it for real and compared the difference. Fascinating.)
I didn’t mean you select it. I meant write a program to select the random distance and then do the swap. Your pixels would be encoded in an array that you’d walk thru row by row swapping columns rather like how card decks are shuffled.
 
  • #14
DaveC426913 said:
Sorry, I m totally not getting the expected results.
You missed this step:
mfb said:
Somewhere else prepare the left column from 1 to 20 or whatever, in my sheet it's Q3 to Q22.
It's the leftmost column in my examples in post 4.

Adding Q[something]/3 creates the gradient.
 
  • #15
mfb said:
You missed this step:
It's the leftmost column in my examples in post 4.
Again, I don't know what you mean by "prepare". You said "write in the numbers". What numbers?
I wrote in all 1's.

mfb said:
Adding Q[something]/3 creates the gradient.
Not in my spreadsheet. I don't know what [something] should be.

I do not know what you're doing well enough to be able to interpolate my actions from your instructions. I have to blindly follow your directions.
 
  • #16
If I'm understanding correctly, he's putting the numbers 0-20 in a column. Then, to make the unrandomised quilt he's taking the value from that column, dividing by 3, and rounding down. That gives him rows of numbers in the range 0-6, which he's using to set the colour.

To make the randomised quilt he's adding a random number before rounding down. Then he's just catching any numbers outside the range 0-6.

So cell Q1 needs to be zero.
Q2 needs to be =Q1+1
Copy/paste Q2 into Q3, Q4,..., Q21.
Set R1 to =min(6,max(0,floor($Q1/3+A3)))
Copy/paste R1 down to R21 and as wide as your quilt.
 
  • Like
Likes jim mcnamara
  • #17
Ibix said:
So cell Q1 needs to be zero.
Q2 needs to be =Q1+1
Copy/paste Q2 into Q3, Q4,..., Q21.
Set R1 to =min(6,max(0,floor($Q1/3+A3)))
Copy/paste R1 down to R21 and as wide as your quilt.
OK, well, I've done everything you said. Still no joy.

1607895294748.png
 
  • #18
Quick test: replace all your random numbers with zero. Do you get the simple gradient?

I'd say you have the standard deviation of your Gaussian a bit high. Your random numbers are quite often 8 or 9. You just want them to be typically in the range ##\pm 1##, so that they occasionally kick a colour 4 into a colour 5. I'd suggest regenerating them with a standard deviation of 1.
 
  • #19
OK, I started from scratch and tried to actually understand what the formulae were doing rather than copy it rote. Getting useful results now.

Whatever the problem was, it was my problem.

1607897551759.png
 
  • #20
There's also a typo - that should be $Q4 in your formula.
 
  • #21
Ibix said:
There's also a typo - that should be $Q4 in your formula.
Well that would sure mess it up all right!
 
  • #22
Thanks everyone, especially @Ibix and @mfb, for your patience.
 
  • Like
Likes jedishrfu and Ibix
  • #23
$Q4/3 gives you a number in the range 0 to 6+2/3. FLOOR() rounds down, so FLOOR($Q4/3) gives you an integer in the range 0-6. That should be enough to get you the un-randomised quilt.

The random numbers are used to push colour 4 to colour 3 or colour 5 occasionally. They are just added to the "base" number before it's rounded. So FLOOR($Q4/3+A4) should usually give you the same as FLOOR($Q4/3), but sometimes push it up ir down one. So you don't want the random numbers to be large.

Finally, pushing the numbers up and down can cause them to be out of range. If we get a negative number, we want it to be treated as zero. MAX(0,FLOOR(...)) does that. Similarly a number larger than 6 we want treated as 6. MIN(6,...) does that. So the final expression is a FLOOR inside a MAX inside a MIN. Which is ugly...
 

1. What is the purpose of adding random noise to a gradient?

Adding random noise to a gradient is a technique used to introduce variation and unpredictability into a dataset. This can be useful in situations where a gradient alone may not accurately reflect the complexity of the data, or when trying to simulate real-world scenarios.

2. How is random noise added to a gradient?

Random noise can be added to a gradient by using algorithms or mathematical functions that generate random values within a specified range. These values are then added to the original gradient, creating variation in the data.

3. What are the potential benefits of adding random noise to a gradient?

Adding random noise to a gradient can help to improve the robustness and generalizability of a dataset. It can also help to prevent overfitting and provide a more accurate representation of the data.

4. Are there any drawbacks to adding random noise to a gradient?

One potential drawback of adding random noise to a gradient is that it can make the data more difficult to interpret or analyze. It can also introduce bias or distortions if not carefully implemented.

5. In what fields or applications is adding random noise to a gradient commonly used?

Adding random noise to a gradient is commonly used in fields such as machine learning, data science, and statistics. It can also be applied in various applications, including image and signal processing, financial modeling, and weather forecasting.

Similar threads

  • Astronomy and Astrophysics
2
Replies
39
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • STEM Academic Advising
Replies
2
Views
1K
  • Biology and Medical
Replies
2
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
Back
Top