<< Chapter < Page
  Digital signal processing - dsp     Page 10 / 27
Chapter >> Page >

Let's see some code!

I will present and explain this class in fragments. A complete listing of the class is provided in Listing 35 near the end of the module.

Beginning of the class named PlotALot01

As mentioned earlier, this class contains a main method. The main method is provided so that the class can be run as an application for self-test purposes, which is common practice in Javaprogramming. The main method also illustrates the proper use of the class.

The beginning of the class and the beginning of the main method are shown in Listing 1 .

Listing 1. Beginning of the class named PlotALot01.
public class PlotALot01{ public static void main(String[]args){ PlotALot01 plotObjectA =new PlotALot01("A",158,237,36,5,4,4); PlotALot01 plotObjectB = new PlotALot01("B");

Instantiate two plotting objects

Listing 1 instantiates two independent plotting objects. The first plotting object, referred to by plotObjectA is instantiated by calling the constructor that accepts plotting parameters. A description of each of theconstructor parameters was provided earlier . You may find it useful to compare the values shown in Listing 1 with the overall plotting parameters listed earlier to confirm how they are related.

The second plotting object, referred to by plotObjectB is instantiated by calling the constructor that accepts only the page title as aparameter and uses default values for all of the plotting parameters. You will see those default values later in the code.

Feed the plotting object titled "A"

Listing 2 contains a for loop that feeds 275 values to the plotting object titled "A". Most of the code in Listing 2 is required to set fifteen specific values to test for propertransitions as described earlier. This code is straightforward and shouldn't require further explanation.

(I was able to determine the correct locations for these values by knowing the size of the Frame, inset values for the Frame, the space betweentraces, the number of pixels dedicated to each sample, etc.)

Listing 2. Feed the plotting object titled "A".
for(int cnt = 0;cnt<275;cnt++){ if(cnt == 147){plotObjectA.feedData(0); }else if(cnt == 148){plotObjectA.feedData(0); }else if(cnt == 149){plotObjectA.feedData(25); }else if(cnt == 150){plotObjectA.feedData(-25); }else if(cnt == 151){plotObjectA.feedData(25); }else if(cnt == 152){plotObjectA.feedData(0); }else if(cnt == 153){plotObjectA.feedData(0); }else if(cnt == 26){plotObjectA.feedData(0); }else if(cnt == 27){plotObjectA.feedData(0); }else if(cnt == 28){plotObjectA.feedData(20); }else if(cnt == 29){plotObjectA.feedData(20); }else if(cnt == 30){plotObjectA.feedData(-20); }else if(cnt == 31){plotObjectA.feedData(-20); }else if(cnt == 32){plotObjectA.feedData(0); }else if(cnt == 33){plotObjectA.feedData(0); }else{plotObjectA.feedData( (Math.random() - 0.5)*25);}//end else }//end for loop

White random noise

The final statement in Listing 2 uses a random number generator to feed white random noise to the plotting object for all data values other than the fifteendata values specified in the preceding statements. You can see the random values plotted and marked by round ovals in Figure 5 .

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