Main Content

smooth

Smooth trajectory data

Since R2024b

Description

smooth(trajectory) smooths the trajectory data in the input Trajectory object trajectory using a Savitzky-Golay filter.

smooth(trajectory,Name=Value) specifies options using one or more name-value arguments. For example, Method="movmedian" smooths the trajectory using the moving median filter.

example

smoothedTrajectory = smooth(___) returns a smoothed trajectory object smoothedTrajectory using any combination of input arguments from previous syntaxes.

Note

This function requires the Scenario Builder for Automated Driving Toolbox™ support package. You can install the Scenario Builder for Automated Driving Toolbox support package from the Add-On Explorer. For more information about installing add-ons, see Get and Manage Add-Ons.

example

Examples

collapse all

Load recorded GPS data into the workspace.

load("recordedGPSData.mat","X","Y","Z","timestamps")

Create a trajectory object by using the loaded timestamps and xyz-coordinates.

traj = scenariobuilder.Trajectory(timestamps,X,Y,Z);

Plot the trajectory object.

plot(traj)
title("GPS Trajectory")

Smooth the trajectory in the trajectory object.

smoothedTrajectory1 = smooth(traj)
smoothedTrajectory1 = 
  Trajectory with properties:

               Name: ''

         NumSamples: 392
           Duration: 19.5498
         SampleRate: 20.0513
         SampleTime: 0.0500
         Timestamps: [392×1 double]

           Position: [392×3 double]
        Orientation: [392×3 double]
           Velocity: [392×3 double]
             Course: [392×1 double]
        GroundSpeed: [392×1 double]
       Acceleration: [392×3 double]
    AngularVelocity: [392×3 double]

        LocalOrigin: [0 0 0]
         TimeOrigin: 0

         Attributes: []

Plot the smoothed trajectory.

plot(smoothedTrajectory1)
title("Smoothed Trajectory Using Savitzky-Golay Filter")

Smooth the trajectory using the moving average filter.

smooth(traj,Method="movmean")

Plot the trajectory smoothed by the moving average filter.

plot(traj)
title("Smoothed Trajectory Using Moving Average Filter")

Input Arguments

collapse all

Trajectory with timestamps and waypoints, specified as a Trajectory object.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: smooth(trajectory,Method="movmedian") smooths the trajectory using the moving median filter.

Smoothing method, specified as one of these options:

  • "sgolay" — Savitzky-Golay filter, which smooths according to a quadratic polynomial fitted over each window. This method can be more effective than other methods when the data varies rapidly.

  • "movmean" — Moving average filter, useful for reducing periodic trends in data.

  • "movmedian" — Moving median filter, useful for reducing periodic trends in data when the data contains outliers.

  • "gaussian" — Gaussian-weighted average filter.

  • "lowess" — Window-based linear regression filter. This method can be computationally expensive, but results in fewer discontinuities than other methods.

  • "loess" — Window-based quadratic regression filter. This method is slightly more computationally expensive than "lowess".

  • "rlowess" — Window based robust linear regression filter. This method is a more computationally expensive version of the "lowess" method, but it is more robust to outliers.

  • "rloess" — Window-based robust quadratic regression filter. This method is a more computationally expensive version of the "loess" method, but it is more robust to outliers.

  • "custom" — Specify a custom filter. When you use "custom", you must specify a custom function by using the SmoothingFcn name-value argument.

Window size factor, specified as a scalar in the range [0, 1]. Generally, the value of SmoothingFactor adjusts the level of smoothing by scaling the window size that the smooth function determines from the entries in the input trajectory object trajectory. Values near 0 produce smaller moving window sizes, resulting in less smoothing. Values near 1 produce larger moving window sizes, resulting in more smoothing. In some cases, depending on the entries that smooth uses to determine the window size, the value of SmoothingFactor might not have a significant impact on the window size.

Savitzky-Golay degree, specified as a nonnegative integer. To use this name-value argument, you must specify Method as "sgolay". The value of Degree corresponds to the degree of the polynomial in the Savitzky-Golay filter that fits the data within each window.

The value of Degree must be less than the window size for uniform sample points. For nonuniform sample points, the value must be less than the maximum number of points in any window.

Custom smoothing function, specified as a function handle.

The function must support this syntax:

smoothedWaypoints = smoothingFunction(waypoints),
where,

  • The waypoints input argument is specified as a real-valued N-by-3 matrix. N is the number of waypoints. Each row represents the x-, y-, and z-coordinates of a waypoint.

  • The smoothedWaypoints output argument is returned as a real-valued N-by-3 matrix. N is the number of waypoints. Each row represents the x-, y-, and z-coordinates of a waypoint.

Note

You must set the Method name-value argument to "custom" to use the SmoothingFcn name-value argument.

Output Arguments

collapse all

Smoothed trajectory, returned as a Trajectory object.

Version History

Introduced in R2024b