This page is optimized for mobile devices, if you would prefer the desktop version just click here

4.2 Java1483-spectrum analysis using java, frequency resolution  (Page 8/19)

The constructor

The constructor begins in Listing 4 . The code in Listing 4 is identical to that shown earlier in Listing 2 . This code generates the five sinusoidal pulses and stores the data representing those pulses in the arrays referred to by data1 through data5 . So far, except for the declaration of some extra variables, this program isn't much different from the program named Dsp031a discussed earlier in this module.

Listing 4. Beginning of the constructor.
public Dsp031(){//constructor //Create the raw datafor(int x = 0;x<len/16;x++){ data1[x]= amp*Math.cos(2*pi*x*freq); }//end for loopfor(int x = 0;x<len/8;x++){ data2[x]= amp*Math.cos(2*pi*x*freq); }//end for loopfor(int x = 0;x<len/4;x++){ data3[x]= amp*Math.cos(2*pi*x*freq); }//end for loopfor(int x = 0;x<len/2;x++){ data4[x]= amp*Math.cos(2*pi*x*freq); }//end for loopfor(int x = 0;x<len;x++){ data5[x]= amp*Math.cos(2*pi*x*freq); }//end for loop

Perform the spectral analysis

The remainder of the constructor is shown in Listing 5 . This code calls the transform method of the ForwardRealToComplex01 class five times in succession to perform the spectral analysis on each of the five pulses shown in Figure 1 .

(I explained the transform method in detail in the previous module titled Spectrum Analysis using Java, Sampling Frequency, Folding Frequency, and the FFT Algorithm .)

Listing 5. Perform the spectral analysis.
mag1 = new double[len];real = new double[len];imag = new double[len];angle = new double[len];ForwardRealToComplex01.transform(data1,real, imag,angle,mag1,zeroTime,lowF,highF);mag2 = new double[len];real = new double[len];imag = new double[len];angle = new double[len];ForwardRealToComplex01.transform(data2,real, imag,angle,mag2,zeroTime,lowF,highF);mag3 = new double[len];real = new double[len];imag = new double[len];angle = new double[len];ForwardRealToComplex01.transform(data3,real, imag,angle,mag3,zeroTime,lowF,highF);mag4 = new double[len];real = new double[len];imag = new double[len];angle = new double[len];ForwardRealToComplex01.transform(data4,real, imag,angle,mag4,zeroTime,lowF,highF);mag5 = new double[len];real = new double[len];imag = new double[len];angle = new double[len];ForwardRealToComplex01.transform(data5,real, imag,angle,mag5,zeroTime,lowF,highF);}//end constructor

Each time the transform method is called, it computes the magnitude spectrum for the incoming data and saves it in the output array.

(Note that the real, imag, and angle arrays are not used later, so they are discarded each time a new spectral analysis is performed.)

The interface methods

The Dsp031 class also implements the interface named GraphIntfc01 . The remaining code in the program consists of the methods required to satisfy thatinterface. Except for the identification of the arrays from which the methods extract data to be returned for plotting, these methods are identical to thosedefined in the earlier class named Dsp031a . Therefore, I won't discuss them further.

<< Chapter < Page Page > Chapter >>

Read also:

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.
Jobilize.com uses cookies to ensure that you get the best experience. By continuing to use Jobilize.com web-site, you agree to the Terms of Use and Privacy Policy.