Audio spectrum analyzer. Sound spectrum analyzers and their applications. Absolute value calculation

Alexey Lukin

Spectrum analyzer is a device for measuring and displaying the spectrum of a signal - the distribution of signal energy by frequency. This article discusses the main types of spectrum analyzers and illustrates their use for audio editing and restoration. Particular attention is paid to modern analyzers based onFFT- fast Fourier transform.

Why analyze the spectrum?

Traditionally, in digital audio recording, an audio track is represented as an oscillogram that displays the sound waveform, that is, the dependence of the sound amplitude on time. This representation is quite clear for an experienced sound engineer: the oscillogram allows you to see the main events in the sound, such as changes in volume, pauses between parts of a piece, and often even individual notes in a solo recording of an instrument. But the simultaneous sound of several instruments on the oscillogram is “mixed” and visual analysis of the signal becomes difficult. However, our ear can easily distinguish individual instruments in a small ensemble. How does this happen?

When a complex sound vibration hits the eardrum, it is transmitted through a series of auditory ossicles to an organ called the cochlea. The cochlea is an elastic tube twisted into a spiral. The thickness and rigidity of the snail changes smoothly from the edge to the center of the spiral. When a complex vibration arrives at the edge of the cochlea, it causes response vibrations in different parts of the cochlea. Wherein resonant frequency Each part of the snail has its own. Thus, the cochlea decomposes complex sound vibrations into individual frequency components. Separate groups of auditory nerves approach each part of the cochlea, transmitting information about the vibrations of the cochlea to the brain (more details about auditory perception can be read in the article “Fundamentals of Psychoacoustics” by I. Aldoshina in the magazine “Sound Engineer” No. 6, 1999). As a result, the brain receives information about sound, already sorted into frequencies, and a person easily distinguishes high sounds from low ones. Additionally, as we'll see shortly, decomposing audio into frequencies helps distinguish individual instruments in a polyphonic recording, greatly enhancing editing capabilities.

Bandpass spectrum analyzers

The first audio spectrum analyzers separated the signal into frequency bands using a set of analog filters. The display of such an analyzer (Fig. 1) shows the signal level in a variety of frequency bands corresponding to filters.

Rice. 1. Specan32 three-octave analyzer, emulating the well-known KlarkTeknik DN60 device

In Fig. Figure 2 shows an example of the frequency characteristics of bandpass filters in an analyzer that meets the GOST 17168-82 standard. Such an analyzer is called a third-octave analyzer, since there are three bands in each octave of the frequency range. It can be seen that the frequency characteristics of the bandpass filters overlap; their slope depends on the order of the filters used.

Rice. 2. Frequency characteristics of filters of a one-third octave spectrum analyzer

An important property of a spectrum analyzer is ballistics - the inertia of level meters in frequency bands. It can be adjusted by setting the rate of rise (attack) and fall of the level. Typical attack and fall times in such an analyzer are about 200 and 1500 ms.

Bandpass spectrum analyzers are often used to adjust the frequency response (amplitude-frequency response) speaker systems at concert venues. If the input to such an analyzer is given pink noise(having the same power in each octave), the display will show a horizontal line, with possible correction for noise variation over time. If pink noise, passing through the sound reinforcement system of the hall, is distorted, then changes in its spectrum will be visible on the analyzer. In this case, the analyzer, like our ear, will be insensitive to narrow dips in the frequency response (less than 1/3 of an octave).

Fourier transform

The Fourier transform is a mathematical apparatus for decomposing signals into sinusoidal oscillations. For example, if the signal x(t) continuous and infinite in time, then it can be represented as a Fourier integral:

The Fourier integral collects the signal x(t) from an infinite number of sinusoidal components of all possible frequencies ω , having amplitudes X ω and phases φ ω .

In practice, we are more interested in the analysis of sounds that are finite in time. Since music is not a static signal, its spectrum changes over time. Therefore, when performing spectral analysis, we are usually interested in individual short fragments of the signal. To analyze such fragments of a digital audio signal, there is discrete Fourier transform:

