[R] Newbie error or bug?

Paul Vickers paul.vickers at unn.ac.uk
Mon Mar 13 17:34:08 CET 2006


Hi

I used R for the first time yesterday. I wanted to plot the aliasing
effect of sampling a 5.5KHz sinusoid at only 8KHz (below the Nyquist
limit). So I wrote a small R script that a) plots 1msec worth of a
5.5KHz sin wave b) plots 1msec of the resulting 2.5KHz alias and c)
plots the 8 sampling points on the 5.5KHz source wave. I think I have
found a bug. The script is as follows:

#truesamplingfreq <- 1000*5.5
freqin1msec = 5.5
#aliassamplingfreq <- 1000*2.5
aliasfreqin1msec = 2.5

drawingpoints = 10000
time = (0:drawingpoints)/drawingpoints

signal = sin(freqin1msec*2*pi*(time))
alias = -sin(aliasfreqin1msec*2*pi*(time))


undersamplinginterval = max(time)/8
seq (0, max(time), by=undersamplinginterval) -> undersamplingtimes
undersampled = sin(freqin1msec*2*pi*undersamplingtimes)

plot(time,signal,type="l", col="blue", xaxs="r", yaxs="r", xlab="Time
(msec)", ylab="Signal", main="Aliasing", sub="Sampling 5KHz source
(blue) at 8KHz (dots) gives 2.5KHz alias(red)")
par(new=TRUE)
plot (time, alias, xaxs="r",  yaxs="r", type="l", lty=2, col="red",
axes=FALSE, xlab="", ylab="")
par(new=TRUE)
plot(undersamplingtimes, undersampled, pch=16,  xaxs="r", yaxs="r",
axes=FALSE, xlab="", ylab="", abline(h=0))

The output is given as attachment alias.jpg in which the line through
y=0 is offset and all the positive sampling points (black dots) are also
offset (interestingly, all the negative points seem to be correct). All
the black dots should line up with 8 intersections of the red and blue
lines. I don't think the script is wrong because if I double everything
up and plot an 11KHz source, its 5KHz alias and 16 sampling points (for
16KHz sampling) everything works as expected (see attachment alias2.jpg)
- ie, the line through y=0 is in the right place as are the 16 sampling
points.

Here's my sessionInfo:
R version 2.2.1, 2005-12-20, powerpc-apple-darwin7.9.0

attached base packages:
[1] "methods"   "stats"     "graphics"  "grDevices" "utils"
[6] "datasets"  "base

I'm running OS X 10.4.5

Can anyone enlighten me?

Cheers

Paul


More information about the R-help mailing list