<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Max Data Tutorial 2: Data Scaling">

<setdocpatch name="02dDataScaling" patch="02dDataScaling.maxpat"/>
<previous name="datachapter01">Data Viewing</previous>
<next name="datachapter03">Gesture Capture</next>
<parent name="00_maxindex">Max Tutorials</parent>

<indexinfo category="Data" title="Data Scaling">Mapping and scaling numerical information</indexinfo>

<h1>Data Tutorial 2: Data Scaling</h1>

<h2>Introduction</h2>

<p>This tutorial is focused on data scaling and smoothing issues.  It introduces the <o>iter</o> object for list iterations, and the <o>trough</o>, <o>peak</o> and <o>slide</o> objects for data scaling and smoothing.  We will also look at additional data display options for the <o>multislider</o> object – in particular, the ability to set a minimum and maximum range, and using the peak-hold monitoring in that object.</p>

<p>Different Max objects and physical devices produce values of different ranges and formats.  One of the tasks that you will often perform is altering the incoming format and scale of a data-set to match the format and scale needed for your output.  While earlier tutorials described using the <o>scale</o> object for this function, we might need to employ other objects to find out the numerical range for our scaling first.  The objects introduced in this tutorial offer new options for scaling, smoothing and viewing data to suit your programming needs.</p>

<p>To open the tutorial patch, click on the <b>Open Tutorial</b> button in the upper right-hand corner of the documentation window.</p>

<h2>Examination of new objects</h2>

<p>Take a look at our tutorial patcher: on the left side of the patcher is an overview of a few new objects (outlined in green).  The top-most patch shows the <o>iter</o> object in use – it accepts a list, and outputs the individual values in order. When we click on the <m>message</m> box, the four-value list is sent to <o>iter</o>, which outputs four messages – each one containing one list item. This is the simplest way to turn a list into a data stream, and (as we will soon see) is useful for <i>atomizing</i> the contents of a list in order to trigger a new event from each list element.</p>

<p>The next subpatch has examples of both the <o>trough</o> and <o>peak</o> objects.  They serve complementary purposes: both objects examine an incoming number stream, with <o>trough</o> capturing (and outputting) the lowest value received thus far, while <o>peak</o> captures and sends the highest value.  When we change the value in the <o>number</o> box, we see that the objects capture the low and high values, with new output occurring whenever a new high or low is reached.</p>

<p>Finally, the bottom subpatch shows three uses of the <o>slide</o> object. The <o>slide</o> object is used for <i>smoothing</i> out data based on a “smoothing factor”, with separate factors for <i>upward</i> and <i>downward</i> movements.  The function of the smoothing factor is rather simple – it is used to divide the difference between the last received message and the current one, thereby reducing the amount of “swing” in any value transition.  The smoothing factors are provided as two arguments to the object: the first argument is the factor for upward movement, and the second argument is the factor for downward movement.</p>

<p>For example, when you first start the patch, the “last” value is <m>0.0</m>.  If you make an immediate transition to <m>0.5</m> (which you can do by entering a value into the <o>number</o> box connected to the <o>slide</o> objects), you will see that the first slider moves to <m>0.01</m>.  This is because there was an upward transition in value, so the first factor (<m>50.</m>) was used: the difference between the initial value of <m>0.00</m> and <m>0.50</m> is <m>0.50</m>; this difference is divided by the factor (<m>50.</m>) to yield  <m>0.01</m>.</p>

<p>The three <o>slide</o> objects show different smoothing factors: the first provides even factors for both upward and downward movement, the second has no upward smoothing (a factor of 1.0), and the third has a small downward factor (a factor of 10.0).  As you move the input slider you will see that each output <o>multislider</o> responds differently – smoothing the output in relation to the upward and downward smoothing factors of each <o>slide</o> object.</p>

<h2>The main patch overview</h2>

