[R] create waveform sawtooth

Michael Tiemann mdtiemann at gmail.com
Mon Nov 13 23:28:41 CET 2017


Here is perhaps a better sawtooth wave generator:

sawtooth <- function(freq, duration = samp.rate, from = 0, samp.rate =
44100, bit = 1,

                     stereo = FALSE, xunit = c("samples", "time"), reverse
= FALSE, ...){

    xunit <- match.arg(xunit)

    durFrom <- preWaveform(freq = freq, duration = duration, from = from,

xunit = xunit, samp.rate = samp.rate)

    channel <-
rep(seq(1,-1,length=samp.rate/freq),length=durFrom["duration"])

    if(!is.logical(reverse) || length(reverse) != 1)

stop("'reverse' must be a logical value of length 1")

    if(reverse) channel <- rev(channel)

    postWaveform(channel = channel, samp.rate = samp.rate,

bit = bit, stereo = stereo, ...)

}


On Sun, Nov 12, 2017 at 6:57 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us>
wrote:

> Ccing the maintainer if the tuneR package.
>
> Looks to me like sawtooth (and square) don't behave as expected when using
> xunit="samples". Workaround is to use xunit="time" instead:
>
> sawtooth(110,duration=1/100,samp.rate=sample_rate,xunit="time")
>
> I looked at the code but found it to be opaque.
> --
> Sent from my phone. Please excuse my brevity.
>
> On November 12, 2017 6:15:45 AM PST, Michael Tiemann <mdtiemann at gmail.com>
> wrote:
> >My tuneR sawtooth wave function generator is broken.
> >
> >When I use the sine function, I get exactly what I expect: a sine wave
> >whose frequency is defined by the freq parameter.  In particular,
> >higher frequencies have shorter wavelengths (more cycles per second
> >means shorter waves).  When I create a sawtooth wave, the opposite
> >seems to occur: higher frequencies result in longer waves.  But that’s
> >not all: as frequencies increase, it appears that wavelengths increase
> >to infinite length, then get shorter again as the wave reverses, then
> >it gets longer and flips again.
> >
> >Here’s a small file that demonstrates the bad sawtooth waves:
> >
> >library(tuneR)
> >
> >sample_rate <- 12000
> >reverse <- FALSE
> >mycolors=c("red","orange","yellow","green","cyan","blue",
> "violet","magenta")
> >plot(sawtooth(110,duration=round(sample_rate/100),samp.
> rate=sample_rate,xunit="samples")@left,type="l")
> >freqs <- c(111,112,113,114,115,116,117,118)
> >for (i in 1:length(freqs)) {
> >temp <-
> >sine(freqs[i],duration=round(sample_rate/100),samp.rate=
> sample_rate,xunit="samples")
> >    lines(temp at left,type="l",lty=2,col=mycolors[i])
> >}
> >
> >
> >
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide
> >http://www.R-project.org/posting-guide.html
> >and provide commented, minimal, self-contained, reproducible code.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list