# Frequency vector and fft

I have got a question concerning the definition of the frequency vector for an fft operation.

Generally, I work with a frequency vector, f, with power of 2 elements (2048, 4096, 8192, ...).

Given a certain simulation analysis time, time (e.g. 600s), I should define f as follows:

% Frequency defition
t = 0:dt:(time-dt);
df = 1/(time);
fn = Nfft/time;


$$f = -f_{n}/2:df:f_{n}/2-1;$$

where $f_{n}$ represent the Nyquist cut-off frequency.

Actually, for:

• computational reasons
• symmetry of the power spectra along f axis
• not throwing away real or imag part of the fft

I aim to define only half of the frequency range, as for example

$$f = 0:df:f_{n}/2-1;$$

After calling the fft of my input signal, I would get the desired time series as

ouput = [real(fft) imag(fft)];


But, this way, I count the 0 frequency term twice and the -fn/2 is completely discarded.

How would it be possible to emcompasses the whole standard frequency range starting from only half of it?

• Maybe fftshift will help you? – Eddy_Em Apr 8 '13 at 19:59
• how should fftshift be helpful? fftshift is only meant to center the fft results about the 0 component. – fpe Apr 8 '13 at 20:02
• I mean that fft return vector for frequencies from 0 to $f_n-1$, fftshift moves zero to center and frequencies vector would be exactly $[ -f_n/2 : df : f_n/2 -1 ]$. I think, I don't understand your question. – Eddy_Em Apr 8 '13 at 20:09
• sorry my bad: I'm already doing this; but ain't I counting 0 component twice this way? – fpe Apr 8 '13 at 20:12
• @fpe your simple test is flawed: you are inserting 0 twice yourself when you do [-fliplr(f) f] – lxop Apr 8 '13 at 20:53

The proper way to define your frequency vector after a DFT is as follows. Let $N$ be your DFT length, and $f_s$ be your sampling rate in Hz. Furthermore, define an $N$-length frequency vector $\bf{f}$, where each element $f_i = i$, for $i = 0, 1, 2, ... N-1$.

Now your frequency vector in hertz is simply going to be $\bf{f}$$\frac{f_s}{N}$

Now, assuming you are DFT'ing a real sequence, simply pick all elements with frequency values less than $\frac{f_s}{2}$, and you are in business!

You can also see my answer here for actual code.

• but I dft complex numbers and not real sequences: does your approach apply anyway? – fpe Apr 8 '13 at 20:33
• @fpe If you are DFT'ing complex sequences, then you need all the frequency vector, meaning all the negative frequencies, and all the positive frequencies. There is no redundancy. If however you are DFT'ing a real sequence, then you can just take half of the frequency vector, since there is redundancy. – Spacey Apr 8 '13 at 20:38

It is quite simple
Frequency step for DFT is

freqStep = sampling_frequency / sample_count

linear independent frequency bin count for fft (up to Nyquist cut-off frequency) is
for even sample count:

N = samples / 2 + 1

for even sample count:

N = (samples - 1) / 2 + 1

The whole number of bins in FFT equal to the sampe count

First frequency bin is a zero frequency one. Second one is the bin for freqStep frequency and so on. The N-th bin is the bin for the Nyquist cut-off frequency. Bins after N are bins those values are complex conjugated symmetrically by N, i.e

fft(N - i) == conj(fft(N + i))

for the 5 - point signal the bins are:

[0]
[f1]
[f2]
[-f2]
[-f1]

frequency for f1 is
f1: 1 * freqStep,
frequency for f2 is
f2: 2 * freqStep

for the 4 - point signal the bins are:

[0]
[f1]
[f2]
[-f1]

frequency for f1 is
f1: 1 * freqStep,
frequency for f2 is
f2: 2 * freqStep