<p>The main patch uses weather data as source material; it drives the display in our <o>multislider</o>, and is also used to create four-note chords for playback on the computer’s internal MIDI synthesizer.  Double-click the two <o>coll</o> objects in the patch to become familiar with their contents - both contain twelve lines of data, where the output of one serves as the index of another.  When you start the <o>metro</o> at the top of the patch with the <o>toggle</o> object, the <m>bang</m> messages are used to generate calendar information – specifically, the output of a <o>counter</o> (which is counting days) is used to query a small <o>coll</o> database that outputs the months based on what days in the calendar year they start - e.g. day <m>90</m> of the year is the first day of month <m>4</m> (April).  This month value (in the range <m>1</m> to <m>12</m>) is then used to query the <o>coll</o> loaded from the file <i>weather.txt</i>, which contains the average monthly temperatures in four cities (New York, Paris, Buenos Aires, and Tokyo).  This data is <o>unpack</o>ed (since it comes out of the <o>coll</o> in the form of a list) and is sent to four <o>float</o> (abbreviated "f") objects.  The <o>metro</o> then bangs the <o>float</o> objects to create a stream of output.  This is a good technique for use in systems where you require something to happen on a regular clock but the incoming data is not produced at a constant speed - in our example, the list of temperatures only come out of the <o>coll</o> object when the month changes.</p>

<p>The output of the <o>float</o> objects are sent to <o>slide</o> objects with a smoothing factor of <m>15.0</m> for both upward and downward movement. This means that, while the same value may be sent as many as 31 times (since there are up to 31 days in a month), the value will still change as the smoothed output catches up to the actual value.  The outputs of the four <o>slide</o> objects are <o>pack</o>ed together and sent into the <o>multislider</o>, where they are displayed.  This newly created dataset is also output from the <o>multislider</o>, where it is split apart (by the <o>iter</o> object) and used to make four notes for synthesizer playback.</p>

<p>This <o>multislider</o> uses a few new techniques for data display.  Open the object inspector for the <o>multislider</o>, and you will see that the <m>Slider Style</m> attribute is set to <m>Bar</m>, which gives us the filled bar.  The <m>Candycane</m> option is also activated, so that the bars are colored based on the colors in the inspector.  Finally, there is a <m>Peak Hold</m> option set, which causes a small bar to be located at the top of the highest point that the value reaches.  This gives us a visual indicator of the peak value seen for each of the four cities.</p>

<p>There is a <o>toggle</o> connected to a <o>message</o> box that can turn off or turn on the peak hold function; this is a good example of using inspector-type messages to control the function of an object. Since any change to the <m>peakhold</m> attribute should reset the peak tracking, a companion message (<m>peakreset</m>) resets that value any time the <o>toggle</o> is changed.</p>

<h2>The min/max section</h2>

<bullet>Open the <m>setup</m> <o>patcher</o> object by double-clickingon it</bullet>

<p>The other part of this patch worth examining is the setup performed just to the left of the main patch.  This patch section has a singular purpose: to set up the <o>multislider</o> for proper display scaling.  A <m>bang</m> message (either sent by clicking the <o>button</o>, or generated by a <o>loadbang</o>) starts by setting the <o>trough</o> and <o>peak</o> objects with very high and very low thresholds, respectively - this guarantees that any new value sent to them will automatically set a new standard within the object. The <m>bang</m> then triggers a <m>dump</m> message to the <o>coll</o> object, which causes <i>all</i> of the entries to be sent from the object, one list at a time.</p>

<p>As the lists are output, they are processed by an <o>iter</o> object, that splits the lists into single-value messages.  These are examined by the <o>trough</o> and <o>peak</o> objects, which find the lowest and highest values in the data set (<m>32</m> and <m>81</m>, respectively).  When either value is found, it causes the <o>pak</o> object to generate a value list, which <o>prepend</o> attaches to the message <m>setminmax</m> that is sent to the <o>multislider</o>.  This changes the display range that will be shown, causing the <o>multislider</o> to limit its display to the actual range of high and low temperature that will be output by the <o>coll</o>.  In this way, the <o>multislider</o> display is automatically calibrated to the temperatures stored in the <i>weather.txt</i> file.</p>

<h2>Summary</h2>

<p>In this tutorial we’ve seen how to analyze data streams (using <o>iter</o>, <o>trough</o> and <o>peak</o>) and smooth their output (using <o>slide</o>).  We’ve also seen some programming patterns that can be useful for automatically setting up UI elements, and for outputting data when new data is sent less frequently than it is needed.  Finally, we further explored the flexible <o>multislider</o> object, using it for a bar display with peak hold values and full-color display.</p>

<seealsolist>
<seealso name="iter">Break a list up into a series of numbers</seealso>
<seealso name="peak">Finds the largest of a stream of numbers</seealso>
<seealso name="trough">Finds the smallest of a stream of numbers</seealso>
<seealso name="slide">Smooth-out changes in float values</seealso>
</seealsolist>

</chapter>