Here N samples of a discrete signal x(n) over the time interval from 0 to N-1 are synthesized as the sum of a finite number of sinusoidal oscillations with amplitudes Xk and phases φk. The frequencies of these sinusoids are equal kF/N, Where F is the signal sampling frequency, and N- number of samples of the original signal x(n) on the analyzed interval. Set of coefficients Xk called amplitude spectrum of the signal. As can be seen from the formula, the frequencies of the sinusoids into which the signal is decomposed are uniformly distributed from 0 (constant component) to F/2 - the maximum possible frequency in a digital signal. This linear arrangement of frequencies differs from the distribution of bands of a one-third octave analyzer.

FFT analyzers

FFT (fast Fourier transform) is an algorithm for fast calculation of the discrete Fourier transform. Thanks to it, it became possible to analyze the spectrum of sound signals in real time.

Let's consider the operation of a typical FFT analyzer. It receives a digital audio signal as its input. The analyzer selects successive intervals from the signal ("window"), on which the spectrum will be calculated, and calculates the FFT in each window to obtain the amplitude spectrum Xk. The calculated spectrum is displayed as a graph of amplitude versus frequency (Fig. 3). Similar to bandpass analyzers, a logarithmic scale is usually used along the frequency and amplitude axes. But due to the linear arrangement of the FFT bands in frequency, the spectrum may appear insufficiently detailed at lower frequencies or excessively oscillating at higher frequencies.

Rice. 3. FFT analyzer display

If we consider the FFT as a bank of filters, then, unlike the bandpass filters of a one-third octave analyzer, the FFT filters will have the same width in hertz rather than in octaves. Therefore, pink noise on an FFT analyzer will no longer be a horizontal line, but an inclined one, with a roll-off of 3 dB/oct. Horizontal line on the FFT analyzer it will be White noise- it contains equal energy in equal linear frequency intervals.

Parameter N- the number of analyzed signal samples is crucial for the type of spectrum. The more N, the denser the grid of frequencies into which FFT decomposes the signal, and the more frequency details are visible on the spectrum. To achieve higher frequency resolution, longer sections of the signal must be analyzed. If the signal within the FFT window changes its properties, then the spectrum will display some average information about the signal from the entire window interval.

When you need to analyze rapid changes in a signal, the window length N choose small. In this case, the resolution of the analysis increases in time and decreases in frequency. Thus, the frequency resolution of the analysis is inversely proportional to the time resolution. This fact is called uncertainty relationship.

Weighing windows

One of the simplest sound signals is a sine wave. What will its spectrum look like on an FFT analyzer? It turns out that it depends on the frequency of the tone. We know that FFT does not decompose the signal according to the frequencies that are actually present in the signal, but according to a fixed uniform frequency grid. For example, if the sampling frequency is 48 kHz and the FFT window size is selected as 4096 samples, then the FFT decomposes the signal into 2049 frequencies: 0 Hz, 11.72 Hz, 23.44 Hz, ..., 24000 Hz.

If the frequency of the tone matches one of the frequencies of the FFT grid, then the spectrum will look “ideal”: a single sharp peak will indicate the frequency and amplitude of the tone (Fig. 4, white graph).

If the frequency of the tone does not match any of the frequencies in the FFT grid, then the FFT will “assemble” the tone from the frequencies available in the grid, combined with various weights. In this case, the spectrum graph is blurred in frequency (Fig. 4, green graph). Such blurring is usually undesirable, since it may obscure more faint sounds at adjacent frequencies. You can also notice that the amplitude of the maximum of the green graph is lower than the real amplitude of the analyzed tone. This is due to the fact that the power of the analyzed tone is equal to the sum of the powers of the spectrum coefficients from which this tone is composed.

Rice. 4. Spectrum of a sine tone of various frequencies with and without weighting windows

To reduce the effect of spectrum blur, the signal is multiplied by weighing windows- smooth functions similar to a Gaussian, falling towards the edges of the interval. They reduce spectrum blur at the expense of some deterioration in frequency resolution. If we consider FFT as a set of bandpass filters, then the weighting windows regulate the mutual penetration of frequency bands.

The simplest window is rectangular: it is a constant 1 that does not change the signal. It is equivalent to the absence of a weight window. One of the popular windows is the Hamming window. It reduces the smear level by approximately 40 dB relative to the main peak.

Weighting windows differ in two main parameters: the degree of expansion of the main peak and the degree of suppression of spectrum blur. ("side lobes"). The more we want to suppress the side lobes, the wider the main peak will be. A rectangular window blurs the top of the peak the least, but has the highest side lobes. The Kaiser window has a parameter that allows you to select the desired degree of sidelobe suppression.

