function [clean] = method(corrupt, noise)
% Fighting Corrupt Speech: method2
% <cleaned-data> = method2( <corrupt-data>, <noise-data> )
%
% Attempts to clean up the corrupt voice data by blurring the
% noise signal by convolving it with a 1-2-1 Gaussian filter.
% Then, if the magnitude of the frequency in the blurred noise data 
% (in the Fourier domain) is greater than some threshold (i.e. 10),
% the corresponding value in the corrupt signal is multiplied by some
% small number to reduce the noise.
%
% This function takes the corrupt and noise signals, as read
% by the wavread function.  The function assumes 11.025 kHz 
% recording.  Example usage:
%
% >> corrupt = wavread('corrupt');
% >> noise = wavread('noise');
% >> data = method2(corrupt, noise);
% >> soundsc(data, 11025);
%

[N, trash]=size(noise);

% subtract the dc offset from the inputs
noise = losedcoff(noise);
corrupt = losedcoff(corrupt);

noiseFFT = fft(noise);
corruptFFT = fft(corrupt);

%plot(abs(noiseFFT))
%pause
%plot(abs(corruptFFT))
%pause

FS = 11025;
Ts = 1/FS;
DELTAF = 1 / ( N * Ts );
FREQ = 0:DELTAF:FS/2;
FREQ = FREQ';
plot(FREQ(1:N/2), abs(noiseFFT(1:N/2)));
pause;
plot(FREQ(1:N/2), abs(corruptFFT(1:N/2)));
pause;

Noiseblur=conv([1 2 1], abs(noiseFFT));
[N, trash]=size(noiseFFT);
Noiseblur = Noiseblur(2:N+2);

% ---- should we blur it again ? for a 1-4-6-4-1 blur? -------
%Noiseblur=conv([1 2 1], abs(noiseFFT));
%[N, trash]=size(noiseFFT);
%Noiseblur = Noiseblur(2:N+2);


plot(FREQ(1:N/2), abs(Noiseblur(1:N/2)));
title('Blurred Noise Data Magnitude Plot (Fourier Domain)');
pause;

for I=1:N,
    if abs(Noiseblur(I)) > 10
        corruptFFT(I)= 0.001*corruptFFT(I);
    end
end

plot(FREQ(1:N/2), abs(corruptFFT(1:N/2)));
title('Rebuilt Speech Data After method2 Processing');
clean= real(ifft(corruptFFT));
    



