<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="MSP Analysis Tutorial 2: Oscilloscope and Spectroscope">

<setdocpatch name="02aOscilloscope" patch="02aOscilloscope.maxpat"/>
<previous name="14_analysischapter01"></previous>
<next name="14_analysischapter03"></next>
<parent name="00_mspindex">MSP Tutorials</parent>


<h1>MSP Analysis Tutorial 2: Oscilloscope and Spectroscope</h1>


<p>There are times when seeing a picture of a signal is instructive. in the real world, this is done with an oscilloscope, which shows a graph of amplitude over time. Max has two ways to do this. The old school method uses a dedicated <o>scope~</o> object, and the new school method uses jitter. The new has not replaced the old, there are applications for each.</p>

<p>There are two problems to overcome when plotting a graph of a signal in real time. First of all, in order for your eye to follow a time-varying signal, an excerpt of the signal must be captured and displayed for a certain period of time (long enough for you really to see it). Therefore, the graph must be displayed periodically, and will always lag a bit behind what you hear. Second, there aren't enough pixels on the screen for you to see a plot of every single sample (at least, not without the display being updated at blinding speed), so the display has to use a single pixel to summarize many samples.</p>

<h2>Old School: <o>scope~</o></h2>
<!-- place scope~ image here-->
<p>
	The <o>scope~</o> object shows up as a rectangular display subdivided with a faint grid. When audio is on, there is a horizontal line showing the waveform connected to the left inlet. (This line is called the <i>trace</i>.) The principle of operation is simple. A group of samples is gathered and a composite value is calculated and placed at one point in a buffer. The process repeats until the buffer is full. When the buffer is full, lines are drawn from point to point across the display. The number of samples per point is set by the <m>calccount</m> attribute (this can be changed by an int in the left inlet). The number of points is set by the <m>buffer size</m> attribute (this can be changed by an int in the right inlet). Getiing a useful display depends on the selection of <m>calccount</m> and <m>buffer size</m>, and those depend on what you are trying to show.
</p>
<h3>Showing Waveforms</h3>
<bullet>Select <m>sine</m> on the <o>umenu</o> in the tutorial patch.</bullet>
<p>
	When a steady signal is applied to <o>scope~</o>, what you see depends on the relationship between the frequency, sampling rate, <m>calccount</m> and <m>buffer size</m>. <m>Buffer size</m> should be as high as possible, so set it to the width of the object up to the maximun of 256. In general, the lower <m>calccount</m> is, the more detail is shown, (it is effectively downsampling the waveform) so it's usually best to start at 2. Use more for very low frequency, but 16 will get you down to 10 Hz.
</p>
<p>
	Chances are a steady waveform won't look too steady&#x2014;it will drift across the display. (OK, it will probably whizz across the display!) The solution to this problem is the <m>trigger</m> attribute, which defaults to <m>0:off</m> but should usually be set to <m>1:up</m>. This will lock the display to a certain value in the waveform: <m>triglevel</m>. If triggering is on, the <o>scope~</o> object will wait for the wave to cross the <m>triglevel</m> in the indicated direction before if begins a new display cycle. Thus a simple waveform will usualy show nice and steady. With a complex waveform, <m>triglevel</m> may need some adjustment for best results.
</p>
<bullet>Choose triangle in the <o>umenu</o>.</bullet>
<bullet>Use the <o>attrui</o> object to set <m>trigger</m> to <m>up</m>.</bullet>
<bullet>Use the <o>attrui</o> object to adjust <m>triglevel</m>.</bullet>
<p>
	Note how the wave slides back and forth as <m>triglevel</m> is changed from -1 to 1.
</p>
<h3>Showing Oscillograms</h3>
<bullet>Connect a music signal to your audio interface and select <m>input</m> on the umenu in the tutorial patch.</bullet>

<p>
	Sometimes we want to look at the long term behavior of a signal. This is akin to turning the sweep rate of a hardware oscilloscope way down. You see the envelope of the signal rather than the individual waves. The equivalant setting on <o>scope~</o> is a high value of <m>calccount</m>. 128 would be a good start. The first thing you will notice is the display takes forever to update. How long? Well, the number of samples needed to fill the dissplay is <m>calccount</m>*<m>buffer size</m>, so divide this by the sampling rate to get the numebr of seconds between updates. With a <m>calccount</m> of 128 and <m>buffer size</m> 256, the update time comes out 0.75 seconds. The same math will help estimate the <m>calccount</m> appropriate to showing a complete waveform&#x2014;you want the calculation to match the period of the wave: one over the frequency.
