Creating a Spectrum Analyzer

In this activity, you will reuse your oscilloscope code to create a spectrum analyzer.

For the oscilloscope, the horizontal axis showed time, and the vertical axis showed volume intensity (loudness). In a spectrum analyzer, the horizontal axis shows frequency, and the vertical axis still shows volume intensity.
animation of an oscilloscope in Snap! that looks like a rapidly changing, horizontal wavey line; the horizontal axis is labeled 'volume intensity' and the vertical axis is labeled 'time'

Creating the Spectrum Analyzer

Want to know more about frequency?

Frequency is how often a repeating sound pressure wave repeats. Music (when its more than just percussion) is about playing different frequencies in a way that people enjoy hearing.

  1. Open your "Sparks-Making-Noise" project if isn't open already.
  2. Explore the spectrum input to the microphone block by changing its input and clicking the block several times. (Try it both while you are making noise and while you aren't.)
    microphone (spectrum)

Selecting the samples option from the microphone block makes it report a list of current microphone volume values sampled across a small period of time. Since reports the volumes set of samples over time, you used it to build your oscilloscope.
microphone (samples) reporting
Selecting the spectrum option from the microphone block makes it report a list of current microphone volume values across a range of frequencies. Since it reports the volumes of a spectrum of frequencies, you'll use it to build a spectrum analyzer.
microphone (spectrum) reporting

  1. Since you will reuse your oscilloscope code, find the show volume samples block in the palette on the left, right-click it, and select "duplicate block definition..."
    Don't see "duplicate block definition..."? Make sure you are clicking the block in the palette on the left.
    show volume samples block with menu open showing 'duplicate block definition...' option selected
    This will create a new block called show volume samples (2).
  2. Rename this block show frequencies by clicking on the name of the block in the Block Editor window and changing or deleting the text in the title.
  3. Edit the show frequencies block with these critical changes:
    1. The second input to the for each block should be microphone (spectrum) instead of microphone (samples) so that it draws the intensity for each frequency in the spectrum instead of the intensity at each sample over time.
    2. Since you'll be plotting the volume of frequencies not volume samples, the first input to the for each block should be frequency instead of volume sample. Be sure to change the variable in the y input for the go to block also.
      To rename a variable and all the places it appears at the same time, right-click the variable and choose "rename all..."
    3. Finally, that y input to the go to block doesn't need to be scaled up by multiplying by 150, but the value could be lowered (using subtraction) so that the drawing starts near the bottom of the stage (for example, you might subtract 150 instead).
      To change a block (like × (times)) to a similar block (like - (minus)), right-click the block, and choose "relabel..."
    4. Click "OK" to close the Block Editor.
  4. Replace the show volume samples block with the show frequencies inside the forever block, and test your code. Make some noise, watch the results, and work with your partner and classmates to debug any issues. The spectrum analyzer should look something like this:
    animation of an oscilloscope in Snap! that looks like a rapidly changing, horizontal wavey line
  5. Now Is a Good Time to Save
  1. Try playing a song while you watch your spectrum analyzer.
In this activity, you changed your oscilloscope into a spectrum analyzer by plotting the intensity of each frequency separately instead of plotting the total intensity across time.