Fancy Filtering with the Teensy 3.6

Arm-ed for DSP

Signal filtering entails some tricky tradeoffs. A fast MCU that provides hardware-based floating-point capability eases some of those trade-offs. Here, Brian has used the Arm-based Teensy MCU modules to serve those needs. Here, Brian taps the Teensy 3.6 Arm MCU module to perform real-time audio FFT-convolution filtering.

By Brian Millier

Signal filtering can be done either with analog circuitry or digitally using a microcontroller (MCU) coupled with analog-to-digital and digital-to-analog converters. The strength of analog filters is that they can cover wide frequency ranges. If they are designed entirely with passive components, the range of signal amplitudes that can be handled is limited only by the voltage rating of the various capacitors that are used. Additionally, they don’t add much, if any, noise to the signal. However, a limitation of analog filters is that they can’t provide a sharp cut-off rate at their corner frequency (Fc), unless you cascade many filter sections and use close-tolerance components.

If you need high-performance filters, then digital filters might be the way to go. You can design very sharp low-pass, high-pass, notch and band-pass filters using digital techniques, if you use high-resolution ADC/DACs to convert the analog signal into the digital domain and (optionally) back to the analog domain. However, the MCU that you use must be fast and, in general, feature hardware-based floating-point operations. Two years ago, I discovered a line of Arm-based MCU modules that fill the bill nicely.

In Circuit Cellar issues 324 (July 2017) and 325 (August 2017), I described a digital guitar amplifier based upon the Teensy 3.2 Module, which contains an Arm Cortex-M4 MCU. The analog guitar signal was converted to a 16-bit digital signal for processing, and then back to an analog signal for power amplification, by an NXP Semiconductor SGTL5000 Codec contained on the PJRC Audio Shield. This project was made possible largely due to the extremely powerful Audio library provided by the manufacturer of the Teensy modules. This library consists of many audio functions, all of which operate using DMA transfers and interrupt service routines (that is, as a background task). The sampling is done at CD quality (44,100 samples/s at 16-bit resolution).

That project involved many different audio functions—some from the Teensy Audio Library, and some that I wrote myself. The filtering I used for the project was in the form of a 5-band parametric equalizer (EQ). This consists of five blocks of band-pass filters, each one centered on a specific frequency in the audible range. Such an EQ is basically a sophisticated “tone control” for the guitar signal. While most of the other guitar signal processing was done within the Teensy 3.2 MCU, using the Audio library, the 5-band parametric EQ was handled by a DSP block contained within the SGTL5000 Codec on the Teensy Audio Shield.

After finishing that project, I became interested in more sophisticated filtering algorithms that could be performed by the Arm MCU found on the Teensy modules. The Teensy Audio Library routines work with all the Arm-based MCUs in the Teensy module family (except the lowest-cost LC model). The Audio library contains three types of digital filters:

1) Biquad (low pass, high pass, band pass, notch)
2) FIR (up to 200 taps)
3) State-variable (Chamberlin)

The Biquad algorithm executes quickly, and its coefficients are easy to calculate on the fly, which makes it easy to change the filter bandwidth and Fc quickly. Finite impulse response (FIR) filters can provide much better filter characteristics, if you configure them with enough “taps”. However, as you increase the number of taps used, the execution time increases proportionately.

All the above filters use 16-bit, fixed-point math (Arm Cortex M4 DSP instructions using the Q15 data format). This is fast and reasonably accurate, but not enough to provide very sharp filter “skirts”. When you attempt to cascade several sections of such filters, you start to see the limitations in the precision of the fixed-point math.

The higher-end Teensy modules (Teensy 3.5 and 3.6) contain the more powerful Arm Cortex M4F core. These devices have hardware floating-point instructions, which basically allow you to do floating-point operations as quickly as you could do the 16-bit fixed-point operations with the DSP instructions available on Teensy 3.2’s Arm Cortex M4 MCU.

Figure 1
Top view of the Teensy 3.6 Arm MCU module. To the right is the on-board MicroSD socket, which accepts the MicroSD card containing the Cabinet Impulse Response file.

By using a Teensy 3.6 with hardware floating-point instructions, I figured that I could handle more sophisticated filtering algorithms. Another consideration was that the Teensy 3.6 MCU runs at 180 MHz, compared to the 72 MHz clock speed of the Teensy 3.2. Also, the Teensy 3.6 can be safely over-clocked at 240 MHz, compared to the 120 MHz maximum overclocked speed of the Teensy 3.2. Figure 1 shows the Teensy 3.6 module. Figure 2 shows the Audio Shield that I used. It contains the NXP SGTL5000 Codec device (A/D and D/A converters, mic preamplification, headphone driver and digital signal processing).

