<< Chapter < Page
  Digital signal processing - dsp     Page 24 / 25
Chapter >> Page >

I also provided two different sample classes that implement the interface for you to use as models as you come up to speed in defining your own classes.

Complete program listings

Complete listings of the programs discussed in this module are shown below.

Listing 37. Graph01Demo.java.
/* File Graph01Demo.java Copyright 2002, R.G.BaldwinThis class is used to demonstrate how to write data-generator classes thatwill operate successfully with the program named Graph01.Tested using JDK 1.8 under Win 7. **************************************/class Graph01Demo implements GraphIntfc01{public int getNmbr(){ //Return number of functions to// process. Must not exceed 5. return 5;}//end getNmbr public double f1(double x){//This is a simple x-squared // function with a negative// sign. return -(x*x)/200.0;}//end f1 public double f2(double x){//This is a simple x-cubed // functionreturn -(x*x*x)/200.0; }//end f2public double f3(double x){//This is a simple cosine // functionreturn 100*Math.cos(x/10.0); }//end f3public double f4(double x){ //This is a simple sine// function return 100*Math.sin(x/20.0);}//end f4 public double f5(double x){//This is function which // returns the product of// the above sine and cosines. return 100*(Math.sin(x/20.0)*Math.cos(x/10.0)); }//end f5}//end sample class Graph01Demo
Listing 38. Dsp002.java,
/* File Dsp002.java Copyright 2002, R.G.BaldwinNote: This program requires access to the interface named GraphIntfc01.This is a sample DSP program whose output is designed to be plottedby the programs named Graph01 and Graph02. This requires that theclass implement GraphIntfc01. It also requires a noarg constructor.This program applies a narrow-band convolution filter to white noise, andthen computes the amplitude spectrum of the filtered result using a simpleDiscrete Fourier Transform (DFT) algorithm. The spectrum of the whitenoise is also computed. The program convolves a 33-pointsinusoidal convolution filter with wide-band noise, and then computes theamplitude spectrum of the raw data and the filtered result. The processingoccurs when an object of the class is instantiated.The input noise, the filter, the filtered output, and the two spectraare deposited in five arrays for later retrieval and display.The input noise, the filter, the filtered output, the spectrum of thenoise, and the spectrum of the filtered result are returned by the methodsnamed f1, f2, f3, f4, and f5 respectively.The output values that are returned are scaled for appropriate display inthe plotting areas provided by the program named Graph01.Tested using JDK 1.8 under Win 7. **************************************/import java.util.*; class Dsp002 implements GraphIntfc01{//Establish data and spectrum // lengths.int operatorLen = 33; int dataLen = 256+operatorLen;int outputLen = dataLen - operatorLen;int spectrumPts = outputLen; //Create arrays for the data and// the results. double[]data = new double[dataLen];double[] operator =new double[operatorLen];double[] output =new double[outputLen];double[] spectrumA =new double[spectrumPts];double[] spectrumB =new double[spectrumPts];public Dsp002(){//constructor //Generate and save some wide-band// random noise. Seed with a // different value each time the// object is constructed. Random generator = new Random(new Date().getTime()); for(int cnt=0;cnt<data.length; cnt++){//Get data, scale it, remove the // dc offset, and save it.data[cnt] = 100*generator.nextDouble()-50; }//end for loop//Create a convolution operator and // save it in the array.for(int cnt = 0; cnt<operatorLen; cnt++){//Note, the value of the // denominator in the argument// to the cos method specifies // the frequency relative to the// sampling frequency. operator[cnt]= Math.cos( cnt*2*Math.PI/4);}//end for loop //Apply the operator to the dataConvolve01.convolve(data,dataLen, operator,operatorLen,output);//Compute DFT of the raw data and // save it in spectrumA array.Dft01.dft(data,spectrumPts, spectrumA);//Compute DFT of the filtered data // and save it in spectrumB array.Dft01.dft(output,spectrumPts, spectrumB);//All of the data has now been // produced and saved. It may be// retrieved by invoking the // following methods named f1// through f5. }//end constructor//---------------------------------// //The following six methods are// required by the interface named // GraphIntfc01.public int getNmbr(){ //Return number of functions to// process. Must not exceed 5. return 5;}//end getNmbr //---------------------------------//public double f1(double x){ int index = (int)Math.round(x);//This version of this method // returns the random noise data.// Be careful to stay within the // array bounds.if(index<0 || index>data.length-1){ return 0;}else{ return data[index]; }//end else}//end f1 //---------------------------------//public double f2(double x){ //Return the convolution operatorint index = (int)Math.round(x); if(index<0 || index>operator.length-1){ return 0;}else{ //Scale for good visibility in// the plot return operator[index]* 50; }//end else}//end f2 //---------------------------------//public double f3(double x){ //Return filtered outputint index = (int)Math.round(x); if(index<0 || index>output.length-1){ return 0;}else{ //Scale to approx same p-p as// input data return output[index]/6; }//end else}//end f3 //---------------------------------//public double f4(double x){ //Return spectrum of raw dataint index = (int)Math.round(x); if(index<0 || index>spectrumA.length-1){ return 0;}else{ //Scale for good visibility in// the plot. return spectrumA[index]/10; }//end else}//end f4 //---------------------------------//public double f5(double x){ //Return the spectrum of the// filtered data. int index = (int)Math.round(x);if(index<0 || index>spectrumB.length-1){ return 0;}else{ //Scale for good visibility in// the plot. return spectrumB[index]/100; }//end else}//end f5 }//end sample class Dsp002//===================================// //This class provides a static method// named convolve, which applies an // incoming convolution operator to// an incoming set of data and deposits // the filtered data in an output// array whose reference is received // as an incoming parameter.//This class could easily be broken out // and put in a library as a stand-// alone class, or the convolve method // could be added to a class containing// a variety of DSP methods. class Convolve01{public static void convolve( double[]data, int dataLen,double[] operator,int operatorLen, double[]output){ //Apply the operator to the data,// dealing with the index // reversal required by// convolution. for(int i=0;i<dataLen-operatorLen;i++){ output[i]= 0; for(int j=operatorLen-1;j>=0; j--){output[i] +=data[i+j]*operator[j]; }//end inner loop}//end outer loop }//end convolve method}//end Class Convolve01 //===================================////This class provides a static method // named dft, which computes and// returns the amplitude spectrum of // an incoming time series. The// amplitude spectrum is computed as // the square root of the sum of the// squares of the real and imaginary // parts.//Returns a number of points in the // frequency domain equal to the number// of samples in the incoming time // series. Deposits the frequency// data in an array whose reference is // received as an incoming parameter.//This class could easily be broken out // and put in a library as a stand-// alone class, or the dft method //could be added to a class containing // a variety of DSP methods.class Dft01{ public static void dft(double[] data,int dataLen, double[]spectrum){ //Set the frequency increment to// the reciprocal of the data // length. This is convenience// only, and is not a requirement // of the DFT algorithm.double delF = 1.0/dataLen; //Outer loop iterates on frequency// values. for(int i=0; i<dataLen;i++){ double freq = i*delF;double real = 0; double imag = 0;//Inner loop iterates on time- // series points.for(int j=0; j<dataLen; j++){ real += data[j]*Math.cos( 2*Math.PI*freq*j);imag += data[j]*Math.sin(2*Math.PI*freq*j); spectrum[i]= Math.sqrt( real*real + imag*imag);}//end inner loop }//end outer loop}//end dft }//end Dft01//===================================//

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Digital signal processing - dsp. OpenStax CNX. Jan 06, 2016 Download for free at https://legacy.cnx.org/content/col11642/1.38
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Digital signal processing - dsp' conversation and receive update notifications?

Ask