Derivative

Description

The Derivative class computes either the first or second order derivative of the input signal. The Derivative class is part of the Preprocessing Modules.

Derivative Example
An example of computing the first and second derivative of a signal (sine wave) filtered using the Derivative class. The red signal is the original signal, the green signal is the first order derivative, and the blue signal is the second order derivative. Note that the three signals have been z-normalized for visualization purposes. DerivativeExampleImage1.jpg

Advantages

The Derivative class is a good preprocessing module to use if you want a quick way of expressing the change in a signal.

Disadvantages

Example Code

/*
 GRT Derivative Example
 This example demonstrates how to create and use the GRT Derivative PreProcessing Module.

 The Derivative module computes either the first or second order derivative of the input signal.

 In this example we create two Derivative instances, one to compute the first derivative of the
 input signal and another to compute the second derivative of a 1-dimensional input signal.

 We then create a sine-wave signal as the input signal to both Derivative instances, the sine signal
 and first and second derivative signals are then saved to a file (so you can plot the results in
 Matlab, Excel, etc. if needed).

 This example shows you how to:
 - Create a new Derivative instance to compute either the first or second derivative
 - Compute the (1st and 2nd) derivative of a sine-wave signal
 - Save the Derivative settings to a file
 - Load the Derivative settings from a file
*/


//Include the GRT header, you may have to change the path of this to match where the GRT is on your system
#include "GRT.h"
using namespace GRT;

int main (int argc, const char * argv[])
{
    //Create a new Derivative instance and set it to compute the first order derivative
    Derivative deriv1(Derivative::FIRST_DERIVATIVE);

    //Create another new Derivative instance and set it to compute the second order derivative
    Derivative deriv2(Derivative::SECOND_DERIVATIVE);

    //Create and open a file to save the data
    fstream file;
    file.open("DerivativeData.txt",fstream::out);

    //Generate some data (sine wave) and compute the derivative of this
    double x = 0;
    const UINT M = 1000;
    Random random;
    for(UINT i=0; i<M; i++){
        double signal = sin( x );

        double deriv1Value = deriv1.computeDerivative( signal );
        double deriv2Value = deriv2.computeDerivative( signal );

        file << signal << "\t" <<deriv1Value << "\t" << deriv2Value << endl;    

        x += TWO_PI/double(M)*10;
    }

    //Close the file
    file.close();

    //Save the derivative settings to a file
    deriv1.saveSettingsToFile("Derivative1Settings.txt");
    deriv2.saveSettingsToFile("Derivative2Settings.txt");

    //We can then load the settings later if needed
    deriv1.loadSettingsFromFile("Derivative1Settings.txt");
    deriv2.loadSettingsFromFile("Derivative2Settings.txt");

    return EXIT_SUCCESS;
}

Code & Resources

DerivativeExample.cpp

Documentation

You can find the documentation for this class at Derivative documentation.