I have an example R script for spectrum calculation. I divide the signal into several blocks and do the calculation for each block.
spect=function(x,samplingfrequency=1,blocksize=2^12)
{
T=length(x)
blocks=trunc(T/blocksize)
localfreq=c() ; localspec=c()
for(i in 1:blocks){localfreq[[i]]=c() ; localspec[[i]]=c() }
for(i in 1:blocks)
{
time=c( (1+(i-1)*blocksize) : (i*blocksize) )
localspectrum=spectrum(x[time],plot=FALSE)
localfreq[[i]]=localspectrum$freq
localspec[[i]]=localspectrum$spec
}
averagespec=rep(0,(blocksize/2))
for(freq in 1:(blocksize/2))
{
for(block in 1:blocks)
{
averagespec[freq]=(averagespec[freq]+localspec[[block]][freq])
}
averagespec[freq]=averagespec[freq]/blocks
}
par(mar=c(5.1,5.1,2.5,1.5))
plot(c(1:(blocksize/2))/(blocksize/2)*samplingfrequency/2,averagespec,log="xy",t="l",xlab="frequency [Hz]",ylab="average spectrum [a.u.]",cex.lab=1.8,cex.axis=1.8)
abline(v=(samplingfrequency/2),col=2)
abline(v=(1/blocksize*samplingfrequency),col=4)
}
here x is your time series input. I don't use the spectrum function from R directly since the result is too noisy. I was wonder if I could somehow avoid those forloops in my script?
Related
Can you confirm if the next break cancels the inner for loop?
for (out in 1:n_old){
id_velho <- old_table_df$id[out]
for (in in 1:n)
{
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}else
if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:
break breaks out of a for, while or
repeat loop; control is transferred to
the first statement outside the
inner-most loop.
So yes, break only breaks the current loop. You can also see it in action with e.g.:
for (i in 1:10)
{
for (j in 1:10)
{
for (k in 1:10)
{
cat(i," ",j," ",k,"\n")
if (k ==5) break
}
}
}
your break statement should break out of the for (in in 1:n).
Personally I am always wary with break statements and double check it by printing to the console to double check that I am in fact breaking out of the right loop. So before you test add the following statement, which will let you know if you break before it reaches the end. However, I have no idea how you are handling the variable n so I don't know if it would be helpful to you. Make a n some test value where you know before hand if it is supposed to break out or not before reaching n.
for (in in 1:n)
{
if (in == n) #add this statement
{
"sorry but the loop did not break"
}
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}
else if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
I think I have a simple question, but I can not figure out how to do it.
I just wanna plot all hist of an data input and check if all var are numerical.
plotting_histogram_data<-function(x) {
for(i in seq_len((ncol(x)))) {
if(is.numeric(x[,i])) {
hist(x[,i])
}
else{
print("Variable is not of the Typ numeric!")
}
}
}
I know that the problem is the index.
Thank and have nice day.
When i execute a crowd simulation, all dots start going to the suggested metting point (reffer to the code). However the dots (people leaving the room) start to step over the other dots, something that shouldnt be happening according to my excersice.
Here is the code:
dimensionX=10
dimensionY=10
numberPeople=20
velocity=0.001
varianzavelocidad=runif(1, min=0, max=5)
x<-dimensionX*runif(numberPeople)
y<-dimensionY*runif(numberPeople)
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
for(i in 1:10000) {
for(j in 1:numberPeople) {
ang <- atan((y[j]-5)/(10-x[j]))
x[j]<-x[j]+velocity*varianzavelocidad*cos(ang)
y[j]<-y[j]-velocity*varianzavelocidad*sin(ang)
}
x[x>10]=10
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
}
My thoughts were that i should be working with an If()/Else() condition inside the J array, however am not sure how to read each J object and set a condition that if the dot/point tries to step over another dot/point it would compare a strength value between the dots trying to step over.
A clue of how i would like to make it work:
dimensionX=10
dimensionY=10
numberPeople=20
velocity=0.001
strength=runit(1) *******
varianzavelocidad=runif(1, min=0, max=5)
x<-dimensionX*runif(numberPeople)
y<-dimensionY*runif(numberPeople)
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
for(i in 1:10000) {
for(j in 1:numberPeople) {
ang <- atan((y[j]-5)/(10-x[j]))
x[j]<-x[j]+velocity*varianzavelocidad*cos(ang)
y[j]<-y[j]-velocity*varianzavelocidad*sin(ang)
}
if(X[j1] = X[J2] && Y[J1] = Y[J2]) {
//MAKE THE DOTS CHOOSE BETWEEN THE STRONGEST //using strength value
} else() { they keep on going }
x[x>10]=10
plot(x,y,xlim=c(0,dimensionX),ylim=c(0,dimensionY))
}
I have tried downloading foreach function but honestly havent been able to find a way to use it correctly. Any thoughts?
I am using RStudio, I have a programm and I want to see the value of each parameter in each iteration. I am seeing only value of parameters on the last iteration.
Here is my R code:
k<-read.csv("D:\\Testc.csv")[,1:5]
window<-64
stelle<-""
spalte<-1
co<-0
r<-1
l<-(2/sqrt(window))
while(spalte < 3)
{
datalist<-matrix(k[,spalte])
while(r+window<=length(datalist[,1]))
{
m<-acf(as.numeric(datalist[r:(r+window),1]),lag.max=32,plot=FALSE)$acf[-1]
for(i in (1:32))
{
if(m[i]>l)
{
co<-co+1
}
}
if(co>5)
{
row<-as.character(r)
spalte<-as.character(spalte)
pos<-rbind(row,spalte)
stelle<-c(stelle,"-",pos)
}
r<-r+30
co<-0
}
spalte<-spalte+1
}
stelle
I want to see the value of co ,spalte, stelle on each iteration. On the left side of RStudio on Workspace tab I see values only for last iteration.
place the following code into your loop at the end:
print(paste("co=", co))
print(paste("spalte=", spalte))
print(paste("stelle=", stelle))
Can you confirm if the next break cancels the inner for loop?
for (out in 1:n_old){
id_velho <- old_table_df$id[out]
for (in in 1:n)
{
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}else
if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}
}
Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:
break breaks out of a for, while or
repeat loop; control is transferred to
the first statement outside the
inner-most loop.
So yes, break only breaks the current loop. You can also see it in action with e.g.:
for (i in 1:10)
{
for (j in 1:10)
{
for (k in 1:10)
{
cat(i," ",j," ",k,"\n")
if (k ==5) break
}
}
}
your break statement should break out of the for (in in 1:n).
Personally I am always wary with break statements and double check it by printing to the console to double check that I am in fact breaking out of the right loop. So before you test add the following statement, which will let you know if you break before it reaches the end. However, I have no idea how you are handling the variable n so I don't know if it would be helpful to you. Make a n some test value where you know before hand if it is supposed to break out or not before reaching n.
for (in in 1:n)
{
if (in == n) #add this statement
{
"sorry but the loop did not break"
}
id_novo <- new_table_df$ID[in]
if(id_velho==id_novo)
{
break
}
else if(in == n)
{
sold_df <- rbind(sold_df,old_table_df[out,])
}
}