% Problem 3

close all
clear all

% Load variables
load variables.mat

% Define parameters
L = length(h);

% Create the Toeplitz matrix needed for both estimators
first_column = [x, zeros(1, L-1)];
first_row = [x(1), zeros(1, L-1)];
C = toeplitz(first_column, first_row);

% LS estimator
h_LS = (C'*C)\(C'*y.');
% Or this way, since \ returns the LS solution anyway
% h_LS = C\y.';

% MMSE estimator
% Compute the covariance matrices
Khy = K_h*C';
Ky = C*K_h*C' + sigma^2*eye(length(y));

% MMSE estimate
h_MMSE = Khy*(Ky\y(:));

% Plot to check the estimates
figure;
plot(abs(h_LS),'-ob'); grid on; hold on;
plot(abs(h_MMSE),'-*r');
plot(abs(h), '--dk'); 
title('Channel impulse response');
xlabel('Sample index'); ylabel('Magnitude');
legend('LS estimation', 'MMSE estimation', 'Actual channel');
