Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB SYSTEM AND SIGNAL QUESTION/COMMUINCATION, EXPERT MUST KNOW MATLAB 3. Matl

ID: 2248185 • Letter: M

Question

MATLAB SYSTEM AND SIGNAL QUESTION/COMMUINCATION, EXPERT MUST KNOW MATLAB

3. Matlab Experiment: Sample CDF and pdf Assume we have N samples of a random variable X with CDF Fx(x) and pdf fx (x), and assume that the CDF (or pdf) is unknown to us. In many cases of random signal analysis, one wishes to discover, or estimate the CDF (or pdf). The problem is sometimes called density estimation. One method for density estimation is to find a sample pdf or sample CDF based on the N measurements A simple choice for a sample pdf is given by 1 That is, the sample pdf is a set of N impulses, each with area The corresponding sample CDF is a staircase function located at the N data samples (1) Use Matlab to generate N = 1000 samples of a N(0, 1) random variable X (use the com- mand randn) (2) Plot the sample CDF overlayed with the true CDF of the Gaussian overlayed. See the command hold to help overlay plots. The sampled CDF can be generated as follows xs-sort (x); sample_cdf (1/N:1/N:1)'; plot (xs, sample-cdf); (3) A sample pdf can be obtained using the hist command k-20; % number of bins [aa , bb]= hist (x , k); delx-bb (2)-bb(1) stem(bb,aa/N/delx, 'r'); Use help hist to understand what aa and bb are. Explain why the scaling 1/N/delx is needed. Note that the plot for the sample pdf depends on the number of frequency bins chosen. Explain qualitatively what happens when the number of bins is changed (4) Let's say we are given a set of N samples of a r.v, but we dont know its CDF (or pdf). One test would be to overlay a sample CDF or pdf with a hypothesized true CDF or pdf and compare. Test this as follows: Generate N 1000 samples of a N(0,1) random variable X. Compare the sample CDF and pdf fits to both a N(0,1) and N(0, (1.1)2) r.v. Comment on whether CDF or pdf comparison is easier to distinguish between the two r.v. models, and

Explanation / Answer

(1)

n = 1000;

U = rand(1; n);

toss = (U < 0:5);

a = zeros(n + 1);

avg = zeros(n);

for i = 2 : n + 1

a(i) = a(i - 1) + toss(i - 1);

avg(i - 1) = a(i)=(i - 1);

end

plot(avg)

=====================================================================

(2)

function DicePlot ( throw_num, die_num )

throw_num=5000

die_num= 10

throws = rand ( throw_num, die_num );

throws = ceil ( 6 * throws );

for i = die_num : die_num*6

    j = find ( score == i );

    y(i-die_num+1) = length ( j ) / throw_num;

end

bar ( x, y )

xlabel ( 'Score' )

ylabel ( 'Estimated Probability' )

score_ave = sum ( score(1:throw_num) ) / throw_num;

score_var = var ( score );

return

end

======================================================

(3)

In most situations, scaling is really not all that important. The overall shape of the spectrum matters much more than the absolute scale.

But if you really are worried about it, there are several different conventions from which you can choose (see definitions below):

================================================

(4)

%Generate and plotsome random data
r=randn(100,1);
hist(r,20)

%Make the plot look prettier (gray faces)
h=findobj(gca,'type','patch')
set(h,'FaceColor',[1,1,1]*0.5)

%Now fit a Gaussian
pd=fitdist(r(:),'normal')

pd =
NormalDistribution

Normal distribution
       mu = 0.0700439   [-0.111376, 0.251463]
    sigma = 0.914313   [0.802773, 1.06213]

%pd is a model object that contains the parameters of the fit.
%We now use the model to calculate the values for the Gaussian
%for our data.

x=-3:0.1:3;
PDF=pdf(pd,x); %PDF is a vector of y values: it's our fit

%Now overlay the fit
PDF=PDF/max(PDF); %normalise
%scale to y axis
y=ylim;
PDF=PDF*y(2);

hold on
plot(x,PDF,'r-','LineWidth',2)
hold off