# Functions for computing the stft and inverse stft
stft = function(y,H,N) {
v = seq(from=0,by=2*pi/N,length=N)
win = (1 + cos(v-pi))/2 # the Hann window
cols = floor((length(y)-N)/H) + 1 # number of frames of stft
stft = matrix(0,N,cols) # create a blank matrix
for (t in 1:cols) {
range = (1+(t-1)*H): ((t-1)*H + N) # the t-th frame
chunk = y[range]
stft[,t] = fft(chunk*win)
}
return(stft)
}
# according to class discusion, here is how we recover audio from
# the stft called Y
istft = function(Y,H,N) {
v = seq(from=0,by=2*pi/N,length=N)
win = (1 + cos(v-pi))/2
y = rep(0,N + H*ncol(Y))
for (t in 1:ncol(Y)) {
chunk = fft(Y[,t],inverse=T)/N
range = (1+(t-1)*H): ((t-1)*H + N) # the audio indices for the t-th frame
y[range] = y[range] + win*Re(chunk) # add in the windowed ifft
}
return(y)
}
library(tuneR)
N = 1024
H = N/4
bits = 16
w = readWave("sound/octaves.wav")
play(w,"play")
sr = w@samp.rate
y = w@left
Y = stft(y,H,N)
ybar = istft(Y,H,N)
u = Wave(round(ybar), samp.rate = sr, bit=bits) # make wave struct
play(u,"play")