diff options
author | Søren Sandmann Pedersen <ssp@redhat.com> | 2010-09-25 01:47:20 -0400 |
---|---|---|
committer | Søren Sandmann Pedersen <ssp@redhat.com> | 2010-09-25 01:47:20 -0400 |
commit | 03869c01eca1506c5f849c77f6e9ac74c8e26e3e (patch) | |
tree | fe6dad6105e589a02e6bd53d7530241d067b5a5b | |
parent | 9b14b53493320ce38e00b5b687e3ad30b389cac1 (diff) |
Make the noise blue
-rw-r--r-- | bluenoise.c | 44 | ||||
-rw-r--r-- | fft.c | 21 | ||||
-rw-r--r-- | fft.h | 5 | ||||
-rw-r--r-- | gtk-utils.c | 4 |
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; } @@ -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; @@ -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; |