[R] average waveform over many cycles

Jim Lemon jim at bitwrit.com.au
Thu Oct 11 11:44:47 CEST 2007


Bill Simpson wrote:
> Thanks Jim for the help.
> 
> I forgot to say that one of my main snags is figuring how how to chop up
> the one long (44100 samples) vector into pieces (400 of them).
> 
> This is my C-inspired way of thinking but there must be a better way in
> R.
> 
> cyclelength=44100/400
> for i= 1 to 400
>  for j= 1 to cyclelength
>  y[i,j]=x[i*j]
> next
> next
> 
> Once that's done I can average over rows
> 
I can understand your problem as C was my main programming language when 
I discovered S-PLUS and then R. If your initial vector is named "y":

x<-matrix(y,nrows=400,byrow=TRUE)

As I get 110.25 elements per row, matrix is going to whinge about this, 
so perhaps you should think about trimming your initial vector somehow.

Jim



More information about the R-help mailing list