Skip to content

Instantly share code, notes, and snippets.

#select peaks in spectrum to filter out
#click on each peak, then end the locator
peaks <- data.frame(locator())
#function to create a filter for circle shaped peaks in the spectrum
#dimensions: dimensions of the spectrum matrix
#x: x position of the centre of the circle to be filtered out
#y: y position of ...
#r: radius of circle to be filtered out of the spectrum
#gaussian: flag that indicates whether the circle should have a Gaussian density or not
#checker board pattern used to centre the Fourier spectrum:
checkerboard <- (-1)^(row(mat_clown) + col(mat_clown))
#modify matrix with checkerboard pattern:
mat_checker <- mat_clown*checkerboard
#Discrete fast Fourier transform:
mat_fft <- fft(mat_checker)
#magnitude in frequency domain
#required packages:
require(jpeg)
#Read a jpeg from a website and store as matrix "mat_clown"
con <- file("http://4.bp.blogspot.com/-uObQLjPG8Xg/VceaZNyI-NI/AAAAAAAAx0o/ZlcTTB1pomU/s1600/fourier_efteling.jpg", "rb")
#make sure 'n' is big enough to hold your entire file:
jpg_clown <- readBin(con, "raw", n = 3e4)
close(con)
rm(con)
mat_clown <- readJPEG(jpg_clown)
@pepijn-devries
pepijn-devries / contoursToSP04.r
Created August 8, 2015 14:22
ContourLines to SpatialPolygons
#create bounding box to which the final polygons needs to be clipped:
CP <- as(extent(0 + 2/ncol(volcano2), 1 - 2/ncol(volcano2), 0 + 2/nrow(volcano2), 1 - 2/nrow(volcano2)), "SpatialPolygons")
#clip polygons and plot:
final_volcano <- gIntersection(poly_volcano, CP, byid=TRUE)
png("volcano04.png", 200, 200)
par(oma = c(0,0,0,0), mar = c(0,0,0,0))
plot(final_volcano, col = terrain.colors(length(poly_volcano)), border = NA)
dev.off()
for (i in 1:length(poly_volcano))
{
temp <- poly_volcano@polygons[[i]]@Polygons
for (j in 1:length(temp))
{
if (length(temp) > 1)
{
for (k in 1:length(temp))
{
if (j != k)
@pepijn-devries
pepijn-devries / contoursToSP02.r
Last active August 29, 2015 14:26
ContourLines to SpatialPolygons
#create a slightly larger matrix and fill with relatively small values (60):
volcano2 <- matrix(60, nrow(volcano) + 2, ncol(volcano) + 2)
#copy the original matrix into the center of the new matrix:
volcano2[-nrow(volcano2),][,-ncol(volcano2)][-1,][,-1] <- volcano
#repeat the previous conversion steps and plot:
poly_volcano <- raster(volcano2)
poly_volcano <- rasterToContour(poly_volcano, levels = breaks)
poly_volcano <- SpatialLines2PolySet(poly_volcano)
@pepijn-devries
pepijn-devries / contoursToSP01.r
Last active August 29, 2015 14:26
ContourLines to SpatialPolygons
#required packages:
require(raster)
require(maptools)
require(Grid2Polygons)
#define breaks for the contour lines:
breaks <- seq(80, 200, length.out = 15)
#first convert the volcano matrix to a raster
poly_volcano <- raster(volcano)