Fourier transform avr. Digital signal processing on ARM7 microcontrollers. Displaying a picture - about the benefits of reading datasheets

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 (DFT in English literature, 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 it is very simple and conditionally it can be divided into digital part and analog.

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 is a preamplifier of the electret microphone signal, 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 an 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 much high value parameter for the two-line character LCD display and is not recommended. The sampling rate is 20 kHz for the 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, as 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 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 of the mate part would be painted ... it is its implementation on MK
  • I also made more interesting analyzers: You Tube or a variant 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?

For digital signal processing (DSP), there are many specialized processors, this DSP from Texas Instruments TMS320 series, which includes simple integer cores, and such monsters as the C6000 family of floating point data processing. There is a whole series of ADSPs from Analog Devices (which includes the more or less universal BlackFin), and there are more simple solutions from MicroChip - dsPIC.

However, a dedicated DSP is good, but is it always so necessary? Yes, with a huge flow of information, it is simply irreplaceable, but there are also simpler processing tasks. Specifically, I was interested in the problem of double conversion - the audio signal is convolved, thereby obtaining a spectrum, then any operations can be performed on the spectrum and perform the inverse transformation, thereby obtaining a processed signal. All this needs to be done in real time and the quality should not be lower than the telephone one.

Now is not the year 2000, there are single-chip solutions based on powerful ARM7 / Cortex-M3 cores and have significantly dropped in price, they are 32-bit, they have a hardware implementation of the 32-bit multiplication operation (moreover, almost a DSP multiply-accumulate operation and 64 bit result), and the Cortex-M3 also includes hardware division.

I want to warn you right away that signal processing is not my profile, almost all knowledge (or rather understanding of principles) has been preserved from the institute, now I just wanted to check and implement them. This I mean that there may be inaccuracies in the description, substitution of concepts and so on. Actually, academic accuracy didn't bother me that much.

For almost any DSP, the above task is simple and straightforward. But how will the RISC core behave on it? general use? If we consider AVR or PIC, then they are hardly enough. Affects 8-bit and low clock speed. Although, Elm-Chan has designs in which he conducts an FFT on the AVR and draws the signal spectrum. However, in this case in real time, just visualization is done (with minimal processing accuracy), and not complete signal processing with acceptable audio quality.

LPC2146, based on the ARM7TDMI-S core and having a maximum clock frequency of 60 MHz (in practice, it does not work at 72 or even 84 MHz), was chosen as a test chip. Why? Firstly, I have a debug board for it, and secondly, there is an ADC and a DAC on board, i.e. a minimum external body kit is required.

A bit of theory

First of all, it was interesting to evaluate the performance of FFT (Fast Fourier Transform) on ARM microcontrollers. Based on this assessment, we can conclude: whether it has enough speed to process the audio data stream, and the signal with what sampling rate and how many channels can be processed on such a microcontroller.

Based on the Fourier transform, you can construct cunning filters (with very tempting characteristics). I was primarily interested in the tasks of changing the tone of the signal (raising and lowering the spectrum) and the "reflection" of the spectrum. The latter is required in SDR radios to listen to radio broadcasts in the lower LSB sideband.

I will not load with theory and explain what the Fourier Transform is, there are quite a lot of materials on this topic, from what I used: a wiki and a chapter from a very good and informative book.

Software implementation

There are a lot of software implementations of the FFT, however, I wrote my own. The main goal that I pursued: optimization of the code for a specific architecture. Firstly, I immediately focused on 32-bit, and secondly, only integer calculations were required and it was desirable to avoid the division operation. It is already problematic to choose something ready for these requirements.

All the constants that could be calculated in advance were calculated and placed in tables (these are mainly the values ​​of trigonometric functions). This is the main optimization of the algorithm, otherwise it almost completely repeats the described algorithm.

The most important is the requirement for integer computation. During the implementation process, an error was even made, due to which an overflow occurred in one of the 32-bit loop variables... Moreover, it did not occur on all test data, so it gave a decent headache until it was found.

Everything source texts I collected in one archive. The implementation is not final, there are duplicate calculations (the symmetry of the spectrum and phases is not taken into account), and optimization of the use of buffers is required, since at the moment too many are used for calculations random access memory(almost 12k for a 1024-point conversion).

First tests

Drum roll: I'm testing the conversion speed for a sample of 1024 points, the processor core frequency is 60MHz. Testing was carried out in an emulator, so it does not claim to be 100% accurate, but this result can be used as an indicator (in my previous experience, the emulator was lying, but not much). Test of the first version of the code, RealView MDK compiler, optimization option O3, ARM-mode of code generation.

So what we see:

6ms for each conversion, totaling a little over 12ms for "round trip" conversion. It turns out that with a sampling rate of 44100Hz (standard for audio) and samples with a resolution of up to 16 bits, the pure computation will take ~ 44 * 12ms = 528ms. And this is on the intermediate version of the firmware, when some code optimizations have not yet been completed (according to estimates, the algorithm can be accelerated almost 2 times)! In my opinion, it's just an excellent indicator.

In total, the core load is assumed to be in the region of 50%, another 50% remains for transformations over the spectrum and overhead costs when working with ADCs, DACs and other data transfers. If we lower the sampling rate to the "telephone" level (about 4800-9600Hz), then the kernel load will be even lower (about 15-30%).

So, the mathematical part is more or less clear. You can start with a specific implementation.

Iron

For test platform taken a debug board Keil MCB2140 with a speaker. A Mini-Jack cord is soldered to connect to the line-out of the device and the simplest input chain is assembled. As already mentioned, a speaker is already installed on the board, connected to the analog output of the microcontroller and there are control elements (button and potentiometer).

Here's a sketch of the input circuit:


The software was debugged in stages:

  1. Debugging of all necessary peripherals: ADC, DAC, timers, LED indication.
  2. Signal digitization test: I digitize the data at the required speed and put it in the buffer, then I extract the data from the buffer and play the signal. Those. simple shift of the signal in time, without any transformations. At this stage, the mechanism for working with 2 buffers is tested, which is necessary for further work.
  3. TO previous version the direct and inverse Fourier transforms are added. This test finally checks the correctness of the FFT code, as well as checking that the code fits into the available performance.
  4. After that, the main framework of the application is done, you can move on to practical applications.

The problem arose after adding an FFT to the code: extraneous noises and whistles appeared in the signal. In general, this behavior seemed to me rather strange, since without conversion, the signal passing through the digital path remained fairly clean. The first reason for this: after the analog circuit, the signal amplitude on the ADC was not complete (0-3.3V), but only within 0.5-2V at the maximum volume of the player, the second: quite strong noise due to integer calculations (+ -1 unit, which turned out to be sufficient for the appearance of audible interference).

To combat the first problem, it was decided to tune the analog part. And to solve the issue with noise - try using a lowpass filter.

Application 1: changing the tone of the signal

A potentiometer (variable resistor) is provided on the board, it can be used for control. In this case, it sets the shift of the signal spectrum up and down, quite enough to "transform" your favorite compositions.

Here's what happens in the frequency domain:


In this case, the conversion result is contained in 2 buffers. One buffer is real and the other is imaginary. The physical meaning of the numbers obtained in them: the real part contains the values ​​of the harmonics, in the imaginary part - the phase shift for these harmonics. Moreover, as you can see, the original signal is described by N-values, and after conversion, 2N-values ​​are obtained. The amount of information does not change, and an increase in 2 times the amount of information occurs due to the fact that the buffer data has redundancy in the form of duplicate values.

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:
Mathematical basis spectral analysis signals is the Fourier transform.

The Fourier transform allows you to represent a 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 cosine) 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, 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 interval (-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

Thus:

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 runs values ​​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 during 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 are no other ways 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 Measuring channel diagram

Signal with measuring transducer arrives at the ADC for a period of time T. The samples of the signal (sample) obtained during 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 rate; 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 more high frequencies stabs.

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 a 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 must be such that it has at least two counts, so the number of harmonics is equal to half the number of counts 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 in 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 measuring sample T coincided with the period of the function T0, then in the spectrum obtained after the Fourier transform, only the first harmonic would be present (a sinusoid with a period equal to the duration of the sample), 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 discrepancy between 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 Example of the 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 is 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 cyclic 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 happened before we started measuring the signal and what will happen after that 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 one to calculate the amplitude and frequency of its components.

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. A too high refresh rate is not recommended for the LCD. 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 10,000. After the conversion, each result must be divided by 10,000. 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 receiving the signal from an 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.

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 time T into digital samples - N pieces. Next, the array of samples is fed into some program, which produces 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

On the spectrum graph, there are 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 spectrum.

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 a 5 Hz stick, several 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 some 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 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 of spectral analysis of signals is the Fourier transform.

The Fourier transform allows you to represent a 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 cosine) 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, 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 interval (-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

Thus:

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 during 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 are no other ways 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 Measuring channel diagram

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 (low-pass filter), which passes frequencies below half the ADC sampling frequency, 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 a 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 must be such that it has at least two counts, so the number of harmonics is equal to half the number of counts 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 in 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 measuring sample T coincided with the period of the function T0, then in the spectrum obtained after the Fourier transform, only the first harmonic would be present (a sinusoid with a period equal to the duration of the sample), 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 discrepancy between 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 Example of the 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 is 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 cyclic 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 happened before we started measuring the signal and what will happen after that 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 one to calculate the amplitude and frequency of its components.

Used materials and other useful materials.