Reputation: 3057
I am trying to understand a function in R. could you please declare some part of it for me:
the function is:
subsignals <- lapply(c(peakind$freqindex, midindex+1), function(x){
upperind <- x
fsub <- f
notnullind <- ((fsub$freqindex >= lowerind
& fsub$freqindex < upperind)
|
(fsub$freqindex > (lindex - upperind + 2)
& fsub$freqindex <= (lindex - lowerind + 2)))
fsub[!notnullind,"coef"] <- 0
lowerind <<- upperind
Re(fft(fsub$coef, inverse=TRUE)/length(fsub$coef))
})
Could some one explain me:
1-What could be the content of notnullind
and generally, what does this part of code do:
notnullind <- ((fsub$freqindex >= lowerind
& fsub$freqindex < upperind)
|
(fsub$freqindex > (lindex - upperind + 2)
& fsub$freqindex <= (lindex - lowerind + 2)))
2-What does fsub[!notnullind,"coef"] <- 0
mean?
3-What does<<-
in lowerind <<- upperind
mean?
further information:
peakind
looks like this:
coef freqindex
9 2.714391+3.327237i 9
17 1.273340+4.023808i 17
25 -0.445424+5.674848i 25
33 -1.378107+3.182281i 33
41 -2.798383+2.340895i 41
49 -4.479888+1.095193i 49
and fsub
:
coef freqindex
1 19.2352397+0.0000000i 1
2 -0.4799684+0.1651822i 2
3 1.5235726+0.0790459i 3
4 -0.1165587+0.1217513i 4
5 2.2376900+1.6763410i 5
6 1.1256711+0.4624819i 6
.....
102 -0.1165587-0.1217513i 102
103 1.5235726-0.0790459i 103
104 -0.4799684-0.1651822i 104
Upvotes: 0
Views: 89
Reputation: 52687
It seems that the code is iterating through fsub
in chunks defined by the difference between adjacent entries in peakind
. Presumably peakind
contains interesting points in fsub
. You can see this because most of the fsub
comparison are between x
(which comes from peakind
), and lowerind
, which is set to be the prior loops x
/upperind
value.
notnullind
will be a logical vector (TRUE, FALSE) that is TRUE for the rows in fsub
that are between this iterations peakind$freqindex
and the prior ones as well as something else based on lindex
that I can't tell you b/c that variable is undefined in your code.fsub$coef
that don't meet the condition described above to zerolowerind<<-upperind
is a global assignment outside of the function being run through lapply
. This allows the function run by lapply
to keep track of the last upperind
from a previous call to that same function in lapply
loop. The assignment must be global as otherwise the value would be lost after every iteration in lapply
.Basically, the function is doing an fft
for the data between in fsub
between adjacent pairs of index values defined in peakind
.
Note that 3. suggests your function isn't structured in the best possible manner. You should generally avoid global assignments unless you really can't. In this case, I would loop through the rows of cbind(head(peakind$freqindex, -1L), tail(peakind$freqindex, -1L))
which contains the range of indices you care about for each iteration.
Upvotes: 2