% A simple Matlab demonstration of detection of harmonic signal in additive
% noise. "An input signal" is additive mixture of randomly shifted segment
% of harmonic signal and Gaussian white noise.
% The detection is based "correlation" principle that uses a-priori information
% about parameters of harmonic signal.
% Main goal of this example is demonstartion of power and simplicity of Matlabb
% environment. Nevertheless, presented principle of signal detection has many
% practical applications in electronic and communication equipments.
% The example uses only basic information from the subject Signals and Systems.
%
% PEEC, M.D. 2007-10-08, v.1.01

fvz = 10000;       % sampling frequency
fsig = 200;        % fequency of "useful" harmonic signal
Asig = 1.0;        % amplitude of "uzitocneho" harmonic signal
Asum = 2.0;        % a parameter determines noise "amplitude" (!!! it is not amplitude)

N = 1024;          % length of the record (number of samples) to be analyzed
T = 100;           % length (number of samples) of the segment of "useful" signal

% generation and visualization of "useful" signal
sig = Asig*sin(2*pi*fsig*(0:T-1)/fvz);  % generation

figure(1);         % display vill be generated in separate window no.1
plot(sig);         % display command
title('Useful signal');               % picture description
xlabel('Discrete time');
ylabel('Amplitude');
grid;              % switch on the grid in dispalyed picture

pause;             % stop processing, waiting for Enter

% define of "random" position of "useful" signal within (N sample long) record
pol = round(1+ rand(1)*(N-T));  % start position
in = [ zeros(1,pol) sig ];      % "place" useful signal into the final record (from the pol+1 position)
in(N) = 0;                      % definitition (force to zero) of all resulting record samples

figure(2);         % display vill be generated in separate window no.2
plot(in);          % display command
title('Randomly shifted useful signal');  % picture description
xlabel('Discrete time');
ylabel('Amplitude');
grid;              % switch on the grid in dispalyed picture

pause;

% generation of noisy signal (additive mixture of signal in + Gaussian white noise)
sum = Asum*randn(1,N); % generation of Gaussian white noise
in_sum = in + sum; % additive mixture

figure(3);         % display vill be generated in separate window no.3
plot(in_sum);      % display command
title('Randomly shifted noisy signal');  % picture description
xlabel('Discrete time');
ylabel('Amplitude');
grid;              % switch on the grid in dispalyed picture

pause;

% Computation of CORRELATION between (vectors) sig and in_sum. The computation is
% performed very inefficiently by using only basic Matlab commands.
% More efficient way of computation will be decribed later during lessons.

out = zeros(1,N-T+1);  % memory allocation, not necessary but can speed up computation
for k=1:(N-T+1)    % for cycle in Matlab
   out(k) = sig*in_sum(k:k+T-1)';   % computation of correlation for shift "k"
end

figure(4);         % display vill be generated in separate window no.2
plot(out);         % display command
title('The output of correlation receiver');  % picture description
xlabel('Shift');
ylabel('Amplitude');
grid;              % switch on the grid in dispalyed picture

[m,odhad] = max(out);  % find a maximum
pol = pol+1;       % real start is shifted by 1 sample
pol                % precise starting position (defined during signals generation)
odhad              % estimation of starting position by processing noisy signal