Another popular choice is the Hahn window. It suppresses the maximum sidelobe less than the Hamming window, but the remaining sidelobes fall off faster with distance from the main peak. The Blackman window has stronger sidelobe suppression than the Hahn window.

For most problems, it is not very important which type of weight window to use. The main thing is that it exists. Popular choices are Khan or Blackman. Using a weighting window reduces the dependence of the spectrum shape on a specific signal frequency and its coincidence with the FFT frequency grid.

Figure 4 is made for sinusoids, however, based on it, it is not difficult to imagine what the spectrum of real sound signals will look like. Each peak in the spectrum will have some blurred shape, depending on its frequency and the selected weighting window.

To compensate for peak broadening when using weighting windows, longer FFT windows can be used: for example, 8192 samples rather than 4096 samples. This will improve the resolution of the analysis in frequency, but degrade it in time.

Spectrogram

There is often a need to monitor how the spectrum of a signal changes over time. FFT analyzers help do this in real time while playing back the signal. However, in some cases it turns out to be convenient to visualize changes in the spectrum in the entire sound passage at once. This representation of the signal is called spectrogram. To construct it, it is used windowed Fourier transform: The spectrum is calculated from successive windows of the signal (Figure 5), and each of these spectra forms a column in the spectrogram.

Rice. 5. Calculation of the signal spectrogram

The horizontal axis of the spectrogram represents time, the vertical axis represents frequency, and the amplitude is indicated by brightness or color. In the spectrogram of a guitar note in Fig. 6 shows the development of the sound: it begins with a sharp attack and continues in the form of harmonics, multiples in frequency of the fundamental tone of 440 Hz. It can be seen that the upper harmonics have a smaller amplitude and decay faster than the lower ones. The spectrogram also shows recording noise - a uniform dark blue background. On the right is a scale for matching colors and signal levels (in decibels below zero).

Rice. 6. Spectrogram of a guitar note with different FFT window sizes

If you change the size of the FFT window, it becomes clearly visible how the frequency and time resolution of the spectrogram changes. As the window is enlarged, the harmonics become thinner and their frequency can be determined more accurately. However, the moment of the attack is blurred in time (on the left side of the spectrogram). When the window size is reduced, the opposite effect is observed.

The spectrogram is especially useful when analyzing rapidly changing signals. In Fig. Figure 7 shows a spectrogram of a vocal passage with vibrato. Using it, it is easy to determine such characteristics of the voice as the frequency and depth of vibrato, its shape and evenness, and the presence of a singing formant. The melody being performed can be traced by changing the pitch of the fundamental tone and harmonics.

Rice. 7. Spectrogram of a vocal passage with vibrato

Applications of spectrogram

Modern audio restoration tools, such as the iZotope RX program, actively use the spectrogram to edit individual time-frequency regions in the signal. Using this technique, you can find and suppress unwanted sounds such as ringing mobile phone during an important recording, the creaking of the pianist's chair, coughing in the auditorium, etc.

Let's illustrate the use of a spectrogram to remove fan whistles from a concert recording.


Rice. 8. Removing unwanted sounds using a spectrogram

In Fig. 8 whistle is easy to find: it is a light curved line around 3 kHz. If the whistling frequency were constant, it could be suppressed using a notch filter. However, in our case the frequency changes. To highlight the whistle in the spectrogram, it is convenient to use the “magic wand” tool from the iZotope RX II program. Pressing once will highlight the fundamental tone of the whistle, pressing again will highlight the harmonics. After this, the whistle can be removed by simply pressing the Del key. However, a more accurate way is to use the Spectral Repair module: this will avoid “holes” in the spectrum after removing the whistle. After using this module in attenuation mode with vertical interpolation (Attenuate vertically), the whistle almost completely disappears from the recording: both visually and audibly.

Another useful application of a spectrogram is to analyze the presence of traces of MP3 compression or other lossy codecs in a recording. Most original (uncompressed) quality recordings have a frequency range extending to 20 kHz and above; in this case, the signal energy smoothly decreases with increasing frequency (as in Fig. 6, 7). As a result of psychoacoustic compression, the upper frequencies of the signal are quantized more strongly than the lower ones, and the upper limit of the signal spectrum is reset to zero (as in Fig. 8). In this case, the cutoff frequency depends on the content of the encoded signal and on the bitrate of the encoder. It is clear that the encoder tends to reset only those frequencies in the signal that are this moment inaudible (masked). Therefore, the cutoff frequency, as a rule, changes over time, which forms a characteristic “fringe” on the spectrogram with islands of energy against a dark background.

