STFT and ISTFT problem. I get noise with the same signal. (Short Time Fourier Transform)

12 views (last 30 days)
Dear friends,
I have a project which goes like this: I have to take a signal (a wav file), then do the STFT (Short Time Fourier Transform) of the signal, and then do the Inverse STFT, and get back the same signal that I had in the first place.
For some reason I get the same signal but with NOISE. I also notticed that the signal that i had at the beggining is smaller than the signal I get at the end by some points.
Here is the code of STFT and ISTFT:
STFT
function Xtwz = stft(x, M, R, N, w)
nframes = floor(1+ (length(x)-M)/R);
Xtwz = zeros(N,nframes); % pre-allocate STFT output array
xoff = 0;
zp = zeros(N-M,1); % zero padding (to be inserted)
for m=1:nframes
xt = x(xoff+1:xoff+M); % extract frame of input data
xtw = w .* xt; % apply window to current frame
if (N-M)~=0
xtwz = [zp(1:((N-M)/2)) ; xtw ; zp(((N-M)/2+1):(N-M))];
else
xtwz=xtw;
end
Xtwz(:,m) = fft(xtwz); % STFT for frame m
xoff = xoff + R; % advance in-pointer by hop-size R
end
% Keep half of the spectrum
if rem(N,2)==0
Xtwz = Xtwz(1:(N/2+1),:);
else
Xtwz = Xtwz(1:(N/2+1/2),:);
end
ISTFT
function g = istft_til(d,R,N,M)
s = size(d);
nframes = s(2);
xlen = M + (nframes-1)*R;
g = zeros(1,xlen);
i=0;
for b = 1:nframes
ft = d(:,b)';
ft = [ft , conj(ft(N/2:-1:2))];
px = real(ifft(ft));
i=(b-1)*R;
a=px((N-M)/2+1:(N+M)/2);
g((i+1):(i+M)) = g((i+1):(i+M)) + a;
end
end
Does anyone have an idea why is this happening, or how could I fix it???
Thanks in advance.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!