Skip to content

Commit

Permalink
fix(dsp): upconvert and downconvert matrix return matrix (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
mchitre committed Aug 8, 2023
1 parent 2b49c9a commit 67fa25e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/dsp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ end
function upconvert(s::AbstractMatrix, sps, fc, pulseshape=rrcosfir(0.25, sps); fs=framerate(s))
mapreduce(hcat, eachcol(s)) do x
upconvert(x, sps, fc, pulseshape; fs=fs)
end
end[:,:]
end

"""
Expand All @@ -143,7 +143,7 @@ end
function downconvert(s::AbstractMatrix, sps, fc, pulseshape=rrcosfir(0.25, sps); fs=framerate(s))
mapreduce(hcat, eachcol(s)) do x
downconvert(x, sps, fc, pulseshape; fs=fs)
end
end[:,:]
end

"""
Expand Down Expand Up @@ -499,19 +499,16 @@ function _istft(iX::AbstractMatrix{T}, nfft::Int, noverlap::Int, window::Union{F
# H. Zhivomirov, TEM Journal, Vol. 8, No. 1, pp. 56-64, 2019.
(window === nothing) && (window = rect)
win, norm2 = Periodograms.compute_window(window, nfft)

nstep = nfft - noverlap
nseg = size(iX, 2)
outputlength = nfft + (nseg-1) * nstep

iX .*= win
x = zeros(T, outputlength)
normw = zeros(eltype(win), outputlength)
for i = 1:nseg
@views x[1+(i-1)*nstep:nfft+(i-1)*nstep] .= x[1+(i-1)*nstep:nfft+(i-1)*nstep] .+ iX[:,i]
@views normw[1+(i-1)*nstep:nfft+(i-1)*nstep] .= normw[1+(i-1)*nstep:nfft+(i-1)*nstep] .+ win .^ 2
end

trimlength = nfft % 2 == 0 ? outputlength - nfft : outputlength - nfft + 1
(sum(@view(normw[1+nfft÷2:end-nfft÷2]) .> 1e-10) != trimlength) && (
@warn "NOLA condition failed, STFT may not be invertible")
Expand Down
10 changes: 10 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,16 @@ end
@test framerate(z) == 100
@test amp2db(rms(x-z)/rms(z)) < -40.0

x = x[:,1:1]
y = upconvert(x, 10, 100)
@test nchannels(y) == 1
@test framerate(y) == 1000
@test y isa AbstractMatrix
z = downconvert(y, 10, 100)[12:end-11,:]
@test nchannels(z) == 1
@test framerate(z) == 100
@test z isa AbstractMatrix

x = rcosfir(0.25, 10)
@test length(x) == 221
@test x[1:10:end] vcat(zeros(11), 0.326598866403003, zeros(11))
Expand Down

0 comments on commit 67fa25e

Please sign in to comment.