clear;clc;clf;clf reset
% Statistical Properties of Noise
n= 5000;t = 0 : 1/n : 1-1/n; 
Noise_Power_dB = -20 ;% Power is defined in dB relative to 1 watt
nt= wgn(1,n,Noise_Power_dB);
Nf = fft(nt,n);Noise_Power_dBt = 10*log10(sum(nt.*nt/n));
Nf = Nf.*conj(Nf)/n;
Noise_Power_dBf = 10*log10(sum(Nf)/length(Nf));
[Noise_Power_dB Noise_Power_dBt Noise_Power_dBf] % Checking Noise Power level in dB Watt
figure(1)
hist(nt,n/10) % Gaussian Magnitude Distribution of Noise
set(gcf,'Renderer','Zbuffer');set(gca,'FontSize',16);set(gcf,'Color','White')
title('Amplitiude distribution of white Gaussian noise with zero mean','FontSize',12,'FontWeight','bold');
xlabel('Noise amplitude - \itn \rm\bf( \itt \rm\bf)','FontSize',16,'FontWeight','bold');
ylabel('pdf - \itp \rm\bf( \itn \rm\bf)','FontSize',16,'FontWeight','bold')
figure(2)
% PLOTTING Noise in Time Domain and Frequency Domain
subplot(2,1,1);plot(t,nt);axis([min(t) max(t) min(min(nt))*1.1 max(max(nt))*1.1]) 
title('Time Waveform of Noise Signal - \itn \rm\bf( \itt \rm\bf)','FontSize',14,'FontWeight','bold')
xlabel('Time (Sec) - \itt','FontSize',14,'FontWeight','bold');ylabel('Amplitude - \itn \rm\bf( \itt \rm\bf)','FontSize',14,'FontWeight','bold')
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',14);
f = n*(0:n-1)/n;
subplot(2,1,2);plot(f,Nf)
title('Spectral density of noise signal against \itf','FontSize',14,'FontWeight','bold')
xlabel('Frequency (Hz) - \itf','FontSize',14,'FontWeight','bold');
ylabel('Spectral density of noise - \itS_n \rm\bf( \itf \rm\bf)','FontSize',14,'FontWeight','bold');
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',14);
% Mixing DSB Signal with Noise
a = 0.3;Ac = 1;fm = 25;fc = 500;
mt = a*cos(2*pi*fm*t);
ct = Ac*cos(2*pi*fc*t);
yt = mt.*ct;
rt = yt + nt;t1 = 400;figure(3)
subplot(2,1,1);plot(t(1:t1),yt(1:t1));
title('DSB signal transmitted','FontSize',12,'FontWeight','bold')
xlabel('Time (Sec) - \itt','FontSize',14,'FontWeight','bold');
ylabel('\itu \rm\bf( \itt \rm\bf) = \itm \rm\bf( \itt\rm\bf ) \itc \rm\bf( \itt \rm\bf)','FontSize',12,'FontWeight','bold');
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',14);
subplot(2,1,2);plot(t(1:t1),rt(1:t1))
title('DSB signal received with noise','FontSize',14,'FontWeight','bold')
xlabel('Time (Sec) - \itt','FontSize',14,'FontWeight','bold');
ylabel('\itr \rm\bf( \itt \rm\bf) = \itu \rm\bf( \itt \rm\bf) + \itn \rm\bf( \itt\rm\bf )','FontSize',14,'FontWeight','bold');
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',14);
%% Demodulating the DSB Signal in Noise %%%
rct = rt.*ct;
n_b = 2;Wn = [2*pi*1 2*pi*50];
[bd,ad] = butter(n_b,Wn/(2*pi*500));
mpt = filter(bd,ad,rct);figure(4)
subplot(2,1,1);plot(t(1:t1),mt(1:t1));
title('Original message signal','FontSize',14,'FontWeight','bold')
xlabel('Time (Sec) - \itt','FontSize',14,'FontWeight','bold');
ylabel('\itm \rm\bf( \itt \rm\bf)','FontSize',14,'FontWeight','bold');
axis([min(t) t(t1) min(min(mt))*1.1 max(max(mt))*1.1]);set(gcf,'Color',[1 1 1]);set(gca,'FontSize',14);
subplot(2,1,2);plot(t(1:t1),mpt(1:t1))
title('Demodulated (noisy) message signal','FontSize',14,'FontWeight','bold')
xlabel('Time (Sec) - \itt','FontSize',14,'FontWeight','bold');
ylabel('\itm_r \rm\bf( \itt \rm\bf)','FontSize',14,'FontWeight','bold')
set(gcf,'Color',[1 1 1]);set(gca,'FontSize',14);