Skip to content

Instantly share code, notes, and snippets.

@r9y9
Last active August 29, 2015 14:10
Show Gist options
  • Save r9y9/93d1645275f7bea9569a to your computer and use it in GitHub Desktop.
Save r9y9/93d1645275f7bea9569a to your computer and use it in GitHub Desktop.
backward_plan2{T<:Union(Float32, Float64)}(X::AbstractArray{Complex{T}}, Y::AbstractArray{T}) =
FFTW.Plan(X, Y, 1, FFTW.ESTIMATE | FFTW.PRESERVE_INPUT, FFTW.NO_TIMELIMIT).plan
function istft2{T<:Union(Float32, Float64)}(S::AbstractMatrix{Complex{T}}, wlen::Int, overlap::Int; nfft=nextfastfft(wlen), window::Union(Function,AbstractVector,Nothing)=nothing)
winc = wlen-overlap
win, norm2 = compute_window(window, wlen)
if win != nothing
win² = win.^2
end
nframes = size(S,2)-1
outlen = nfft + nframes*winc
out = zeros(T, outlen)
tmp2 = zeros(T, nfft)
p = backward_plan2(S[:,1], tmp2)
wsum = zeros(outlen)
for k = 1:size(S,2)
FFTW.execute(p, sub(S,1:size(S,1), k), tmp2)
scale!(tmp2, FFTW.normalization(tmp2))
if win != nothing
ix = (k-1)*winc
for n=1:nfft
@inbounds out[ix+n] += tmp2[n]*win[n]
@inbounds wsum[ix+n] += win²[n]
end
else
copy!(out, 1+(k-1)*winc, tmp2, 1, nfft)
end
end
if win != nothing
for i=1:length(wsum)
@inbounds wsum[i] != 0 && (out[i] /= wsum[i])
end
end
out
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment