# weight.m version 26Jun23
(Sound Level Weighting Filters)

Author: Paul Mennen
Email: paul@mennen.org

## Overview

Displays the frequency response for four filters used in sound level measurements (as defined by IEC 651). These four filters are:

A weighting | This was developed as an equal-loudness filter. This means that if you sweep a constant amplitude sine wave through this filter, the output will appear to be of uniform loudness to those with normal hearing. Logically, minimizing the A-weighted sound level should also minimize the danger to people exposed to the sound. Newer research suggests that other weightings could do better in this regard, but never-the-less A-weighting is still the universally accepted standard for these safety measurements.
C weighting | C weighting is often used when measuring peak amplitudes of short-duration sounds.
B weighting | B weighting is a compromise, falling between the curves for A and C weighting, but is rarely used anymore and should be considered obsolete.
D weighting | D weighting is sometimes used for sound level measurements of military jet engines and has few (if any) civilian uses.

Typing weight at the command prompt will produce a figure similar to this:

- The primary plot is in the middle, showing the magnitude of all four weightings in dB with a logarithmic frequency axis.
- The lower plot is the same except the magnitude is displayed in linear units. The D-weighted curve is clipped by default so that you can more easily see the shape of the other curves. You can easily rescale the Y axis if you like to see the entire D-weighted curve.
- The upper plot shows the phase of these filters. This plot was included as a whimsical curiosity since the phase is really of no consequence because of the way these filters are used.
  Since there is some overlapping of these curves, it is easier to see the shapes by looking at one weighting function at a time. You can do this by RIGHT clicking on one of the trace IDs in the black TraceID box near the lower left corner of the figure. (More information on the use of the TraceID box is given here.) To alert the first-time user of this feature, a two-line help text (in orange characters) was placed at the lower edge of the figure. (The HelpText pseudo object makes this easy.) So as not to be a distraction, this help text is removed as soon as you click in the plot area (to move the cursor) or when you left or right click on any of the four items in the TraceID box.
  The primary purpose of this application is mentioned above, but an important secondary purpose is to highlight some of the programming techniques used:
  The first line of this program defines a logarithmically spaced set of frequency points between 10 and 20,000 Hz.
  The second line identifies the 87th point of that vector as corresponding to 1000Hz which is used as the reference amplitude. (All the weighting curves have a value of 1 at this reference frequency).
  The next four lines define the transfer function of the four weighting functions as shown here. The 4 poles of the C weighting filter are also used by the A and B weightings, so the computations for HA and HB can be simplified by referring to HC in the calculations.
  The transfer function is evaluated along the imaginary axis at the points defined by the frequency vector. The magnitude and phase are then computed, with the magnitude normalized to 1 at 1000Hz.
  And finally, plt can be called to display the results of these computations. Notice the inclusion of the 'SubPlot',[30 50 20] parameter in the plt call. This tells plot to allocate 30%, 50%, and 20% of the plot area to the lower, middle, and upper plots respectively. The lower plot is referred to as the main plot and the remaining plots are called subplots. Usually (and by default) plt assigns only one trace to each subplot and puts all the remaining traces on the main plot. So in this case, since there are 12 traces, it would put the first 10 traces on the lower plot, trace 11 on the middle plot and trace 12 on the upper plot. One reason that plt assigns traces in that way is that it makes it easy to define a logical cursoring scheme that allows you to cursor all the traces (i.e. Each subplot trace has its own Y cursor readout and then one more Y cursor readout is used for the remaining traces depending on which trace in the main plot was clicked on last.)
  Of course, that wouldn't be very useful for this application. Fortunately, there is a way of overriding these default trace assignments. We follow the SubPlot parameter with 'SubTrace',[4 4 4] which tells plt to assign 4 traces to each plot. For plots intended for publication, that's all there is to it, but in this case, we are using plt more for data exploration. It would be difficult to predefine a cursoring scheme that would be appropriate for all situations involving the SubTrace parameter, so it's pretty much left up to the application to define the desired cursor behavior. That is the purpose of the last 4 lines of the weight function as well as the cursor callback function (curCB).
  Note that there are three Y cursor edit boxes. These edit boxes (left to right) are created for the lower, middle, and upper plots respectively. These are created by plt as they would be for any plot including a SubPlot parameter, however here we have repurposed them. The left-most edit box displays the cursor for the lower plot (linear Magnitude) in the same way, it would for any plot created by plt, so no modifications are required for that one. The second edit box is used to read out the dB Magnitude from the middle plot. The characters "dB" are added to the edit box to make it clear that this is for the middle plot. The third edit box displays the Phase (top plot) and a degree symbol is added at the end to make this obvious.
  Normally, clicking on an element in the TraceID box will only enable or disable one of the traces in the main (lower) plot. However here we want the TraceID box to control all three plots. This is the purpose of the TraceID callback function (tidCB). This function ensures that the three plots are synchronized with respect to which weighting functions are being displayed. So for example, if the A and C weighting functions are the only ones currently enabled in the lower plot, the same will be true for the other two plots.
  Both the curCB and tidCB functions include the command plt('HelpText','off'); so that the help text gets turned off after any user action, insuring that the help text does not become a distraction.
  Copyright © 2024
  Paul Mennen