RANDOM
SIGNAL AND NOISE
Assignment Matlab
BU21EECE0100153
[Link]
1)Generate a discrete time sequence Xn of N = 10000 independent random
numbers in the interval [-1/2, 1/2]. Assume uniform distribution.
a)Obtain a histogram plot with 10 bins and compare it with the actual PDF
% Parameters
N = 10000; % Number of samples
bins = 10; % Number of histogram bins
interval = [-1/2, 1/2]; % Interval of random numbers
% Generate random numbers
Xn = interval(1) + (interval(2) - interval(1)) * rand(1, N);
% Plot histogram
figure;
histogram(Xn, bins, 'Normalization', 'pdf');
title('Histogram of Random Numbers');
xlabel('Value');
ylabel('Probability Density');
b) Transform this into another random variable Y as follows: Y = X². Obtain
its histogram plot with suitable number of bins and compare with actual
PDF of Yn
% Parameters
N = 10000; % Number of samples
numBins = 50; % Number of bins for histogram
% Generate random numbers in the interval [-1/2, 1/2]
Xn = rand(N, 1) - 0.5;
% Transform Xn to Y = X^2
Y = Xn.^2;
% Calculate the actual PDF of Y
pdf_Y = 1 ./ sqrt(2*pi*Y) .* exp(-Y/2);
% Plot histogram of Y
figure;
histogram(Y, numBins, 'Normalization', 'pdf');
hold on;
% Plot actual PDF of Y
y_values = linspace(0, max(Y), 1000);
plot(y_values, pdf_Y, 'r', 'LineWidth', 2);
% Set plot title and labels
title('Histogram and PDF of Y');
xlabel('Y');
ylabel('Probability Density');
% Add legend
legend('Histogram', 'Actual PDF');
c) Generate and plot any 2 realizations of Z(t) = cos(2πXnt)
% Parameters
N = 10000; % Number of samples
Fs = 1000; % Sampling frequency (Hz)
T = 1/Fs; % Sampling period (s)
t = 0:T:(N-1)*T; % Time vector
% Generate random numbers
Xn = rand(1, N) - 0.5; % Random numbers in the interval [-1/2, 1/2]
% Generate two realizations of Z(t)
Z1 = cos(2*pi*Xn.*t);
Z2 = cos(2*pi*Xn.*t);
% Plot the realizations
figure;
subplot(2,1,1);
plot(t, Z1);
title('Realization 1');
xlabel('Time (s)');
ylabel('Z(t)');
subplot(2,1,2);
plot(t, Z2);
title('Realization 2');
xlabel('Time (s)');
ylabel('Z(t)');
D) Plot the theoretical and estimated mean functions of Z(t).
N = 10000; % Number of random numbers
a = -1/2; % Lower bound of interval
b = 1/2; % Upper bound of interval
% Generate random numbers
Xn = (b-a) * rand(1, N) + a;
% Compute theoretical mean function
theoretical_mean = (a + b) / 2;
% Compute estimated mean function
estimated_mean = cumsum(Xn) ./ (1:N);
% Plotting
t = 1:N;
figure;
hold on;
plot(t, theoretical_mean * ones(1, N), 'r--', 'LineWidth', 1.5); % Theoretical
mean
plot(t, estimated_mean, 'b', 'LineWidth', 1.5); % Estimated mean
hold off;
xlabel('Time (t)');
ylabel('Mean (Z(t))');
title('Theoretical and Estimated Mean Functions of Z(t)');
legend('Theoretical Mean', 'Estimated Mean');
e) Find and plot the autocorrelation function and power spectrum of Z(t).
N = 10000; % number of samples
T = 1; % sampling interval
t = linspace(0, T*(N-1), N); % time vector
dt = t(2) - t(1); % time step
% generate uniform random numbers in [-1/2, 1/2]
Xn = rand(1, N) - 0.5;
% compute autocorrelation function
acf = xcorr(Xn, 'biased');
% compute power spectrum
Pxx = abs(fft(Xn)).^2/N/dt;
% plot results
subplot(2,1,1)
plot(t, Xn)
xlabel('Time (s)')
ylabel('Amplitude')
title('Random Signal')
subplot(2,1,2)
plot(t, acf(N:end))
xlabel('Time Lag (s)')
ylabel('Autocorrelation')
title('Autocorrelation Function')
figure
f = linspace(0, 1/dt, N);
plot(f(1:N/2), Pxx(1:N/2))
xlabel('Frequency (Hz)')
ylabel('Power')
title('Power Spectrum')
2)Perform the same experiment assuming Gaussian distribution with zero
mean and unit variance
N = 10000;
Xn = randn(N, 1);
edges = linspace(-0.5, 0.5, 11);
counts = histcounts(Xn, edges);
pdf = counts / (N * diff(edges));
bar(edges(1:end-1), pdf, 'hist');
Yn = Xn.^2;
edges = linspace(0, 0.25, 26);
counts = histcounts(Yn, edges);
pdf = counts / (N * diff(edges));
bar(edges(1:end-1), pdf, 'hist');
t = linspace(0, 1, N);
Z1 = cos(2*pi*Xn*t);
Z2 = cos(2*pi*Xn*(t+1));
plot(t, Z1, t, Z2);
mu_theoretical = zeros(size(t));
mu_estimated = cumsum(Z1) ./ (1:N);
plot(t, mu_theoretical, t, mu_estimated);
R = xcorr(Z1);
R = R(N:end);
plot(t, R)
S = abs(fft(R));
f = linspace(0, 1, N);
plot(f, S);