summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSøren Sandmann Pedersen <ssp@redhat.com>2010-09-25 01:47:20 -0400
committerSøren Sandmann Pedersen <ssp@redhat.com>2010-09-25 01:47:20 -0400
commit03869c01eca1506c5f849c77f6e9ac74c8e26e3e (patch)
treefe6dad6105e589a02e6bd53d7530241d067b5a5b
parent9b14b53493320ce38e00b5b687e3ad30b389cac1 (diff)
Make the noise blue
-rw-r--r--bluenoise.c44
-rw-r--r--fft.c21
-rw-r--r--fft.h5
-rw-r--r--gtk-utils.c4
4 files changed, 63 insertions, 11 deletions
diff --git a/bluenoise.c b/bluenoise.c
index ed57192..54dd4a1 100644
--- a/bluenoise.c
+++ b/bluenoise.c
@@ -4,7 +4,7 @@
#include "fft.h"
-#define N_SAMPLES 512
+#define N_SAMPLES 256
#define K ((2 * M_PI) / N_SAMPLES)
@@ -25,7 +25,8 @@ main ()
{
complex_t *noise = malloc (sizeof (complex_t) * N_SAMPLES * N_SAMPLES);
int i, j;
-
+ double max, min;
+
for (i = 0; i < N_SAMPLES; ++i)
{
for (j = 0; j < N_SAMPLES; ++j)
@@ -36,10 +37,45 @@ main ()
}
fft_2d (noise, N_SAMPLES);
-
+
shift_2d (noise, N_SAMPLES);
- show_image (noise, N_SAMPLES);
+ max = noise[0].re;
+ min = noise[0].re;
+
+ for (i = 0; i < N_SAMPLES; ++i)
+ {
+ for (j = 0; j < N_SAMPLES; ++j)
+ {
+ double d = sqrt ((i - N_SAMPLES/2) * (i - N_SAMPLES/2) +
+ (j - N_SAMPLES/2) * (j - N_SAMPLES/2));
+ double f = (d / (sqrt (2) * N_SAMPLES)); // * (d / N_SAMPLES);
+
+
+ if (d != 0.0)
+ {
+#if 0
+ noise[i * N_SAMPLES + j].re *= 0.5 * (cos (M_PI * (1 - 2 * f)) + 1.0);
+ noise[i * N_SAMPLES + j].im *= 0.5 * (cos (M_PI * (1 - 2 * f)) + 1.0);
+#endif
+ noise[i * N_SAMPLES + j].re *= f;
+ noise[i * N_SAMPLES + j].im *= f;
+ }
+ }
+ }
+
+ printf ("%f %f\n", noise[i * (N_SAMPLES/2) + N_SAMPLES/2].re, noise[i * (N_SAMPLES/2) + N_SAMPLES/2].im);
+
+#if 0
+ noise[i * (N_SAMPLES / 2) + N_SAMPLES / 2].re = 1.5;
+ noise[i * (N_SAMPLES / 2) + N_SAMPLES / 2].im = 0;
+#endif
+
+ shift_2d (noise, N_SAMPLES);
+
+ ifft_2d (noise, N_SAMPLES);
+
+ show_image ("blue noise", noise, N_SAMPLES);
return 0;
}
diff --git a/fft.c b/fft.c
index 9a9682f..46215cb 100644
--- a/fft.c
+++ b/fft.c
@@ -90,9 +90,10 @@ shift (complex_t *buffer, int n)
}
}
+typedef void (* fft_1d_t) (complex_t *buffer, int n);
-void
-fft_2d (complex_t *buffer, int n)
+static void
+do_fft_2d (complex_t *buffer, int n, fft_1d_t func1d)
{
int i;
@@ -101,7 +102,7 @@ fft_2d (complex_t *buffer, int n)
{
complex_t *b = buffer + i * n;
- fft (b, n);
+ func1d (b, n);
}
/* Then the columns */
@@ -113,7 +114,7 @@ fft_2d (complex_t *buffer, int n)
for (j = 0; j < n; ++j)
tmp[j] = buffer[j * n + i];
- fft (tmp, n);
+ func1d (tmp, n);
for (j = 0; j < n; ++j)
buffer[j * n + i] = tmp[j];
@@ -123,6 +124,18 @@ fft_2d (complex_t *buffer, int n)
}
void
+fft_2d (complex_t *buffer, int n)
+{
+ do_fft_2d (buffer, n, fft);
+}
+
+void
+ifft_2d (complex_t *buffer, int n)
+{
+ do_fft_2d (buffer, n, ifft);
+}
+
+void
shift_2d (complex_t *buffer, int n)
{
int i, j;
diff --git a/fft.h b/fft.h
index d82a992..31d7667 100644
--- a/fft.h
+++ b/fft.h
@@ -74,6 +74,9 @@ ifft (complex_t *buffer, int n);
void
fft_2d (complex_t *buffer, int n);
+void
+ifft_2d (complex_t *buffer, int n);
+
/* Shifts the zero component to the center of the array */
void
shift (complex_t *buffer, int n);
@@ -82,4 +85,4 @@ void
shift_2d (complex_t *buffer, int n);
void
-show_image (complex_t *image, int n);
+show_image (const char *name, complex_t *image, int n);
diff --git a/gtk-utils.c b/gtk-utils.c
index 39f697a..0e2c848 100644
--- a/gtk-utils.c
+++ b/gtk-utils.c
@@ -51,13 +51,13 @@ on_expose (GtkWidget *widget, GdkEventExpose *expose, gpointer data)
}
void
-show_image (complex_t *image, int n)
+show_image (const char *name, complex_t *image, int n)
{
GtkWidget *window;
GdkPixbuf *pixbuf;
int argc;
char **argv;
- char *arg0 = g_strdup ("pixman-test-program");
+ char *arg0 = g_strdup (name);
argc = 1;
argv = (char **)&arg0;