A similar situation sometimes arises with low-frequency interference, such as wind blowing into the microphone or a constant component (shift in DC, DC offset). They can be located at infra-low frequencies and cannot be detected without the help of a spectrum analyzer or oscilloscope.

Conclusion

Among experienced old-school sound engineers, there is a widespread belief that signals should be analyzed and edited solely by ear, without relying on indicators and analyzers. Of course, analyzers are not a panacea for hearing loss. It’s unlikely that anyone takes seriously the idea of ​​mixing a composition “by instruments”.

Spectrum and spectrogram are ways of representing sound that are closer to auditory perception than an oscillogram. I hope that this article will open up new possibilities in analyzing and editing sound for those who have not previously worked with these representations.

Spectrum analyzers as a weapon of the recording proletariat

The opportunity to make music in your own home studio makes many creative people dizzy and forces them to take a university course while sitting at their home computer. The desire to quickly make your demo-recording breaks down against the monolith of modern technology. Dark circles under the eyes, slurred speech, and constant experimentation with software are signs that a person is on the right track. It’s good if you have experience dealing with kilometers of cable and effects regulators. In this case, the sufferer will disappear from the family for only six months... It’s not without reason that they say “The computer is the best contraceptive.”

What is needed for high-quality audio processing on a computer? Good sound card fast processor, a capacious hard drive and fresh software. And of course, musical taste, coupled with professional experience. Sound processing by software simulators of studio equipment can be helped by an important, in my opinion, addition in the form of signal analyzers arriving at the ports of the sound card. Even having high-quality music equipment will not prevent confusion with the characteristics of the final audio product. And if the work is carried out on a commercial basis, specialists from the radio studio for which your video was intended can quickly debunk your creation due to non-compliance with their standards. They won’t pay attention to the originality of the idea and the level of arrangement, it’s none of their business, but they will delve into the frequency characteristics with passion. And the prospect of a long rework of what seemed to be a finished video, or even a refund of the fee, could become quite real.

When do people pay attention to the dynamics and harmonic amplitude of sound? Mainly in the process of processing each tool individually or a group of tools interconnected by a batch. A good sound engineer must have an idea of ​​the overall picture of the tonal palette and, in compromise with the musicians, find the best sound. And if you have no experience and nowhere to ask for advice, I suggest you get hold of the following software.

In my work I use audio spectrum analyzers PAS ANALIS CENTER and PINGUIN AUDIO METER. A remarkable feature of this software is that when running the program, it not only provides all the characteristics of the sound signal from the input and output ports, but also does not conflict when working simultaneously with sound editors. That is, messages indicating that the port is busy do not appear. Information about the properties of the passing sampled sound is collected within the system, and not from the sound card ports. In this case, the range from 0.022 kHz / -60 dB to 22.050KHz / 0.00 dB is easily analyzed.

These programs are filters that work on the principle of an oscilloscope, dressed in a Windows interface. The evaluation of digitized sound occurs in real time, without taking away system resources from sound editors. Some programs have their own spectrum analyzers, but they work only inside their editor and often only for output. Such modules are useful when you need to evaluate a section of the sample being processed, but for the entire video you have to combine all the tracks into one, and only after that the overall picture will become visible. The above-mentioned analyzers are another matter. Even MIDI sound can be viewed across the spectrum without converting to another format.

The analyzer interface is an active display with a graphical interpretation of frequency bands. It can have several views such as oscilloscope, waveform analysis and spectral analysis. In the wave mode, scrolling is possible as a constantly running wave of the signal. During spectral analysis, all harmonics are divided into separate columns and, when passing a given frequency, are activated, showing its characteristics. It is enough to run the sound file once and the program will remember what, where and when it sounded. This layout can be saved as a file with its own extension. The next time you start this music video, you will not need to configure the analyzer again. Just load the saved installation. The same applies to the arrangement of windows within the program.

Using PAS ANALIS CENTER as an example, let's look at the items in the analyzer settings. They are standard for other similar programs. Unless you come across an analyzer with a Chinese interface.

