knitr::opts_chunk$set(fig.width=5, fig.height=5)
We are going to run through an example of using hangler to analyse a simple closed curve shape.
First we need to generate our data set. This is equivalent to a human digitising a curve. We come out with only a set of xy coordinates sampled along the shape.
library(hangler)
theta = seq(0,2*pi, 0.01) radius = 6 wiggle = 7 x = (sin(theta*wiggle) + radius) * cos(theta) y = (sin(theta*wiggle) + radius) * sin(theta) rm(theta) rm(wiggle) rm(radius)
We can now plot it and see our apeture.
plot(x,y,col="black") lines(c(x,x[1]),c(y,y[1]),col="blue")
We now get the tangets.
tangents = computeTangents(x,y)
Now we can plot the tangents and notice that the tangent curve we get is nice and continuous.
plot(tangents) lines(tangents,col="blue")
We can turn them back into a shape and see that they give the original.
with(tangentsToXY(tangents), { plot(x, y,col=rainbow(length(tangents))); lines(x,y) })
At this point we need to resample the tangent curve. This is because FFT requires evenly spaced points, but we cannot be certain that our digitisation / landmark capture is even along the curve length. We will resample the default number of points.
newTangents = resampleTangents(x,y, tangents)
We can now plot our new tangents to make sure they are the same as the old ones.
plot(newTangents) lines(newTangents,col="blue")
That done, we now need to remove the normal shift associated with a unit circle. Hangle characterises shape by deviation from unit circle rather than directly.
newTangents = flattenTangents(newTangents)
Now we can plot the results to make sure they look sensible (like a wave).
plot(newTangents) lines(newTangents,col="blue")
From this point one we can get our coefficients by using R's inbuilt FFT.
coeffs = fft(newTangents)/length(newTangents)
We start by taking only the first 30 and now trying to reconstruct the original curve.
coeffs = coeffs[1:30]
We now want to go back from fft coefficients to fft parameters to go backwards.
backCoeffs = fftoToCoeffs(coeffs)
Now we can reconstruct the original curve.
reconstructedTangents = sapply(seq(0,2*pi,length.out=length(newTangents)), function(s) getTangentAtS(s,backCoeffs))
We can now look at the reconstructed tangents and compare them to the originals
plot(reconstructedTangents) lines(reconstructedTangents,col="blue")
And now reconstruct the curve
with(tangentsToXY(reconstructedTangents), { plot(x, y,col=rainbow(length(newTangents))); lines(x,y) })
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.