# How do i select my frequency range to plot when fft is made on time domain

I have a vibration signal that i need to convert from time domain to frequency domain using fft in python.

from scipy.fftpack import fft
yf =  fft(df["x"])
plt.plot(df["x"])


And i would like to plot it without DC value at 0Hz. So i neglected yf[0] and took N/2 frequencies to plot as per Nyquist theorem.

But I am having little trouble how to calculate x axis frequencies. how do i select my range of frequencies to plot? Also the amplitude is plotted from 0 to 25. Does this amplitude (Frequency domain) corresponds to sum of amplitude of a particular signal in time domain? How do i know which amplitude from time domain corresponds to amplitude in frequency domain?

## 2 Answers

A DFT doesn't have any external units inherently associated with it. There are N sampled points in a frame, and you get N output bins from taking the DFT. If the signal is real valued, the upper half of the DFT will be the conjugate mirror image of the lower half. The frequency of a bin is its bin index in units of cycles per frame.

The frequency of a bin in your application, in external units, depends on your sampling frequency, and is easiest understood by following the units in your calculation. The key elements are:

• $N$ the number of samples in your frame.

• $f_s$ in samples per unit time. The unit time is so often seconds that many people memorize these formulas in terms of Hz. This can be confusing because Hz can refer to either samples per second when talking about sampling rate, or cycles per second when talking about tones.

• $k$ the bin index which is cycles per frame.

So the frequency of a bin value is calculated like this:

$$f_k = k \cdot \frac{f_s}{N}$$

Where the units are:

$$\frac{cycles}{second} = \frac{cycles}{frame} \cdot \frac{\frac{samples}{second}}{\frac{samples}{frame}}$$

This answers your question, but there is a lot more to understand in order to get meaningful results from a DFT.

Hope this helps,

Ced

=================================================

Answers to the comment:

It's all in the units.

Your sampling frequency is

$$1 \frac{sample}{minute} \cdot \frac{ 1 minute}{60 seconds} = \frac{1}{60} \frac{samples}{second}$$

So, correct, $f_s = \frac{1}{60} Hz$

No, your sampling frequency pertains to how often you are taking readings per unit of time, it has nothing to do with how many samples you take.

You can either choose to use minutes or seconds (or hours, days, etc.) for your time scale, and substitute it in the equation above. For instance, if you did your DFT on sixty samples. the most straightforward unit for your frequencies would be cycles per hour. The equation would be:

$$f_k = k \cdot \frac{60}{60} = k$$

Where the units are:

$$\frac{cycles}{hour} = \frac{cycles}{frame} \cdot \frac{\frac{samples}{hour}}{\frac{samples}{frame}}$$

So bin 1 would mean one cycle per hour, bin 2 means two cycles per hour, etc.

You are not required to use per seconds, aka Hz.

• I get vibration value for every 60 seconds. And i have 1024 samples (Values). So my Fk would be Fk = k * (1024/60) / 1024? where fs = 1024/ 60 = 17Hz ? – Jonreyan Feb 8 '18 at 4:38
• You changed your comment on me. It makes my followup seem out of context. No, your $f_k = k \cdot \frac{1}{60\cdot1024}Hz$ which is why Hz probably isn't a good choice for your unit. – Cedron Dawg Feb 8 '18 at 4:49
• So i have to take cycles per minute if i want minutes on my scale. Thank you. – Jonreyan Feb 8 '18 at 5:00

The Discrete Fourier Transform takes a signal from the discrete time domain to a discrete frequency one. In the process, it is possible to evaluate the DFT in less (or more) "frequency bins".

In the time domain, the signal is indexed by a variable that denotes discrete samples. For example $x[n]$ where $n$ relates to physical time by $n \cdot T_s$ where $T_s$ is the sampling period.

In the frequency domain, the spectrum of the signal is indexed by a variable that denotes discrete frequency. For example $X[k]$ where $k$ relates to physical frequency by $k \cdot \frac{Fs}{N_F}$ where $N_F$ is the length of the DFT.

So, given a signal that was acquired at $Fs = 44100$ Hz in a buffer that was 1024 samples long (that is the $x[n]$) which was then transformed by a 512 point DFT (that is the $X[k]$), the correspondence between $k \in [0 .. 511]$ and physical frequency $f$ would be $f_k = k \cdot \frac{44100}{512}$ or a spacing of 86 Hz.

You can then use this correspondence to choose which part of the spectrum you want to plot.

As a side note, your accelerometer board is pausing data logging to write the acquired buffer to an SD card or send it over some interface elsewhere. This creates very short zero value gaps, which, overall, are going to distort your spectrum into something that looks "wavy" in a very distinct way. In fact, your spectrum is effectively the spectrum of your vibration data as modulated at 100% by a square wave at whatever frequency is implied by the length of the buffer and the speed of the SD Card / interface of your data logger. Depending on how short your recordings are going to be and how they are timed, you are going to have to do something about this, otherwise your spectrum will not be the "real" one.

Hope this helps.