Spectrum Analyzer Window

It is a frequency grid marked by frequencies horizontally and dB or volts vertically. The last two are switched between each other by pressing the Space Bar on the keyboard.

    FFT length selects the filter shape and its girth range.

    Scale fine-tuning the filter based on the harmonics of the sound file.

    DisplayConfiguring the graphical shell of the analyzer window. Grid and peak field on/off functions.

    Kind method for displaying harmonics in the analyzer window. Useful when working with rollers of different dynamics. Thus, the method of displaying a smooth, wide melody may be inconvenient due to the fact that smoothly tuned harmonics will jump wildly when running a file with rock music.

    Peaks When listening to an audio file through an analyzer, you can see how the dominant frequencies are highlighted in white letters, showing the level and harmonics of a given peak. How this will happen and when is configured in this tab.

    Decay Speed ​​and delay of harmonic graphics. Very useful when you need to accurately understand the amplitude of a sound. The same effect is achieved with one click of the left mouse button in the work area. The click point value appears and the image freezes. When you press the button, everything continues in real time.

Spectrogram window. Spectrogram Window

The difference between a spectrogram and spectral analysis is that a spectrogram is an imprint of events in sound file and allows you to compare harmonics throughout the video. Spectral analysis is only a real-time reading of harmonics, the exact location of which is impossible to remember. The color shades of the spectrogram will help you find extra and missing frequencies, and evaluate the entire file.

    FFT lenght selection of signal presentation option and capture range.

    Scale adjusting the window for the sound file.

    The settings are made more precisely than in spectral analysis.

    Display - setting the graphic content of the window. The ability to change the color to the opposite direction, acceleration of the spectrum.

Outfit color mode selection. Very helpful. Personally, my eyes got tired after two minutes.

Oscilloscope Window

      It is better to learn how to use an oscilloscope from specialized literature or ask a familiar electronics engineer about this. In the program described, this device is presented only with basic functions, the settings of which are given below.

Scale Effect enables the following two effects:

Peak representation in harmonics of peak values ​​only

Split separation by channels by color Powered by a Quattro device, ACE provides 32-bit floating point digital signal processing with sampling rates up to 204.8 kHz on all channels simultaneously. All inputs are connected to a dedicated 24-bit sigma-delta ADC, and both outputs have their own 24-bit DACs. Inputs and outputs are protected by built-in anti-aliasing filters. Computing capabilities are provided in real time over 120 dB up to 94 kHz, while simultaneously measuring and displaying 1600 lines of transfer functions, coherence and all other related measurements, and easily providing high-resolution measurements up to 25600 lines, real-time zoom, 3D measurement and display "Waterfall" graphics and simultaneous recording to disk. All signal processing is performed by the central signal processor built into the Quattro; your Windows computer is only required as the operator interface and for displaying the data.

All possibilities in one device

Buying a spectrum analyzer is not a problem these days. It is important to understand that there are huge differences between these instruments, in particular, many companies offer compact instruments with different names: frequency spectrum analyzer, vibration spectrum analyzer, frequency spectrum analyzer, audio spectrum analyzer, etc. As a rule, all these devices are equipped with a screen with a scale on which the tester looks at the readings. Such a real-time spectrum analyzer typically does not record measured data and does not have the ability to conduct a full FFT analysis. The FFT spectrum decoding or fast discrete Fourier transform algorithm allows you to use the signal analyzer as a spectrum analyzer.

SIgnalCalc ACE digital spectrum analyzers combine all the capabilities of a portable analyzer in a compact package.

SignalCalc ACE allows for real-time analysis of vibroacoustic signals using FFT, with the ability to record raw data on HDD computer for subsequent thorough post-analysis.

Comprehensive Measurements

ACE Quattro performs analysis in the time, frequency, amplitude and order domains. Use synchronous averaging to reconstruct noisy repeating events. Compare signals to detect their similarity and overlap in time (correlation). Use spectrum analysis with FFT (Fast Fourier Transform) to identify dominant frequencies and spectral density levels. Determine the linearity of systems through their transfer functions, coherence, and responses to impulsive disturbances. Measure probability functions and check the normality of your experimental data.
High computation speed allows you to process overlapping data, which speeds up averaging. It also allows you to capture machine starts and stops, speech signatures and other rapidly changing time/frequency events. Your computer screen becomes an instant viewing window, displaying the occurrence and content of signals.

