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

what does preconceived mean
sammie Reply
physiological Psychology
Nwosu Reply
How can I develope my cognitive domain
Amanyire Reply
why is communication effective
Dakolo Reply
Communication is effective because it allows individuals to share ideas, thoughts, and information with others.
effective communication can lead to improved outcomes in various settings, including personal relationships, business environments, and educational settings. By communicating effectively, individuals can negotiate effectively, solve problems collaboratively, and work towards common goals.
it starts up serve and return practice/assessments.it helps find voice talking therapy also assessments through relaxed conversation.
miss
Every time someone flushes a toilet in the apartment building, the person begins to jumb back automatically after hearing the flush, before the water temperature changes. Identify the types of learning, if it is classical conditioning identify the NS, UCS, CS and CR. If it is operant conditioning, identify the type of consequence positive reinforcement, negative reinforcement or punishment
Wekolamo Reply
please i need answer
Wekolamo
because it helps many people around the world to understand how to interact with other people and understand them well, for example at work (job).
Manix Reply
Agreed 👍 There are many parts of our brains and behaviors, we really need to get to know. Blessings for everyone and happy Sunday!
ARC
A child is a member of community not society elucidate ?
JESSY Reply
Isn't practices worldwide, be it psychology, be it science. isn't much just a false belief of control over something the mind cannot truly comprehend?
Simon Reply
compare and contrast skinner's perspective on personality development on freud
namakula Reply
Skinner skipped the whole unconscious phenomenon and rather emphasized on classical conditioning
war
explain how nature and nurture affect the development and later the productivity of an individual.
Amesalu Reply
nature is an hereditary factor while nurture is an environmental factor which constitute an individual personality. so if an individual's parent has a deviant behavior and was also brought up in an deviant environment, observation of the behavior and the inborn trait we make the individual deviant.
Samuel
I am taking this course because I am hoping that I could somehow learn more about my chosen field of interest and due to the fact that being a PsyD really ignites my passion as an individual the more I hope to learn about developing and literally explore the complexity of my critical thinking skills
Zyryn Reply
good👍
Jonathan
and having a good philosophy of the world is like a sandwich and a peanut butter 👍
Jonathan
generally amnesi how long yrs memory loss
Kelu Reply
interpersonal relationships
Abdulfatai Reply
What would be the best educational aid(s) for gifted kids/savants?
Heidi Reply
treat them normal, if they want help then give them. that will make everyone happy
Saurabh
What are the treatment for autism?
Magret Reply
hello. autism is a umbrella term. autistic kids have different disorder overlapping. for example. a kid may show symptoms of ADHD and also learning disabilities. before treatment please make sure the kid doesn't have physical disabilities like hearing..vision..speech problem. sometimes these
Jharna
continue.. sometimes due to these physical problems..the diagnosis may be misdiagnosed. treatment for autism. well it depends on the severity. since autistic kids have problems in communicating and adopting to the environment.. it's best to expose the child in situations where the child
Jharna
child interact with other kids under doc supervision. play therapy. speech therapy. Engaging in different activities that activate most parts of the brain.. like drawing..painting. matching color board game. string and beads game. the more you interact with the child the more effective
Jharna
results you'll get.. please consult a therapist to know what suits best on your child. and last as a parent. I know sometimes it's overwhelming to guide a special kid. but trust the process and be strong and patient as a parent.
Jharna
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