</p>

<h3>What do the gridlines mean?</h3>
<p>
	The <o>scope~</o> is divided into a grid by horizontal and vertical lines. The center horizontal line marks the zero point. The other horizontals are simply halfway to the edges. The values at the top and bottom are set by the <m>range</m> attribute, which defaults to &#177;1.0. The meaning of the vertical lines depends on <m>calccount</m> and <m>buffer size</m>. As mentioned above, the product of these divided by the sample rate determine the <i>display period</i>. The vertical lines divide this by 8. So if <m>calccount</m> is 2 and <m>buffer size</m> 256, the <i>display period</i> will be 0.0116 at 44.1 kHz. The inverse of this  gives the frequency of a waveform that would exactly fill the window, so 86.1 Hz should just fit. The time interval represented by the vertical lines is about 1.5 ms.
</p>

<h3>X-Y Display</h3>
<!--insert X-Y image here-->
<bullet>Connect a stereo music signal to your audio interface and open the X-Y subpatch.</bullet>
<p>
	If you connect an audio signal to the right inlet of <o>scope~</o> that signal will be used to determine the X location (left to right) of each point. If the same signal is connected to left and right, you get a diagonal line from lower left to upper right. If the signal in the right is an out of phase version of the left, the line slants the other way. When a stereo signal is connected, the result is sort of a scribble, and you can estimate the phase difference of left and right by the width and slope of the shape.
</p>

<p>
	If you connect geometric waveforms to both inlets of <o>scope~</o>, you can generate some interesting evolving shapes. These shapes are called Lissajous figures, and are worth some exploration in themselves. The <o>scope~</o> help file has some examples.
</p>


<h2>New School: <o>jit.catch~</o> and <o>jit.graph</o></h2>
<!--insert jitter image here-->
<p>
	We can also display waveforms in Jitter with the <o>jit.catch~</o> and <o>jit.plot</o> objects. <o>Jit.catch~</o> takes samples from an audio signal and packs them into a one dimensional matrix. The number of samples per matrix is set by the <m>framesize</m> attribute, and has exactly the same meaning as the <m>calccount</m>*<m>buffer size</m> calculation in <o>scope~</o>. <o>Jit.catch~</o> has several operation modes, but the one we are interested is <m>mode 3</m>, which is triggered in an oscilloscope-like way. Of course <o>jit.catch~</o> requires a series of bangs to prompt the output of matrices.
</p>
<p>
	<o>Jit.graph</o> displays data in one dimensional matrices in a two dimension array of char. Connect that between <o>jit.catch~</o> and a <o>jit.pwindow</o>, and you have a waveform display. Jit.graph also has modes, which determine the style of display:
</p>
<p>
	0: Points<br/>1: Lines<br/>2: Area<br/>3: Bi-polar area<br/>4: bars
</p>
<p>
	Lines is the most scope-like.
</p>
<p>
	The threshold feature in jit.catch~ is a bit odd in that the threshold point is always placed in the <m>middle</m> of the matrix. Thus, if you adjust <m>framesize</m>, the waveform will remain centered in the display. That also means that the trigger direction (<m>trigdir</m>) has to be set in the down (1) mode to match what <o>scope~</o> does with the same wave. The display can be further altered with the <m>rangelo</m> and <m>rangehi</m> attributes which set the values that hit the bottom or top of the display. There is also a height attribute, which sets the vertical dimension of the matrix. This should be set to match the height of the <o>jit.pwindow</o> to avoid size conversion artifacts.
</p>

<p>
	The choice between using <o>scope~</o> or Jitter to display audio depends on what you want to do. If an audio display is needed in the patch, <o>scope~</o> is usually the best choice. If you want to process the image further &#224; la iTunes, Jitter is the way to go.
</p>