In this project, a real-time audio spectrum analyzer is implemented using an 8-bit PIC18F4550 microcontroller. Frequency spectrum analysis is performed by an optimized 64-bit Fast Fourier Transformation (FFT), written entirely in C. The output from the FFT is displayed using a 128x64 graphical LCD display for visualization sound signal in real time.

In order to perform FFT calculations on an audio signal, you need to prepare audio data for the PIC18F4550. The MCU has several analog-to-digital converters (ADCs) that can be used to measure voltages from 0V to 5V with 10-bit precision (0-1023). A typical audio line output signal is an analog wave with an amplitude of 2V relative to 0V (i.e. an AC signal ranging from +1V to -1V), as shown in the following waveform (from the W2 pin of the demo board):

The picture shows a full size 5000 Hz sine wave generated using a PC. If we were to feed this signal directly to the PIC, we would have a very low input voltage range (0-0.5V), and would only be able to sample the top of the signal, which would make the FFT incorrect.
In order to sample a signal correctly, you need to do two things. First, we must boost the signal to ensure that we can make the most of the 0-5V range. Second, we must shift the signal ground (0 volts) to the 2.5V "virtual ground". This will allow the PIC to sample both the positive and negative signals. For this purpose, a simple integrated amplifier (LM386-1) is used on the demo board. Since the chip is powered by a 0V and 5V supply, it has the convenient side effect of shifting the signal to the middle of the desired range. LM386-1 was used because it's cheap and easy, however you can use operational amplifier with full input and output signal swing to achieve this with multiple external elements.

The following waveform shows the signal from LM386-1 (for the signal shown above), the voltage limit set to 5 volts (from pin W3 of the demo board):

The hardware mixes the stereo line input using two 10k resistors that act as a simple mixer. The signal is then sent to LM386-1 through a 10kΩ potentiometer, which allows the signal to be adjusted. The output from the LM386-1 amplifier is then passed through a simple RC filter, which cuts the signal to approximately 10 kHz. Then the received signal is applied to the pin of the PIC18F4550 ADC. The 10 kHz filter acts as a "smoothing" filter for the FFT, which cannot correctly detect a signal with a frequency greater than 10 kHz. The RC filter is a very simple type of filter (and very inefficient), but was chosen because it is easy to manufacture and only requires 2 passive elements. Typically a professional spectrum analyzer will smooth the FFT at 80% of the Nyquist frequency (see below), but since we are limited by PIC speed, this is not possible in this project.

The demo board also controls a standard 128x64 dot matrix LCD display, as well as 3 LEDs (for testing sound-to-light conversion). Additionally, there are 2 switches that allow the user to control the output of the LCD depending on what is being measured and how it will be displayed. The second jack allows you to directly input the signal to other audio devices such as headphones or speakers.

Here circuit diagram demo board:

The board is single-sided and only pin-out components are used for ease of repeatability. I used a PIC18F4550 for the additional I/O pins, however it can be replaced with the smaller PIC18F2550 which is pin compatible. The scheme is simple enough to build on breadboard, if you want to experiment with tracing. Here is a drawing of the board that can be downloaded below.

Firmware

The firmware is written entirely in C and can be downloaded below. The firmware is divided into 4 parts:

ADC samples

The ADC samples the voltage level on RA0 every 50 µs. This gives us a sampling rate of 20 kHz (20,000 times per second). For FFT, it is important that samples are taken evenly and accurately. To achieve this, there is a slight delay in the sampling cycle, which is calibrated using an oscilloscope on pin W4 of the demo board. The total duty cycle of the rectangular signal should be exactly 50 μs. The ADC samples at full 10-bit resolution and is then LSB-shifted with 512 to set the input signal's virtual ground back to zero. This means that the resulting samples range from -512 to +512 exactly as required by the FFT mathematics.

ADC routing takes just over 64x50 Us = 32 ms (3200 µs) to run time for each cycle.

64-bit FFT

The FFT routine was taken from an example provided on the Internet (links to the source code can be found in source code). The mathematics of FFT is complex and I don't pretend to fully understand it! The code has been reduced to the minimum required commands and ported to the PIC18F. The PIC18F4550 has a hardware 8x8 multiply function in the processor's ALU, so I also optimized the calculations to ensure the compiler made proper use of the chip's capabilities.

