<< 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//===================================//

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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