%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Siddhartha Kasivajhula
% PSYCH 221/ EE 362 (Winter 2007-2008)
% Final Project: A Probabilistic Model of the Visual System
% 
% MakeObservation.m
% REPRESENTATION OF AN OBSERVATION
%
% Here, we model an observation as a noisy set of features
% derived from the set of true features F present in the scene,
% but with the noise implying that features outside this true
% set (but in the known feature space) may also be perceived
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function observation = MakeObservation(scene)

% size of feature space
N = size(scene,2);

% true number of features in scene
N_s = size(scene, 1);

% uniquely represent all possible features as vectors
all_features = eye(N);

% Let's define a noise parameter as the standard deviation of the normal
% distribution centered at the correct number of features present in 
% the scene. So, for example, if noise parameter is 0.7 and we have 5
% features present in the scene, then we will observe a normal random
% number of features with mean 5 and s.d. 0.7 (the number of features will
% be rounded).
noise = N_s*0.1;


% number of observed features (random, centered at true number of features
% in scene
N_o = round(random('norm', N_s, noise, 1, 1));


observation = [];

% Our observation is a sample from the true set of features in the scene.
% The method I use is similar to a method called "Roulette sampling". I
% assign a weight of 0.9 to features present in the scene, and a weight of
% 0.09 to all other features in the feature space
weight = 0;
for i=1:N
    inS = 0;
    for j=1:N_s
        if(all_features(i,:) == scene(j,:))
            inS = 1;
            weight = weight + 0.9;
            if(weight > 1)
                observation = [observation; all_features(i,:)];
                weight = weight - 1;
            end
        end
    end
    if(~inS)
        weight = weight + 0.09;
        if(weight > 1)
            observation = [observation; all_features(i,:)];
            weight = weight - 1;
        end
    end
    % if we have reached our maximum allowed number of samples (determined
    % by the random variable N_o), then break out of loop
    % There are better ways of doing this.
    if(size(observation, 1) > N_o) break;
    end
end
