0
$\begingroup$

I'm using numpy.fft in python to compute Fast Fourier Transforms. In particular, I'm using rfft as I have a real signal and don't need negative frequencies.
My question is: my signal has 184320 points currently and encompasses multiple periods. Should I be truncating the signal after a certain number of periods for best results? I know the sample rate in which the data was collected. What's sparking this question is that applying various windows is changing the amplitudes at each frequency of interest quite significantly, and I'm wondering if perhaps I should be truncating my signal.

$\endgroup$
9
  • $\begingroup$ what's the sampling rate? $\endgroup$
    – Jdip
    Aug 16, 2022 at 19:10
  • $\begingroup$ @Jdip, my sampling rate is 15360 Hz $\endgroup$
    – Bern
    Aug 16, 2022 at 19:26
  • $\begingroup$ Read my edited answer. If you still have trouble, you can post the frequencies you’re trying to get an amplitude for and I’ll help you out further ;) $\endgroup$
    – Jdip
    Aug 16, 2022 at 19:31
  • $\begingroup$ Looking to compare my results so the frequencies I'm interested in are from 60 - 2940 (in increment steps of 60) $\endgroup$
    – Bern
    Aug 16, 2022 at 19:55
  • $\begingroup$ Ok so you need frequency bins $k=f_n \cdot nfft/15360$ at $f_n = n \cdot 60$ so $nfft = 256$ is the smallest chunk of your data you can use (assuming your signal is stationary, I.e the frequencies don’t change over time). You can also just use 15360 samples chunk, I.e 1s of your data, giving you exactly 1hz per bin $\endgroup$
    – Jdip
    Aug 16, 2022 at 20:10

3 Answers 3

0
$\begingroup$

Choose an appropriate FFT length (for your signal, such as 2^18), and scale your FFT result by $1/sum(\texttt{window})$ (for no window, that would be $1/N$ with $N$ your input signal length).

EDIT That's if you are set on using the FFT as an amplitude estimation tool. As pointed out by @OverLordGoldDragon, there are better tools for that.

EDIT: Updated answer with frequency precision and padding explained

Each frequency bin $k$ has center frequency $f_{k_{(\texttt{Hz})}} = kdF_{(\texttt{Hz})}$, with $dF_{(\texttt{Hz})} = f_s/\texttt{Nfft}$ the spectral precision, $\texttt{Nfft}$ the length of the DFT, i.e the length of your input sequence (padded or not), and $f_s$ the sampling frequency.

Here's an example: let $x(t)$ be a $0.5s$ long ($\texttt{Nfft} = 22050$) signal sampled at $44100 \texttt{Hz}$. Its DFT, $X[k]$, has precision $dF = 2 \texttt{Hz}$ with no padding. That means that component at $2000 \texttt{Hz}$ would exactly fall at bin $k = f / dF = f \cdot \texttt{Nfft}/f_s = 2000 \cdot 22050 / 44100 = 1000$

But a component at $2001 \texttt{Hz}$ would fall between bins 1000 and 1001 (because $f/dF = 1000.5$ so the fft output will spread between bins $1000$ and $1001$).
By padding your signal to twice its original length, you can double the signal length $\texttt{Nfft}$, effectively doubling the frequency precision $dF$ to $1\texttt{Hz}$, and a $2000 \texttt{Hz}$ component now falls at the center of bin $k = f \cdot \texttt{Nfft} /f_s = 2000 \cdot 44100 / 44100 = 2000$ and a component at $2001\texttt{Hz}$ falls at the center of bin $2001$ by the same calculations.

All this to say, if you know the frequencies of interest, you can deduce the exact frequency precision $dF$ you need, from there you can deduce the input length you need by doing $\texttt{Nfft} = f_s/dF$ and use that to guide how much padding/truncating you need to apply.

$\endgroup$
10
  • $\begingroup$ Thanks for the insight. I'm working on a project with the requirement that it be done using FFT. In the future I'll definitely check out the tools proposed. The signal that I'm dealing with can be considered as a bunch of sine waves added together (with a slight phase) so that the resulting waveform isn't a perfect sine wave. I read online that numpy fft doesn't require it to be an integer power of 2. Would you recommend padding with zeros to make it 2^18 length? $\endgroup$
    – Bern
    Aug 16, 2022 at 17:55
  • $\begingroup$ You don’t need to pad, no. You might want to pad however to get better frequency precision, which in your case might be useful to get exact amplitude measurements. The power of 2 length requirement is only for efficiency, so unless you’re worried about that, no need. $\endgroup$
    – Jdip
    Aug 16, 2022 at 17:57
  • $\begingroup$ good to know. That isn't much of a concern but I'll keep it in the back of my mind for the future. Going back to the idea of truncating the signal, do you have any more guidance on how to properly do that? I'm currently unsure on what point to do so, which of course is changing quite significantly the output amplitudes. $\endgroup$
    – Bern
    Aug 16, 2022 at 18:03
  • $\begingroup$ Why do you want to truncate your signal? And it shouldn’t change the signal amplitude if you make sure to scale your result like I mentioned… $\endgroup$
    – Jdip
    Aug 16, 2022 at 18:04
  • $\begingroup$ I'm attempting to window the my spectrum to get "cleaner" results. One of the things I'm noticing is that the application of windows changes the output amplitudes at frequencies of interest, quite significantly. It was recommended to me to try looking at only a chunk of my signal to see if that helps at all. I'm pretty new to this stuff so figured I'd give it a shot, but if you have any suggestions for windowing, I'd be grateful for those too! $\endgroup$
    – Bern
    Aug 16, 2022 at 18:07
0
$\begingroup$

Title: no such thing. The DFT (which is what FFT computes) doesn't require anything beyond the input being finite in length and values.

Body: keep truncating until the FFT is perfect impulses (just peaks, no leaky stuff). To the DFT, the input is a perfect sine if there's an integer number of cycles of it. Note however, truncation will change the peak's location, and its value, which need accounting for: frequency normalization for former, amplitude for latter via *= N_orig / N_trunc (for pure sines only). If the signal is measured rather than synthetically generated, then you'll most likely never get a perfect impulse.

If the goal is reliable amplitude estimation, FFT is a suboptimal tool; I recommend time-frequency analysis (CWT, STFT), which also don't suffer from the 'measured' problem. Details here under "Modulation Model vs Fourier Transform".

$\endgroup$
1
  • $\begingroup$ //"The DFT (which is what FFT computes) doesn't require anything beyond the input being finite in length and values.// --- That's true, but because of the nature of the basis functions of the DFT, certain properties are inherent. $\endgroup$ Aug 16, 2022 at 22:43
0
$\begingroup$

Number of periods of signal required when doing an FFT: $1$

$\endgroup$
4
  • 1
    $\begingroup$ Of course Robert's answer is as right and short as it could ever be. To add to my previous comments, in your case, your lowest frequency of interest being 60Hz, at a sampling rate $f_s = 15360$, a full period would be 1/60 = 0.0167s, which is exactly 256 samples. $\endgroup$
    – Jdip
    Aug 16, 2022 at 22:26
  • 1
    $\begingroup$ yup. never said anything about sample rate. just something about the inherent nature of the DFT. $\endgroup$ Aug 16, 2022 at 22:36
  • $\begingroup$ "Lengths of sides required to make a rectangle must sum to its perimeter". $\endgroup$ Aug 16, 2022 at 22:57
  • 1
    $\begingroup$ I wish there was a +🤣 option for voting. $\endgroup$
    – Peter K.
    Aug 17, 2022 at 0:02

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.