1
\$\begingroup\$

This is my first time studying the Fourier transform. I can't understand some basic and somewhat more "practical" concepts from the formulas .

EXAMPLE: I have a 1kHz sinewave with 1ms period. With my microcontroller, in that 1ms period I sample the sine wave getting, for example, 1024 samples.

  1. When is the right instant to calculate the FFT? Is it after one period? After two?

  2. What happens if I calculate the FFT after 1.5 periods? In my opinion, logically, the right instant to calculate the FFT of a sine wave is generally after a whole number of complete periods. In other words, it is better to calculate the FFT at the end of one period, two periods, three periods, etc., so that the signal is consistent with the samples of a complete period. I would like to have your confirmation, though.

  3. What is the difference between collecting 1024 samples in 1 period (1ms) and computing the FFT and collecting 2048 samples (twice as many) but computing the FFT after 2 periods (2ms)? In my opinion, logically, it is the same thing.

  4. If my reasoning for 1) and 2) is correct, how can I apply these two points if the frequency of the sine wave varies, for example, between 0.5kHz and 1.5kHz? In other words, after how many periods do I apply the FFT for a signal whose frequency varies between 0.5kHz and 1.5kHz? The period varies.

Please answer respecting the bulleted list so as not to confuse the different questions.

\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

When you calculate an FFT (or more strictly speaking a DFT, FFT is just a fast algorithm for calculating it), the algorithm treats the signal as if it was repeated indefinitely, aka a periodic or cyclic extension.

1 ) That means if you have the samples for one whole cycle of sinewave, and I mean samples 0 to N-1, where N samples make one exact cycle, this periodic extension to the signal will behave as you might expect it to. You will see one sinewave frequency.

2 ) If you have collected 1.5 cycles of sinewave, then the FFT will again do its periodic extension, and the large step between the last and first sample will get repeated as well, as if it's part of the signal. The result of that will be a splat of power in every output bin. Not what you want, but easy to spot.

Perhaps a worse case than that is where you collect 1.001 cycles of sinewave. It's difficult to see the error in the time domain, and the power in all the other frequencies might be low enough for you to miss what's gone wrong, and blame something else.

3 ) This periodic extension works fine for any number of whole cycles. You can collect one cycle, or two cycles, or 10, as long as they are whole cycles, you'll get the answer you expect. You will have more samples, for the same frequency range, so your output frequency resolution is higher for a large number of cycles than for one. We normally express this as the frequency resolution (in Hz) being the inverse of the length of the time record (in seconds).

4 ) But the real world isn't that tidy, and we are always having to cope with taking the FFT of an incomplete number of cycles. This is where windowing comes in. A window tapers the record off smoothly to near zero at both ends, so that there's a much reduced step when the periodic extension happens.

As a window reduces the effective length of the time domain data, by de-emphasising the data at the ends, the frequency resolution is worse than without a window. Generally, we need to capture several cycles to see a reasonable spectrum. There are different window shapes that are optimised for better frequency resolution, or better suppression of the power splat from the repeated step. There's a whole section of mathematics devoted to the construction and performance of these windows.

If the signal frequency is varying in time, whether linearly or non-linearly, then we need what is called a Short-Time FFT, STFT. In this, you split the signal up into short sections, and perform an FFT on each section. There is a tradeoff here between time and frequency resolution. For a short record, resolution is limited by the FFT. For a long record, resolution is limited by how much the signal frequency varies in that time. For any given rate of variation, there's an optimum window length. As before, you need at least 5 or so cycles in each record to have reasonable resolution.

The window choice for an STFT can make a small difference. With a varying signal, it tends to be better to use a wider window like a Hamming to minimise loss of frequency resolution, at the expense of poor suppression of the non-signal bins, compared to for instance a Blackman Harris or Gaussian 4. As the window loses data at the edge of the bins, successive records should be overlapped, typically by 50%, more overlap would result in little more benefit.

The STFT is a non-parametric method, which can be used on any signal, or multiple signals, any rate or linearity of variation, but only averagely well. If you have a good model of your process, you can get better resolution by model fitting, for instance with MUSIC or ESPRIT algorithms. These come at a cost of complexity, and performing very poorly on signals outside their model.

