Fourier transform avr. Spectral analysis of signals. Discrete Signals and Discrete Fourier Transform

In many cases, the task of obtaining (calculating) the signal spectrum is as follows. There is an ADC, which with a sampling rate Fd converts a continuous signal arriving at its input during the time T into digital samples - N pieces. Further, the array of samples is fed into a certain program that outputs N / 2 of some numerical values ​​(a programmer who pulled from the Internet wrote a program, claims that it does the Fourier transform).

To check if the program is working correctly, let's form an array of samples as the sum of two sinusoids sin (10 * 2 * pi * x) + 0.5 * sin (5 * 2 * pi * x) and slip it into the program. The program drew the following:

Fig. 1 The graph of the time function of the signal


Fig. 2 Signal spectrum graph

The spectrum graph has two sticks (harmonics) of 5 Hz with an amplitude of 0.5 V and 10 Hz - with an amplitude of 1 V, everything is as in the formula of the original signal. Everything is fine, well done programmer! The program is working correctly.

This means that if we feed a real signal from a mixture of two sinusoids to the ADC input, then we will get a similar spectrum, consisting of two harmonics.

Total, our real measured signal, lasting 5 sec, digitized ADC, that is, presented discrete counts, has discrete non-periodic range.

From a mathematical point of view, how many mistakes are there in this phrase?

Now the bosses decided we decided that 5 seconds is too long, let's measure the signal in 0.5 seconds.



Fig. 3 Graph of the function sin (10 * 2 * pi * x) + 0.5 * sin (5 * 2 * pi * x) at a measurement period of 0.5 sec


Fig. 4 Function spectrum

Something seems to be wrong! The 10 Hz harmonic is drawn normally, and instead of the 5 Hz stick, some incomprehensible harmonics appeared. We look on the Internet, what and how ...

In, they say that zeros must be added to the end of the sample and the spectrum will be drawn normal.


Fig. 5 We finished off zeros up to 5 sec


Fig. 6 Received the spectrum

Still not what it was at 5 seconds. We'll have to deal with the theory. Go to Wikipedia- source of knowledge.

2. Continuous function and its representation by the Fourier series

Mathematically, our signal with a duration of T seconds is a function f (x) defined on the interval (0, T) (X in this case is time). Such a function can always be represented as a sum of harmonic functions (sinusoids or cosines) of the form:

(1), where:

K - number of trigonometric function (number of harmonic component, number of harmonic)
T - the segment where the function is defined (signal duration)
Ak is the amplitude of the kth harmonic component,
θk is the initial phase of the kth harmonic component

What does it mean to "represent a function as the sum of a series"? This means that by adding at each point the values ​​of the harmonic components of the Fourier series, we get the value of our function at this point.

