<< Chapter < Page | Chapter >> Page > |
Listing 16 . The program named Sinc01. |
---|
/*File Sinc01.java
Copyright 2014, R.G.BaldwinRevised 08/31/14
This program produces an output text file containing values for y as a functionof x. The contents of the text file can be converted to audio and either played
or written into an audio file of type AU for playback later by the programnamed AudioGraph01.
Function:y = Math.sin(2*Math.PI*f*x)/x
This is the classic sin(x)/x function, otherwise known a the sincfunction. It appears frequently in digital signal processing (DSP).
To modify this program to handle other functions, you only need to modify theinstance variables in the class named Runner and modify the code in the method
named getYval.Tested using Java SE 8 under Win 7
******************************************************************************/import java.io.BufferedWriter;
import java.io.File;import java.io.FileWriter;
import java.io.IOException;public class Sinc01{//Driver class
public static void main(String[]args){
//Do not modify the code in this method.Runner obj = new Runner();
obj.run();}//end main
}//end class Sinc01//============================================================================/
class Runner{//Modify the following instance variables as needed.
double xMin = -20;//Minimum value for xdouble xMax = 20;//Maximum value for x
double xInc = 0.25;//Used to determine x-values for evaluation of y-valueString fileName = "Data/Sinc01.txt";//Output file name in Data foldervoid run(){
//Do not modify the code in this method.double xVal = xMin;
String outString = "";while(xVal<= xMax){
//Construct an output string contain comma-separated values of y as a// function of x.
outString += getYval(xVal) + ",";//Increment x
xVal += xInc;}//end while loopwriteOutputFile(fileName,outString);}//end run method
//-------------------------------------------------------------------------////This method evaluates the function. Modify it to evaluate different// functions.
//Function://y = Math.sin(2*Math.PI*f*x)/x
//This is the classic sin(x)/x function, otherwise known a the sinc// function. It appears frequently in digital signal processing (DSP).
double getYval(double xVal){//Evaluate the function here
double f = 0.25;double result = 0;
if(xVal != 0.0){result = Math.sin(2*Math.PI*f*xVal)/xVal;
}else{//Don't divide by zero
result = Math.sin(2*Math.PI*f*0.00001)/0.00001;}//end else//Limit the return value to three decimal digits
return (Math.rint(1000.0*result))/1000.0;}//end getY
//-------------------------------------------------------------------------////This method writes the output file. Do not modify this method.void writeOutputFile(String fileName,String outString){
try{File file = new File(fileName);
//if the file doesn't exists, create it. If it does exist, overwrite it.if (!file.exists()) {
file.createNewFile();}//end if
FileWriter fileWriter = new FileWriter(file.getAbsoluteFile());BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(outString);bufferedWriter.close();
}catch (IOException e) {e.printStackTrace();
}//end catch}//end writeOutputFile
//-------------------------------------------------------------------------//}//end class Runner |
Notification Switch
Would you like to follow the 'Accessible objected-oriented programming concepts for blind students using java' conversation and receive update notifications?