%% Generate GPS Position Measurements From Stationary Input
% Create a gpsSensor System object™ to model GPS receiver data. Assume a typical one Hz sample rate and a 1000-second simulation time. Define the reference location in terms of latitude, longitude, and altitude (LLA) of Natick, MA (USA). Define the sensor as stationary by specifying the true position and velocity with zeros.

fs = 1;
duration = 1000;
numSamples = duration*fs;

refLoc = [42.2825 -71.343 53.0352];

truePosition = zeros(numSamples,3);
trueVelocity = zeros(numSamples,3);

gps = gpsSensor('SampleRate',fs,'ReferenceLocation',refLoc);

%
% Call gps with the specified truePosition and trueVelocity to simulate receiving GPS data for a stationary platform.

position = gps(truePosition,trueVelocity);

%
% Plot the true position and the GPS sensor readings for position.

t = (0:(numSamples-1))/fs;

subplot(3, 1, 1)
plot(t, position(:,1), ...
     t, ones(numSamples)*refLoc(1))
title('GPS Sensor Readings')
ylabel('Latitude (degrees)')

subplot(3, 1, 2)
plot(t, position(:,2), ...
     t, ones(numSamples)*refLoc(2))
ylabel('Longitude (degrees)')

subplot(3, 1, 3)
plot(t, position(:,3), ...
     t, ones(numSamples)*refLoc(3))
ylabel('Altitude (m)')
xlabel('Time (s)')

%
% The position readings have noise controlled by HorizontalPositionAccuracy, VerticalPositionAccuracy, VelocityAccuracy, and DecayFactor. The DecayFactor property controls the drift in the noise model. By default, DecayFactor is set to 0.999, which approaches a random walk process. To observe the effect of the DecayFactor property:
% Reset the gps object.
% Set DecayFactor to 0.5.
% Call gps with variables specifying a stationary position.
% Plot the results.
% The GPS position readings now oscillate around the true position.

reset(gps)
gps.DecayFactor = 0.5;
position = gps(truePosition,trueVelocity);

subplot(3, 1, 1)
plot(t, position(:,1), ...
     t, ones(numSamples)*refLoc(1))
title('GPS Sensor Readings - Decay Factor = 0.5')
ylabel('Latitude (degrees)')

subplot(3, 1, 2)
plot(t, position(:,2), ...
     t, ones(numSamples)*refLoc(2))
ylabel('Longitude (degrees)')

subplot(3, 1, 3)
plot(t, position(:,3), ...
     t, ones(numSamples)*refLoc(3))
ylabel('Altitude (m)')
xlabel('Time (s)')

%
% Copyright 2018 The MathWorks, Inc.
%