I am working on a sinusoidal encoder & decoder to compress .wav files. For now I am trying to regenerate the audio by combining all the frequencies of the original .wav file. Here is the code I am using:

[x fs] = wavread('clip.wav');
x  = mean(x,2);
NFFT = 1024;
hop = NFFT/2;
win = hann(NFFT);

[S F]= spectrogram(x, win, hop, NFFT, fs);
freq = linspace(0, fs/2, hop + 1);
numFrames = size(S,2);
mag = abs(S);
phase = angle(S);

output = zeros(size(x));
t = 0:(1/fs):(((NFFT-1)/fs));
sumOfCosine = zeros(1, NFFT );
frameStart = 1;
frameEnd = NFFT;

%f = frame
for f = 1:numFrames
    sumOfCosine = zeros(1, NFFT );

    %k = sinusoids in each frame
    for k = 1:hop+1
       sumOfCosine =  sumOfCosine + (mag(k,f)*cos(2*pi*F(k)*t + phase(k,f)));

 sumOfCosine = sumOfCosine.*win';
 a = hop;
 % Normalize the window
 sumOfCosine = (sumOfCosine./a);
 sumOfCosine = sumOfCosine';
 output(frameStart:frameEnd) = ((output(frameStart:frameEnd)+ sumOfCosine));
 frameStart = frameStart + (hop);
 frameEnd = frameEnd + (hop);


When I play play output, there is still some noise in it. I tried comparing the original matrix x multiplied by the window, win to the output frame by frame, and I can see that output represented by green doesn't completely overlap with the original windowed signal. I am pasting the images for the first three frames below.

Frame 1

Frame 2

Frame 3

As it can be seen there is some error due to the overlapping of windows maybe? I'd appreciate help in fixing this and make the output sound cleaner. Thanks!


1 Answer 1


For the phases of sinusoids within the frame overlaps to sum constructively, either t has to be adjusted to be frame relative, or the fft phase result has to be adjusted to be relative to the same absolute time as t.

  • $\begingroup$ t lasts for 1 frame(1024 samples or 23 ms). What do you mean by "fft phase result has to be adjusted to be relative to the same absolute time as t"? Sorry if it seems like a stupid question. $\endgroup$
    – dawnoflife
    Dec 1, 2012 at 20:06
  • $\begingroup$ I tried changing the size of t to 23/2 ms, but that didn't do anything... $\endgroup$
    – dawnoflife
    Dec 2, 2012 at 1:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.