Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB Builder NE   

Spectral Analysis Example

Purpose

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

The component SpectraComp analyzes a signal and graphs the result. The class, SignalAnalyzer, 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 class, Plotter, graphs the returned data using the plotfft method. These two methods, computefft and plotfft, encapsulate MATLAB functions.

The computefft method computes the FFT and power spectral density of the input data and computes a vector of frequency points based on the length of the data entered and the sampling interval. The plotfft method plots the FFT data and the power spectral density in a MATLAB figure window. The MATLAB code for these two methods resides in two M-files, computefft.m and plotfft.m, which can be found in:

matlabroot\toolbox\dotnetbuilder\Examples\VS8\NET\SpectraExample\SpectraComp

 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 the MATLAB product to your work folder:

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

  2. 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\SpectraComp.

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

    deploytool

  4. Build the .NET component. See the instructions in Building Your Component for more details. Use the following information:

    Project NameSpectraComp
    Class NamesPlotterSignalAnalyzer
    Files to compilecomputefft.mplotfft.m

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

    The sample application for this example is in SpectraExample\SpectraCSApp\SpectraApp.cs.

    The program listing is shown here.

     SpectraApp.cs

    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

    • Instantiates a SignalAnalyzer object

    • Calls the computefft method, which computes the FFT, frequency, and the spectral density

    • Instantiates a Plotter object

    • Calls the plotfft method, which plots the data

    • Uses a try/catch block to handle exceptions

    The following statement

    MWNumericArray data= new MWNumericArray(MWArrayComplexity.Real, 
    MWNumericType.Double, numSamples);

    shows how to use the MWArray class library to construct a MWNumericArray that is used as method input to the computefft function.

    The following statement

    SignalAnalyzer signalAnalyzer = new SignalAnalyzer(); 

    creates an instance of the class SignalAnalyzer, and the following statement

    MWArray[] argsOut= signalAnalyzer.computefft(3, data, interval);
    

    calls the method computefft.

  6. Build the SpectraApp application using Visual Studio .NET.

    1. The SpectraCSApp folder contains a Visual Studio .NET project file for this example. Open the project in Visual Studio .NET by double-clicking SpectraCSApp.csproj in Windows Explorer. You can also open it from the MATLAB desktop by right-clicking SpectraCSApp.csproj > Open Outside MATLAB.

    2. Add a reference to the MWArray component, which is matlabroot\toolbox\dotnetbuilder\bin\architecture\framework_version
      \mwarray.dll.

    3. If necessary, add (or fix the location of) a reference to the SpectraComp component which you built in a previous step. (The component, SpectraComp.dll, is in the \SpectraExample\SpectraComp\x86\V2.0\Debug\distrib subfolder of your work area.)

  7. Build and run the application in Visual Studio .NET.

  


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