Figure 2
Top view of the Teensy Audio Shield. The two rows of 14 holes are fitted with header pins that plug directly into the Teensy 3.6 MCU module. All interconnections between the two boards are via these 28 pins.

CONVOLUTION FILTERING

Although I have used digital filters in FIR and Biquad configurations, prior to this project I wasn’t familiar with the term “convolution” filtering. As part of my music/recording hobby, I had encountered the term convolution regarding:

1) Guitar amplifier cabinet simulation
2) High-end, “space-accurate” reverberation processors

Convolution reverberation processors are not relevant to this discussion. However, guitar amplifier cabinet simulation is basically a fancy way of saying that you are simulating the exact frequency/phase response of a guitar amplifier and its loudspeaker(s), mounted in a specific cabinet, with the recording microphone oriented a specific way.

The “shape” of the frequency response curve of any given guitar amplifier/speaker combination will not be a “flat” response over the useful range of guitar notes. Instead it will consist of many small peaks and dips over the frequency range of interest. These “aberrations” provide the distinctive sound of interest to the musician. To some extent, one can simulate a given guitar amplifier/speaker by using a multiband parametric equalizer (EQ) and fiddling with it until it sounds the way you know the actual amplifier/speaker sounds. However, experts in the field learned that they could go one step further using the following method.

Rather than feeding an actual guitar signal into the amplifier/speaker cabinet, they feed it a short pulse, with rise/fall times as fast as possible. This short pulse is called a “finite-impulse signal.” The sound emitted by the speaker cabinet is then picked up by a professional-quality microphone, amplified, converted to digital form and stored in a file. This file represents the FIR of the guitar amplifier/speaker cabinet. I admit that I don’t have the best understanding of the mathematical “magic” involved here, but suffice it to say that all the frequency response “personality” of the guitar amplifier/speaker cabinet is contained in the finite-impulse-response (FIR) file that has been collected. The higher the sample rate used to record the impulse, the better the simulation, and the larger this FIR file will be.

Once you have this FIR file, you can use it to provide the coefficients needed for a digital FIR filter. If you pass your “raw” guitar signal through this FIR filter, it will be modified in virtually the same way that it would be if it were sent out to the specifically modeled guitar amplifier/speaker cabinet. Effectively, you can digitally record a “raw” guitar signal, which, when converted back to analog and listened to, will sound as if you were listening to it “live,” through the specific guitar amplifier/speaker that you have modeled. The FIR filter routine does what’s called a “convolution” of the guitar’s time-domain signal with the FIR array of coefficients—which is also time-domain data.

FOCUSING ON FIR

Once you absorb the idea behind this simulation technique, it becomes clear that you could implement a complex digital filter to reproduce almost any complex frequency response with this technique. I’m certain that mathematicians and electronics engineers in the communication field discovered and used this technique to design complex filters long before guitar players saw its usefulness. However, it was the guitar cabinet simulation concept that led me to investigate the FIR filtering technique more fully..  …

Read the full article in the May 346 issue of Circuit Cellar
(Full article word count: 6284 words; Figure count: 8 Figures.)

Don’t miss out on upcoming issues of Circuit Cellar. Subscribe today!

Note: We’ve made the October 2017 issue of Circuit Cellar available as a free sample issue. In it, you’ll find a rich variety of the kinds of articles and information that exemplify a typical issue of the current magazine.

Evaluating Oscilloscopes (Part 3)

In Part 3 of my series on selecting an oscilloscope, I look at the software running the oscilloscope and details such as remote control, fast Fourier transform (FFT) features, digital decoding, and buffer types.

Two weeks ago, I covered the differences between PC-based and stand-alone oscilloscopes and discussed the physical probe characteristics. Last week I discussed the “core” specifications: analog bandwidth, sample rate, and analog-to-digital converter (ADC) resolution. Next week, I will look into a few remaining features such as external trigger and clock synchronization, and I will summarize all the material I’ve covered.

Topic 1: Memory Depth
The digital oscilloscope works by sampling an ADC and then stores these samples somewhere. Thus an important consideration will be how many samples it can actually store. This especially becomes apparent at higher sample rates—at 5 gigasamples per second (GS/s), for example, even 1 million samples (i.e., 1 megasample or 1 MS) means 200 µs of data. If you are looking at very low-cost oscilloscopes, be aware that many of them have very small buffers. Searching on eBay, you can find an oscilloscope such as the Hantek DSO5202P, which has a 1 GS/s sample rate and costs only $400. The record length is only 24 kilosamples (KS) however, which would be 24 µs of data. You can find even smaller buffers:  the Tektronix TDS2000C series has only a 2,500-sample (2.5 KS) buffer length. If you only want to look around the trigger signal, you can live with a small buffer. Unfortunately, when it comes to troubleshooting you rarely have a perfect trigger, and you may need to do a fair amount of “exploration.”  A small buffer means the somewhat frustrating experience of trying to capture the signal of interest within your tiny window of opportunity.

Even if the buffer space is advertised as being huge, you may not be able to easily access the entire space. The Pico Technology PicoScope PS6403D advertises a 1-GS buffer space, one of the largest available. With the PC-based software you can configure a number of parameters; however, it always seems to limit the sample buffer to about 500 MS.  I do admit it’s fairly impressive that this still works at the 5 GS/s sample rate, since that suggests a memory bandwidth of 40 Gb/s! Using the segmented buffer (discussed later in this article) enables use of the full sample memory, but it cannot record a full continuous 1 GS trace, which you might expect based on the sales pitch.

Topic 2: FFT Length
Oscilloscope advertisements often allude to their ability to perform in a “spectrum analyzer” mode. In reality, what the oscilloscope is doing is performing an FFT of the measured signal. One critical difference is that a spectrum analyzer typically has a “center frequency” and you are able to measure a certain bandwidth amount to either side of that center frequency. By sweeping the center frequency, you can get a graph of the power present in the frequency system over a very wide range.

Using the oscilloscope’s FFT mode, there is no such thing as the center frequency. Instead you are always measuring from 0 Hz up to some limit, which is usually user-adjustable. The limit is, at most, half the oscilloscope’s sample rate but may be further limited by the oscilloscope’s analog bandwidth. Now here is the trick—the oscilloscope will specify a certain “FFT length,” which is how many points are used in calculating the FFT. This will also define the number of “bins” (i.e., horizontal frequency resolution) in the output graph. Certain benchtop oscilloscopes may have very limited FFT lengths, such as those containing only 2,048 points.  This may seem fine for viewing the entire spectrum from 0–100 MHz. But what if you want to zoom in on the 95–98 MHz range? Since the oscilloscope is actually calculating the FFT from 0 Hz, it will have only ~60 points it can display in that range. It suddenly becomes apparent why you want very long FFT lengths—it allows you to zoom in and still obtain accurate results. You can set the oscilloscope sample rate down to zoom in on frequencies around 0 Hz. So, for example, if you want to accurately do some measurements at 1–10 kHz, it’s not a big issue since you can set a low enough sample rate so that the 2,048 points are distributed between 0–20 kHz or similar. And when you zoom in you’ve got lots of detail.

In addition to the improved horizontal detail, longer FFT lengths push down the noise floor.  If you do wish to use the oscilloscope for frequency analysis, having a long FFT length can be a huge asset. This is shown in Figure 1, which compares an FFT taken using a magnetic field probe of a microcontroller board. Here I’ve zoomed in on a portion of the spectrum, with the left FFT having 2,048 points, the right FFT having 131,072 points.

Figure 1: When zooming in on a portion of the fast Fourier transform (FFT), having a larger number of points for the original calculation becomes a huge asset. Also, notice the lower noise floor for the figure on the right, calculated with 131,072 points, compared to the 2,048 used for the figure on the left.

Figure 1: When zooming in on a portion of the fast Fourier transform (FFT), having a larger number of points for the original calculation becomes a huge asset. Also, notice the lower noise floor for the figure on the right, calculated with 131,072 points, compared to the 2,048 used for the figure on the left.

A note on selecting a unit: The very low-cost oscilloscopes with small data buffers will obviously use a very small FFT length. But specifications for some of the larger memory depth oscilloscopes, such as the Rigol Technologies DS2000, DS4000, and DS6000 models, show they use smaller FFT lengths.  These models use only 2,048 points, according to a document posted on Rigol’s website, despite their large memory (131 MS).  PC-based oscilloscopes seem to be the best, as they can perform the FFT on a powerful desktop PC, rather than requiring it be done in an embedded digital signal processor (DSP) or field-programmable gate array (FPGA). For example, the PicoScope 6403D allows the FFT length to be up to 1,048,576 points.

Topic 3: Segmented Buffer
A feature I consider almost a “must-have” is a segmented buffer. This means you can configure the oscilloscope to trigger on a certain event, and it will record a number of waveforms of a certain length. For glitches that occur only occasionally (which is, 90% of the time, why you are troubleshooting in the first place), this can speed up your ability to find details of what the system is doing during a glitch.

Figure 2 shows an example of the segmented buffer viewer on the PicoScope software, where the number of buffers can be configured up to 10,000. Similar features exist in the Rigol DS4000 and DS6000, which call each segment a “frame” and can record up to 200,000 frames! Once you have a number of segments/frames, you can either manually flip through looking for the glitch, or use features such as mask limit testing to highlight segments/frames that differ from the “usual.”

Figure 2

Figure 2: Segmented buffers allow you to capture a number of traces and then flip through them looking for some specific feature. Using mask-based testing will also speed this up, since you can quickly find “odd man out”-type waveforms.

