0% found this document useful (0 votes)
41 views

Lab5 Solutions

This document contains code to generate and analyze random signals. It generates random signals with random phases, plots the signals and histograms, and calculates statistics like the mean and variance. It also loads sensor signals, calculates autocorrelation and cross-correlation functions, and analyzes the normality of the distributions. Finally, it builds an identification model with noise, simulates input and output signals, and analyzes the correlation between the signals.

Uploaded by

kcyh8gnszv
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

Lab5 Solutions

This document contains code to generate and analyze random signals. It generates random signals with random phases, plots the signals and histograms, and calculates statistics like the mean and variance. It also loads sensor signals, calculates autocorrelation and cross-correlation functions, and analyzes the normality of the distributions. Finally, it builds an identification model with noise, simulates input and output signals, and analyzes the correlation between the signals.

Uploaded by

kcyh8gnszv
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

clc;

clear;
close all;

%% Task 1

f = 1/3; % 1.a) Initiating parameters


w = 2*pi*f; % 1.a) Initiating parameters
t = 0:0.1:10; % 1.b) Initiating parameters
A = 1; % 1) Initiating parameters

for i = 1:8
N = rand(1, 1); % 2.a) Generating random signal realizations
th = 2*pi*N; % 2.b)) Generating random signal realizations
x = A*cos(w*t+th); % 2.c) Generating random signal realizations
plot(t, x); grid on; hold all; % 2.d) Plotting the random signal
realizations
end

title(['Realizations of the process x(t)' char(10)...


'(Example 1: Random Phase Constant Envelope Cosine)'])

%% Task 2

t1 = 0.5; %a) Initiating the variables


N = rand(1, 10000); %b) Initiating uniformly distributed random numbers
x1 = A*cos(w*t1+2*pi*N); %c) Generating random signal x(t1)

figure
hist (x1, 100); grid on; %d) Generating histogram for the signal

[N1, X1] = hist(x1, 100); %e) Defining the number of elements in each bin (N)
and the positions of bin centers (X)

figure
bar(X1, N1/10000); grid on; hold on; %f) Normalized histogram result

delta1 = X1(2)-X1(1); %g) Width of each

pdf1 = inline('1/pi./sqrt(1-x.^2)'); %h) Analytical result for 1st order pdf


of random signal

plot(X1, pdf1(X1)*delta1, 'r', 'linewidth', 3); %i) Plotting the analytical


result

t2 = 1.92; %j) Repeating the same test for another time constant
(t2)
x2 = A*cos(w*t2+2*pi*N); % Generating random signal x(t1)

figure
hist (x2, 100); grid on; % Generating histogram for the signal

[N2, X2] = hist(x2, 100); % Defining the number of elements in each bin (N) and
the positions of bin centers (X)

figure
bar(X2, N2/10000); grid on; hold on; % Normalized histogram result

delta2 = X2(2)-X2(1); %Width of each

pdf2 = inline('1/pi./sqrt(1-x.^2)'); %h) Analytical result for 1st order pdf


of random signal

plot(X2, pdf2(X2)*delta2, 'r', 'linewidth', 3); %i) Plotting the analytical


result

%% Task 4

x_mean = mean(x); % mean


x_var = var(x); % variance

[x_ACVF, lags1] = xcov(x); % Autocovariance Functions (ACVF)


figure
plot(lags1, x_ACVF); grid on;

[x_ACF, lags2] = xcorr(x, 'coeff'); % Autocorrelation Function (ACF)


figure
plot(lags2, x_ACF); grid on;

%% Task 5

load noisysignals s1 s2; % a) Load sensor signals

[s1_ACVF,lag1_s1] = xcov(s1, 1000); % b) ACVF of s1


[s1_ACF,lag2_s1] = xcorr(s1, 1000, 'coeff'); % b) ACF of s1

[s2_ACVF,lag1_s2] = xcov(s2, 1000); % c) ACVF of s2


[s2_ACF,lag2_s2] = xcorr(s2, 1000, 'coeff'); % c) ACF of s2

[s1_s2_CCVF,lagVx] = xcov(s1, s2, 1000); % d) CCVF of s1 and s2


[s1_s2_CCF,lagCx] = crosscorr(s2, s1, 1000); % d) CCF of s1 and s2

[X, I] = max(abs(s1_s2_CCVF)); %e) lag difference


lagDiff = lagVx(I); %e) lag difference

figure
plot(lag1_s1, s1_ACVF); title('Auto-covariance function of s1'); grid on; xlim([-
size(lag1_s1, 2)/2 size(lag1_s1, 2)/2]);

figure
plot(lag2_s1, s1_ACF); title('Auto-correlation function of s1'); grid on;

figure
plot(lag1_s2, s2_ACVF); title('Auto-covariance function of s2'); grid on; xlim([-
size(lag1_s2, 2)/2 size(lag1_s2, 2)/2]);

figure
plot(lag2_s2, s2_ACF); title('Auto-correlation function of s2'); grid on;

figure
plot(lagVx, s1_s2_CCVF); title('Cross-covariance between s1 and s2'); grid on;

figure
plot(lagCx, s1_s2_CCF); title('Cross-correlation between s1 and s2'); grid on;
% g) Testing the normal distribution of the signals
figure
histfit(s1);

figure
histfit(s2);

figure
normplot(s1);

figure
normplot(s2);

%% Task 6

m = idpoly([1 -0.7], [0 2], 1, 1, 1, 'Noisevariance', 1); %a) IDPOLY model


uk = idinput(2046, 'prbs', [0 0.4], [-1 1]); %b) PRBS input signal

m.NoiseVariance = 0;
ykstar = sim(m, uk); %c) Noise-free output
signal
m.Noisevariance = 0.9*var(ykstar)/10; %d) Changing thee noise
variance of the model
yk = sim(m, uk, simOptions('AddNoise', true)); %e) Noisy output
e = yk-ykstar; %f) Noise(error) signal

%g) Plotting the signals

figure
subplot(411); plot(uk(1:500)); title('Input signal');
subplot(412); plot(ykstar(1:500)); title('Noise-free output signal');
subplot(413); plot(yk(1:500)); title('Noisy output signal');
subplot(414); plot(e(1:500));title('Error signal');

% h) Cross-correlation function between input and noisy output


figure
[yu_CCVF,lag_yu] = xcorr(yk, uk, 500);
y_ACVF0 = xcorr(yk, 0);
u_ACVF0 = xcorr(uk, 0);
stem(lag_yu, yu_CCVF/(sqrt(y_ACVF0*u_ACVF0))); grid on;
title('Correlation between y and u');

% i) Cross-correlation function between noise and noisy output


figure
[ye_CCVF,lag_ye] = crosscorr(yk, e, 500);
stem(lag_ye, ye_CCVF); grid on;
title('Correlation between y and e');

% j) Cross-correlation function between noise and noise-free output


figure
[yse_CCF,lag_yse] = crosscorr(ykstar, e, 500);
stem(lag_yse, yse_CCF); grid on;
title('Correlation between ys and e');

% k) Auto-correlation function between the values of noise signal


figure
[e_ACVF,lag_e] = xcorr(e, 500);
e_ACVF0 = xcorr(e, 0);
stem(lag_e, e_ACVF/e_ACVF0); grid on;
title('Autocorrelation between the values of e');

You might also like