1
- export SpectrumArray, tologfreq
1
+ export SpectrumArray, SpectrumDomain, tologfreq
2
2
3
3
# ----- SpectrumArray ---------------------------
4
+ const SpectrumDomain = Union{AbstractVector{<: Frequency }, AbstractRange{<: Frequency }}
5
+ const SpectrumDomainReal = Union{AbstractVector{<: Real }, AbstractRange{<: Real }}
4
6
5
- struct SpectrumArray{T} <: AbstractSpectrumArray{T} # TODO complete implemetation!
7
+ struct SpectrumArray{T,F <: SpectrumDomainReal } <: AbstractSpectrumArray{T}
6
8
data:: Matrix{T} # non-interleaving frequencies x channels
7
9
rate:: Float64 # original sampling rate
8
- freqs:: Vector{Float64}
10
+ freqs:: F # in Hz
9
11
names:: Vector{Symbol}
10
12
11
- function SpectrumArray {T} (data:: Matrix{T} , rate:: Float64 , freqs:: Vector{Float64} , names:: Vector{Symbol} ) where T
13
+ function SpectrumArray {T,F } (data:: Matrix{T} , rate:: Float64 , freqs:: F , names:: Vector{Symbol} ) where {T,F}
12
14
if length (freqs) != size (data, 1 )
13
15
throw (DimensionMismatch (" data size $(size (data, 1 )) does not match the number of frequencies $(length (freqs)) " ))
14
16
end
15
17
_check_channel_names (names)
16
18
length (names) == size (data, 2 ) || throw (DimensionMismatch (" the number of names ($(length (names)) ) does not match the number of channels ($(size (data, 2 )) )!" ))
17
19
length (unique (freqs)) == length (freqs) || throw (ArgumentError (" non-unique freqs!" ))
18
- new (data, rate, freqs, copy (names))
20
+ new {T,F} (data, rate, freqs, copy (names))
19
21
end
20
22
end
21
23
22
- SpectrumArray (X:: AbstractMatrix{T} , rate:: Frequency , freqs:: AbstractVector{<:Frequency} , names:: Vector{Symbol} ) where T =
23
- SpectrumArray {T} (X, toHz (rate), Float64 .(toHz .(freqs)), names)
24
- SpectrumArray (X:: AbstractMatrix{T} , rate:: Frequency , freqs:: AbstractVector{<:Frequency} ) where T =
25
- SpectrumArray {T} (X, toHz (rate), Float64 .(toHz .(freqs)), _default_channel_names (size (X, 2 )))
24
+ function SpectrumArray (X:: AbstractMatrix{T} , rate:: Frequency , freqs:: SpectrumDomain , names:: Union{Nothing,Vector{Symbol}} = nothing ) where {T}
25
+ freqs_ = toHz (freqs)
26
+ F = typeof (freqs_)
27
+ SpectrumArray {T,F} (X, toHz (rate), freqs_, isnothing (names) ? _default_channel_names (size (X, 2 )) : names)
28
+ end
26
29
27
- SpectrumArray (X:: AbstractVector{T} , rate:: Frequency , freqs:: AbstractVector{<:Frequency} , names:: Vector{Symbol} ) where T =
28
- SpectrumArray {T} (reshape (X, :, 1 ), toHz (rate), Float64 .(toHz .(freqs)), names)
29
- SpectrumArray (X:: AbstractVector{T} , rate:: Frequency , freqs:: AbstractVector{<:Frequency} ) where T =
30
- SpectrumArray {T} (reshape (X, :, 1 ), toHz (rate), Float64 .(toHz .(freqs)), _default_channel_names (1 ))
30
+ SpectrumArray (X:: AbstractVector{T} , rate:: Frequency , freqs:: F , names:: Union{Nothing,Vector{Symbol}} = nothing ) where {T, F<: SpectrumDomain } =
31
+ SpectrumArray (reshape (X, :, 1 ), rate, freqs, names)
31
32
32
33
domain (X:: SpectrumArray ) = X. freqs
33
34
_findno0freqs (X:: SpectrumArray ) = findall (! iszero, domain (X))
@@ -39,7 +40,7 @@ function toindex(X::SpectrumArray{T}, t::Frequency) where T
39
40
findmin (diffs)[2 ]
40
41
end
41
42
42
- function Base. similar (X:: SpectrumArray , t:: Type{T} , dims:: Dims , freqs:: AbstractVector{<:Frequency} ) where T
43
+ function Base. similar (X:: SpectrumArray , t:: Type{T} , dims:: Dims , freqs:: SpectrumDomain ) where T
43
44
# tries to copy channel names
44
45
# if there are fever names in the source array use default ones
45
46
# TODO : move to abstracttypes.jl?
@@ -55,13 +56,13 @@ function Base.similar(X::SpectrumArray, t::Type{T}, dims::Dims) where T
55
56
end
56
57
57
58
# redefined so frequencies & channel names are treated
58
- function Base. getindex (X:: SpectrumArray{T} , I:: R , J:: S ) where {T, R <: FrameIndex , S <: ChannelIndex }
59
+ function Base. getindex (X:: SpectrumArray{T,F } , I:: R , J:: S ) where {T, F , R <: FrameIndex , S <: ChannelIndex }
59
60
I2 = toframeidx (X, I)
60
61
J2 = tochannelidx (X, J)
61
62
freqs_ = domain (X)[I2]
62
63
names_ = names (X)[J2]
63
64
data_ = data (X)[I2, J2]
64
- SpectrumArray {T} (data_, rate (X), freqs_, names_)
65
+ SpectrumArray {T,F } (data_, rate (X), freqs_, names_)
65
66
end
66
67
67
68
Base. getindex (X:: SpectrumArray{T} , I:: R ) where {T, R <: FrameIndex } = X[I, :]
@@ -72,7 +73,9 @@ function Base.hcat(X::SpectrumArray...)
72
73
length (unique (domain .(X))) == 1 || throw (ArgumentError (" hcat: non-unique domains!" ))
73
74
newnames = _unique_channel_names (X... )
74
75
data_ = hcat (map (data, X)... )
75
- return eltype (X)(data_, rate (X[1 ]), domain (X[1 ]), newnames) # eltype gives common supertype
76
+ rate_ = rate (X[1 ])
77
+ domain_ = domain (X[1 ])
78
+ return SpectrumArray {eltype(data_),typeof(domain_)} (data_, rate_, domain_, newnames) # eltype gives common supertype
76
79
end
77
80
78
81
function Base. vcat (X:: SpectrumArray... )
@@ -82,7 +85,7 @@ function Base.vcat(X::SpectrumArray...)
82
85
length (unique (namelists)) == 1 || throw (ArgumentError (" vcat: non-unique channel names!" ))
83
86
data_ = vcat (map (data, X)... )
84
87
freqs_ = vcat (map (domain, X)... )
85
- return eltype (X) (data_, rate (X[1 ]), freqs_, namelists[1 ])
88
+ return SpectrumArray { eltype(data_),typeof(freqs_)} (data_, rate (X[1 ]), freqs_, namelists[1 ])
86
89
end
87
90
88
91
function Base. show (io:: IO , :: MIME"text/plain" , X:: SpectrumArray{T} ) where T
0 commit comments