(More strictly, the root-mean-square deviation of the series from the function f (x) will tend to zero, but despite the root-mean-square convergence, the Fourier series of the function, generally speaking, is not obliged to converge to it pointwise. See https://ru.wikipedia.org/ wiki / Fourier_Row.)

This series can also be written as:

(2),
where , k-th complex amplitude.

The relationship between the coefficients (1) and (3) is expressed by the following formulas:

Note that all these three representations of the Fourier series are completely equivalent. Sometimes, when working with Fourier series, it is more convenient to use exponents of the imaginary argument instead of sines and cosines, that is, to use the Fourier transform in complex form. But it is convenient for us to use formula (1), where the Fourier series is presented as a sum of cosine waves with the corresponding amplitudes and phases. In any case, it is incorrect to say that the result of the Fourier transform of a real signal will be the complex amplitudes of the harmonics. As the Wiki correctly says, "The Fourier transform (ℱ) is an operation that assigns one function to a real variable to another function, also a real variable."

Total:
The mathematical basis for spectral analysis of signals is the Fourier transform.

The Fourier transform allows you to represent the continuous function f (x) (signal), defined on the segment (0, T) as the sum of an infinite number (infinite series) trigonometric functions(sine and / or cosine) with certain amplitudes and phases, also considered on the interval (0, T). Such a series is called a Fourier series.

Let's note some more points, the understanding of which is required for the correct application of the Fourier transform to signal analysis. If we consider the Fourier series (the sum of sinusoids) on the entire X axis, then we can see that outside the segment (0, T), the function represented by the Fourier series will periodically repeat our function.

For example, in the graph in Fig. 7, the original function is defined on the segment (-T \ 2, + T \ 2), and the Fourier series represents a periodic function defined on the entire x-axis.

This is because sinusoids themselves are periodic functions, and accordingly, their sum will be a periodic function.


Fig. 7 Representation of a non-periodic original function by the Fourier series

In this way:

Our original function is continuous, non-periodic, defined on some segment of length T.
The spectrum of this function is discrete, that is, it is presented in the form of an infinite series of harmonic components - the Fourier series.
In fact, the Fourier series defines a certain periodic function that coincides with ours on the segment (0, T), but for us this periodicity is not essential.

The periods of the harmonic components are multiples of the value of the segment (0, T), on which the original function f (x) is defined. In other words, the periods of the harmonics are multiples of the duration of the signal measurement. For example, the period of the first harmonic of the Fourier series is equal to the interval T, on which the function f (x) is defined. The period of the second harmonic of the Fourier series is equal to the interval T / 2. And so on (see fig. 8).


Fig. 8 Periods (frequencies) of harmonic components of the Fourier series (here T = 2π)

Accordingly, the frequencies of the harmonic components are multiples of 1 / T. That is, the frequencies of the harmonic components Fk are equal to Fk = k \ T, where k ranges from 0 to ∞, for example k = 0 F0 = 0; k = 1 F1 = 1 \ T; k = 2 F2 = 2 \ T; k = 3 F3 = 3 \ T; ... Fk = k \ T (at zero frequency - constant component).

Let our original function be a signal recorded for T = 1 sec. Then the period of the first harmonic will be equal to the duration of our signal T1 = T = 1 sec and the frequency of the harmonic is 1 Hz. The second harmonic period will be equal to the signal duration divided by 2 (T2 = T / 2 = 0.5 sec) and the frequency is 2 Hz. For the third harmonic, T3 = T / 3 sec and the frequency is 3 Hz. Etc.

The step between harmonics in this case is 1 Hz.

Thus, a signal with a duration of 1 sec can be decomposed into harmonic components (to obtain a spectrum) with a frequency resolution of 1 Hz.
To increase the resolution by 2 times to 0.5 Hz, it is necessary to increase the measurement duration by 2 times - up to 2 sec. A signal with a duration of 10 seconds can be decomposed into harmonic components (to obtain a spectrum) with a frequency resolution of 0.1 Hz. There is no other way to increase the frequency resolution.

There is a way to artificially increase the signal duration by adding zeros to the sample array. But it does not increase the real frequency resolution.

3. Discrete signals and discrete Fourier transform

With the development of digital technology, the methods of storing measurement data (signals) have also changed. If earlier the signal could be recorded on a tape recorder and stored on tape in analog form, now the signals are digitized and stored in files in the computer's memory as a set of numbers (counts).

A typical scheme for measuring and digitizing a signal is as follows.


Fig. 9 Diagram of the measuring channel

The signal from the measuring transducer arrives at the ADC for a period of time T. The samples of the signal (sample) obtained during the time T are transferred to the computer and stored in memory.


Fig. 10 Digitized signal - N samples obtained during time T

What are the requirements for the signal digitization parameters? A device that converts an input analog signal into a discrete code (digital signal) is called an analog-to-digital converter (ADC) (Wiki).

One of the main parameters of the ADC is the maximum sampling rate (or sampling rate, English sample rate) - the sampling rate of a continuous signal in time during its sampling. Measured in hertz. ((Wiki))

According to the Kotelnikov theorem, if a continuous signal has a spectrum limited by the frequency Fmax, then it can be completely and unambiguously reconstructed from its discrete samples taken at time intervals , i.e. with a frequency of Fd ≥ 2 * Fmax, where Fd is the sampling frequency; Fmax is the maximum frequency of the signal spectrum. In other words, the signal sampling frequency (ADC sampling frequency) must be at least 2 times higher than the maximum frequency of the signal that we want to measure.

And what will happen if we take samples with a lower frequency than is required by the Kotelnikov theorem?

In this case, the effect of "aliasing" (aka stroboscopic effect, moiré effect) occurs, in which a high-frequency signal, after digitization, turns into a low-frequency signal, which in fact does not exist. In fig. 11 high frequency red sine wave is a real signal. The blue sinusoid of a lower frequency is a dummy signal that arises due to the fact that during the sampling time it manages to pass more than half a period of the high-frequency signal.


Rice. 11. The appearance of a false signal of low frequency with insufficiently high sampling rate

To avoid the effect of aliasing, a special anti-aliasing filter is installed in front of the ADC - a low-pass filter (filter low frequencies), which passes frequencies below half the sampling frequency of the ADC, and cuts higher frequencies.

In order to calculate the spectrum of the signal from its discrete samples, the discrete Fourier transform (DFT) is used. Note again that the spectrum of the discrete signal is "by definition" limited by the frequency Fmax, less than half of the sampling frequency Fd. Therefore, the spectrum of a discrete signal can be represented by the sum of a finite number of harmonics, in contrast to the infinite sum for the Fourier series of a continuous signal, the spectrum of which can be unlimited. According to the Kotelnikov theorem, the maximum frequency of a harmonic should be such that it has at least two counts, so the number of harmonics is equal to half the number of samples of a discrete signal. That is, if there are N samples in the sample, then the number of harmonics in the spectrum will be equal to N / 2.

Consider now the discrete Fourier transform (DFT).

Comparing with the Fourier series

We see that they coincide, except that the time in the DFT is discrete and the number of harmonics is limited to N / 2, which is half the number of counts.

DFT formulas are written in dimensionless integer variables k, s, where k are the numbers of signal samples, s are the numbers of spectral components.
The value of s shows the number of total harmonic oscillations at the period T (the duration of the signal measurement). Discrete Fourier transform is used to find the amplitudes and phases of harmonics numerically, i.e. "on the computer"

Going back to the results at the beginning. As mentioned above, when expanding a non-periodic function (our signal) in a Fourier series, the resulting Fourier series actually corresponds to a periodic function with a period T. (Fig. 12).


Fig. 12 Periodic function f (x) with a period T0, with a measurement period T> T0

As can be seen in Fig. 12, the function f (x) is periodic with a period T0. However, due to the fact that the duration of the measuring sample T does not coincide with the period of the function T0, the function obtained as a Fourier series has a discontinuity at the point T. As a result, the spectrum of this function will contain a large number of high-frequency harmonics. If the duration of the measurement sample T coincided with the period of the function T0, then in the spectrum obtained after the Fourier transform, only the first harmonic (sinusoid with a period equal to the duration of the sample) would be present, since the function f (x) is a sinusoid.

In other words, the DFT program “does not know” that our signal is a “piece of a sinusoid,” but tries to represent a periodic function as a series, which has a discontinuity due to the inconsistency of individual pieces of a sinusoid.

As a result, harmonics appear in the spectrum, which should summarize the shape of the function, including this discontinuity.

Thus, in order to obtain a "correct" spectrum of a signal, which is the sum of several sinusoids with different periods, it is necessary that an integer number of periods of each sinusoid fit into the signal measurement period. In practice, this condition can be met for a sufficiently long signal measurement duration.


Fig. 13 An example of a function and spectrum of the signal of the kinematic error of the gearbox

With a shorter duration, the picture will look "worse":


Fig. 14 Example of rotor vibration signal function and spectrum

In practice, it can be difficult to understand where are the "real components" and where are the "artifacts" caused by the fact that the periods of the components and the duration of the signal sampling are not multiple, or the "jumps and breaks" of the waveform. Of course, the words "real components" and "artifacts" are not in vain taken in quotes. The presence of many harmonics on the spectrum graph does not mean that our signal in reality "consists" of them. It is like thinking that the number 7 "consists" of the numbers 3 and 4. The number 7 can be represented as the sum of the numbers 3 and 4 - this is correct.

So our signal ... or rather not even "our signal", but a periodic function composed by repeating our signal (sample) can be represented as a sum of harmonics (sinusoids) with certain amplitudes and phases. But in many cases that are important for practice (see the figures above), it is really possible to associate the harmonics obtained in the spectrum with real processes that have a cyclical nature and make a significant contribution to the signal shape.

Some results

1. The real measured signal, duration T sec, digitized by the ADC, that is, represented by a set of discrete samples (N pieces), has a discrete non-periodic spectrum, represented by a set of harmonics (N / 2 pieces).

2. The signal is represented by a set of real values ​​and its spectrum is represented by a set of real values. Harmonic frequencies are positive. The fact that mathematicians find it more convenient to represent the spectrum in a complex form using negative frequencies does not mean that "this is correct" and "this should always be done."

3. The signal measured at the time interval T is determined only at the time interval T. What was before we started measuring the signal, and what will happen after that - this is unknown to science. And in our case, it is not interesting. The DFT of a time-limited signal gives its “true” spectrum, in the sense that, under certain conditions, it allows the amplitude and frequency of its components to be calculated.

Used materials and other useful materials.

Fourier's theorem states that any signal can be expanded in a series in terms of an orthonormal set of periodic functions (for example, in sines and cosines) with frequencies that are multiples of the frequency of the periodic signal. Thus, the spectral analysis of the signal is based on the search for weight coefficients (in the general case, complex), the modulus of which corresponds to the fraction of the oscillation power of the corresponding harmonic introduced into the general superposition of all harmonics.

Fast Fourier Transform

Fast Fourier Transform is a computation algorithm that successfully uses the periodicity properties of trigonometric functions to avoid unnecessary calculations in the discrete Fourier transform (DFT), which makes it faster to find the coefficients in the Fourier expansion. The main difference from discrete transformation is only in the method of calculating numerical values ​​(algorithm), and not in the signal processing itself. For both FFT and DFT, the computation results are the same. The only requirement for the FFT algorithm is the sample size, which is a multiple of N = 2L, where L is any positive integer. The most common base 2 FFT algorithms are time decimation and frequency decimation.

In this paper, we have implemented a base 2 FFT algorithm with decimation in time (Cooley - Tukey algorithm). It is easy to obtain by examining some of the DFT patterns. Let's introduce the so-called rotational factor:

In this case, in the DFT, the Fourier coefficients for a number of signal values ​​(f0, f1, ..., fN-1) are expressed by the ratio:

Consider a series of signals of 4 values: (f0, f1, f2, f3). We represent the Fourier transform in matrix form (the normalization factor 1 / N is introduced into the column vector Сij on the right side of the expression):

Having written down the rotary coefficients according to the Euler formula and having determined their values ​​at k = 0, 1, 2, .. 9, it is possible to construct a diagram (Fig. 2), from which the regularity of the repeated coefficients can be seen.

Figure 2. Power series w for N = 4

Substituting the numerical values ​​in (4), we get:

That is, the values ​​of w, starting from w4, are equal to the corresponding value from w0 to w3. Next, we rewrite the matrix equation (4) in a non-standard form (similar designations were introduced for clarity of further operations):

Let's swap the columns of the matrix, dividing it into two groups: by even f0, f2 and odd f1, f3 indices:

Let's take into account that wk + 1 = wkw1, then expression (6) will be rewritten as:

Using the ratios:

We get the required expansion coefficients in the form of a column vector with cell values:

The graphical representation of the algorithm (Fig. 3) looks like a butterfly with open wings, therefore this method of calculation is called a "butterfly".

Figure 3. Graph "Butterfly" for a row of 4 members

So, at the first step of the algorithm, there is a partition by even and odd indices of the members of a number of signal values. Then the "butterfly" graph is executed, it consists of two steps, their number is equal to a power of two of the sample size (N = 4 = 22). At each stage, two "butterflies" are performed and their total number is unchanged. Each butterfly operation corresponds to one multiplication operation. For comparison: in a DFT with a sample (f0, f1, f2, f3), the multiplication operation would have to be performed 4 × 4 = 16 times, and in the case of an FFT, only 4 times.

This series can also be written as:

(2),
where, k-th complex amplitude.

The relationship between the coefficients (1) and (3) is expressed by the following formulas:

Note that all these three representations of the Fourier series are completely equivalent. Sometimes, when working with Fourier series, it is more convenient to use exponents of the imaginary argument instead of sines and cosines, that is, to use the Fourier transform in complex form. But it is convenient for us to use formula (1), where the Fourier series is presented as a sum of cosine waves with the corresponding amplitudes and phases. In any case, it is incorrect to say that the result of the Fourier transform of a real signal will be the complex amplitudes of the harmonics. As the wiki correctly says, "The Fourier transform (?) Is an operation that assigns one function to a real variable to another function, also a real variable."

Total:
The mathematical basis for spectral analysis of signals is the Fourier transform.

The Fourier transform allows you to represent the continuous function f (x) (signal), defined on the segment (0, T) as the sum of an infinite number (infinite series) of trigonometric functions (sinusoids and \ or cosines) with certain amplitudes and phases, also considered on the segment (0, T). Such a series is called a Fourier series.

Let's note some more points, the understanding of which is required for the correct application of the Fourier transform to signal analysis. If we consider the Fourier series (the sum of sinusoids) on the entire X axis, then we can see that outside the segment (0, T), the function represented by the Fourier series will periodically repeat our function.

For example, in the graph in Fig. 7, the original function is defined on the segment (-T \ 2, + T \ 2), and the Fourier series represents a periodic function defined on the entire x-axis.

This is because sinusoids themselves are periodic functions, and accordingly, their sum will be a periodic function.


Fig. 7 Representation of a non-periodic original function by the Fourier series

In this way:

Our original function is continuous, non-periodic, defined on some segment of length T.
The spectrum of this function is discrete, that is, it is presented in the form of an infinite series of harmonic components - the Fourier series.
In fact, the Fourier series defines a certain periodic function that coincides with ours on the segment (0, T), but for us this periodicity is not essential.

The periods of the harmonic components are multiples of the value of the segment (0, T), on which the original function f (x) is defined. In other words, the periods of the harmonics are multiples of the duration of the signal measurement. For example, the period of the first harmonic of the Fourier series is equal to the interval T, on which the function f (x) is defined. The period of the second harmonic of the Fourier series is equal to the interval T / 2. And so on (see fig. 8).


Fig. 8 Periods (frequencies) of harmonic components of the Fourier series (here T = 2?)

Accordingly, the frequencies of the harmonic components are multiples of 1 / T. That is, the frequencies of the harmonic components Fk are equal to Fk = k \ T, where k ranges from 0 to?, For example, k = 0 F0 = 0; k = 1 F1 = 1 \ T; k = 2 F2 = 2 \ T; k = 3 F3 = 3 \ T; ... Fk = k \ T (at zero frequency - constant component).

Let our original function be a signal recorded for T = 1 sec. Then the period of the first harmonic will be equal to the duration of our signal T1 = T = 1 sec and the frequency of the harmonic is 1 Hz. The second harmonic period will be equal to the signal duration divided by 2 (T2 = T / 2 = 0.5 sec) and the frequency is 2 Hz. For the third harmonic, T3 = T / 3 sec and the frequency is 3 Hz. Etc.

The step between harmonics in this case is 1 Hz.

Thus, a signal with a duration of 1 sec can be decomposed into harmonic components (to obtain a spectrum) with a frequency resolution of 1 Hz.
To increase the resolution by 2 times to 0.5 Hz, it is necessary to increase the measurement duration by 2 times - up to 2 sec. A signal with a duration of 10 seconds can be decomposed into harmonic components (to obtain a spectrum) with a frequency resolution of 0.1 Hz. There is no other way to increase the frequency resolution.

There is a way to artificially increase the signal duration by adding zeros to the sample array. But it does not increase the real frequency resolution.

3. Discrete signals and discrete Fourier transform

With the development of digital technology, the methods of storing measurement data (signals) have also changed. If earlier the signal could be recorded on a tape recorder and stored on tape in analog form, now the signals are digitized and stored in files in the computer's memory as a set of numbers (counts).

A typical scheme for measuring and digitizing a signal is as follows.


Fig. 9 Diagram of the measuring channel

The signal from the measuring transducer arrives at the ADC for a period of time T. The samples of the signal (sample) obtained during the time T are transferred to the computer and stored in memory.


Fig. 10 Digitized signal - N samples obtained during time T

What are the requirements for the signal digitization parameters? A device that converts an input analog signal into a discrete code (digital signal) is called an analog-to-digital converter (ADC) (Wiki).

One of the main parameters of the ADC is the maximum sampling rate (or sampling rate, English sample rate) - the sampling rate of a continuous signal in time during its sampling. Measured in hertz. ((Wiki))

According to the Kotelnikov theorem, if a continuous signal has a spectrum limited by the frequency Fmax, then it can be completely and unambiguously reconstructed from its discrete samples taken at time intervals , i.e. with a frequency Fd? 2 * Fmax, where Fd is the sampling frequency; Fmax is the maximum frequency of the signal spectrum. In other words, the signal sampling frequency (ADC sampling frequency) must be at least 2 times higher than the maximum frequency of the signal that we want to measure.

And what will happen if we take samples with a lower frequency than is required by the Kotelnikov theorem?

In this case, the effect of "aliasing" (aka stroboscopic effect, moiré effect) occurs, in which a high-frequency signal, after digitization, turns into a low-frequency signal, which in fact does not exist. In fig. 5 high frequency red sine wave is real signal. The blue sinusoid of a lower frequency is a dummy signal that arises due to the fact that during the sampling time it manages to pass more than half a period of the high-frequency signal.


Rice. 11. The appearance of a false signal of low frequency with insufficiently high sampling rate

To avoid the effect of aliasing, a special anti-aliasing filter is installed in front of the ADC - a low-pass filter (low-pass filter), which passes frequencies below half the sampling frequency of the ADC, and cuts higher frequencies.

In order to calculate the spectrum of the signal from its discrete samples, the discrete Fourier transform (DFT) is used. Note again that the spectrum of the discrete signal is "by definition" limited by the frequency Fmax, less than half of the sampling frequency Fd. Therefore, the spectrum of a discrete signal can be represented by the sum of a finite number of harmonics, in contrast to the infinite sum for the Fourier series of a continuous signal, the spectrum of which can be unlimited. According to the Kotelnikov theorem, the maximum frequency of a harmonic should be such that it has at least two counts, so the number of harmonics is equal to half the number of samples of a discrete signal. That is, if there are N samples in the sample, then the number of harmonics in the spectrum will be equal to N / 2.

Consider now the discrete Fourier transform (DFT).

Comparing with the Fourier series

We see that they coincide, except that the time in the DFT is discrete and the number of harmonics is limited to N / 2, which is half the number of counts.

DFT formulas are written in dimensionless integer variables k, s, where k are the numbers of signal samples, s are the numbers of spectral components.
The value of s shows the number of total harmonic oscillations at the period T (the duration of the signal measurement). Discrete Fourier transform is used to find the amplitudes and phases of harmonics numerically, i.e. "on the computer"

Going back to the results at the beginning. As mentioned above, when expanding a non-periodic function (our signal) in a Fourier series, the resulting Fourier series actually corresponds to a periodic function with a period T. (Fig. 12).


Fig. 12 Periodic function f (x) with a period T0, with a measurement period T> T0

As can be seen in Fig. 12, the function f (x) is periodic with a period T0. However, due to the fact that the duration of the measuring sample T does not coincide with the period of the function T0, the function obtained as a Fourier series has a discontinuity at the point T. As a result, the spectrum of this function will contain a large number of high-frequency harmonics. If the duration of the measurement sample T coincided with the period of the function T0, then in the spectrum obtained after the Fourier transform, only the first harmonic (sinusoid with a period equal to the duration of the sample) would be present, since the function f (x) is a sinusoid.

In other words, the DFT program “does not know” that our signal is a “piece of a sinusoid,” but tries to represent a periodic function as a series, which has a discontinuity due to the inconsistency of individual pieces of a sinusoid.

As a result, harmonics appear in the spectrum, which should summarize the shape of the function, including this discontinuity.

Thus, in order to obtain a "correct" spectrum of a signal, which is the sum of several sinusoids with different periods, it is necessary that an integer number of periods of each sinusoid fit into the signal measurement period. In practice, this condition can be met for a sufficiently long signal measurement duration.


Fig. 13 An example of a function and spectrum of the signal of the kinematic error of the gearbox

With a shorter duration, the picture will look "worse":


Fig. 14 Example of rotor vibration signal function and spectrum

In practice, it can be difficult to understand where are the "real components" and where are the "artifacts" caused by the fact that the periods of the components and the duration of the signal sampling are not multiple, or the "jumps and breaks" of the waveform. Of course, the words "real components" and "artifacts" are not in vain taken in quotes. The presence of many harmonics on the spectrum graph does not mean that our signal in reality "consists" of them. It is like thinking that the number 7 "consists" of the numbers 3 and 4. The number 7 can be represented as the sum of the numbers 3 and 4 - this is correct.

So our signal ... or rather not even "our signal", but a periodic function composed by repeating our signal (sample) can be represented as a sum of harmonics (sinusoids) with certain amplitudes and phases. But in many cases that are important for practice (see the figures above), it is really possible to associate the harmonics obtained in the spectrum with real processes that have a cyclical nature and make a significant contribution to the signal shape.

Some results

1. The real measured signal, duration T sec, digitized by the ADC, that is, represented by a set of discrete samples (N pieces), has a discrete non-periodic spectrum, represented by a set of harmonics (N / 2 pieces).

2. The signal is represented by a set of real values ​​and its spectrum is represented by a set of real values. Harmonic frequencies are positive. The fact that mathematicians find it more convenient to represent the spectrum in a complex form using negative frequencies does not mean that "this is correct" and "this should always be done."

3. The signal measured at the time interval T is determined only at the time interval T. What was before we started measuring the signal, and what will happen after that - this is unknown to science. And in our case, it is not interesting. The DFT of a time-limited signal gives its “true” spectrum, in the sense that, under certain conditions, it allows the amplitude and frequency of its components to be calculated.

Used materials and other useful materials.

This article describes a small audio spectrum analyzer (0 - 10 kHz), consisting of a 16x2 LCD and an ATmega32 microcontroller. A simple DFT (Discrete Fourier Transform) algorithm is used. FFT (Fast Fourier Transform) differs from DFT only in its higher speed but also in a more complex algorithm.

DFT is slow compared to FFT. This LCD spectrum analyzer does not require the high speed that the FFT can provide, and if the image on the screen changes at a rate of about 30 frames / sec, then this is more than enough to visualize the audio spectrum. Not recommended for LCD too high frequency updates. Audio with a sampling rate of 20 kHz gives 32 DFT points. Since the conversion result is symmetric, I only need to use the first 16 results. Accordingly, the maximum frequency is 10 kHz. So 10kHz / 16 = 625Hz.

You can increase the speed of calculating the DFT. If there is an N DFT point, then we need to find the sine and cosine (N ^ 2) / 2. For a 32-point DFT, we need to find the sine and cosine 512. Before looking for the sine and cosine, we need to find the angle (degrees) that takes some cpu time. To speed up this, tables for sine and cosine are made. The sine and cosine are made 16-bit variables by multiplying the sine and cosine values ​​by 10000. After the conversion, each result must be divided by 10000. Now you can calculate 120 32-point DFTs per second, which is more than enough for a spectrum analyzer.

Display

Custom LCD symbols loaded into 64 Bytes of the display's internal memory are used to display the bars.

Audio input

One of the most important parts of a spectrum analyzer is the acquisition of a signal from electret microphone... Particular attention should be paid to the design of a microphone preamplifier. We need to set a zero level at the ADC input and a maximum level equal to half the supply voltage, i.e. 2.5V. It can be supplied with voltage from -2.5V to + 2.5V. The preamplifier should be tuned so as not to exceed these limits. The circuit uses an LM324 operational amplifier as a pre-amplifier for the microphone.

A two-line character LCD is used as a display device. The main point in the implementation of this project is not the hardware part, but the software, or rather the implementation of the discrete Fourier transform (DFT) on an 8-bit microcontroller. It should be noted right away that the author is not an expert in this area and therefore started from the basics - with a simple discrete Fourier transform. The Fast Fourier Transform algorithm is not only fast but also quite complex.

Discrete Fourier Transform (in English literature DFT, Discrete Fourier Transform) is one of the Fourier transforms widely used in digital signal processing algorithms (its modifications are used in audio compression to MP3, image compression to JPEG, etc.), as well as in others. areas related to the analysis of frequencies in a discrete (for example, digitized analog) signal. The Discrete Fourier Transform requires a discrete function as input. Such functions are often created by sampling (sampling values ​​from continuous functions).

Schematic diagram of a spectrum analyzer sound signal is very simple and conditionally it can be divided into a digital part and an analog one.

The digital part is formed by a microcontroller and an LCD indicator connected to it. The microcontroller is clocked from a 16 MHz quartz resonator, and the +5 V supply voltage is used as a reference voltage for the microcontroller's ADC.
The data bus of the LCD indicator is connected to the C port of the microcontroller (I / O lines PC0-PC3), the control bus is connected to the D port (PD5, PD6) of the microcontroller. The indicator works in 4-bit mode. A variable resistor of 4.7 kΩ is used to adjust the contrast. To work with the indicator, custom symbols were created to display 8 horizontal bars of the analyzer, these custom symbols occupy all 64 Bytes of LCD indicator RAM.

The microcontroller is powered by an external 16 MHz quartz crystal.

The analog part of the device is the most important part and represents preamplifier the signal of an electret microphone, the output of which is connected to the ADC0 channel of the ADC built into the microcontroller. We need to set the zero level at the ADC input to exactly half the reference voltage, i.e. 2.5 V. In this case, we can use the positive and negative half-wave of the signal, but its amplitude should not exceed the specified limit, i.e. the gain must be fine tuned to prevent overload. All of the above conditions are met by a common low-power operational amplifier microcircuit.

The DFT algorithm is slightly slower compared to the FFT. But our spectrum analyzer does not require a high speed, and if it is able to provide a refresh rate of about 30 frames per second, this will be more than enough to visualize the spectrum of the audio signal. In any case, in our version it is possible to achieve a speed of 100 frames per second, but this is already too high a parameter value for a two-line character LCD indicator and it is not recommended. The sampling rate is 20 kHz for a 32-point discrete Fourier transform and since the result of the transform is symmetrical, we only need to use the first half, i.e. first 16 results. Therefore, we can display the frequency spectrum up to 10 kHz and the analyzer resolution is 10 kHz / 16 = 625 Hz.

The author of the design attempted to increase the speed of calculating the DFT. If this transformation has N points, then we have to find N2 / 2 sine and cosine values. For our 32 point transformation, we need to find 512 sine and cosine values. But, before finding them, we need to calculate the angle (degrees), which will take some processor time, so it was decided to use tables of values ​​for these calculations. The calculations in the microcontroller program do not use floating point calculations and double precision numbers (double), since this will take longer to process on an 8-bit microcontroller. Instead, the values ​​in the lookup tables use 16-bit integer data multiplied by 10000. Then, after performing the conversion, the results are divided by 10000. With this approach, it is possible to perform 120 32-point conversions per second, which is more than enough for our devices.

Demonstration of the spectrum analyzer on the ATmega32 microcontroller

Downloads

Source code (microcontroller program, sine, cosine and angle data tables) -

  • It is clear that on the AVR-ke it is difficult to go further than light and music, the parameters are not right. But 120 32-point conversions per second may be sufficient for most tasks. A sample of 625Hz, you can of course take another one, or rather, losing the refresh rate. It is worth noting that the MK will feel bad, in terms of performance, there is little else you hang on it. But here you can also organize the output of the result by hardware data transfer methods. Then it will be an auxiliary MC, and the main one will only receive data from it and process it compatible with other processes. By and large, it still rests on the frequency of the processor. Once it was possible to overclock the mega above 20 MHz, but for these tasks we will probably only get glitches at high frequencies. The idea is good, if only more mate part would be painted ... it is its implementation on MK
  • I also made more interesting analyzers: You Tube or a version on a color LCD: You Tube is based on the famous Chen library :)
  • "we need to calculate the angle (degrees)" Can someone tell you in more detail how the values ​​for these tables are calculated?
  • Everything is clear with the table of sines and cosines. Not clear how the values ​​in the degree_lookup table are calculated?