
function den_sig = SpectralSubtraction(sig, noisepss, alpha)


% sig= squeeze(data(epoch1,:));
%  sig= sig-wthresh(sig,'h',MagThresh);
% % 
% %  sig1= wthresh(sig,'h',MagThresh);
% %  sig2= abs(sig1) > 0;
% %  sig= sig.*(1-sig2);

%alpha
sig= squeeze(sig);
N_samples= length(sig)*2;
%sig1= sig- mean(sig(1:round(N_samples/2)));            % remove the mean
sig1= [sig ; fliplr(sig)];
%sig1= sig;

%  figure(1);
%  subplot(5,1,1); plot(sig);
%  subplot(5,1,3); plot(sig(1:10000));

f_sig= (fft((sig1)));
ph_sig= angle(f_sig);
pss_sig= abs(f_sig).^2;

% noise_pss= mean(pss_sig(round(N_samples*.3):round(N_samples*0.7)));
% noise_pss2= std(pss_sig(round(N_samples*.3):round(N_samples*0.7)));
noise_pss= max(pss_sig)*noisepss;

den_pss_sig= wthresh(pss_sig,'s', noise_pss*alpha);

den_f_sig= sqrt(den_pss_sig).*exp(j*ph_sig);

den_sig=real((ifft((den_f_sig))));
den_sig= den_sig(1:N_samples/2);
%  subplot(5,1,2); plot(den_sig);
%  subplot(5,1,4); plot(den_sig(1:10000));
%  subplot(5,1,5); plot(sig(1:10000)-den_sig(1:10000));
% 
% pause


 
