http://www.developer.com/

## Spectrum Analysis Using JavaNovember 16, 2004 Java Programming, Notes # 1485 ## Preface
A previous lesson entitled Fun with Java, How and Why Spectral Analysis Works explained some of the fundamentals regarding spectral analysis. The lesson entitled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm presented and explained several Java programs for doing spectral analysis, including both DFT programs and FFT programs. That lesson illustrated the fundamental aspects of spectral analysis that center around the sampling frequency and the Nyquist folding frequency. The lesson entitled Spectrum Analysis using Java, Frequency Resolution versus Data Length used similar Java programs to explain spectral frequency resolution. The lesson entitled Spectrum Analysis using Java, Complex Spectrum and Phase Angle explained issues involving the complex spectrum, the phase angle, and shifts in the time domain. This lesson will illustrate and explain forward and inverse Fourier transforms using both DFT and FFT algorithms. I will also illustrate and explain the implementation of frequency filtering by modifying the complex spectrum in the frequency domain and then transforming the modified complex spectra back into the time domain.
You may find it useful to open another copy of this lesson in a separate browser window. That will make it easier for you to scroll back and forth among the different figures and listings while you are reading about them.
I recommend that you also study the other lessons in my extensive collection of online Java tutorials. You will find those lessons published at Gamelan.com. However, as of the date of this writing, Gamelan doesn't maintain a consolidated index of my Java tutorial lessons, and sometimes they are difficult to locate there. You will find a consolidated index at www.DickBaldwin.com. ## PreviewIn this lesson, I will present and explain the following new programs: **Dsp035**- Illustrates the reversible nature of the Fourier transform. This program transforms a real time series into a complex spectrum, and then reproduces the real time series by performing an inverse Fourier transform on the complex spectrum. This is accomplished using a DFT algorithm.**InverseComplexToReal01**- Class that implements an inverse DFT algorithm for transforming a complex spectrum into a real time series.**Dsp036**- Replicates the behavior of the program named Dsp035 but uses an FFT algorithm instead of a DFT algorithm.**InverseComplexToRealFFT01**- Class that implements an inverse FFT algorithm for transforming a complex spectrum into a real time series.**Dsp037**- Illustrates filtering in the frequency domain. Uses an FFT algorithm to transform a time-domain impulse into the frequency domain. Modifies the complex spectrum, eliminating energy within a specific band of frequencies. Uses an inverse FFT algorithm to produce the filtered version of the impulse in the time domain.
In addition, I will use the following programs that I explained in the lesson entitled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm. **ForwardRealToComplex01**- Class that implements a forward DFT algorithm for transforming a real time series into a complex spectrum.**ForwardRealToComplexFFT01**- Class that implements a forward FFT algorithm for transforming a real time series into a complex spectrum.**Graph03**- Used to display various types of data.*(The concepts were explained in an earlier lesson.)***Graph06**- Also used to display various types of data in a somewhat different format.*(The concepts were also explained in an earlier lesson.)*
## Discussion and Sample Code
The program named The program performs spectral analysis on a time series consisting of pulses and a sinusoid. Then it passes the resulting real and complex parts of the spectrum to an inverse Fourier transform program. This program performs an inverse Fourier transform on the complex spectral data to reconstruct the original time series. This program can be run with either
The program was tested using J2SE 1.4.2 under WinXP.
When the data is plotted - The input time series
- The real spectrum of the input time series
- The imaginary spectrum of the input time series
- The amplitude spectrum of the input time series
- The output time series produced by the inverse Fourier transform
There were 256 values plotted horizontally in each section. I plotted the values on a grid that is 270 units wide to make it easier to view the plots on the rightmost end. This leaves some blank space on the rightmost end to contain the numbers, preventing the numbers from being mixed in with the plotted values. The last actual data value coincides with the rightmost tick mark on each plot.
A static method named
The
method named
A static method named
Before getting into the technical details of the program, let's take a look at the results shown in Figure 1. The top plot in Figure 1 shows the input time series used in this experiment.
The time series is 256 samples long. Although the DFT algorithm can accommodate time series of arbitrary lengths, I set the length of this time series to a power of two so that I can compare the results with results produced by an FFT algorithm later in the lesson.
As you can see, the input time series consists of three concatenated pulses separated by blank spaces. The pulse on the leftmost end consists simply of some values that I entered into the time series to create a pulse with an interesting shape. The middle pulse is a truncated sinusoid. The rightmost pulse is a truncated square wave.
The objective of the experiment is to confirm that it is possible to transform this time series into the frequency domain using a forward Fourier transform, and then to recreate the time series by using an inverse Fourier transform to transform the complex spectrum back into the time domain.
The real part of the complex spectrum is shown in the second plot from the
top in Figure 1. It will become important later to note that the real part of the
spectrum is symmetrical about the folding frequency near the center of the plot
Without attempting to explain why, I will simply tell you that the real part of the Fourier transform of a complex series whose imaginary part is all zeros is always symmetrical about the folding frequency.
The imaginary part of the complex spectrum is shown in the third plot from the top. Again, it will become important later to note that the imaginary part of the spectrum is asymmetrical about the folding frequency. Once again, without attempting to explain why, the imaginary part of the Fourier transform of a complex series whose imaginary part is all zeros is always asymmetrical about the folding frequency.
It is also true that the values of the imaginary part of the Fourier transform of a complex spectrum whose real part is symmetrical about the folding frequency and whose imaginary part is asymmetrical about the folding frequency will be all be zero. I will take advantage of these facts later to simplify the computing and plotting process.
The amplitude spectrum is shown in the fourth plot down from the top. Recall from previous lessons that the amplitude values are always positive, consisting of the square root of the sum of the squares of the real and imaginary parts.
The output time series, produced by performing an inverse Fourier transform on the complex spectrum is shown in the bottom plot in Figure 1. Compare the bottom plot to the top plot. As you can see, they are the same, demonstrating the reversible nature of the Fourier transform.
I will discuss this program in fragments. A complete listing of the program is provided in Listing 14 near the end of the lesson. The beginning of the class for
The constructor begins in Listing 2. The code in Listing 2 creates the input time series data shown in the top plot of Figure 1.
Note that I deleted much of the code from Listing 2 for brevity. You can view the missing code in Listing 14 near the end of the lesson.
The code in Listing 3 invokes the static
I explained the The angle results returned by the One of the parameters The parameters also specify that the complex spectrum is to be computed at a
set of equally spaced frequencies ranging from zero
The code in Listing 4 invokes the static
I will explain the
Listing 4 also signals the end of the constructor. Once the constructor
has completed executing, an object of the
All of the remaining code in If you have studied the previous lessons in this series, you probably don't want to hear any more about those methods, so I won't discuss them further. You can view the six interface methods in Listing 14 near the end of the lessons.
The static method named There are more efficient ways to write this method taking known symmetry and asymmetry conditions into account. However, I wrote the method the way that I did because I wanted it to mimic the behavior of an FFT algorithm. Therefore, the complex input must extend from zero to the sampling frequency. The method does not implement an FFT algorithm. Rather, the
The parameters to the - double[] realIn - incoming real data
- double[] imagIn - incoming imag data
- double[] realOut - outgoing real data
The method considers the data length to be
The method returns a number of points equal to the data length. It assumes that the real input consists of positive frequency points for a symmetric real frequency function. That is, the real input is assumed to be symmetric about the folding frequency. The method does not test this assumption. The method assumes that imaginary input consists of positive frequency points for an asymmetric imaginary frequency function. That is, the imaginary input is assumed to be asymmetric about the folding frequency. Once again, the method does not test this assumption.
A symmetric real part and an
asymmetric imaginary part guarantee that the
imaginary output will be all zero values. Having made that assumption, the program makes no attempt The program was tested using J2SE v1.4.2 under WinXP.
The beginning of the class and the beginning of the static
Listing 5 declares and initializes some variables that will be used later.
Listing 6 contains a pair of nested
If you have been studying the earlier lessons in this series, you should be
able to understand the code in Listing 6 without further explanation. Pay
particular attention to the comments that describe the two
The program named
The output produced by running the program named
Compare Figure 2 with Figure 1. The two should be identical. The
program named
I'm only going to show you one short code fragment from the program named
The
The static I'm not going to discuss
this method in detail either, because it is very similar to the method named
There are a couple of things, however, that I do want to point out. The
The signature for the
The
A value of +1 for the first
parameter causes the A value of -1 for the first parameter causes the
Although I didn't include the code in this lesson,
Similarly, the
As evidenced in Figure 1 and Figure 2, the program named
The program named
The program begins by using an FFT algorithm to perform a forward Fourier transform on a single impulse in the time domain.
The impulse is shown as the input time series in the topmost plot in Figure 4.
Then the program eliminates all energy between one-sixth and five-sixths of the sampling frequency by setting the real and imaginary parts of the FFT output to zero. The second, third, and fourth plots in Figure 4 show the real part, imaginary part, and amplitude respectively of the modified complex spectrum.
The folding frequency in these three plots is near the center of the plot at the eighth tick mark.
The input data length was 256 samples. All but one of the input data values was set to zero resulting in a single impulse in the input time series near the second tick mark in Figure 4.
There were 256 values plotted horizontally in each separate plot. Once again, to make it easier to view the plots on the rightmost end, I plotted the values on a grid that is 270 units wide. This leaves some blank space on the rightmost end to contain the numbers, thus preventing the numbers from being mixed in with the plotted values. The last actual data value coincides with the rightmost tick mark on each plot.
After modifying the complex spectrum as described above, the program performs an inverse Fourier transform on the modified complex spectrum to produce the filtered impulse.
The filtered impulse is shown as the bottom plot in Figure 4. As you can see, the pulse is smeared out in time relative to the input pulse in the top plot. This is the typical result of reducing the bandwidth of a pulse.
Listing 8 shows the beginning of the class definition for the program named
Listing 8 simply declares and initializes some variables that will be used later.
The constructor begins in Listing 9.
Listing 9 creates the raw pulse data shown in the topmost plot in Figure 4. When the array object referred to by
Still in the constructor, the code in Listing 10 uses an FFT algorithm in the
method named
The results of the Fourier transform are stored in the array objects referred
to by
Listing 11 applies the filter by setting sample values in a portion of the real and imaginary parts of the complex spectrum to zero.
This code eliminates all energy between one-sixth and five-sixths of the sampling frequency. The modified data for the real and imaginary parts of the complex spectrum are shown in the second and third plots in Figure 4.
Listing 12 recomputes the magnitude values for the modified real and imaginary values of the complex spectrum.
The modified data for the amplitude of the complex spectrum are shown in the fourth plot in Figure 4.
Listing 13 uses the
The results of the inverse transform are shown in the bottom plot in Figure 4. Listing 13 also signals the end of the constructor.
Once the constructor returns, all of the data that is to be plotted has been
stored in the various array objects. The remaining code in the program
consists of the definition of the six methods required by the interface named I have discussed these methods on numerous previous occasions, and won't repeat that discussion here.
Figure 5 illustrates one more example of performing frequency filtering by modifying the complex spectrum and then performing an inverse transform on the modified spectrum. While discussing the program named
Figure 5 shows the output produced by the program named
The basic plotting format of Figure 5 is the same as Figure 4. The first difference to note between the two figures is that I moved the
impulse in the input time series in the topmost plot sixteen samples further to
the right in
The second difference to note is shown in the modified amplitude spectrum in the fourth plot in the two figures. The bandwidth of the pass band is significantly narrower in Figure 5 than in Figure 4. Also, the pass band in Figure 4 extends all the way down to zero frequency, while Figure 5 eliminates all energy below a frequency of three thirty-seconds of the sampling frequency.
Finally, note the waveforms of the two filtered impulses. The overall amplitude of the filtered impulse in Figure 5 is less than in Figure 4, simply because it contains less total energy. In addition, the filtered impulse in Figure 5 is broader than the filtered impulse in Figure 4. This is because it has a narrower bandwidth.
## Run the ProgramsI encourage you to copy, compile, and run the programs provided in this lesson. Experiment with them, making changes and observing the results of your changes. Create more complex experiments. For example, use more complex input time series when experimenting with frequency filtering. Apply different modifications to the complex spectrum when experimenting with frequency filtering. Most of all enjoy yourself and learn something in the process. ## SummaryThis lesson illustrates and explains forward and inverse Fourier transforms using both DFT and FFT algorithms. The lesson also illustrates and explains the implementation of frequency filtering by modifying the complex spectrum in the frequency domain and then transforming the modified complex spectrum back into the time domain. ## Complete Program ListingsComplete listings of the programs discussed in this lesson follow.
Copyright 2004, Richard G. Baldwin. Reproduction in whole or in part in any form or medium without express written permission from Richard Baldwin is prohibited. ## About the author
Richard Baldwin
is a college professor (at Austin Community College in Austin, TX) and
private consultant whose primary focus is a combination of Java, C#, and
XML. In addition to the many platform and/or language independent benefits
of Java and C# applications, he believes that a combination of Java, C#,
and XML will become the primary driving force in the delivery of structured
information on the Web.
-end- |