If you know you have a single frequency only, then a better method might be to estimate the frequency directly. One route is via a PLL, ideally digitally at this low rate. The other route is via FM demodulation, for which we first pass the signal through a Hilbert filter to make it into an analytic signal. Either technique has to be designed with an assumption of what the minimum frequency is. Either technique should be better than STFT for resolution, and cheaper in terms of calculations, because it's only dealing with a single frequency.

\$\endgroup\$
5
  • \$\begingroup\$ Thank you for the explanation! I'm asking you what I asked @Rohat. In point 4) I was referring to a hypothetical case where the signal varies its frequency nonlinearly. Imagine tying a resistive band around a patient's chest that varies in resistance depending on how much it is widened (how much he inhales/exhales). If we assume from a medical point of view that the frequency varies between [0.2Hz and 0.7Hz] ... it doesn't necessarily vary linearly within this range, because he could start running at any moment and then immediately stop and start again. How do you implement a good FFT here? \$\endgroup\$
    – KaleM
    Commented Nov 12, 2023 at 11:25
  • 1
    \$\begingroup\$ @KaleM too long for a comment, so I've added another para or two to my answer. \$\endgroup\$
    – Neil_UK
    Commented Nov 12, 2023 at 12:33
  • \$\begingroup\$ 5) So, in practice, as soon as the first half of the buffer of the ADC where I receive the data is filled ... I perform the FFT on that half. Same thing when the second half of the buffer is filled (i.e. I will do the FFT only on the second half of the buffer). Correct? Then should I sum the two results into a single array? \$\endgroup\$
    – KaleM
    Commented Nov 15, 2023 at 10:15
  • \$\begingroup\$ 6) What if I also want to calculate the magnitude of the FFT I just performed ... do I also calculate it on the first half of the buffer when it fills up and then on the second half when it fills up? Or should I calculate the magnitude on the total? (i.e., I should sum the two FFTs calculated on the two buffer halves and then find magnitude of the sum) \$\endgroup\$
    – KaleM
    Commented Nov 15, 2023 at 10:15
  • \$\begingroup\$ @KaleM 5) No. 6) No. You completely misunderstand. Look up Welch's Method on wikipedia. With the exception that instead of averaging all the periodograms as you would to estimate a stationary spectrum, you plot them against time to get the STFT plot, or you average a few neighbouring ones (rather than all of them) to improve the variance of the STFT plot at the expense of time resolution. \$\endgroup\$
    – Neil_UK
    Commented Nov 15, 2023 at 12:01
2
\$\begingroup\$

1 & 2) The data collection must be of an integer number of periods for best results. Otherwise the content output of FFT will include high frequency components that are not present in the original waveform. That is because the discontinuity caused by non-integer number of periods (i.e. the discontinuity that you can spot instantly when you put your n-th data collection next to your (n-1)th data collection) is basically a very high frequency which could possibly be beyond Nyquist frequency. Research terms here: windowing, smearing.

3) It's 1024 samples per period (1 ms) for both cases, so the FFT would produce the same thing. This is also a part of 1 and 2 above i.e. as long as you have integer number of periods for data collection you are fine.

4) I'm not sure I understood this question. FFT is applicable to periodic signals. If the frequency variation of the input signal is periodic/repetitive (for example, the frequency changes from 0.5 kHz to 1.5 kHz linearly in a second, and changes back to 0.5 kHz from 1.5 kHz linearly in half a second, and repeats this cycle continuously) then it's fine because the signal is still periodic even though the fundamental changes with time. Any signal can be represented as sum of sine waves so this periodic weird signal is still FFT-able. And, again, integer number of periods is required although in practice it's not always possible. Research terms again: windowing, smearing.

\$\endgroup\$
2
  • \$\begingroup\$ Thank you for the detailed explanation. In point 4) I was referring to a hypothetical case where the signal varies its frequency nonlinearly. Imagine tying a resistive band around a patient's chest that varies in resistance depending on how much it is widened (how much he inhales/exhales). If we assume from a medical point of view that the frequency varies between [0.2Hz and 0.7Hz] ... it doesn't necessarily vary linearly within this range, because he could start running at any moment and then immediately stop and start again. How do you implement a good FFT here? \$\endgroup\$
    – KaleM
    Commented Nov 12, 2023 at 11:24
  • 2
    \$\begingroup\$ @KaleM well both me and Neil mentioned windowing. Also check his updated answer as there's really a good explanation there. \$\endgroup\$ Commented Nov 13, 2023 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.