%% Student ID: 792226
%%% Student Name: F M Anim Hossain %%%
%% Lab 3 Tasks Start Here
%% Part 1
close all;
clear all;
clc;
fs=200; %Sampling rate
delta1=1; %Path 1 delay
delta2=2.5; %Path 2 delay
a=2;
b=1;
%% Task 1: Sine Burst
t=0:1/fs:0.5; %Define Sampling Period & Signal Duration
s=sin(2*pi*10*t); %0.5s Sine Burst
N=4*fs; % ensure 4s long signal
s1=[zeros(1,delta1*fs) a*s]; %Signal with delta1 delay
s1=[s1 zeros(1,N-length(s1))];
s2=[zeros(1,delta2*fs) b*s]; %Signal with delta2 delay
s2=[s2 zeros(1,N-length(s2))];
s3=s1+s2; %Adding the Signals
l=[0:length(s3)-1]/fs;
figure(1)
plot(l,s3)
title('Noiseless Signal')
xlabel('Time(s)')
ylabel('x(t)')
%% Task 2: Autocorrelation of Sine Burst
maxlags=2.5*fs; %Setting the Maximum Lag
[Rxx,lag]=xcorr(s3,maxlags);
lag=lag/fs;
figure(2)
plot(lag,Rxx);
title('Autocorrelation of Noiseless Signal')
xlabel('Lag(tau)')
ylabel('Rxx')
%% Task 3: Noise Added to Sine Burst
randn('state',0);
noise=a*std(s)*randn(size(s3)); %Define Noise
s4=s3+noise; %Adding Noise
figure(1)
plot(l,s4)
title('Signal with Noise Added')
xlabel('Time(s)')
ylabel('x(t)')
maxlags=2.5*fs; %Define Maximum Lag
[Rxx,lag]=xcorr(s4,s4,maxlags);
lag=lag/fs; %Normalize Lag
figure(2)
plot(lag,Rxx)
title('Autocorrelation of Noisy Signal')
xlabel('Lag(tau)')
ylabel('Rxx')
axis([-2.5 2.5 -400 400])
%% Lab 3 Part 2: Task 6
close all;
clear all;
clc;
fs=200; %Sampling Frequency
delta=2; %Reflected Waveform Delay
a=0.1; %Relative amplitude of the reflected waveform
%% Creating the Signal
N=6*fs;
t=0:1/fs:1; %Define Signal Duration
x=chirp(t,5,1,15); %Create Chirp Signal
figure(4)
plot(t,x)
xlabel('Time(s)')
ylabel('x(t)')
title('Chirp Signal')
h=fliplr(x);
figure(5)
impz(h)
xlabel('tau')
ylabel('h(tau)')
title('Matched Filter Impulse Response')
y=[zeros(1,delta*fs) a*x]; %Delay Signal
y=[y zeros(1,N-length(y))];
randn('state',0)
noise=2*std(a*x)*randn(size(y));
y=y+noise; %Add Noise to the Signal
t1=(0:length(y)-1)/fs; %Define Signal Length
figure(6)
plot(t1,y);
xlabel('Time(s)')
ylabel('y(t)')
title('Received Signal')
%% Direct Method Cross Correlation
maxlags=5*fs; %Define Maximum Lag
[Rxy,lag]=xcorr(y,x,maxlags);
lag=lag/fs;
figure(7)
plot(lag(maxlags+1:end),Rxy(maxlags+1:end))
xlabel('Lag(tau)')
ylabel('Rxy')
title('Cross-Correlation Obtained by Direct Method')
%% Indirect method
output=filter(h,1,y); %Sending the Signal Through the Filter
output=output(1:length(y)); %
figure(8)
plot(t1(1:maxlags),output(1:maxlags))
xlabel('Time(s)')
ylabel('Output(t)')
title('Output of the Matched Filter')
%% Part 3 (Impulse Response Estimation Using Wiener Hopf)
close all;
clear all;
clc;
Fs = 8000; % sampling frequency
fcl = 300; % lower cut-off frequency
fch = 500; % higher cut-off frequency
norder = 2; % filter order
N=50; %Number of samples
[b,a] = butter(norder,[fcl/(Fs/2) fch/(Fs/2)]);
figure(9)
subplot(211)
impz(b,a);
xlim([0 50])
for i=1:1000 %Repeats the process 1000 times
x=randn(1,N); %Generate white noise with N samples
maxlags=50;
y=filter(b,a,x); %Put the signals through the created filter
[Ryx,lags]=xcorr(x,y,maxlags); %Cross correlation of the input and the output
ryx=fliplr(Ryx(1:N)); %Select the needed portion
z(:,i)=ryx'; % Column Vector of All Cross-correlations
end
h_est=(sum(z')/1000)/var(x);
h_est=(h_est/max(h_est));
subplot(212)
stem(lags(1:maxlags),h_est)
xlabel('Lags(tau)')
ylabel('Estimated Impulse Response')
title('Estimated Impulse Response Using System Estimation')
%% End of Lab 3 Tasks
%% Lab 4 Starts Here %%
%% Lab 4 Task 1: LMS Function [Uncomment Before Using]
% function y = lms(x,d,N,mu)
%
% M = max(size(x));
% y=zeros(1,M); %Initialize the output vector
% h=zeros(1,N); %Initialize filter coeeficient
% for i=N:M
% x1 = x(i:-1:i-(N-1));
% y(i)=h*x1';
% e(i)=d(i)-y(i);
% h=h+2*mu*e(i)*x1;
%
% end
%
% end
%% Lab 4 Task 4: Modified LMS Function [Uncomment Before Using]
% function [y,e,c] = lms2(x,d,N,mu)
% M = max(size(x));
% y = zeros(1,M); %Initialize output vector
% e = zeros(1,M); %Initialize error vector
% c = zeros(N,1); %Initialize coeeficient vector
% h = zeros(1,N); % initialise weight vector
% for i = N:M;
% x1 = x(i:-1:i-(N-1)); %Negative increment for x from N
% y(i) = h * x1';
% e(i) = d(i) - y(i);
% h = h +2* mu * e(i) * x1;
% c = h(1,:);
% end
%% Task 2: Testing LMS Function with N=1
clc;
clear all;
close all;
N=1; %Number of Adaptive Filter Coefficients
x=2*ones(1,999); %Input Signal
d=ones(1,999); %Desired Signal
%% Step Size Very Slow
mu=0.0001; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with Very Slow Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size Medium
mu=0.001; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with Medium Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size Very Fast
mu=0.01; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with Very Fast Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size 0.5
mu=0.5; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with mu=0.05')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Task 3: With N=2
clc;
clear all;
close all;
N=2;
x=2*ones(1,999);
d=ones(1,999);
%% Step Size Very Slow
mu=0.0001; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=2 and Very Slow Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size Medium
mu=0.001; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=2 Medium Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size Very Fast
mu=0.01;
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=2 Very Fast Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size 0.5
mu=0.5; %Step Size
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=2 and mu=0.5')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Task 3: With N=10
clc;
clear all;
close all;
N=10;
x=2*ones(1,999);
d=ones(1,999);
%% Step Size Very Slow
mu=0.0001;
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=10 and Very Slow Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size Medium
mu=0.001;
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=10 and Medium Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size Very Fast
mu=0.01;
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=10 and Very Fast Step Size')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Step Size 0.5
mu=0.5;
y=lms(x,d,N,mu);
plot([y(:),d(:),x(:)])
title('Adaptive Filter Performance with N=10 and mu=0.5')
xlabel('Iterations')
legend('actual output', 'desired output', 'input')
%% Task 5: Learning Curve with N=1 and Variable Step Size
clc;
clear all;
close all;
N=1;
x=2*ones(1,999);
d=ones(1,999);
%% With Very Slow Step Size
mu=0.0001; %Step Size
[y,e,c]=lms2(x,d,N,mu);
J=e.^2; %MSE
plot(J(:))
title('Learning Curve with Slow Step Size')
xlabel('Iterations')
%% With Medium Step Size
mu=0.001; %Step Size
[y,e,c]=lms2(x,d,N,mu);
J=e.^2; %MSE
plot(J(:))
title('Learning Curve with Medium Step Size')
xlabel('Iterations')
%% With Fast Step Size
mu=0.01;
[y,e,c]=lms2(x,d,N,mu);
J=e.^2;
plot(J(:))
title('Learning Curve with Fast Step Size')
xlabel('Iterations')
%% Task 6: Signal With Noise Added
clc;
clear all;
close all;
x=2*ones(1,999);
d=ones(1,999);
A=0.1;
mu=0