-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update Nodal stuff * Fix CorrData filtering typo
- Loading branch information
Showing
7 changed files
with
231 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
export gpu,cpu, NodalCorr | ||
|
||
mutable struct NodalCorr <: NoiseData | ||
n::Int64 | ||
id::Array{String,1} # id | ||
name::Array{String,1} # name | ||
loc::Array{<:InstrumentPosition,1} # loc | ||
fs::Float64 # fs | ||
src::String # src | ||
t::Array{Array{Int64,2},1} # time | ||
corr::AbstractArray{Float32, 2} # actual data | ||
|
||
function NodalCorr( | ||
n ::Int64, | ||
id ::Array{String,1}, | ||
name ::Array{String,1}, | ||
loc ::Array{<:InstrumentPosition,1}, | ||
fs ::Float64, | ||
src ::String, | ||
t ::Array{Array{Int64,2},1} , | ||
corr ::AbstractArray{<:AbstractFloat,2}, | ||
) | ||
|
||
return new(n, id, name, loc, fs, src, t, corr) | ||
end | ||
end | ||
|
||
NodalCorr(; | ||
n ::Int64 = 0, | ||
id ::Array{String,1} = Array{String}(undef,0), | ||
name ::Array{String,1} = Array{String}(undef,0), | ||
loc ::Array{InstrumentPosition,1} = Array{InstrumentPosition}(undef,0), | ||
fs ::Float64 = zero(Float64), | ||
src ::String = "", | ||
t ::Array{Array{Int64,2},1} = Array{Array{Int64,2},1}(undef,0), | ||
corr ::AbstractArray{<:AbstractFloat,2} = Array{Float32,2}(undef, 0, 2), | ||
) = CorrData(n, id, name, loc, fs, src, t, corr) | ||
|
||
|
||
# functions for adapting NodalData to GPU | ||
cpu(m::NodalData) = adapt(Array,m) | ||
gpu(x::NodalData) = use_cuda[] ? cu(x) : x | ||
|
||
# function for adapting NodalCorr to GPU | ||
cpu(m::NodalCorr) = adapt(Array,m) | ||
gpu(x::NodalCorr) = use_cuda[] ? cu(x) : x | ||
|
||
Adapt.adapt_structure(to, N::NodalData) = NodalData( | ||
N.n, | ||
N.ox, | ||
N.oy, | ||
N.oz, | ||
N.info, | ||
N.id, | ||
N.name, | ||
N.loc, | ||
N.fs, | ||
N.gain, | ||
N.resp, | ||
N.units, | ||
N.src, | ||
N.misc, | ||
N.notes, | ||
N.t, | ||
adapt(to,N.data), | ||
) | ||
|
||
Adapt.adapt_structure(to, N::NodalCorr) = NodalCorr( | ||
N.n, | ||
N.id, | ||
N.name, | ||
N.loc, | ||
N.fs, | ||
N.gain, | ||
N.resp, | ||
N.units, | ||
N.src, | ||
N.t, | ||
adapt(to,N.corr), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
export scalablexcorr, nodalxcorr, autocorrelate | ||
|
||
function scalablexcorr(A::AbstractArray,Ntau::Int,kthresh::AbstractFloat) | ||
V,S,U = svd(A) | ||
kind = findall(S .> maximum(S) * 0.05) | ||
kmax = maximum(kind) | ||
|
||
Nt,Ns = size(A) | ||
|
||
# check that Ntau < Nt / 2 | ||
Ntau = min(Ntau,Nt ÷ 2) | ||
|
||
# preallocate view | ||
V0 = @view V[Ntau÷2:Nt-Ntau÷2-1,1:kmax] | ||
U0 = @view U[:,1:kmax] | ||
if isa(V,AbstractGPUArray) | ||
VT0 = transpose(V[Ntau÷2:Nt-Ntau÷2-1,1:kmax]) | ||
UT0 = transpose(U[:,1:kmax]) | ||
else | ||
VT0 = V0' | ||
UT0 = U0' | ||
end | ||
|
||
Cout = similar(A,Ns,Ns,Ntau) | ||
|
||
for itau = -Ntau ÷ 2 + 1 : Ntau ÷ 2 | ||
W = VT0 * V[itau + Ntau÷2:Nt + itau - Ntau÷2-1,1:kmax] | ||
Cout[:,:,itau+Ntau÷2] .= U0 * W * UT0 | ||
end | ||
return Cout | ||
end | ||
|
||
function scalablexcorr(N::NodalData,Ntau::Real;kthresh=0.05) | ||
if isa(Ntau,AbstractFloat) | ||
Ntau = convert(Int,round(Ntau * N.fs[1])) | ||
end | ||
return scalablexcorr(N.data,Ntau,kthresh) | ||
end | ||
|
||
function nodalxcorr(N::NodalData,maxlag::Real) | ||
if isa(maxlag,AbstractFloat) | ||
maxlag = convert(Int,round(maxlag * N.fs[1])) | ||
end | ||
return nodalxcorr(N.data,maxlag) | ||
|
||
end | ||
|
||
function nodalxcorr(A::AbstractArray,maxlag::Int) | ||
Nt,Ns = size(A) | ||
Ncorr = Ns * (Ns -1) ÷ 2 | ||
Cout = similar(A,maxlag * 2 + 1,Ncorr) | ||
FFTs = rfft(A,1) | ||
cstart = 0 | ||
cend = 0 | ||
for ii = 1:Ns-1 | ||
cstart = cend + 1 | ||
cend = cstart + Ns - ii - 1 | ||
Cout[:,cstart:cend] .= correlate(FFTs[:,ii],FFTs[:,ii+1:end],Nt,maxlag) | ||
end | ||
return Cout | ||
end | ||
|
||
function autocorrelate(N::NodalData,maxlag::Real) | ||
if isa(maxlag,AbstractFloat) | ||
maxlag = convert(Int,round(maxlag * N.fs[1])) | ||
end | ||
return autocorrelate(N.data,maxlag) | ||
end | ||
|
||
function autocorrelate(A::AbstractArray,maxlag::Int64) | ||
Nt,Ns = size(A) | ||
FFT = rfft(A,1) | ||
corrT = irfft(conj.(FFT) .* FFT,Nt,1) | ||
# return corr[-maxlag:maxlag] | ||
t = vcat(0:Int(Nt / 2)-1, -Int(Nt / 2):-1) | ||
ind = findall(abs.(t) .<= maxlag) | ||
newind = fftshift(ind,1) | ||
return corrT[newind,:] | ||
end |