[R] Newbie error or bug?

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


That also works and is even more concise.Many thanks,

Paul

Uwe Ligges wrote:
> Your error:
> If you use plot(), the coordinate system of user coordinates is set up 
> each time, but you do want to plot in the coordinate system of your 
> first plot, hence use:
> 
> 
> 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)")
> lines(time, alias, lty=2, col="red")
> points(undersamplingtimes, undersampled, pch=16)
> abline(h=0)
> 
> 
> 
> Uwe Ligges
> 
> 
> 
> 
> Paul Vickers wrote:
>> 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
>>
>>
>> ------------------------------------------------------------------------
>>
>> ______________________________________________
>> R-help at stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide! 
>> http://www.R-project.org/posting-guide.html
> 
> 

-- 
____________________________________
Dr P. Vickers BSc PhD CEng MIEE ILTM
Reader in Human-Computer Interaction &
Visiting Research Fellow of Loughborough University

School of Computing, Engineering, & Information Sciences
Northumbria University
Pandon Building, Camden Street
Newcastle-upon-Tyne
NE2 1XE

Tel +44 (0)191 243-7614
Fax +44 (0)870 133-9127
e-mail: paul.vickers at northumbria.ac.uk
web: www.paulvickers.com/northumbria

Any views expressed in this message are those of the individual sender,
except where the sender specifically states them to be the views of
Northumbria University.

====

This e-mail is intended solely for the addressee. It may contain private
and confidential information. If you are not the intended addressee,
please take no action based on it nor show a copy to anyone. Please
reply to this e-mail to highlight the error. You should also be aware
that all electronic mail from, to, or within Northumbria University may
be the subject of a request under the Freedom of Information Act 2000
and related legislation, and therefore may be required to be disclosed
to third parties.

This e-mail and attachments have been scanned for viruses prior to
leaving Northumbria University. Northumbria University will not be
liable for any losses as a result of any viruses being passed on.




More information about the R-help mailing list