
close all
% (1) Propuštanje zašumljenog signala kroz sustav
%zadavanje istog signala
x = linspace(0,4*pi,1024);
ulaz_orig=diric(x,7);

%zadavanje bijelog šuma
bijeli_sum=randn(1,1024);

%ulazni zašumljeni signal
ulaz=ulaz_orig+bijeli_sum/10;
figure(1),plot(ulaz_orig),title('Ulaz')
figure(2),plot(ulaz),title('Ulaz_sum')

ulaz_fft = fft(ulaz);
ulaz_orig_fft = fft(ulaz_orig);

% sum_fft = fft(bijeli_sum/10);
Sxx =  abs(ulaz_orig_fft).^2;
% ulaz
% pause
Snn = var(abs(ulaz)).^2;
figure(5),plot(Sxx);

wiener_fft = Sxx./(Sxx + Snn);
% figure(3),plot(wiener_fft),title('spektar od wienera');
wiener = ifft(wiener_fft);
wiener = real(wiener);
wiener = fftshift(wiener);

izlaz  = conv(wiener,ulaz);

figure(4),plot(izlaz),title('Izlaz iz wienera');



% %SNR na ulazu
SNRi = 10*log10(sum(abs(fft(ulaz_orig)).^2)/sum(abs(fft(bijeli_sum/10)).^2));
SNRo = 10*log10(sum(abs(fft(ulaz_orig)).^2)/sum(abs(fft(izlaz(512:1:1023+512)-ulaz_orig)).^2));
% % SNR = 10*log10(Ps/Pe)
% % Ps = Sum of s squared
% % Pe = Sum of e squared
% Psi=(sum(ulaz_orig)*sum(ulaz_orig)); % setting Ps to sum of s squared
% Pei=(sum(bijeli_sum/10)*sum(bijeli_sum/10)); % setting Pe to sum of e squared
% SNRi=10*log10(Psi/Pei);
% 
% %SNR na izlazu
% Pso=(sum(ulaz_orig))*(sum(ulaz_orig)); % setting Ps to sum of s squared
% Peo=(sum(izlaz(512:1:512+1023)-ulaz_orig))*(sum(izlaz(512:1:512+1023)-ulaz_orig)); % setting Pe to sum of e squared
% SNRo=10*log10(Pso/Peo);


