%% Perturb imuSensor Parameters
% Create an imuSensor object and show its perturbable properties.

imu = imuSensor;
perturbations(imu)

% Specify the perturbation for the NoiseDensity property of the accelerometer as a uniform distribution.

perturbations(imu,'Accelerometer.NoiseDensity', ...
    'Uniform',1e-5,1e-3);

% Specify the perturbation for the RandomWalk property of the gyroscope as a truncated normal distribution.

 perts = perturbations(imu,'Gyroscope.RandomWalk', ...
    'TruncatedNormal',2,1e-5,0,Inf);

% Load pre-recorded IMU data.

load imuSensorData.mat 
numSamples = size(orientations);

% Simulate the imuSensor three times with different perturbation realizations.

rng(2021); % For repeatable results
numRuns = 3;
colors = ['b' 'r' 'g'];
for idx = 1:numRuns

    % Clone IMU to maintain original values
    imuCopy = clone(imu);

    % Perturb noise values
    offsets = perturb(imuCopy);

    % Obtain the measurements 
    [accelReadings,gyroReadings] = imuCopy(accelerations,angularVelocities,orientations);
    
    % Plot the results
    plot(times,gyroReadings(:,3),colors(idx));
    hold on;
end
xlabel('Time (sec)')
ylabel('Z-Component of Gyro Readings (rad/s)')
legend("First Pass","Second Pass","Third Pass");
hold off

% Copyright 2020 The MathWorks, Inc.