The fact that the 18F has an 8x8 hardware multiplier is really key to how such a low-power chip can calculate real-time FFTs. Cycle speed is an advantage even when compared to 64-bit array computing.

Absolute value calculation

The output from the FFT is 32 "complex" numbers, which consist of a real and an imaginary part, represented by two arrays (you should read about FFT on Google if you want to know more). In order to show the result in a meaningful way, it is necessary to calculate the absolute value of the complex number, which is done by using the Pythagorean calculation to calculate the distance of the complex number from the origin 0. This involves calculating the root of the number, which is implemented software very fast, using integer SQRT() is equivalent since any floating point operations will be too slow.

The FFT and absolute magnitude calculation procedure takes approximately 70 ms (7000 μs) for each cycle

LCD Update

The 128x64 LCD display needs to update as quickly as possible. To do this, I used a very simple diagram drawing algorithm that requires the minimum possible number of commands to the display.

Two switches on the board allow the user to switch between x1 and x8 output boost (since the music frequency is quite low on average), as well as between linear output or logarithmic output (dB based). It's simple different ways output display depending on whether you want an accurate representation of the frequency level, or a more visually pleasing output.

The LCD update procedure takes approximately 45ms for each update.

Overall (average) FFT speed

The approximate speed on the spectrum analyzer display is one frame per 150 ms, resulting in a total frame rate of about 6.5 frames per second (or 10 frames per second without an LCD display). This could easily be improved by reducing the required frequency blocks (which would reduce sampling and FFT execution time) or by using a faster updating display device. If you would like to use an FFT to drive the LEDs of a lighting device, you can easily do both.

Frequency blocks

The FFT's Nyquist frequency (the highest frequency it can detect) is 10 kHz. The 32 frequency blocks are evenly distributed throughout the entire range, however, due to the operation of the FFT routine, the lower blocks cannot be used. This means that the displayed frequency for each block looks like in the following way(in Hz):

  • 1: 312.5 - 625
  • 2: 625 - 937.5
  • 3: 937.5 - 1250
  • 4: 1250 - 1562.5
  • 5: 1562.5 - 1875
  • 6: 1875 - 2187.5
  • 7: 2187.5 - 2500
  • 8: 2500 - 2812.5
  • 9: 2812.5 - 3125
  • 10: 3125 - 3437.5
  • 11: 3437.5 - 3750
  • 12: 3750 - 4062.5
  • 13: 4062.5 - 4375
  • 14: 4375 - 4687.5
  • 15: 4687.5 - 5000
  • 16: 5000 - 5312.5
  • 17: 5312.5 - 5625
  • 18: 5625 - 5937.5
  • 19: 5937.5 - 6250
  • 20: 6250 - 6562.5
  • 21: 6562.5 - 6875
  • 22: 6875 - 7187.5
  • 23: 7187.5 - 7500
  • 24: 7500 - 7812.5
  • 25: 7812.5 - 8125
  • 26: 8125 - 8437.5
  • 27: 8437.5 - 8750
  • 28: 8750 - 9062.5
  • 29: 9062.5 - 9375
  • 30: 9375 - 9687.5
  • 31: 9687.5 - 10000

Conclusion

I have no doubt that software and Hardware could be improved. I'm not an FFT expert, but I'd like to hear any ideas on how to speed up this process. Also, the anti-aliasing filter on the demo board is not that effective and could easily be replaced with an op-amp based filter. I just don't want to use more than the minimum hardware required to get the job done.

I would also like to say a special thank you to my good friend Richard Stagg, without his mathematical persistence this project would probably never have been completed!

List of radioelements

Designation Type Denomination Quantity NoteShopMy notepad
U1 MK PIC 8-bit

PIC18F4550

1 To notepad
U2 Audio amplifier

LM386

1 To notepad
U3 Linear regulator

LM7805CT

1 To notepad
D1 Rectifier diode

1N4001

1 To notepad
C1 Capacitor1 nF1 To notepad
C2 Capacitor47 nF1 To notepad
C3-C5, C7 Capacitor100 nF3 To notepad
C6 Electrolytic capacitor10 µF1 To notepad
R1, R2, R5, R7, R9, R10 Resistor

10 kOhm

1