spectrademoapp.cs
来自「是“MATLAB混合编程与工程应用”一书的源码」· CS 代码 · 共 104 行
CS
104 行
// *******************************************************************************//// SpectraDemoApp.cs//// This file is an example application for the MATLAB Builder for .NET product.//// Copyright 2001-2005 The MathWorks, Inc.//// *******************************************************************************using System;using MathWorks.MATLAB.NET.Utility;using MathWorks.MATLAB.NET.Arrays;using SpectraDemoComp;namespace MathWorks.Demo.SpectraDemoApp{ /// <summary> /// This application computes and plots the power spectral density of an input signal. /// </summary> class SpectraDemoApp { #region MAIN /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { try { const double interval= 0.01; // The sampling interval const int numSamples= 1001; // The number of samples // Construct input data as sin(2*PI*15*t) + (sin(2*PI*40*t) plus a // random signal. Duration= 10; Sampling interval= 0.01 MWNumericArray data= new MWNumericArray(MWArrayComplexity.Real, MWNumericType.Double, numSamples); Random random= new Random(); // Initialize data for (int idx= 1; idx <= numSamples; idx++) { double t= (idx-1)* interval; data[idx]= Math.Sin(2.0*Math.PI*15.0*t) + Math.Sin(2.0*Math.PI*40.0*t) + random.NextDouble(); } // Create a new signal analyzer object SignalAnalyzer signalAnalyzer= new SignalAnalyzer(); // Compute the fft and power spectral density for the data array MWArray[] argsOut= signalAnalyzer.computefft(3, data, interval); // Print the first twenty elements of each result array int numElements= 20; MWNumericArray resultArray= new MWNumericArray(MWArrayComplexity.Complex, MWNumericType.Double, numElements); for (int idx= 1; idx <= numElements; idx++) { resultArray[idx]= ((MWNumericArray)argsOut[0])[idx]; } Console.WriteLine("FFT:\n{0}\n", resultArray); for (int idx= 1; idx <= numElements; idx++) { resultArray[idx]= ((MWNumericArray)argsOut[1])[idx]; } Console.WriteLine("Frequency:\n{0}\n", resultArray); for (int idx= 1; idx <= numElements; idx++) { resultArray[idx]= ((MWNumericArray)argsOut[2])[idx]; } Console.WriteLine("Power Spectral Density:\n{0}", resultArray); // Create a new plotter object Plotter plotter= new Plotter(); // Plot the fft and power spectral density for the data array plotter.plotfft(argsOut[0], argsOut[1], argsOut[2]); Console.ReadLine(); // Wait for user to exit application } catch(Exception exception) { Console.WriteLine("Error: {0}", exception); } } #endregion }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?