diff --git a/Project.toml b/Project.toml index 77871ab..e1e36ed 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,7 @@ keywords = ["AeroAcoustics"] license = "MIT" desc = "A package for post-processing of microphone array measurements" authors = ["Oliver Lylloff "] -version = "0.2.2" +version = "0.2.3" [deps] DSP = "717857b8-e6f2-59f4-9121-6e50c889abd2" diff --git a/src/AeroAcoustics.jl b/src/AeroAcoustics.jl index 3a931b3..1733aa9 100644 --- a/src/AeroAcoustics.jl +++ b/src/AeroAcoustics.jl @@ -12,6 +12,10 @@ using ThreadsX import Base.length, Base.push!, Base.reshape + #Base.*, + #Base./, + #Base.+, + #Base.- export Environment, FreqArray, diff --git a/src/types.jl b/src/types.jl index 21de779..2014c5b 100644 --- a/src/types.jl +++ b/src/types.jl @@ -7,12 +7,33 @@ struct FreqArray{T, N, AA<:AbstractVector} <: AbstractArray{T, N} end # Standard array functions and indexing +FreqArray(arr::AbstractArray{T,N}, fc::AbstractVector) where {T,N} = + FreqArray{eltype(arr),N,typeof(fc)}(arr, fc) Base.size(A::FreqArray) = size(A.arr) Base.axes(A::FreqArray) = axes(A.arr) Base.getindex(A::FreqArray, i::Int) = A.arr[i] Base.getindex(A::FreqArray{T, N}, I::Vararg{Int, N}) where {T, N} = A.arr[I...] -Base.setindex!(A::FreqArray,v,i) = A.arr[i] = v +Base.getindex(A::FreqArray, ::Colon) = A.arr[:] +Base.getindex(A::FreqArray, kr::AbstractRange) = A.arr[kr] +Base.setindex!(A::FreqArray, v, i::Int) = (A.arr[i] = v) +Base.setindex!(A::FreqArray, v, I::Vararg{Int,N}) where {N} = (A.arr[I...] = v) +Base.setindex!(A::FreqArray, v, ::Colon) = (A.Arr[:] .= v) +Base.setindex!(A::FreqArray, v, kr::AbstractRange) = (A.arr[kr] .= v) + +Base.similar(A::FreqArray) = FreqArray(similar(A.arr),A.fc) +Base.similar(A::FreqArray,::Type{T}) where T = FreqArray(similar(A.arr,T),A.fc) + +Base.:*(x::Number, A::FreqArray) = FreqArray(x*A.arr, A.fc) +Base.:*(A::FreqArray, x::Number) = FreqArray(x*A.arr, A.fc) +Base.:/(A::FreqArray, x::Number) = FreqArray(A.arr/x, A.fc) +Base.:/(x::Number, A::FreqArray) = FreqArray(A.arr/x, A.fc) + +# TODO: check that A.fc == B.fc before computing: +#Base.:*(B::FreqArray, A::FreqArray) = FreqArray(B.arr.*A.arr, A.fc) +#Base.:*(A::FreqArray, B::FreqArray) = FreqArray(A.arr.*B.arr, A.fc) +#Base.:/(A::FreqArray, B::FreqArray) = FreqArray(A.arr./B.arr, A.fc) +#Base.:/(B::FreqArray, A::FreqArray) = FreqArray(B.arr./A.arr, A.fc) """ Environment diff --git a/test/utility_tests.jl b/test/utility_tests.jl index 1ef59bb..b2f16ea 100644 --- a/test/utility_tests.jl +++ b/test/utility_tests.jl @@ -80,3 +80,18 @@ end win = DSP.rect(n) @test enbw(fs,win) == fs/n end + +@testset "FreqArray:" begin + T = ComplexF64 + A = ones(ComplexF64,10,10,2) + b = ones(Int64,2) + F = FreqArray(A,b) + @test typeof(FreqArray(A,b)) === FreqArray{T,3,typeof(b)} + @test typeof(F) === typeof(F/10) === typeof(10/F) + @test typeof(F) === typeof(F*10) === typeof(10*F) + @test F.fc == (F*10).fc == (10*F).fc + @test F.fc == (F/10).fc == (10/F).fc + @test (F*10)[1] == 10.0 + 0.0im + F[1,1,1] = 2.0 + @test typeof(F) == FreqArray{T,3,typeof(b)} # is it still a FreqArray? +end \ No newline at end of file