<h2>Plotting a spectrum</h2>
<p>
	Two popular buzzwords in the DSP community are <i>time domain</i> and <i>frequency domain</i>. <i>Time domain</i> simply means we are graphing how signal values change over time, as we have shown above. The <i>frequency domain</i> shows audio at one moment in time, but shows the Fourier frequency content. Thus the graph (called a <i>spectrogram</i>) has frequency across the bottom. You will remember the Fourier theorm states that any waveform can be deconstructed into a sum of sine waves. A spectrogram plots the amplitude of those sine waves. We will go into detail in <i><link type="tutorial" module="msp" name="14_analysischapter04">Analysis Chapter 4: Simple Fourier Analysis</link></i>. Here we will just explore a simple way to display a spectrogram.
</p>
<h3>Spectrogram</h3>
<!--insert spectrogram image here-->
<bullet>Select <m>triangle</m> in the <o>umenu</o> and set the frequency to 440 Hz</bullet>
<p>
	The <o>spectroscope~</o> object displays a lovely spectrogram right out of the box. The peaks in the trace show the frequency and amplitude of the partials in the triangle waveform. In an ideal system, these peaks would be thin lines, but the algorithm required to show a spectrum in real time produces "skirts" at the bottom of the peaks. The main attribute to be aware of is the frequency axis scaling, which is chosen by the <m>logfreq</m> attribute and shown as <m>Display Frequency Axis Scale</m> in the <o>attrui</o>. In <i>linear mode</i> the entire audible range of 20-22 kHz is evenly spaced left to right. The spectroscope~ display should show a nice set of evenly spaced harmonics. It's actually every other harmonic, as you can see by selecting <m>rectangle</m>. If you choose <m>input</m> most of the action will be crowded into the left side. That's because music is mostly confined to a fairly low range of frequencies.
</p>
<bullet>Select <m>Display frequency axis scale</m> in the <o>attrui</o> attached to the <o>spectroscope~</o> object and choose <m>Logarithmic Scale</m>.</bullet>
<bullet>Select <m>sine</m> in the <o>umenu</o> and set the frequency to 43 Hz.</bullet>

<p>
	In logarithmic mode, <i>octaves</i> are evenly spaced across the display. The display should show a wide peak about a third of the way in. (Ignore what is to the left of the peak&#x2014;the display  resolution is only 43 Hz, so anything below that is garbage.) Now change the frequency to 86 Hz and note how the peak moves. As you step up octaves: 129, 172, 215, etc., the peak will move an equal amount. The peak will get narrower, too. The 43 Hz band of resolution is less of the total as we move up. Now select <m>input</m>. The music fills the display nicely.
</p>

<p>
	Both modes of display are very useful: Linear mode shows the harmonic structure of steady waveforms and logarithmic mode shows the tonal balance of music in an intuitive way.
</p>

<h3>Sonogram</h3>
<p>
	We can get both time domain and frequency domain information in one display by choosing sonogram mode.
</p>
<!--insert sonogram image here-->
<bullet>Select <m>Display mode</m> in the <o>attrui</o> and choose <m>Sonogram</m>.</bullet>
<bullet>Select <m>input</m> in the <o>umenu</o>.</bullet>

<p>
	You should now see an image drawn with streaks of black, grey, and white. A sonogram is a two dimensional plot showing time across the bottom and frequency from bottom to top. The amplitude at each point is represented by the darkness of the color. As you listen to the music you will see how the low notes and high notes show up and even see the harmonic structure of the sound. (The <m>logfreq</m> mode affects sonograms also.) For an extra special treat, select <m>sonogram color</m> in the <o>attrui</o> and choose <m>color</m>. Now the intensity of the frequency components show as red fading through yellow and green to purple.
</p>

<p>
	The <o>spectroscope~</o> object can be customized in varioius ways, including vertical orientation, drawing or scrolling mode, reverse direction, and colors. You can also restrict the range to display to get more detail in a small speace.
</p>


<summary>Summary</summary>

<p>The <o>scope~</o> object gives an oscilloscope view of a signal, graphing amplitude over time.  Waveforms can also be displayed in the Jitter matrix format by <o>jit.catch~</o> and <o>jit.graph</o>. The <o>spectroscope~</o> object can display frequency content as either a dynamic spectrogram or as a moving sonogram.
</p>
<seealsolist>

<seealso name="scope~">Signal oscilloscope</seealso>

<seealso name="jit.catch~">Convert audio signal to jitter matrix</seealso>

<seealso name="jit.graph">Display values in one dimensional matrix in two dimensions</seealso>

<seealso name="spectroscope~">Display frequency content of audio signal</seealso>
</seealsolist>

</chapter>
