<< Chapter < Page | Chapter >> Page > |
Shifting a pulse in time introduces a linear phase angle to the complex spectrum. Conversely, introducing a linear phase angle to the complex spectrumcauses a pulse to be shifted in time.
The next module in this series will introduce the inverse Fourier transform (as opposed to the forward Fourier transform) and will explain the reversible nature of the Fourier transform.
A complete listing of the program discussed in this module is provided in Listing 6 below. Listings for other programs mentioned in the module, such as Graph03 and Graph06 , are provided in other modules. Those modules are identified in the text of this module.
Listing 6. Dsp034.java. |
---|
/* File Dsp034.java
Copyright 2004, R.G.BaldwinRev 5/21/04
Computes and plots the amplitude, real, imag,and phase angle spectrum of a pulse that is read
from a file named Dsp034.txt. If that filedoesn't exist in the current directory, the
program uses a set of default parametersdescribing a damped sinusoidal pulse.
The program also plots the pulse. When the datais plotted using the programs Graph03 or Graph06,
the order of the plots from top to bottom in thedisplay are:
The pulseThe amplitude spectrum
The real spectrumThe imaginary spectrum
The phase angle in degreesEach parameter value must be stored as characters
on a separate linein the file named Dsp034.txt.
The required parameters are as follows:Data length as type int
Pulse length as type intSample number representing zero time as type int
Low frequency bound as type doubleHigh frequency bound as type double
Sample values for the pulse as type doubleThe number of sample values must match the value
for the pulse length.All frequency values are specified as a
double representing a fractional part of thesampling frequency. For example, a value of 0.5
specifies a frequency that is half the samplingfrequency.
Here is a set of sample parameter values that canbe used to test the program. This sample data
describes a triangular pulse. Don't allow blanklines at the end of the data in the file.
40011
00.0
0.50
00
4590
13590
450
00
The plotting program that is used to plot theoutput data from this program requires that the
program implement GraphIntfc01. For example,the plotting program named Graph03 can be used
to plot the data produced by this program. Whenit is used, the usage information is:
java Graph03 Dsp034A static method named transform belonging to the
class named ForwardRealToComplex01 is used toperform the actual spectral analysis. The
method named transform does not implement an FFTalgorithm. Rather, it is more general than, but
much slower than an FFT algorithm. (See theprogram named Dsp030 for the use of an FFT
algorithm.)Tested using SDK 1.4.2 under WinXP.
************************************************/import java.util.*;
import java.io.*;class Dsp034 implements GraphIntfc01{
final double pi = Math.PI;//for simplificationint dataLen;//data length
int pulseLen;//length of the pulseint zeroTime;//sample that represents zero time
//Low and high frequency limits for the// spectral analysis.
double lowF;double highF;
double[]pulse;//data describing the pulse
//Following array stores input data for the// spectral analysis process.
double[]data;
//Following arrays receive information back// from the spectral analysis process
double[]real;
double[]imag;
double[]angle;
double[]mag;
public Dsp034(){//constructor//Get the parameters from a file named
// Dsp034.txt. Create and use default// parameters describing a damped sinusoidal
// pulse if the file doesn't exist in the// current directory.
if(new File("Dsp034.txt").exists()){getParameters();
}else{//Create default parameters
dataLen = 400;//data lengthpulseLen = 100;//pulse length
//Sample that represents zero time.zeroTime = 0;
//Low and high frequency limits for the// spectral analysis.
lowF = 0.0;highF = 0.5;//half the sampling frequency
pulse = new double[pulseLen];
double scale = 240.0;for(int cnt = 0;cnt<pulseLen;cnt++){
scale = 0.94*scale;//damping scale factorpulse[cnt] =scale*Math.sin(2*pi*cnt*0.0625);
}//end for loop//End default parameters
}//end else//Create the data array and deposit the pulse
// in it.data = new double[dataLen];for(int cnt = 0;cnt<pulse.length;cnt++){
data[cnt]= pulse[cnt];}//end for loop
//Print parameter values.System.out.println(
"Data length: " + dataLen);System.out.println(
"Pulse length: " + pulseLen);System.out.println(
"Sample for zero time: " + zeroTime);System.out.println(
"Lower frequency bound: " + lowF);System.out.println(
"Upper frequency bound: " + highF);System.out.println("Pulse Values");
for(int cnt = 0;cnt<pulseLen;cnt++){
System.out.println(pulse[cnt]);
}//end for loop//Create array objects to receive the results
// and perform the spectral analysis.mag = new double[dataLen];real = new double[dataLen];imag = new double[dataLen];angle = new double[dataLen];ForwardRealToComplex01.transform(data,real,
imag,angle,mag,zeroTime,lowF,highF);}//end constructor
//-------------------------------------------////This method gets processing parameters from
// a file named Dsp034.txt and stores those// parameters in instance variables belonging
// to the object of type Dsp034.void getParameters(){
int cnt = 0;//Temporary holding area for strings. Allow
// space for a few blank lines at the end// of the data in the file.
String[]data = new String[20];try{
//Open an input stream.BufferedReader inData =
new BufferedReader(new FileReader("Dsp034.txt"));
//Read and save the strings from each of// the lines in the file. Be careful to
// avoid having blank lines at the end,// which may cause an ArrayIndexOutOfBounds
// exception to be thrown.while((data[cnt] =inData.readLine()) != null){
cnt++;}//end while
inData.close();}catch(IOException e){}
//Move the parameter values from the// temporary holding array into the instance
// variables, converting from characters to// numeric values in the process.
cnt = 0;dataLen =
(int)Double.parseDouble(data[cnt++]);
pulseLen =(int)Double.parseDouble(data[cnt++]);zeroTime =
(int)Double.parseDouble(data[cnt++]);
lowF = Double.parseDouble(data[cnt++]);
highF = Double.parseDouble(data[cnt++]);
//Create a new array object for the pulse// and populate it from the file data.
pulse = new double[pulseLen];
for(int pCnt = 0;pCnt<pulseLen;pCnt++){
pulse[pCnt]= Double.parseDouble(
data[cnt++]);
}//end for loopSystem.out.println(
"Parameters read from file");}//end getParameters
//-------------------------------------------////The following six methods are required by the
// interface named GraphIntfc01. The plotting// program pulls the data values to be plotted
// by calling these methods.public int getNmbr(){
//Return number of functions to process.// Must not exceed 5.
return 5;}//end getNmbr
//-------------------------------------------////Provide the input data for plotting.
public double f1(double x){int index = (int)Math.round(x);
if(index<0 || index>data.length-1){
return 0;}else{
return data[index];
}//end else}//end function
//-------------------------------------------////Provide the amplitude spectral data for
// plotting. Attempt to scale it so that it// will plot well in the range 0 to 180.
public double f2(double x){int index = (int)Math.round(x);
if(index<0 || index>mag.length-1){
return 0;}else{
return (4*dataLen/pulseLen)*mag[index];
}//end else}//end function
//-------------------------------------------////Provide the real spectral data for
// plotting. Attempt to scale it so that it// will plot well in the range -180 to 180.
public double f3(double x){int index = (int)Math.round(x);
if(index<0 || index>real.length-1){
return 0;}else{
//Scale for convenient displayreturn (4*dataLen/pulseLen)*real[index];}//end else
}//end function//-------------------------------------------//
//Provide the imaginary spectral data for// plotting. Attempt to scale it so that it
// will plot well in the range -180 to 180.public double f4(double x){
int index = (int)Math.round(x);if(index<0 || index>imag.length-1){
return 0;}else{
//Scale for convenient displayreturn (4*dataLen/pulseLen)*imag[index];}//end else
}//end function//-------------------------------------------//
//Provide the phase angle data for plotting.// The angle ranges from -180 degrees to +180
// degrees. This is thing that drives the// attempt to cause the other curves to plot
// well in the range -180 to+180.
public double f5(double x){int index = (int)Math.round(x);
if(index<0 || index>angle.length-1){
return 0;}else{
return angle[index];
}//end else}//end function
//-------------------------------------------//}//end class Dsp034 |
This section contains a variety of miscellaneous information.
Baldwin discusses the complex spectrum and explains the relationship between the phase angle and shifts in the time domain.
Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.
I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.
In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.
Affiliation :: I am a professor of Computer Information Technology at Austin Community College in Austin, TX.
-end-
Notification Switch
Would you like to follow the 'Digital signal processing - dsp' conversation and receive update notifications?