Skip to content

Commit

Permalink
Add normalise[!] to normalise traces by maximum value
Browse files Browse the repository at this point in the history
  • Loading branch information
anowacki committed Jun 21, 2018
1 parent fde0a8f commit b1c5ecf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/SAC.jl
Expand Up @@ -10,6 +10,10 @@ module SAC
using Compat
using Compat.LinearAlgebra

@static if VERSION >= v"0.7-"
import StatsBase: linreg
end

import DSP, Dierckx
import Glob
import Base: ==, copy, getindex, fft, setindex!, time, write
Expand Down Expand Up @@ -37,6 +41,8 @@ export
modify,
multiply!,
multiply,
normalise!,
normalise,
read_wild,
rmean!,
rotate_through!,
Expand Down Expand Up @@ -114,6 +120,14 @@ for (name, abbrev) in copying_funcs
end

# Build methods which can be used with chaining (`|>`)
"""Dict with keys given by name of each function to have a chaining version.
Where an abbreviated version exists, that is given as the value; otherwise
the value is nothing."""
const chaining_funcs = Dict(
copying_funcs...,
:normalise! => nothing
)

for (name, abbrev) in copying_funcs
new_name = Symbol(string(name)[1:end-1])
@eval begin
Expand Down
18 changes: 18 additions & 0 deletions src/operations.jl
Expand Up @@ -135,6 +135,24 @@ julia> modify(s, x->x^2)[:depmax]
"""
modify(s::SACtr, f) = modify!(deepcopy(s), f)

"""
normalise!(s::SACtr) -> s
Normalise a SACtr `s` so that its maximum absolute amplitude is unity.
"""
function normalise!(s::SACtr)
maxval = maximum(abs, s.t)
s.t .= s.t./maxval
update_headers!(s)
end

"""
normalise(s::SACtr) -> s_new
Return a copy of `s` where the maximum absolute amplitude is unity.
"""
normalise(s::SACtr) = normalise!(deepcopy(s))

# Linear operations on multiple traces
check_traces_combine(s1, s2) = begin s1.b s2.b && s1.npts == s2.npts ||
throw(ArgumentError("Traces must have same strart time and length")) end
Expand Down
1 change: 1 addition & 0 deletions src/precompile.jl
Expand Up @@ -54,6 +54,7 @@ function _precompile()
precompile(divide!, (Array{SACtr}, f1))
precompile(multiply!, (SACtr, f1))
precompile(multiply!, (Array{SACtr}, f1))
precompile(normalise!, (SACtr,))
for f2 in number_types
precompile(cut!, (SACtr, f1, f2))
precompile(cut!, (Array{SACtr}, f1, f2))
Expand Down
6 changes: 6 additions & 0 deletions test/operations.jl
Expand Up @@ -148,4 +148,10 @@ end
rtrend!(s)
@test all(s .== s)
end

## Normalisation
let s = SACtr(rand(SAC.SACFloat, 100), 0.1), s′ = deepcopy(s)
@test all(normalise(s).t .== s′.t./maximum(abs, s.t))
@test normalise!(s) == normalise(s′)
end
end

0 comments on commit b1c5ecf

Please sign in to comment.