Assgn 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

clc

num = rand(1, 20000) * 2 - 1;


histogram(num, 'Normalization', 'Probability')
sim_length = 20000;
N = 5;
X = -1 + 2 * rand(N, sim_length);
Z = sum(X);
step = 0.04;
nbins = -10:step:10;
[Zd, y] = hist(Z, nbins);
pdf_Z = Zd / (sim_length * step);
M = mean(Z);
V = var(Z);
Gd = 1 / sqrt(2 * pi * V) * exp(-0.5 * (y - M).^2 / V);
figure;
plot(y, pdf_Z, 'o', 'color', [0, 0, 1]);
hold on;
plot(y, Gd, '-', 'color', [1, 0, 0]);
legend('PDF of Z', 'Gaussian PDF');

% Calculate relative error


sigma = sqrt(V);
sigma_values = [0, 1, 2, 3, 4];
for i = 1:length(sigma_values)
sigma_val = sigma_values(i);
idx = find(abs(y - M) < sigma_val * sigma);
rel_error = abs(pdf_Z(idx) - Gd(idx)) ./ pdf_Z(idx);
fprintf('Relative error at %d sigma: %f\n', sigma_val, mean(rel_error));
end

% Parameters
No = 1e-12; % Power spectral density of white Gaussian noise
fc = 1e6; % Mid-band frequency of the band-pass filter
B = 1e5; % Bandwidth of the band-pass filter
T = 1e-6; % Sampling period
Fs = 1 / T; % Sampling frequency

% Generate white Gaussian noise


t = 0:T:1; % Time vector
n = sqrt(No) * randn(size(t)); % White Gaussian noise
% Define band-pass filter
f = linspace(-Fs/2, Fs/2, length(t)); % Frequency vector
H = (abs(f) >= fc - B) & (abs(f) <= fc + B); % Ideal band-pass filter response

% Compute filtered noise


n_filtered = ifft(fft(n) .* fftshift(H));

% Compute in-phase and quadrature components


n_in_phase = real(n_filtered);
n_quadrature = imag(n_filtered);

% Compute autocorrelation functions


lag = -length(t)+1:length(t)-1;
autocorr_n = xcorr(n, 'coeff');
autocorr_in_phase = xcorr(n_in_phase, 'coeff');
autocorr_quadrature = xcorr(n_quadrature, 'coeff');

% Plot results
figure;
subplot(3,1,1);
plot(lag, autocorr_n);
title('Autocorrelation of White Gaussian Noise');
xlabel('Lag');
ylabel('Autocorrelation');
grid on;
grid minor;
subplot(3,1,2);
plot(lag, autocorr_in_phase);
title('Autocorrelation of In-phase Component');
xlabel('Lag');
ylabel('Autocorrelation');
grid on;
grid minor;
subplot(3,1,3);
plot(lag, autocorr_quadrature);
title('Autocorrelation of Quadrature Component');
xlabel('Lag');
ylabel('Autocorrelation');
grid on;
grid minor;

% Expressions
fprintf('Autocorrelation of White Gaussian Noise: E[n(t)*n(t+tau)] = No/2 *
delta(tau)\n');
fprintf('Autocorrelation of In-phase Component:
E[cos(2*pi*fc*t)*cos(2*pi*fc*(t+tau))] = (No/4) * cos(2*pi*fc*tau)\n');
fprintf('Autocorrelation of Quadrature Component:
E[sin(2*pi*fc*t)*sin(2*pi*fc*(t+tau))] = (No/4) * cos(2*pi*fc*tau)\n');

You might also like