s-news
[Top] [All Lists]

Summary: square wave function

To: s-news@lists.biostat.wustl.edu
Subject: Summary: square wave function
From: FolkesM@pac.dfo-mpo.gc.ca
Date: Tue, 27 Nov 2001 19:22:48 -0500
The following is a compilation of possible approaches to building a square
wave data set.
Thanks to all who helped me.
Note the final message, which is from Terry Therneau, it does the obvious
work around using modulus - not trigonometric estimation.  I was too
impatient to work that one out...but kept butting my head against the
trigonometric wall!

As mentioned in an earlier message, (hopefully somebody will hear this)
"IT SURE WOULD BE NICE TO FIND A LONG CHAPTER OR BOOK THAT GIVES A REALLY
GOOD FOUNDATION AND SET OF EXAMPLES ON VECTOR STATEMENTS AND FUNCTIONS IN
SPLUS!!"

Thanks!
Michael Folkes


From:   Nick.Ellis@csiro.au
Sent:   Tue November 27, 2001 3:48 PM
To:     FolkesM@pac.dfo-mpo.gc.ca
Subject:        RE: [S] square wave function

outer(rads,0:terms,function(x,k) sin((2*k+1)*pi*x)/(2*k+1)) %*%
rep(1,terms+1)

Nick Ellis
CSIRO Marine Research   mailto:Nick.Ellis@csiro.au
PO Box 120                      ph    +61 (07) 3826 7260
Cleveland QLD 4163      fax   +61 (07) 3826 7222
Australia                       http://www.marine.csiro.au
 

From:   Stephen Kaluzny [spk@insightful.com]
Sent:   Tue November 27, 2001 2:28 PM
To:     FolkesM@pac.dfo-mpo.gc.ca
Subject:        Re: [S] square wave function

Michael,


This function returns the same thing as yours without explicit for loops:

"sqrWave"<-
function(rads, terms)
{
        terms <- seq(0, terms)
        rowSums(outer(rads, terms, function(x, y)
        sin((2 * y + 1) * pi * x)/(2 * y + 1)))
}

-Stephen Kaluzny

From:   Gerald.Jean@spgdag.ca
Sent:   Tue November 27, 2001 12:48 PM
To:     FolkesM@pac.dfo-mpo.gc.ca
Subject:        Réf. : [S] square wave function


Hello Michael,

the following should do the trick.

"sqr.wave" <- function(rads, terms)
{k <- 0:terms
 apply((-1)^k * (sin(as.matrix((2 * k + 1) * pi) %*%
                     t(as.matrix(rads)))) / (2 * k + 1), 2, sum)
}

Have fun,

Gérald Jean
Analyste-conseil (statistiques), Actuariat
télephone            : (418) 835-4900 poste (7639)
télecopieur          : (418) 835-6657
courrier électronique: gerald.jean@spgdag.ca

"In God we trust all others must bring data"  W. Edwards Deming

From:   Bill Dunlap [bill@insightful.com]
Sent:   Tue November 27, 2001 12:02 PM
To:     FolkesM@pac.dfo-mpo.gc.ca
Subject:        Re: [S] square wave function

If you typically have a long vector of rads and a relatively small
number of terms you will do a lot better by getting rid of the outer
loop.  Try

sqr.wave.2 <- function(rads, terms)
{
        sqr.data <- rep(0, length(rads))
        for(k in 0:terms) {
                sqr.data <- sqr.data + (sin((2 * k + 1) * pi * rads))/(2 * k
+ 
                        1)
        }
        return(sqr.data)
}

Using Splus 6 I get 

> sys.time(sqr.wave.2(seq(0,10*pi,len=1000),17)->z2)
[1] 0.06 0.08
> sys.time(sqr.wave(seq(0,10*pi,len=1000),17)->z)
[1] 34.42 34.64
> identical(z2,z)
[1] T

----------------------------------------------------------------------------
Bill Dunlap                                      22461 Mt Vernon-Big Lake Rd
Insightful Corporation                           Mount Vernon, WA 98274
bill@insightful.com                              360-428-8146
 "Formerly known as MathSoft, Insightful Corporation provides analytical
 solutions leveraging S-PLUS, StatServer and consulting services."

 "All statements in this message represent the opinions of the author and do
 not necessarily reflect Insightful Corporation policy or position."

From:   Terry Therneau [therneau@mayo.edu]
Sent:   Tue November 27, 2001 12:01 PM
To:     FolkesM@pac.dfo-mpo.gc.ca
Subject:        Re: [S] square wave function

 How about:
 
 sqr.wave2 <- function(rads) {
        1- 2*(floor(rads)%%2)
        }


(You said you wanted to generate a square wave -- so why compute it the hard
way).

        Terry T.
                
        
 

<Prev in Thread] Current Thread [Next in Thread>