<< Chapter < Page | Chapter >> Page > |
The code in Listing 1 defines a convenience constant representing the value of pi and then defines the set of default parameters that will be used by the program in the event that the file named Dsp029.txt does not exist in the current directory.
The code in Listing 2 creates five array objects that will be populated with sinusoidal data.
Listing 2. Create array objects to hold sinusoidal data. |
---|
double[] data1 = new double[len];
double[]data2 = new double[len];double[] data3 = new double[len];
double[]data4 = new double[len];double[] data5 = new double[len]; |
The constructor begins in Listing 3 . The code in this fragment calls the method named getParameters to read the parameters from the file named Dsp029.txt .
Listing 3. Get the parameters. |
---|
public Dsp029(){//constructor
if(new File("Dsp029.txt").exists()){getParameters();
}//end if |
Before calling the getParameters method, however, the program calls the exists method of the File class to confirm that the file actually exists. If the file doesn't exist, thecall to getParameters is skipped, causing the default parameters defined in Listing 1 to be used instead.
The getParameters method is straightforward, so I won't discuss it in detail. You can view it in Listing 16 . Suffice it to say that the method reads the input parameters from the disk file and writes their valuesinto the variables declared in Listing 1 , overwriting the default values stored in those variables.
In addition, the getParameters method displays the values read from the disk file in the format shown in Figure 4 and Figure 6 .
For simplicity, this program always generates five sinusoids, even if fewer than five were requested as the input parameter value for numberSinusoids . In that case, the extra sinusoids are generated using default values and are simply ignored when the sinusoids are plotted.
The code fragment in Listing 4 creates the sinusoidal data for each of the five specified frequencies and saves that data in the array objects that werecreated in Listing 2 .
Listing 4. Create the sinusoidal data. |
---|
for(int n = 0;n<len;n++){
data1[n]= amp[0]*Math.cos(2*pi*n*freq[0]);
data2[n]= amp[1]*Math.cos(2*pi*n*freq[1]);
data3[n]= amp[2]*Math.cos(2*pi*n*freq[2]);
data4[n]= amp[3]*Math.cos(2*pi*n*freq[3]);
data5[n]= amp[4]*Math.cos(2*pi*n*freq[4]);
}//end for loop}//end constructor |
Listing 4 also signals the end of the constructor. When the constructor terminates, an object of the Dsp029 class has been instantiated. The five arrays shown in Listing 4 have been populated with sinusoidal data according to the parameters read from the file named Dsp029.txt or according to the default values of the parameters shown in Listing 1 .
In order to better understand what is going on in the plotting process, it would be helpful for you to review the module titled Plotting Engineering and Scientific Data using Java . However, assuming that you don't have the time to do that, I will provide a very brief explanation asto how the plotting programs work.
Notification Switch
Would you like to follow the 'Digital signal processing - dsp' conversation and receive update notifications?