forked from ssumithra/PowerLawCriticalityPaper
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrspec_ews.R
executable file
·65 lines (49 loc) · 1.5 KB
/
rspec_ews.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#function to calculate r-spectrum and theta-spectrum.
#Originally written by Vincent Deblauwe, novembre 2007 in Matlab.
#Translated to R by Vishwesha Guttal, Nov 2013.
#test=replicate(100,rnorm(100));
rspec_ews = function(rawmatrix){
test= data.matrix(rawmatrix)
nr=dim(test)[1]
nc=dim(test)[2]
n0x = floor(nc/2)+1
n0y = floor(nr/2)+1
#Create distance and angle matrices
f1 = t(replicate(nr,seq(1,nc))) - n0x
f2 = replicate(nc,seq(1,nr)) - n0y
DIST = sqrt(f1^2 + f2^2)
ANGLE=atan2(-f2,f1)*180/pi
#Calculate DFT
mi=1
ma=min(c(n0x,n0y))
DISTMASK = DIST>=mi & DIST<=ma
tmp=fft(test)
tmpshift=myfftshift(tmp)
tmpshift[n0x,n0y]=0
aspectr2D=abs(tmpshift)^2/(n0x*n0y)^4
sig2=sum(aspectr2D[DISTMASK])
aspectr2D=aspectr2D/sig2
#Now calculate r-spectrum
STEP=1
ray=seq(mi,ma,STEP)
rspectr=numeric(length(ray))
for (i in 1:length(ray))
{
m = DIST>=ray[i]-STEP/2 & DIST<ray[i]+STEP/2
rspectr[i] = mean(aspectr2D[m])
}
#Now calculate theta-spectrum
DISTMASK = DIST>=mi & DIST<=ma
STEP=5 #increments of 5 degrees
anglebin=seq(STEP,180,STEP)
tspectr=numeric(length(anglebin))
for (i in 1:(length(tspectr)-1))
{
m = which(DISTMASK & ANGLE>=anglebin[i]-STEP & ANGLE<anglebin[i])
tspectr[i] = sum(aspectr2D[m])/length(m)
}
m = which(DISTMASK & ANGLE >=anglebin[length(anglebin)]-STEP & ANGLE <=anglebin[length(anglebin)])
tspectr[length(tspectr)] = sum(aspectr2D[m])/length(m)
out = list(tspectr, rspectr)
return(out)
}