Certain oscilloscopes might make the segmented buffer an add-on. For example, only certain Agilent Technologies 3000 X-Series models contain segmented buffers by default; others in that same family require you to purchase this feature for an extra $800! Of course, always review any promotional offers—Agilent has recently advertised that it will enable all features on that oscilloscope model for the price of a single option.

Topic 4: Remote Control/Streaming
One more advanced feature is controlling the oscilloscope from your computer. If you wish to use the oscilloscope in applications beyond electronics troubleshooting, you should seriously consider the features different oscilloscopes provide.

PC-based oscilloscopes tend to have a considerable advantage here, as they are typically designed to interface to the computer. It seems most PC-based oscilloscopes from popular suppliers come with nice application programming interfaces (APIs) for most languages: I’ve found examples in C, C#, C++, MATLAB, Python, LabVIEW, and Delphi for most PC-based oscilloscopes. Some of the “no-name” PC-based oscilloscopes you find on eBay do not have an API, so always check closely for your specific device.

Most of the stand-alone oscilloscopes also have a method of sending commands, typically using a standard such as the Virtual Instrument Software Architecture (VISA). However, I’ve found these stand-alone oscilloscopes seem to have a considerably slower interface compared to a PC-based oscilloscope. Presumably for the PC-based oscilloscope, this interface is critical to overall performance, whereas for the stand-alone it’s simply an “add-on” feature. This isn’t a sure thing, of course—for example, see the PC interface for the Teledyne LeCroy oscilloscope, as described in a company blog post. It looks to give you access to features similar to those of PC-based oscilloscopes (multiple windows, etc.).

Beyond just controlling the oscilloscope, another interesting feature is streaming mode. In streaming mode data is not downloaded to an internal buffer on the oscilloscope. Instead it streams directly over the PC interface (typically USB or Ethernet). This feature is considerably more complex to work with than simple PC-based control, as achieving fast streams via USB is not trivial. However, using streaming mode opens up many interesting features. For example, you could use your oscilloscope as part of a software defined radio (SDR). If you wish to use such a feature, be sure to carefully read the specification sheets for the streaming mode limitations.

Topic 5: Decoding Serial Protocols
Decoding of serial protocols is another useful feature. If you have a digital logic analyzer, it will almost certainly include the ability to decode serial protocols. But it can be helpful to have this feature in the oscilloscope as well. If you are chasing down an occasional parity error, you can use the oscilloscope’s analog display to see if the issue is simply a weak or noisy signal.

While most oscilloscopes seem to support this feature, many require you to pay for it. Typically PC-based oscilloscopes will include it for free, but stand-alone oscilloscopes require you to purchase it. For example, this feature costs $500 for the Rigol Technologies DS4000 series, $800 for the Agilent Technologies 3000X, and $1,100 for the Tektronix DPO/MSO3000 series. Depending on the vendor, it may include multiple protocols or only one. But if you wish to enable all available protocols, it could cost more than your oscilloscope! It would typically be cheaper to purchase a PC-based logic analyzer than it would be to buy the software module for your oscilloscope.

This is one of the major reasons I prefer PC-based oscilloscopes: There tends to be no additional cost for extra features! Without the decoding you can look at the signal and see if it “looks” noisy, but having the decoding built-in means you can easily point to the specific moment when the error occurs. I’ve got some examples of such serial decoding in my video below.

Topic 6: Software Features
I’ve already mentioned it a few times in passing, but you should always check to see what software features are actually included. You may be surprised to find out some features require payment—even some models adding the FFT or other “advanced math” features require payment of a substantial fee.

There is hope on the horizon for getting access to all features in stand-alone oscilloscopes at a reasonable cost. As I mentioned earlier, Agilent Technologies recently announced it would be providing access to all software features for the cost of one module in the X-2000, X-3000, and X-4000 series. Once this goes into effect, it means that it’s really just $500–$1,500 for decoding of all serial protocols and all math features, depending on your oscilloscope. They sell this as saving you up to $16,500. (Which to me just shows how insanely expensive all these software add-ons really are!) With luck, other vendors will follow suit, and perhaps even finally include these software options in the selling price.

If you’re looking at PC-based oscilloscopes, you’ll often be allowed to download the software and play with it, even if you don’t have an instrument. This can give you an idea of how “polished” the user interface is. Considering how long you’ll spend inside this user interface, it’s good to know about it!

Closing Comments
This week I covered a number of features revolving around the software running the oscilloscope. Next week I’ll be looking into a few remaining features such as external trigger and clock synchronization, which will round out this guide.

Author’s note: Every reasonable effort has been made to ensure example specifications are accurate. There may, however, be errors or omissions in this article. Please confirm all referenced specifications with the device vendor.