Created
September 2, 2021 09:23
-
-
Save teunbrand/a93649f9c6e02fd7f4862c7cd6c341a1 to your computer and use it in GitHub Desktop.
Code for making fourier transform mistakes and producing plots for weird wavy patterns.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
accidental_art <- function(x, r = 1.9) { | |
x <- cor(x) | |
n <- dim(x)[1] | |
mask <- matrix(0, ncol(x), nrow(x)) | |
rad <- n/2 | |
xcen <- 0 | |
ycen <- 0 | |
r <- n / r | |
mask <- ((row(mask) - xcen)^2 + (col(mask) - ycen)^2) <= rad^2 | |
mask <- mask + (((row(mask) - r)^2 + (col(mask) - r)^2) <= rad^2) | |
mask <- mask + (((row(mask) - 0)^2 + (col(mask) - r)^2) <= rad^2) | |
mask <- mask + (((row(mask) - r)^2 + (col(mask) - 0)^2) <= rad^2) | |
y <- fft(x) | |
yim <- asinh(Re(y)) | |
yim <- sinh(yim * mask) | |
yim <- complex(real = yim, imaginary = Im(y)) | |
yim <- matrix(yim, ncol = ncol(x)) | |
z <- fft(yim, inverse = T)/length(yim) | |
df <- reshape2::melt(Re(z)) | |
ggplot(df, aes(x = Var1, y = Var2, fill = value^2)) + | |
geom_raster() + | |
scale_fill_gradientn(colours = c('white', '#f5a623', '#d0021b', 'black'), | |
limits = c(0, NA), oob = scales::oob_squish, trans = "sqrt") + | |
scale_y_continuous(trans = "reverse") | |
} | |
accidental_art(t(iris[, 1:4])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment