# What does it mean for the DFT phase to be relative to a cosine wave?

The following paragraph from Understanding Digital Signal Processing got me puzzled:

The answer is: The DFT phase at the frequency $mf_s/N$ is relative to a cosine wave at that same frequency of $mf_s/N$ Hz where $m = 1, 2, 3, ..., N−1$. For example, the phase of $X(1)$ is −90 degrees, so the input sinusoid whose frequency is $1\cdot f_s/N = 1000$ Hz was a cosine wave having an initial phase shift of −90 degrees.

I have no idea how the DFT phase is related to a cosine wave in the way just mentioned above. Any hints would be greatly appreciated.

• I think what this is saying is that the phase of the frequency in bin 1 after the DFT ($X(1)$) is related to the phase of a cosine wave at frequency related to the bin. if the phase ($tan^-1(\frac{imaginary}{real})$ is -90 degrees, that means in the time domain your cosine wave at that frequency has a phase of -90 (which would be the same as a negative sine wave) the bin numbers are related to some frequency which is $\frac{(bin number)*f_s}{N}$. lets say your sample rate $f_s = 1000$ and $N = 64$, the first bin would relate to frequency 15.625Hz (bin 0 is 0Hz or the DC component). Apr 28 '16 at 16:11
• let me know if that makes sense. If not i'll try to explain it further. Apr 28 '16 at 16:20
• @gerrgheiser It seems you're just repeating my question. My question is why the relationship between the two is valid. Apr 28 '16 at 16:24
• I'd humbly disagree: @gerrgheiser tried to point out that, yes, that is the relation. I don't really think you asked for anything else! Apr 28 '16 at 16:45
• @MarcusMüller If that's the case, then it's my fault. My intent was not fully delivered in this post, I suppose. Apr 28 '16 at 16:47

A cosine wave is an even function around the origin (X axis = 0). The basis vectors of the real components of a DFT are all cosine waves that start at 1 at the first sample. Imaginary components of DFT basis vectors are all sine waves that are odd functions around the origin 0, and start at zero at the first sample in a DFT window.

However, for strictly real input, you can add a phase offset p to a cosine wave function to make cosine(x+p) look like any ratio mix of real and imaginary basis vectors (cosine waves and sine waves) in the DFT result, and that phase offset is what this book is calling the DFT phase.

For even length FFTs of non-fictional data, I prefer to do an fftshift to move the phase reference to the middle of the FFT window, instead of leaving the phase reference at the potential circular discontinuity at the first sample of the DFT window.

• I wonder why adding a phase offset can make those effects. Can you elaborate on the second paragraph? If it's hard to do in a single post, you can also suggest the future direction for my study. Apr 28 '16 at 16:35
• It a basic trig identity in any trigonometry textbook. Apr 28 '16 at 16:36
• Oh, I get what you meant! Apr 28 '16 at 16:44
• The basic trig identities needed seem to be commonly called the sine and cosine sum and difference formulas. Apr 28 '16 at 21:24

So let's assume you have a cosine wave: $$x_m(t) = \cos(2\pi m f_s t /N + \phi_m)$$ which, after ideal sampling becomes $$x_m[n] = \cos(2\pi m n /N + \phi_m)$$ Then, for $m=1$ we get $$x_1[n] = \cos(2\pi n/N + \phi_1)$$

Then the DFT of $x_1$ is: $$X_1[k] = \sum_{n=0}^{N-1} \cos(2\pi n/N + \phi_1) e^{-j2\pi nk/N }\\ = \frac{1}{2}\sum_{n=0}^{N-1} \left [ e^{j(2\pi n/N + \phi_1)} + e^{-j(2\pi n/N + \phi_1)} \right]e^{-j2\pi nk/N }\\ = \left\{ \begin{array}{lr} 0, & {\mbox{ for } k \not=1}\\ \frac{1}{2}\displaystyle \sum_{n=0}^{N-1} \left [ e^{j\phi_1} + e^{-j(4\pi n/N + \phi_1)} \right], & {\mbox{ for } k =1} \end{array} \right.$$ So $$X = \frac{N}{2} e^{j\phi_1} + \frac{1}{2} \sum_{n=0}^{N-1} e^{-j(4\pi n/N + \phi_1)} = \frac{N}{2} e^{j\phi_1} + e^{-j\phi_1} \frac{1}{2} \sum_{n=0}^{N-1} e^{-j4\pi n/N }$$ and the term $$\sum_{n=0}^{N-1} e^{-j4\pi n/N } = \frac{1 - e^{-j4\pi} }{1 - e^{-j4\pi/N}} = 0$$ so $$X = \frac{N}{2} e^{j\phi_1}$$

And so a cosine wave of frequency $f_s/N$, having an initial phase shift of $\phi_1$, means that the argument (angle) of $X$ is $\phi_1$.

The fft of a real valued sequence gives complex conjugate values apart from a real value corresponding to the DC component. Every pair of complex conjugate values are associated with respective pairs of a positive and negative frequency. For example if a typical pair of complex conjugates at a frequency w1 are of the form a+jb (=R exp(jw1)) and a-jb (=R exp(-jw1)), The total component at any instant of time is R exp(jw1)+ R exp(-jw1)= 2RCos(w1t) Thus the basis function for fft is a Cosine function and gives the angle associated with the cosine function. The phase plot for fft (2RCos(w1t)) gives zero phase for a frequency of w1. The phase plot for fft (2RCos(w1t-pi/3)) gives –pi/3 for a frequency of w1 and so on. If we want to consider a sequence as consisting of sinusoids with different phase, the fft gives a phase spectrum considering equivalent cosinusoids replacing the sinusoids. For example 2Sin(w1t+pi/3)=2Cos(w1t+pi/3-pi/2)= 2Cos(w1t-pi/6) so that the phase spectrum shows an angle –pi/6 and NOT pi/3. The following programme takes the fft of a signal with two sinusoids and illustrates the above principle. On running, the programme displays magnitude spectrum and waits 5 seconds before displaying the phase spectrum.

clc
clear all

%To find the fft of a signal with two sinusoids with different amplitudes
%and phase and verify magnitude and phase relations
f1=100;%frequency in hertz of one of the sinusoids
f2=300;%frequency in hertz of the second sinusoid
fs=1024;%sampling frequency, chosen as a power of 2 and also more than

% 2 times the highest frequency.
t=0:1/fs:2-(1/fs);%analysis time duration of 2 secs
x1=5*sin(2*pi*f1*t+pi/6);%sinusoid with amplitude 5 and phase pi/6 rad.
x2=2*sin(2*pi*f2*t-pi/3);%sinusoid with amplitude 2 and phase -pi/3 rad.
%x1,x2,expressed as EQUIVALENT COSINE FUNCTIONS

%are given by:-
%x1=5*cos(2*pi*f1*t+pi/6-pi/2);
%x2=2*cos(2*pi*f2*t-pi/3-pi/2);

%simplifying x1 and x2,
x1=5*cos(2*pi*f1*t-pi/3);%cosinusoid with amplitude 5 and phase -pi/3 rad.
x2=2*cos(2*pi*f2*t-5*pi/6);%cosinusoid with amplitude 2 and phase 5pi/6 rad.
z=x1+x2;%waveform containg both cosinusoids

n=1024;% fft size
zft=fft(z,n);%fft of z
zft2=zft;%zft2 is same as zft
freq_resolution=fs/n;% frequency resolution = sampling freq/fft size;

stem((0:n/2)*fs/n,2/n*abs(zft(1:n/2+1)));%magnitude plot for +ve frequencies
xlabel('Frequency, in Hertz');
ylabel('Amplitude')
title('MAGNITUDE SPECTRUM');
grid

pause(5)

threshold = max(abs(zft))/10000; %tolerance to eliminate tiny magnitude
components
zft2(abs(zft)<threshold) = 0;%sets tiny magnutude components to zero
zftg=(angle(zft2)*180/pi);%computes phase angles of fft in degrees

stem((0:n/2)*fs/n,zftg(1:n/2+1));%plots phase spectrum for +ve frequencies
title('PHASE SPECTRUM');
xlabel('Frequency, in Hertz');
ylabel('Phase Angle in Degrees')
grid