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); %Decoding 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))); end 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); end
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.
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!