Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder JA   

Spectral Analysis Example

Purpose

The purpose of the example is to show you the following:

The component spectralanalysis analyzes a signal and graphs the result. The class, fourier, performs a fast Fourier transform (FFT) on an input data array. A method of this class, computefft, returns the results of that FFT as two output arrays—an array of frequency points and the power spectral density. The second method, plotfft, graphs the returned data. These two methods, computefft and plotfft, encapsulate MATLAB functions.

The MATLAB code for these two methods is in computefft.m and plotfft.m, which is found in matlabroot\toolbox\javabuilder\Examples\SpectraExample\SpectraDemoComp.

 computefft.m

 plotfft.m

Procedure

  1. If you have not already done so, copy the files for this example as follows:

    1. Copy the following folder that ships with MATLAB to your work folder:

      matlabroot\toolbox\javabuilder\Examples\SpectraExample
      
    2. At the MATLAB command prompt, cd to the new SpectraExample subfolder in your work folder.

  2. If you have not already done so, set the environment variables that are required on a development machine. See Settings for Environment Variables (Development Machine).

  3. Write the M-code that you want to access.

    This example uses computefft.m and plotfft.m, which are already in your work folder in SpectraExample\SpectraDemoComp.

  4. While in MATLAB, issue the following command to open the Deployment Tool window:

    deploytool

  5. You create a Java application by using the Deployment Tool GUI to build a Java class that wraps around your M-code.

    To compile or build the Java application using the Deployment Tool, use the following information as you work through this example in Building the Java Component:

    Project Namespectralanalysis
    Class Namefourier
    File to compileplotfft.m

      Note   In this example, the application that uses the fourier class does not need to call computefft directly. The computefft method is required only by the plotfft method. Thus, when creating the component, you do not need to add the computefft function, although doing so does no harm.

  6. Write source code for an application that accesses the component.

    The sample application for this example is in SpectraExample\SpectraDemoJavaApp\powerspect.java.

    The program listing is shown here.

     powerspect.java

    The program does the following:

    • Constructs an input array with values representing a random signal with two sinusoids at 15 and 40 Hz embedded inside of it

    • Creates an MWNumericArray array that contains the data, as shown:

      data = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
      
    • Instantiates a fourier object

    • Calls the plotfft method, which calls computeftt and plots the data

    • Uses a try-catch block to handle exceptions

    • Frees native resources using MWArray methods

  7. Compile the powerspect.java application using javac. When entering this command, ensure there are no spaces between path names in the matlabroot argument. For example, there should be no space between javabuilder.jar; and .\distrib\spectralanalysis.jar in the following example.

    Open a Command Prompt window and cd to the matlabroot\spectralanalysis folder. cd to your work folder. Ensure powerspect.java is in your work folder

    • On Windows, execute the following command:

      javac -classpath
      .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar;
      .\distrib\spectralanalysis.jar powerspect.java
      
    • On UNIX, execute the following command:

      javac -classpath
      .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar:
      ./distrib/spectralanalysis.jar powerspect.java
      

      Note   For matlabroot substitute the MATLAB root folder on your system. Type matlabroot to see this folder name.

  8. Run the application.

    • On Windows, execute the powerspect class file:

      java -classpath
      .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar
      .\distrib\spectralanalysis.jar 
      powerspect
      
    • On UNIX, execute the powerspect class file:

      java -classpath
      .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar:
      ./distrib/spectralanalysis.jar 
      powerspect
      

      Note   The supported JRE version is 1.6.0. To find out what JRE you are using, refer to the output of 'version -java' in MATLAB or refer to the jre.cfg file in matlabroot/sys/java/jre/arch or mcrroot/sys/java/jre/arch.

      Note   If you are running on Mac or Solaris 64-bit platforms, you must add the -d64 flag in the Java command. See Limitations and Restrictions for more specific information.

    The powerspect program should display the output:

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS