SSP Assignment 2
SSP Assignment 2
Monsoon 2024
Assignment - 2
SUBMITTED BY
SYAMANTAK SARKAR (M230922EC)
M.Tech – Signal Processing
Part I and ii
Theory:
Code:
clc;
clear;
close all;
%Generation of random signal
N = 100;
v = randn(1, N);
x = zeros(1, N);
for n=1:N
if n==1
x(n) = 0.447 *[v(3)+v(2)+v(1)];
elseif n==2
x(n)= 0.447 *[v(4)+v(3)+v(2)+v(1)];
elseif n== 3:N
x(n) = 0.447 *[v(n+2)+v(n+1)+v(n)+v(n-1)+v(n-2)] ;
elseif n==N-1
x(n)= 0.447 *[v(n+1)+v(n)+v(n-1)+v(n-2)];
else
x(n)= 0.447 *[v(n)+v(n-1)+v(n-2)];
end
end
figure;
subplot (2,1,1);
stem(1:N, v, 'LineWidth', 2);
xlabel('n');
ylabel('v(n)');
title(' random noise v(n)');
grid on;
subplot (2,1,2);
stem(1:N, x, 'LineWidth', 2);
xlabel('n');
ylabel('x(n)');
title('Generated random signal x(n)');
x_hat = zeros(N);
A = [1 0.8; 0.8 1];
B = [0.8 ;0.6];
W = inv(A )*B;
w0 = W(1);
w1 = W(2);
for i = 1:1:N
if i==1
x_hat(i) = x(i);
elseif i==2
x_hat(i) = w0*x(i-1);
else
x_hat(i) = w0*x(i-1) + w1*x(i-2);
end
end
figure;
subplot(3,1,1);
stem(1:N,x);
title('Generated Signal');
subplot(3,1,2);
stem(1:N,x_hat);
title('x_h (Estimated Signal)');
e = zeros(N);
for i = 1:1:N
e(i) = x(i) - x_hat(i);
end
subplot(3,1,3);
stem(1:N,e);
title('error signal');
Mx = max(x_hat,[],"all");
del_2 = Mx/2.^3;
quan_dpcm = (del_2.*del_2) /12;
SNR_dpcm = 1/quan_dpcm;
Me = max(e,[],"all");
del_1 = Me/2.^3;
quan_pcm = (del_1.*del_1) /12;
SNR_pcm = 1/quan_pcm;
Results:
Signal to Noise Ratio for PCM
397.2769
end
subplot(3,1,3);
stem(1:N,e);
title('error signal');
Mx_max = max(x_hat,[],"all");
Mx_min = min(x_hat,[],"all");
del_2 = (Mx_max - Mx_min)/2.^4;
quan_dpcm = (del_2.*del_2) /12;
SNR_dpcm = 1/quan_dpcm;
Me_max = max(e,[],"all");
Me_min = min(e,[],"all");
del_1 = (Me_max - Me_min)/2.^4;
quan_pcm = (del_1.*del_1) /12;
SNR_pcm = 1/quan_pcm;
function Rx = autocorrelationMatrix(N)
Rx = zeros(N);
for i = 1:N
for j = i:N
Rx(i, j) = autocorr_func(i - j);
Rx(j, i) = Rx(i, j);
end
end
end
%rx vector creation
function rx = autocorrelationvector(N)
rx = zeros(1:N);
for i = 1:N
rx(i) = autocorr_func(i);
end
end
%function wie = wiener_func(i,j,T_3)
%wie = w(i,i) + T_3 * conj(w(i,j));
function r_x = autocorr_func(k)
r_x = 1 - 0.2*abs(k);
end
Results:
>> exp2_2
w matrix(L=2)
0.8889
-0.1111
P2 = 0.3556
gamma_3 = -0.0444
T_3 = 0.1250
w matrix(L=3)
0.8750
-1.0686e-15
0.1250
P_3 =
0.3500
P3 = 0.3500
gamma_4 = -0.2500
T_4 = 0.7143
w matrix(L=4)
0.9643
-1.8319e-15
0.7500
0.7143
P4 = 0.1714
gamma_5 = -1.0214
T_5 = 5.9583
w matrix(L=5)
5.2202
4.4687
0.7500
6.4598
5.9583
• As we can see the coefficients for the 5 tap weiner estimer is much higher.
• As we are increasing the no of taps from L=2 to L=5 at each of the step the filter coefficeents are
changing. So there is need for recalculation for all the filter coefficients at each step.
• The SNR values are higher than L = 2 case in both PCM and DPCM. This is due to the fact that it is
a memory less correlation function and the the prediction will be more and more accurate as
the number of tape(i.e. the dependencies on the previous inputs) are increased.
Part iv
Theory:
Code:
For White Noise
%White Noise
clc;
clear;
close all;
%Generation of random signal
N = 100;
v = randn(1, N);
x = zeros(1, N);
y = zeros(1, N);
for n=1:N
if n==1
x(n) = 0.447 *[v(3)+v(2)+v(1)];
elseif n==2
x(n)= 0.447 *[v(4)+v(3)+v(2)+v(1)];
elseif n== 3:N
x(n) = 0.447 *[v(n+2)+v(n+1)+v(n)+v(n-1)+v(n-2)] ;
elseif n==N-1
x(n)= 0.447 *[v(n+1)+v(n)+v(n-1)+v(n-2)];
else
x(n)= 0.447 *[v(n)+v(n-1)+v(n-2)];
end
end
figure;
subplot (2,1,1);
stem(1:N, x, 'LineWidth', 2);
xlabel('n');
ylabel('x(n)');
title('Generated random signal x(n)')
% white Noise
u = normrnd(0,sqrt(0.25),N);
for i = 1:1:N
y(i) = x(i)+ u(i);
end
subplot(2,1,2);
stem(1:N,y);
title('Y (x+white noise)');
x_hat = zeros(N);
A = [1.25 0.8; 0.8 1.25];
B = [0.8 ;0.6];
W = inv(A )*B;
w0 = W(1);
w1 = W(2);
for i = 1:1:N
if i==1
x_hat(i) = y(i);
elseif i==2
x_hat(i) = w0*y(i-1);
else
x_hat(i) = w0*y(i-1) + w1*y(i-2);
end
end
figure;
subplot(3,1,1);
stem(1:N,x);
title('Generated Signal');
subplot(3,1,2);
stem(1:N,x_hat);
title('x_h (predicted signal)');
e = zeros(N);
for i = 1:1:N
e(i) = x(i) - x_hat(i);
end
subplot(3,1,3);
stem(1:N,e);
title('error signal');
Mx = max(x_hat,[],"all");
del_2 = Mx/2.^3;
quan_dpcm = (del_2.*del_2) /12;
SNR_dpcm = 1/quan_dpcm;
Me = max(e,[],"all");
del_1 = Me/2.^3;
quan_pcm = (del_1.*del_1) /12;
SNR_pcm = 1/quan_pcm;
x_hat = zeros(N);
A = [1.25 0.925; 0.8 0.925];
B = [0.8 ;0.6];
W = inv(A )*B;
w0 = W(1);
w1 = W(2);
for i = 1:1:N
if i==1
x_hat(i) = y(i);
elseif i==2
x_hat(i) = w0*y(i-1);
else
x_hat(i) = w0*y(i-1) + w1*y(i-2);
end
end
figure;
subplot(3,1,1);
stem(1:N,x);
title('Generated Signal');
subplot(3,1,2);
stem(1:N,x_hat);
title('x_h (predicted signal)');
e = zeros(N);
for i = 1:1:N
e(i) = x(i) - x_hat(i);
end
subplot(3,1,3);
stem(1:N,e);
title('error signal');
Mx = max(x_hat,[],"all");
del_2 = Mx/2.^3;
quan_dpcm = (del_2.*del_2) /12;
SNR_dpcm = 1/quan_dpcm;
Me = max(e,[],"all");
del_1 = Me/2.^3;
quan_pcm = (del_1.*del_1) /12;
SNR_pcm = 1/quan_pcm;
Results:
For White Noise
Signal to Noise Ratio for PCM
230.5587
A comparison table is given below for different DPCM and PCM SNR values.
Prediction using L = Prediction using L = Prediction using L = Prediction using L =
2 Weiner filter 5 Weiner filter 2 Weiner filter 2 Weiner filter
(No noise) (No noise) (White noise) (Colored noise)