### 28.2.13 Visualizing power spectra

The plotspectrum command displays the power spectrum of an audio clip.

- plotspectrum takes one mandatory argument and a sequence of optional arguments:
  - A, an audio clip.
  - Optionally, opts, a sequence of options each of which is one of:
    - span, which is either a list [fmin,fmax] or frequencies=fmin..fmax, specifying the frequency band in Hertz (by default, fmin=0 and fmax=samplerate(A)/2).
    - bins=n, where n>2 is a positive integer which must be a power of two and specifies the number of bins for the frequency range [0,samplerate(A)/2] (by default, n=512).
    - color=disp, where disp is a sum of plot attributes (e.g. an Xcas color). By default, the spectrum is drawn in blue.
- plotspectrum(A⟨,opts⟩) displays the power spectrum of A in the frequency range [fmin,fmax].
- If the audio clip has more than one channel, the channels are mixed down into a single channel before computing the spectrum.

#### Examples

With the file flute-C5.wav donwloaded from here:

flute:=readwav("/home/luka/Downloads/flute-C5.wav"):; plotspectrum(flute)

The audio file flute-C5 consists of a single C5 tone played by a flute. In the graph above, the fundamental of that tone is clearly visible at the corresponding frequency of about 523 Hz. You can also see its first overtone at about 1046 Hz (the next two overtones are barely visible).

With the file gettysburg10.wav downloaded from here:

clip:=readwav("/home/luka/Downloads/gettysburg10.wav"):; plotspectrum(clip,[0,2000],bins=4096,color=purple)

With the file sf1_n0H.wav downloaded from here:

clip:=readwav("/home/luka/Downloads/sf1_n0H.wav"):; plotspectrum(clip,frequencies=0..5000,bins=1024)

In the above graph you can see the two components of the stationary noise contained in ranges 0-440 Hz and 2500-3000 Hz. The rest of the spectrum belongs to the female speaker. Use the playsnd command to hear the audio (see Section 28.2.14).